-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork_assess-process_R64-1-1-gff3_categorize-Trinity-transfrags_part-2.R
1663 lines (1469 loc) · 54.1 KB
/
work_assess-process_R64-1-1-gff3_categorize-Trinity-transfrags_part-2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env Rscript
# work_assess-process_R64-1-1-gff3_categorize-Trinity-transfrags_part-2.R
# KA
# Initialize arguments -------------------------------------------------------
write_dataframe <- FALSE #ARGUMENT
analyze_w_pct <- FALSE #ARGUMENT #NOTE Code for categorization with pct not written
# Get situated ---------------------------------------------------------------
suppressMessages(library(GenomicRanges))
suppressMessages(library(IRanges))
suppressMessages(library(plyr))
suppressMessages(library(readxl))
suppressMessages(library(rtracklayer))
suppressMessages(library(tidyverse))
options(scipen = 999)
options(ggrepel.max.overlaps = Inf)
if(stringr::str_detect(getwd(), "kalavattam")) {
p_local <- "/Users/kalavattam/Dropbox/FHCC"
} else {
p_local <- "/Users/kalavatt/projects-etc"
}
p_wd <- "2022-2023_RRP6-NAB3/results/2023-0215"
setwd(paste(p_local, p_wd, sep = "/"))
getwd()
rm(p_local, p_wd)
# Initialize functions -------------------------------------------------------
`%notin%` <- Negate(`%in%`)
calculate_percent_overlap <- function(x_start, x_end, y_start, y_end) {
x_length <- abs((x_end + 1) - x_start)
# Determine "largest" start
max_start <- max(c(
x_start, y_start
))
# Determine "smallest" end
min_end <- min(c(
(x_end + 1), (y_end + 1)
))
overlap <- ifelse(
(min_end - max_start) <= 0, 0, (min_end - max_start)
)
percent_overlap <- ((overlap / x_length) * 100)
return(percent_overlap)
}
analyze_feature_intersections <- function(
overlap_Tr_v_all = overlap_Q_v_all,
gtf_Tr = gtf_Q,
gtf_all = gtf_all
) {
# Perform debuging
debug <- FALSE
if(base::isTRUE(debug)) {
gtf_Tr <- gtf_Q
# gtf_Tr <- gtf_G1
# gtf_Tr <- gtf_all
overlap_Tr_v_all <- overlap_Q_v_all
# overlap_Tr_v_all <- overlap_G1_v_all
}
# Create a tibble of overlapping features --------------------------------
#+ ...in "gtf_Tr" overlapping features in "gtf_all"
wrt_Tr_all <- dplyr::bind_cols(
gtf_Tr[queryHits(overlap_Tr_v_all), ],
gtf_all[subjectHits(overlap_Tr_v_all), ]
) %>% dplyr::rename(
seqnames = seqnames...1,
start = start...2,
end = end...3,
width = width...4,
strand = strand...5,
source = source...6,
type = type...7,
gene_id = gene_id...9,
transcript_id = transcript_id...10,
seqnames_all = seqnames...14,
start_all = start...15,
end_all = end...16,
width_all = width...17,
strand_all = strand...18,
source_all = source...19,
type_all = type...20,
gene_id_all = gene_id...21,
transcript_id_all = transcript_id...22,
category_all = type.1,
orf_classification_all = orf_classification,
source_id_all = source_id,
)
# Cast dataframe types and alter names of ORF classifications
wrt_Tr_all <- tibble::as_tibble(data.frame(
lapply(wrt_Tr_all, function(x) gsub("^NA", NA_character_, x))
))
wrt_Tr_all <- tibble::as_tibble(data.frame(lapply(
wrt_Tr_all,
function(x) gsub("Verified\\|silenced_gene", "verified", x)
)))
wrt_Tr_all <- tibble::as_tibble(data.frame(lapply(
wrt_Tr_all,
function(x) gsub("Dubious", "dubious", x)
)))
wrt_Tr_all <- tibble::as_tibble(data.frame(lapply(
wrt_Tr_all,
function(x) gsub("Verified", "verified", x)
)))
wrt_Tr_all <- tibble::as_tibble(data.frame(lapply(
wrt_Tr_all,
function(x) gsub("Uncharacterized", "uncharacterized", x)
)))
wrt_Tr_all$start <- as.numeric(wrt_Tr_all$start)
wrt_Tr_all$end <- as.numeric(wrt_Tr_all$end)
wrt_Tr_all$width <- as.numeric(wrt_Tr_all$width)
wrt_Tr_all$start_all <- as.numeric(wrt_Tr_all$start_all)
wrt_Tr_all$end_all <- as.numeric(wrt_Tr_all$end_all)
wrt_Tr_all$width_all <- as.numeric(wrt_Tr_all$width_all)
run <- FALSE
if(base::isTRUE(run)) str(wrt_Tr_all)
# Clean up the names of ORF classifications
index <-
wrt_Tr_all$category_all == "antisense_gene" &
wrt_Tr_all$orf_classification_all == "dubious"
wrt_Tr_all[index, ]$orf_classification_all <- "antisense_dubious"
index <-
wrt_Tr_all$category_all == "antisense_gene" &
wrt_Tr_all$orf_classification_all == "uncharacterized"
wrt_Tr_all[index, ]$orf_classification_all <- "antisense_uncharacterized"
index <-
wrt_Tr_all$category_all == "antisense_gene" &
wrt_Tr_all$orf_classification_all == "verified"
wrt_Tr_all[index, ]$orf_classification_all <- "antisense_verified"
rm(index)
# Check that all gtf_Tr "id" elements are found in the wrt_Tr_all "id"
#+ elements
if(base::isFALSE(all(gtf_Tr$id %in% wrt_Tr_all$id))) {
stop(paste(
"Not all gtf_Tr$id %in% wrt_Tr_all$id. Stopping the script."
))
}
if(base::isFALSE(all(wrt_Tr_all$id %in% gtf_Tr$id))) {
stop(paste(
"Not all wrt_Tr_all$id %in% gtf_Tr$id. Stopping the script."
))
}
# Add an additional "category_all" column, "detailed_all", that subsets
#+ the "gene" category into ORF classifications
wrt_Tr_all$detailed_all <- ifelse(
wrt_Tr_all$category_all == "gene",
wrt_Tr_all$orf_classification_all,
ifelse(
wrt_Tr_all$category_all == "antisense_gene",
wrt_Tr_all$orf_classification_all,
wrt_Tr_all$category_all
)
)
wrt_Tr_all <- wrt_Tr_all %>%
dplyr::relocate(detailed_all, .after = category_all)
# Create columns of categories that are a bit easier to read
wrt_Tr_all$category_all_easy <- wrt_Tr_all$category_all %>%
gsub("^antisense_gene", "AS (gene)", .) %>%
gsub("^antisense_ncRNA", "AS (ncRNA)", .) %>%
gsub("^antisense_PG", "AS (PG)", .) %>%
gsub("^antisense_rRNA", "AS (rRNA)", .) %>%
gsub("^antisense_snoRNA", "AS (snoRNA)", .) %>%
gsub("^antisense_TE", "AS (TE)", .) %>%
gsub("^antisense_tRNA", "AS (tRNA)", .) %>%
gsub("^intergenic", "inter", .)
wrt_Tr_all$detailed_all_easy <- wrt_Tr_all$detailed_all %>%
gsub("^antisense_verified", "AS (verified)", .) %>%
gsub("^antisense_dubious", "AS (dubious)", .) %>%
gsub("^antisense_uncharacterized", "AS (unchar)", .) %>%
gsub("^antisense_ncRNA", "AS (ncRNA)", .) %>%
gsub("^antisense_PG", "AS (PG)", .) %>%
gsub("^antisense_rRNA", "AS (rRNA)", .) %>%
gsub("^antisense_snoRNA", "AS (snoRNA)", .) %>%
gsub("^antisense_TE", "AS (TE)", .) %>%
gsub("^antisense_tRNA", "AS (tRNA)", .) %>%
gsub("^uncharacterized", "unchar", .) %>%
gsub("^intergenic", "inter", .)
wrt_Tr_all <- wrt_Tr_all %>%
dplyr::relocate(category_all_easy, .after = category_all) %>%
dplyr::relocate(detailed_all_easy, .after = detailed_all)
# For any rows that overlap after stratifying for 'chr' and 'strand', then
#+ organize said rows into groups
wrt_Tr_all_group <- plyr::ddply(
wrt_Tr_all,
c("seqnames", "strand"),
function(x) {
# Check if a record should be linked with the previous record
y <- c(NA, x$end[-nrow(x)])
z <- ifelse(is.na(y), 0, y)
z <- cummax(z)
z[is.na(y)] <- NA
x$previous_end <- z
return(x)
}
)
wrt_Tr_all_group <- wrt_Tr_all_group %>%
dplyr::relocate(c(start_all, end_all), .after = end)
wrt_Tr_all_group$new_group <-
is.na(wrt_Tr_all_group$previous_end) |
(
wrt_Tr_all_group$start >=
wrt_Tr_all_group$previous_end
)
wrt_Tr_all_group$group <- cumsum(wrt_Tr_all_group$new_group)
wrt_Tr_all_group <- wrt_Tr_all_group %>%
dplyr::mutate(type_id_all = paste0(category_all, ": ", gene_id_all))
# Create abbreviated categories
run <- FALSE
if(base::isTRUE(run)) {
# Surveying our categories
wrt_Tr_all_group %>%
dplyr::group_by(category_all) %>%
summarize(dplyr::n())
}
wrt_Tr_all_group$abbrev_all <- wrt_Tr_all_group$category_all %>%
stringr::str_replace_all("^antisense_gene", "&G") %>%
stringr::str_replace_all("^antisense_ncRNA", "&N") %>%
stringr::str_replace_all("^antisense_PG", "&P") %>%
stringr::str_replace_all("^antisense_rRNA", "&R") %>%
stringr::str_replace_all("^antisense_snoRNA", "&O") %>%
stringr::str_replace_all("^antisense_TE", "&M") %>%
stringr::str_replace_all("^antisense_tRNA", "&T") %>%
stringr::str_replace_all("^ARS", "A") %>%
stringr::str_replace_all("^gene", "G") %>%
stringr::str_replace_all("^intergenic", "I") %>%
stringr::str_replace_all("^ncRNA", "N") %>%
stringr::str_replace_all("^PG", "P") %>%
stringr::str_replace_all("^rRNA", "R") %>%
stringr::str_replace_all("^snRNA", "S") %>%
stringr::str_replace_all("^snoRNA", "O") %>%
stringr::str_replace_all("^TE", "M") %>%
stringr::str_replace_all("^telomere", "E") %>%
stringr::str_replace_all("^tRNA", "T")
wrt_Tr_all_group$abbrev_detailed_all <- wrt_Tr_all_group$detailed_all %>%
stringr::str_replace_all("^antisense_dubious", "&D") %>%
stringr::str_replace_all("^antisense_ncRNA", "&N") %>%
stringr::str_replace_all("^antisense_PG", "&P") %>%
stringr::str_replace_all("^antisense_rRNA", "&R") %>%
stringr::str_replace_all("^antisense_snoRNA", "&O") %>%
stringr::str_replace_all("^antisense_TE", "&M") %>%
stringr::str_replace_all("^antisense_tRNA", "&T") %>%
stringr::str_replace_all("^antisense_uncharacterized", "&U") %>%
stringr::str_replace_all("^antisense_verified", "&V") %>%
stringr::str_replace_all("^ARS", "A") %>%
stringr::str_replace_all("^dubious", "D") %>%
stringr::str_replace_all("^intergenic", "I") %>%
stringr::str_replace_all("^ncRNA", "N") %>%
stringr::str_replace_all("^PG", "P") %>%
stringr::str_replace_all("^rRNA", "R") %>%
stringr::str_replace_all("^snRNA", "S") %>%
stringr::str_replace_all("^snoRNA", "O") %>%
stringr::str_replace_all("^TE", "M") %>%
stringr::str_replace_all("^telomere", "E") %>%
stringr::str_replace_all("^tRNA", "T") %>%
stringr::str_replace_all("^uncharacterized", "U") %>%
stringr::str_replace_all("^verified", "V")
wrt_Tr_all_group <- wrt_Tr_all_group %>%
dplyr::relocate(abbrev_all, .before = category_all) %>%
dplyr::relocate(abbrev_detailed_all, .before = detailed_all)
run <- FALSE
if(base::isTRUE(run)) {
wrt_Tr_all_group %>%
dplyr::group_by(abbrev_all) %>%
summarize(dplyr::n())
wrt_Tr_all_group %>%
dplyr::group_by(abbrev_detailed_all) %>%
summarize(dplyr::n()) %>%
print(n = 100)
}
# Calculate percent overlaps ---------------------------------------------
#+ ...between "Q" or "G1" and "all" features, and vice versa
wrt_Tr_all_group$pct_Tr_over_all <- mapply(
calculate_percent_overlap,
wrt_Tr_all_group$start,
wrt_Tr_all_group$end,
wrt_Tr_all_group$start_all,
wrt_Tr_all_group$end_all
)
wrt_Tr_all_group$pct_all_over_Tr <- mapply(
calculate_percent_overlap,
wrt_Tr_all_group$start_all,
wrt_Tr_all_group$end_all,
wrt_Tr_all_group$start,
wrt_Tr_all_group$end
)
wrt_Tr_all_group <- wrt_Tr_all_group %>%
dplyr::relocate(
c(pct_Tr_over_all, pct_all_over_Tr, type_id_all, group),
.after = end_all
)
# Aggregate the data -----------------------------------------------------
wrt_Tr_all_agg <- plyr::ddply(
wrt_Tr_all_group,
.(seqnames, strand, group),
plyr::summarize,
start = min(start),
end = max(end),
width = (end - start) + 1,
id = paste0(id, collapse = "; "),
trinity = paste0(gene_id, collapse = "; "),
category_abbrev = paste0(abbrev_all, collapse = " "),
detailed_abbrev = paste0(abbrev_detailed_all, collapse = " "),
category = paste0(category_all, collapse = "; "),
category_easy = paste0(category_all_easy, collapse = ", "),
detailed = paste0(detailed_all, collapse = "; "),
detailed_easy = paste0(detailed_all_easy, collapse = ", "),
complete = paste0(type_id_all, collapse = "; "),
pct_Tr_over_all = paste0(round(pct_Tr_over_all, 2), collapse = ", "),
pct_all_over_Tr = paste0(round(pct_all_over_Tr, 2), collapse = ", "),
orf_classification = paste0(orf_classification_all, collapse = "; "),
source_id = paste0(source_id_all, collapse = "; ")
) %>%
dplyr::select(-group) %>%
dplyr::arrange(seqnames, start, strand) %>%
dplyr::relocate(
c(seqnames, start, end, width, strand), .before = id
) %>%
dplyr::mutate(
n_features = stringr::str_count(complete, "\\:\ ")
) %>%
tibble::as_tibble()
# Collapse redundant strings in cells of column "id"
wrt_Tr_all_agg$id <- vapply(
stringr::str_split(wrt_Tr_all_agg$id, "; "),
`[`,
1,
FUN.VALUE = character(1)
)
# Return the various data objects ----------------------------------------
list_return <- list()
list_return[["wrt_Tr_all"]] <- wrt_Tr_all
list_return[["wrt_Tr_all_group"]] <- wrt_Tr_all_group
list_return[["wrt_Tr_all_agg"]] <- wrt_Tr_all_agg
return(list_return)
}
convert_character_0_NA <- function(x) {
z <- lapply(
x, function(y) if(identical(y, character(0))) NA_character_ else y
) %>%
unlist()
return(z)
}
flatten_elements_to_one <- function(x) {
# For character list elements with two or more subelements, collapse
# ("flatten") the subelements into a single character element
#
# :param x: <list>
# :return: vector of collapsed list elements <chr>
l_collapsed <- x[lengths(x) >= 2] %>% length()
collapsed <- vector(mode = "character", length = l_collapsed)
for(i in 1:l_collapsed) {
# i <- 1
# cat(i, "\n")
# cat(x[lengths(x) >= 2][[i]], "\n")
collapsed[i] <- stringr::str_c(
x[lengths(x) >= 2][[i]],
collapse = ", "
)
}
return(collapsed)
}
process_list_column <- function(x) {
x[lengths(x) == 0] <- NA_character_
if(length(x[lengths(x) >= 2]) != 0) {
x[lengths(x) >= 2] <- x[lengths(x) >= 2] %>%
flatten_elements_to_one()
}
y <- x %>% unlist()
return(y)
}
run_assignment_logic <- function (feat_Tr) {
# Initialize variable "up" -------------------------------
#+ Tally number of "upstream expression" non-feature regions (associated
#+ with intergenic, antisense, ARS, or telomeric regions) in categories
#+
#+ -----
#+ Logic
#+ -----
#+ IF string meets "R64_feat" condition:
#+ {
#+ (IN string interior | IN string end) AND
#+ NOT IN string start AND
#+ string nchar > 2
#+ }
#+ AND IF string meets "R64_etc" condition:
#+ {
#+ (NOT IN string interior & NOT IN string end) AND
#+ IN string start AND
#+ string nchar > 2
#+ }
#+ THEN assign: 1
#+ ELSE assign: 0
#+
#+ -----
#+ Where
#+ -----
#+ - "R64_feat" is G, N, P, R, S, O, M, T
#+ - "R64_etc" is I, A, E, &.
#+
#+ ----
#+ Note
#+ ----
#+ - Condition of nchar > 2 excludes features without putative 5' or
#+ putative 3' UTRs (info that will be captured elsewhere)
feat_Tr$up <- ifelse(
# Select for category strings associated with listed "R64_feat"
#+ features in interior or at end: G, N, P, R, S, O, M, T (use literal
#+ pattern matching)
stringr::str_detect(
feat_Tr$category_abbrev,
" G | G$| N |N$| P |P$| R |R$| S |S$| O |O$| M |M$| T |T$"
) &
# ...while excluding category strings that begin with listed features
#+ (use literal pattern mismatching)
!stringr::str_detect( #
feat_Tr$category_abbrev,
"^G|^N|^P|^R|^S|^O|^M|^T"
) &
# ...and while excluding category strings with string character counts
#+ less than or equal to two
nchar(feat_Tr$category_abbrev) %notin% c(0, 1, 2),
# Assign value of 1 for matches to category strings beginning with
#+ listed "R64_etc" features: I, A, &, or E
stringr::str_count(
feat_Tr$category_abbrev[
stringr::str_detect(
# Select for categories with listed features in interior
#+ or at end: G, N, P, R, S, O, M, T (use literal pattern
#+ matching)
feat_Tr$category_abbrev,
" G | G$| N |N$| P |P$| R |R$| S |S$| O |O$| M |M$| T |T$"
) &
!stringr::str_detect(
# ...while excluding category strings that begin with
#+ listed features (use literal pattern mismatching)
feat_Tr$category_abbrev,
"^G|^N|^P|^R|^S|^O|^M|^T"
) &
# ...and while excluding category strings with string
#+ character counts less than or equal to two
nchar(feat_Tr$category_abbrev) %notin% c(0, 1, 2)
],
"^I|^A|^&|^E" # (use literal pattern matching to start of string)
),
# Otherwise, assign value of 0
0
)
run_checks <- FALSE
if(base::isTRUE(run_checks)) {
feat_Tr$up_what <- ifelse(
nchar(feat_Tr$category_abbrev) >= 2,
stringr::str_extract(feat_Tr$category_abbrev, "^.{2}"),
stringr::str_extract(feat_Tr$category_abbrev, "^.{1}")
) %>%
gsub(" ", "", .)
}
# Initialize variable "dn" -------------------------------
#+ Tally number of "downstream expression" non-feature regions (associated
#+ with intergenic, antisense, ARS, or telomeric regions) in categories
#+
#+ -----
#+ Logic
#+ -----
#+ IF string meets "R64_etc" condition:
#+ {
#+ IN string end AND
#+ string nchar > 2
#+ }
#+ THEN assign: 1
#+ ELSE assign: 0
#+
#+ -----
#+ Where
#+ -----
#+ - "R64_etc" is I, A, E, &.
#+
#+ ----
#+ Note
#+ ----
#+ - Condition of nchar > 2 excludes features without putative 5' or
#+ putative 3' UTRs (info that will be captured elsewhere)
feat_Tr$dn <- ifelse(
# Select for category strings ending with features &?, I, A, or E
stringr::str_detect(
feat_Tr$category_abbrev,
"&.$|I$|A$|E$"
) &
# ...while excluding category strings with string character counts
#+ less than or equal to two
nchar(feat_Tr$category_abbrev) %notin% c(0, 1, 2),
stringr::str_count(
feat_Tr$category_abbrev[
# Select for category strings ending with features &?, I, A,
#+ or E
stringr::str_detect(
feat_Tr$category_abbrev,
"&.$|I$|A$|E$"
) &
# ...while excluding category strings with string character
#+ counts less than or equal to two
nchar(feat_Tr$category_abbrev) %notin% c(0, 1, 2)
],
"&.$|I$|A$|E$"
),
# Otherwise, assign value of 0
0
)
run_checks <- FALSE
if(base::isTRUE(run_checks)) {
feat_Tr$dn_what <- feat_Tr$category_abbrev %>%
stringr::str_sub(-2) %>%
stringr::str_remove(" ")
}
# Initialize and define "up_dn" --------------------------
#+ "up_dn" is assigned 1 if both "up" and "dn" are 1, else it is assigned
#+ 0; if assigned 1, then this variable signifies that both a putative 5'
#+ and a putative 3' UTR is present for a feature
#+
#+ -----
#+ Logic
#+ -----
#+ IF variables "up" AND "dn" meet condition:
#+ {
#+ "up" IS 0 AND
#+ "dn" IS (0 OR 1)
#+ }
#+ THEN assign: 0
#+ ELSE IF variables "up" AND "dn" meet condition:
#+ {
#+ "up" IS 1 AND
#+ "dn" IS 0
#+ }
#+ THEN assign: 0
#+ ELSE IF variables "up" AND "dn" meet condition:
#+ {
#+ "up" IS 1 AND
#+ "dn" IS 1
#+ }
#+ THEN assign: 1
#+
#+ -----
#+ Where
#+ -----
#+ - "up" is putative 5' UTR
#+ - "dn" is putative 3' UTR
feat_Tr$up_dn <- ifelse(
feat_Tr$up == 0 & (feat_Tr$dn == 0 | feat_Tr$dn == 1),
0,
ifelse(
feat_Tr$up == 1 & feat_Tr$dn == 0,
0,
ifelse(
feat_Tr$up == 1 & feat_Tr$dn == 1,
1,
NA_integer_
)
)
)
# Begin to define variable "completeness"; here's the rough logic:
#+ - IF "up_dn" IS 1, THEN assign "complete"
#+ - IF ("up" IS 1 AND "dn" IS 0) OR ("up" IS 0 AND "dn" IS 1), THEN
#+ "partial"
#+
#+ -----
#+ Logic
#+ -----
#+ IF variables "up" AND "dn" meet condition:
#+ {
#+ "up" IS 0 AND
#+ "dn" IS (0 OR 1)
#+ }
#+ THEN assign: 0
#+ ELSE IF variables "up" AND "dn" meet condition:
#+ {
#+ "up" IS 1 AND
#+ "dn" IS 0
#+ }
#+ THEN assign: 0
#+ ELSE IF variables "up" AND "dn" meet condition:
#+ {
#+ "up" IS 1 AND
#+ "dn" IS 1
#+ }
#+ THEN assign: 1
#+
#+ -----
#+ Where
#+ -----
#+ - "up" is putative 5' UTR
#+ - "dn" is putative 3' UTR
feat_Tr$completeness <- ifelse(
feat_Tr$up_dn == 1,
"complete",
"partial"
)
#NOTE Currently, not including the logic in this code chunk because we are
#+ defining the putative nature of transcripts with respect to 5' and 3'
#+ UTRs elsewhere using a combination of different variables and logic
run <- FALSE
if(base::isTRUE(run)) {
#+ - if "up_dn" is 0 and feat. is "I", "&.", "M", "A", or "E", then
#+ "putative"
feat_Tr$completeness_etc <- ifelse(
feat_Tr$up_dn == 0 &
feat_Tr$category_abbrev %in%
c("A", "I", "E", "&G", "M", "&M", "&O", "&R"),
"putative",
NA_character_
)
}
# For ease of checking the dataframe, move column "completeness" to just
#+ after column "tally"
feat_Tr <- feat_Tr %>%
dplyr::relocate(completeness, .after = tally)
# Tally numbers of features assoc. with categories -------
#+ Features are anywhere in the category string: beginning, interior, or
#+ end
#+
#+ -----
#+ Logic
#+ -----
#+ FOR category string A, G, I, N, P, R, S, O, M, E, T:
#+ COUNT number of category strings in element that meets condition:
#+ NOT category string preceded by "&" (e.g., "(?<!&)G")
#+ assign to variable for specific category string: COUNT
#+
#+ FOR category string &G, &N, &P, &R, &O, &M, &T:
#+ COUNT number of category strings in element that meets condition:
#+ IS ONLY category string preceded by "&" (e.g., "&G\\b")
#+
#+ -----
#+ Where
#+ -----
#+ - "Trinity" category is transcript fragment overlap with R64
#+ annotations
#+ - "dn" is putative 3' UTR
#+
#+ -----
#+ Notes
#+ -----
#+ - The regex pattern "(?<!&)G" uses a negative lookbehind assertion
#+ ((?<!&)) to check that the character preceding the character of
#+ interest is not "&"; thus, it looks at each character in the
#+ string individually to match any instances of the character of
#+ interest not preceded by "&"; it does not involve a lookahead to
#+ scan the entire string
#+ - The regex pattern "&G\\b" is matching the literal string "&G"
#+ followed by a word boundary; "&" matches the ampersand character
#+ "&" literally, and "G" matches the letter "G" literally. "\\b"
#+ represents a word boundary, meaning it matches the position
#+ between a word character (as defined by \w) and a non-word
#+ character; in other words, it matches the position at the start or
#+ end of a word
# ARS
feat_Tr$no_A <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)A")
# gene
feat_Tr$no_G <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)G")
# intergenic
feat_Tr$no_I <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)I")
# ncRNA
feat_Tr$no_N <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)N")
# pseudogene
feat_Tr$no_P <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)P")
# rRNA
feat_Tr$no_R <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)R")
# snRNA
feat_Tr$no_S <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)S")
# snoRNA
feat_Tr$no_O <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)O")
# transposable element
feat_Tr$no_M <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)M")
# telomere
feat_Tr$no_E <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)E")
# tRNA
feat_Tr$no_T <- feat_Tr$category_abbrev %>%
stringr::str_count("(?<!&)T")
# antisense (gene)
feat_Tr$no_aG <- feat_Tr$category_abbrev %>%
stringr::str_count("&G\\b")
# antisense (ncRNA)
feat_Tr$no_aN <- feat_Tr$category_abbrev %>%
stringr::str_count("&N\\b")
# antisense (pseudogene)
feat_Tr$no_aP <- feat_Tr$category_abbrev %>%
stringr::str_count("&P\\b")
# antisense (rRNA)
feat_Tr$no_aR <- feat_Tr$category_abbrev %>%
stringr::str_count("&R\\b")
# antisense (snoRNA)
feat_Tr$no_aO <- feat_Tr$category_abbrev %>%
stringr::str_count("&O\\b")
# antisense (transposable element)
feat_Tr$no_aM <- feat_Tr$category_abbrev %>%
stringr::str_count("&M\\b")
# antisense (tRNA)
feat_Tr$no_aT <- feat_Tr$category_abbrev %>%
stringr::str_count("&T\\b")
# Calculate row sums across the above-defined variables; this is used to,
#+ e.g., identify rows with only 1 feature or rows with >1 features
no_b <- grep("no_A", colnames(feat_Tr))
no_e <- grep("no_aT", colnames(feat_Tr))
feat_Tr$no_sum <- rowSums(feat_Tr[, no_b:no_e])
# Create columns of Booleans for "R64_feat" elements
feat_Tr$lgl_G <- ifelse(feat_Tr$no_G > 0, TRUE, FALSE)
feat_Tr$lgl_N <- ifelse(feat_Tr$no_N > 0, TRUE, FALSE)
feat_Tr$lgl_P <- ifelse(feat_Tr$no_P > 0, TRUE, FALSE)
feat_Tr$lgl_R <- ifelse(feat_Tr$no_R > 0, TRUE, FALSE)
feat_Tr$lgl_S <- ifelse(feat_Tr$no_S > 0, TRUE, FALSE)
feat_Tr$lgl_O <- ifelse(feat_Tr$no_O > 0, TRUE, FALSE)
feat_Tr$lgl_M <- ifelse(feat_Tr$no_M > 0, TRUE, FALSE)
feat_Tr$lgl_T <- ifelse(feat_Tr$no_T > 0, TRUE, FALSE)
# Create columns of Booleans for "R64_etc" elements
feat_Tr$lgl_A <- ifelse(feat_Tr$no_A > 0, TRUE, FALSE)
feat_Tr$lgl_I <- ifelse(feat_Tr$no_I > 0, TRUE, FALSE)
feat_Tr$lgl_E <- ifelse(feat_Tr$no_E > 0, TRUE, FALSE)
feat_Tr$lgl_aG <- ifelse(feat_Tr$no_aG > 0, TRUE, FALSE)
feat_Tr$lgl_aN <- ifelse(feat_Tr$no_aN > 0, TRUE, FALSE)
feat_Tr$lgl_aP <- ifelse(feat_Tr$no_aP > 0, TRUE, FALSE)
feat_Tr$lgl_aR <- ifelse(feat_Tr$no_aR > 0, TRUE, FALSE)
feat_Tr$lgl_aO <- ifelse(feat_Tr$no_aO > 0, TRUE, FALSE)
feat_Tr$lgl_aM <- ifelse(feat_Tr$no_aM > 0, TRUE, FALSE)
feat_Tr$lgl_aT <- ifelse(feat_Tr$no_aT > 0, TRUE, FALSE)
# Determine "mixedness" of "Trinity" categories ----------
#+ ...with respect to "R64_feat" and "R64_etc" elements
# Specify "R64_feat" column start and stop indices
lgl_b <- grep("lgl_G", colnames(feat_Tr))
lgl_e <- grep("lgl_T", colnames(feat_Tr))
# Determine mixedness with respect to "R64_feat"
#+
#+ -----
#+ Logic
#+ -----
#+ IF row sums for "R64_feat" START and STOP indices meet condition:
#+ GREATER THAN: 1
#+ THEN assign: "mixed"
#+ ELSE IF row sums for "R64_feat" START and STOP indices meet condition:
#+ EQUALS: 0
#+ THEN assign: NA
#+ ELSE IF row sums for "R64_feat" START and STOP indices meet condition:
#+ EQUALS: 1
#+ THEN assign: "unmixed"
feat_Tr$mixedness_feat <- ifelse(
rowSums(feat_Tr[, lgl_b:lgl_e]) > 1,
"mixed",
ifelse(
rowSums(feat_Tr[, lgl_b:lgl_e]) == 0,
NA_character_,
"unmixed"
)
)
# Specify "R64_etc" column start and stop indices
lgl_b <- grep("lgl_A", colnames(feat_Tr))
lgl_e <- grep("lgl_aT", colnames(feat_Tr))
# Determine mixedness with respect to "R64_etc"
#+
#+ -----
#+ Logic
#+ -----
#+ IF row sums for "R64_etc" START and STOP indices meet condition:
#+ GREATER THAN: 1
#+ THEN assign: "mixed"
#+ ELSE IF row sums for "R64_etc" START and STOP indices meet condition:
#+ EQUALS: 0
#+ THEN assign: NA
#+ ELSE IF row sums for "R64_etc" START and STOP indices mee condition:
#+ EQUALS: 1
#+ THEN assign: "unmixed"
feat_Tr$mixedness_etc <- ifelse(
rowSums(feat_Tr[, lgl_b:lgl_e]) > 1,
"mixed",
ifelse(
rowSums(feat_Tr[, lgl_b:lgl_e]) == 0,
NA_character_,
"unmixed"
)
)
# For ease of checking the dataframe, move columns "mixedness_feat",
#+ "mixedness_etc" to just after column "completeness"
feat_Tr <- feat_Tr %>%
dplyr::relocate(
c(mixedness_feat, mixedness_etc),
.after = ifelse(
"completeness" %in% colnames(feat_Tr),
"completeness",
"tally"
)
)
# Determine "repetitiveness" -------------------------------
#+ ...of "R64_feat" and "R64_etc" feature overlaps
# Determine repetitiveness of "R64_feat" elements in "Trinity" categories
#+
#+ -----
#+ Logic
#+ -----
#+ IF "R64_feat" tally and row sum meet condition:
#+ {
#+ tally EQUALS 1 AND
#+ row sum EQUALS 1
#+ }
#+ THEN assign: "single"
#+ ELSE IF "R64_feat" tally meets condition:
#+ tally GREATER THAN 1
#+ THEN assign: "repetitive"
#+ ELSE assign: "nonrepetitive"
feat_Tr$repetitiveness_feat <- ifelse(
feat_Tr$no_G == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_N == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_P == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_R == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_O == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_M == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_T == 1 & feat_Tr$no_sum == 1,
"single",
ifelse(
feat_Tr$no_G > 1 |
feat_Tr$no_N > 1 |
feat_Tr$no_P > 1 |
feat_Tr$no_R > 1 |
feat_Tr$no_O > 1 |
feat_Tr$no_M > 1 |
feat_Tr$no_T > 1,
"repetitive",
"nonrepetitive"
)
)
# Determine repetitiveness of "R64_etc" elements in "Trinity" categories
#+
#+ -----
#+ Logic
#+ -----
#+ IF "R64_etc" tally meets condition:
#+ tally GREATER THAN 1
#+ THEN assign: "repetitive"
#+ IF "R64_etc" tally and row sum meet condition:
#+ {
#+ tally EQUALS 1 AND
#+ row sum EQUALS 1
#+ }
#+ THEN assign: "single"
#+ ELSE assign: "nonrepetitive"
feat_Tr$repetitiveness_etc <- ifelse(
feat_Tr$no_A > 1 |
feat_Tr$no_E > 1 |
feat_Tr$no_I > 1 |
feat_Tr$no_aG > 1 |
feat_Tr$no_aN > 1 |
feat_Tr$no_aP > 1 |
feat_Tr$no_aR > 1 |
feat_Tr$no_aO > 1 |
feat_Tr$no_aM > 1 |
feat_Tr$no_aT > 1,
"repetitive",
ifelse(
feat_Tr$no_A == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_E == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_I == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_aG == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_aN == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_aP == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_aR == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_aO == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_aM == 1 & feat_Tr$no_sum == 1 |
feat_Tr$no_aT == 1 & feat_Tr$no_sum == 1 ,
"single",
"nonrepetitive"
)
)
# For ease of checking the dataframe, move columns "repetitiveness_feat",
#+ "repetitiveness_etc" to just after column "mixedness_etc"
feat_Tr <- feat_Tr %>%
dplyr::relocate(
c(repetitiveness_feat, repetitiveness_etc),
.after = "mixedness_etc"
)
# Assign formal categories to "Trinity" categories -------
#+ ...using the column of abbreviated categories assigned to Trinity
#+ transcripts and the new "completeness", "mixedness", and "repetitiveness" columns
#+ assigned above
# Assign value "coding" to variable "assignment"
#+
#+ -----
#+ Logic
#+ -----
#+ IF conditions are met: