-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrough-draft_estimate-RNA-degredation.R
751 lines (637 loc) · 25.2 KB
/
rough-draft_estimate-RNA-degredation.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
#!/usr/bin/env Rscript
# rough-draft_estimate-RNA-degradation.R
# KA
# Goal: Estimate RNA degredation using a model based in Vock and Simon, RNA
#+ 2023
#+
#+ From the paper (p. 4, partial ¶ 1):
#+ "If RNA levels are assumed to be at steady-state, meaning that the total RNA
#+ concentration during the experiment is the ratio of k_syn to k_deg, the
#+ fraction new (denoted θ) is θ = 1 - e^(-k_deg * t_label)"
#+
#+ (Hereafter, denoting "k_deg" as "k" and "t_label" as "t".)
#+
#+ Rearrange θ = 1 - e^(-kt) to solve for k:
#+ 1. θ = 1 - e^(-kt)
#+ 2. θ - 1 = -e^(-kt)
#+ 3. 1 - θ = e^(-kt)
#+ 4. ln(1 - θ) = ln(e^(-kt))
#+ 5. ln(1 - θ) = -kt
#+ 6. ln(1 - θ)/t = -k
#+ 7. k = -ln(1 - θ)/t
#+
#+ ...where
#+ - θ is the fraction of new transcription; we estimate this value by
#+ dividing feature_N/input (feature_SS)
#+ - k is RNA degradation in absolute units of inverse time, since θ is
#+ dimensionless
#+ - t is labeling time, which we set to 6 minutes per Alison's benchwork
#+
#+ Then, draw distributions for the samples G1 rep1, G1 rep2, Q rep1, Q rep2
# Initialize arguments =======================================================
#TODO Parser
type <- "mRNA" #ARGUMENT
samples <- "Ovation" #ARGUMENT
filtering <- "min-4-cts"
# Load libraries, set options ================================================
# suppressMessages(library(DESeq2))
suppressMessages(library(tidyverse))
options(scipen = 999)
options(ggrepel.max.overlaps = Inf)
# Initialize functions and ggplot2 themes ====================================
`%notin%` <- base::Negate(`%in%`)
filter_process_counts_matrix <- function(
counts_matrix,
named_character_vector
) {
# ...
#
# :param counts_matrix: counts matrix from htseq-count
# :param named_character_vector: ...
# :return df: counts matrix as tibble
# Perform debugging
debug <- FALSE
if(base::isTRUE(debug)) {
counts_matrix <- t_cm
named_character_vector <- col_cor
}
df <- dplyr::bind_cols(
counts_matrix[, 1],
counts_matrix[
, colnames(counts_matrix) %in% named_character_vector
]
)
df <- dplyr::bind_cols(
df[, 1],
df[, 2:ncol(df)][
, match(named_character_vector, colnames(df)[2:ncol(df)])
]
)
names(df)[2:ncol(df)] <- names(named_character_vector)
return(df)
}
# Load custom ggplot2 themes -------------------------------------------------
theme_slick <- theme_classic() +
theme(
panel.grid.major = ggplot2::element_line(linewidth = 0.4),
panel.grid.minor = ggplot2::element_line(linewidth = 0.2),
axis.line = ggplot2::element_line(linewidth = 0.2),
axis.ticks = ggplot2::element_line(linewidth = 0.4),
axis.text = ggplot2::element_text(color = "black"),
axis.title.x = ggplot2::element_text(),
axis.title.y = ggplot2::element_text(),
plot.title = ggplot2::element_text(),
text = element_text(family = "")
)
theme_AG <- theme_classic() +
theme(
panel.grid.major = ggplot2::element_line(linewidth = 3),
panel.grid.minor = ggplot2::element_line(linewidth = 2),
axis.line = ggplot2::element_line(linewidth = 0.5),
axis.ticks = ggplot2::element_line(linewidth = 1.0),
axis.text = ggplot2::element_text(
color = "black", size = 20, face = "bold"
),
axis.title.x = ggplot2::element_text(size = 25, face = "bold"),
axis.title.y = ggplot2::element_text(size = 25, face = "bold"),
plot.title = ggplot2::element_text(size = 20),
text = element_text(family = "")
)
theme_AG_boxed <- theme_AG +
theme(
axis.line = ggplot2::element_line(linewidth = 0),
panel.border = element_rect(linewidth = 2, color = "black", fill = NA)
)
theme_slick_no_legend <- theme_slick + theme(legend.position = "none")
theme_AG_no_legend <- theme_AG + theme(legend.position = "none")
theme_AG_boxed_no_legend <- theme_AG_boxed + theme(legend.position = "none")
# Get situated, load counts matrix ===========================================
if(stringr::str_detect(getwd(), "kalavattam")) {
p_base <- "/Users/kalavattam/Dropbox/FHCC"
} else {
p_base <- "/Users/kalavatt/projects-etc"
}
p_exp <- "2022-2023_RRP6-NAB3/results/2023-0215"
# Set work dir
paste(p_base, p_exp, sep = "/") %>% setwd()
# getwd()
# Determine mRNA counts matrix to work with, then load it
# Check on "type" option
if(base::isTRUE(type %notin% c(
"mRNA", "pa-ncRNA", "Trinity-Q", "Trinity-G1", "Trinity-Q-unique",
"Trinity-G1-unique", "representation"
))) {
stop(paste(
"Variable \"type\" must be \"mRNA\", \"pa-ncRNA\",",
"\"Trinity-Q\", \"Trinity-G1\", \"Trinity-Q\", \"Trinity-G1\",",
"\"representation\""
))
}
# Read in htseq-count counts matrix ------------------------------------------
if(type == "mRNA") {
p_cm <- "outfiles_htseq-count/already/combined-SC-KL-20S/UT_prim_UMI"
f_cm <- "all-samples.combined-SC-KL-20S.hc-strd-eq.mRNA.tsv"
p_gtf <- "infiles_gtf-gff3/already"
f_gtf <- "combined_SC_KL_20S.gff3"
}
# Check that counts matrix exists
run <- FALSE
if(base::isTRUE(run)) {
paste(p_base, p_exp, p_cm, f_cm, sep = "/") %>%
file.exists() # [1] TRUE
}
# Load counts matrix
if(type == "mRNA") {
t_cm <- paste(p_base, p_exp, p_cm, f_cm, sep = "/") %>%
readr::read_tsv(show_col_types = FALSE) %>%
dplyr::slice(-1) # Slice out the first row, which contains file info
} else {
t_cm <- paste(p_base, p_exp, p_cm, f_cm, sep = "/") %>%
readr::read_tsv(show_col_types = FALSE)
}
# "Clean up" counts matrix column names and "features" elements
if(base::isTRUE(type == "mRNA")) {
colnames(t_cm) <- colnames(t_cm) %>%
gsub(".UT_prim_UMI.hc-strd-eq.tsv", "", .)
} else {
colnames(t_cm)[1] <- "features"
colnames(t_cm) <- colnames(t_cm) %>%
gsub("bams_renamed/UT_prim_UMI/", "", .) %>%
gsub(".UT_prim_UMI.bam", "", .)
}
if(type == "mRNA") {
t_cm <- t_cm %>%
dplyr::mutate(
features = features %>%
gsub("^transcript\\:", "", .) %>%
gsub("_mRNA", "", .)
)
}
# Clean up, correct, and abbreviate sample names
col_cor <- setNames(
c(
"n3-d_Q_day7_tcn_N_aux-T_tc-F_rep1_tech1",
"n3-d_Q_day7_tcn_N_aux-T_tc-F_rep2_tech1",
"n3-d_Q_day7_tcn_N_aux-T_tc-F_rep3_tech1", #EXCLUDE
"n3-d_Q_day7_tcn_SS_aux-T_tc-F_rep1_tech1",
"n3-d_Q_day7_tcn_SS_aux-T_tc-F_rep2_tech1",
"n3-d_Q_day7_tcn_SS_aux-T_tc-F_rep3_tech1", #EXCLUDE
"o-d_Q_day7_tcn_N_aux-T_tc-F_rep1_tech1",
"o-d_Q_day7_tcn_N_aux-T_tc-F_rep2_tech1",
"o-d_Q_day7_tcn_SS_aux-T_tc-F_rep1_tech1",
"o-d_Q_day7_tcn_SS_aux-T_tc-F_rep2_tech1",
"r1-n_Q_day8_tcn_N_aux-F_tc-F_rep1_tech1", #FIXME* ∆ rep1 → rep2
"r1-n_Q_day8_tcn_N_aux-F_tc-F_rep2_tech1", #FIXME* ∆ rep2 → rep1
"r1-n_Q_day8_tcn_SS_aux-F_tc-F_rep1_tech1", #FIXME* ∆ rep1 → rep2
"r1-n_Q_day8_tcn_SS_aux-F_tc-F_rep2_tech1", #FIXME* ∆ rep2 → rep1
"r6-n_DSm2_day2_tcn_SS_aux-F_tc-T_rep1_tech1", #FIXME* ∆ rep1 → rep2
"r6-n_DSm2_day2_tcn_SS_aux-F_tc-T_rep2_tech1", #FIXME* ∆ rep2 → rep1
"r6-n_DSp24_day3_tcn_SS_aux-F_tc-T_rep1_tech1", #FIXME* ∆ rep1 → rep2
"r6-n_DSp24_day3_tcn_SS_aux-F_tc-T_rep2_tech1", #FIXME* ∆ rep2 → rep1
"r6-n_DSp2_day2_tcn_SS_aux-F_tc-T_rep1_tech1", #FIXME* ∆ rep1 → rep2
"r6-n_DSp2_day2_tcn_SS_aux-F_tc-T_rep2_tech1", #FIXME* ∆ rep2 → rep1
"r6-n_DSp48_day4_tcn_SS_aux-F_tc-T_rep1_tech1", #FIXME* ∆ rep1 → rep2
"r6-n_DSp48_day4_tcn_SS_aux-F_tc-T_rep2_tech1", #FIXME* ∆ rep2 → rep1 #FIXME‡ ∆ tech1 → tech2
"r6-n_G1_day1_tcn_SS_aux-F_tc-F_rep1_tech1", #FIXME* ∆ rep1 → rep2 #FIXME‡ ∆ tech1 → tech2
"r6-n_G1_day1_tcn_SS_aux-F_tc-F_rep2_tech1", #FIXME* ∆ rep2 → rep1 #FIXME‡ ∆ tech1 → tech2
"r6-n_Q_day8_tcn_N_aux-F_tc-F_rep1_tech1", #FIXME* ∆ rep1 → rep2
"r6-n_Q_day8_tcn_N_aux-F_tc-F_rep2_tech1", #FIXME* ∆ rep2 → rep1
"r6-n_Q_day8_tcn_SS_aux-F_tc-F_rep1_tech1", #FIXME* ∆ rep1 → rep2 #OK
"r6-n_Q_day8_tcn_SS_aux-F_tc-F_rep1_tech2", #FIXME* ∆ rep1 → rep2 #OK
"r6-n_Q_day8_tcn_SS_aux-F_tc-F_rep2_tech1", #FIXME* ∆ rep2 → rep1
"t4-n_DSm2_day2_tcn_SS_aux-F_tc-T_rep1_tech1",
"t4-n_DSm2_day2_tcn_SS_aux-F_tc-T_rep2_tech1",
"t4-n_DSp24_day3_tcn_SS_aux-F_tc-T_rep1_tech1",
"t4-n_DSp24_day3_tcn_SS_aux-F_tc-T_rep2_tech1",
"t4-n_DSp2_day2_tcn_SS_aux-F_tc-T_rep1_tech1",
"t4-n_DSp2_day2_tcn_SS_aux-F_tc-T_rep2_tech1",
"t4-n_DSp48_day4_tcn_SS_aux-F_tc-T_rep1_tech1",
"t4-n_DSp48_day4_tcn_SS_aux-F_tc-T_rep2_tech1",
"WT_DSm2_day2_tcn_SS_aux-F_tc-T_rep1_tech1",
"WT_DSm2_day2_tcn_SS_aux-F_tc-T_rep2_tech1",
"WT_DSp24_day3_tcn_SS_aux-F_tc-T_rep1_tech1",
"WT_DSp24_day3_tcn_SS_aux-F_tc-T_rep2_tech1",
"WT_DSp2_day2_tcn_SS_aux-F_tc-T_rep1_tech1",
"WT_DSp2_day2_tcn_SS_aux-F_tc-T_rep2_tech1",
"WT_DSp48_day4_tcn_SS_aux-F_tc-T_rep1_tech1", #OK
"WT_DSp48_day4_tcn_SS_aux-F_tc-T_rep1_tech2", #OK
"WT_DSp48_day4_tcn_SS_aux-F_tc-T_rep2_tech1",
"WT_G1_day1_ovn_N_aux-F_tc-F_rep1_tech1",
"WT_G1_day1_ovn_N_aux-F_tc-F_rep2_tech1",
"WT_G1_day1_ovn_SS_aux-F_tc-F_rep1_tech1",
"WT_G1_day1_ovn_SS_aux-F_tc-F_rep2_tech1",
"WT_G1_day1_tcn_SS_aux-F_tc-F_rep1_tech1", #FIXME‡ ∆ tech1 → tech2
"WT_G1_day1_tcn_SS_aux-F_tc-F_rep2_tech1", #FIXME‡ ∆ tech1 → tech2
"WT_Q_day7_ovn_N_aux-F_tc-F_rep1_tech1",
"WT_Q_day7_ovn_N_aux-F_tc-F_rep2_tech1",
"WT_Q_day7_ovn_SS_aux-F_tc-F_rep1_tech1",
"WT_Q_day7_ovn_SS_aux-F_tc-F_rep2_tech1",
"WT_Q_day7_tcn_N_aux-F_tc-F_rep2_tech1", #FIXME† Duplicated #1
"WT_Q_day7_tcn_SS_aux-F_tc-F_rep2_tech1", #FIXME† Duplicated #2
"WT_Q_day8_tcn_N_aux-F_tc-F_rep1_tech1",
"WT_Q_day8_tcn_N_aux-F_tc-F_rep2_tech1", #FIXME† Duplicated #1
"WT_Q_day8_tcn_SS_aux-F_tc-F_rep1_tech1",
"WT_Q_day8_tcn_SS_aux-F_tc-F_rep2_tech1" #FIXME† Duplicated #2
),
c(
"n3d_Q_N_rep1_tech1",
"n3d_Q_N_rep2_tech1",
"n3d_Q_N_rep3_tech1", #EXCLUDE
"n3d_Q_SS_rep1_tech1",
"n3d_Q_SS_rep2_tech1",
"n3d_Q_SS_rep3_tech1", #EXCLUDE
"od_Q_N_rep1_tech1",
"od_Q_N_rep2_tech1",
"od_Q_SS_rep1_tech1",
"od_Q_SS_rep2_tech1",
"r1n_Q_N_rep2_tech1", #DONE* ∆ rep1 → rep2
"r1n_Q_N_rep1_tech1", #DONE* ∆ rep2 → rep1
"r1n_Q_SS_rep2_tech1", #DONE* ∆ rep1 → rep2
"r1n_Q_SS_rep1_tech1", #DONE* ∆ rep2 → rep1
"r6n_DSm2_SS_rep2_tech1", #DONE* ∆ rep1 → rep2
"r6n_DSm2_SS_rep1_tech1", #DONE* ∆ rep2 → rep1
"r6n_DSp24_SS_rep2_tech1", #DONE* ∆ rep1 → rep2
"r6n_DSp24_SS_rep1_tech1", #DONE* ∆ rep2 → rep1
"r6n_DSp2_SS_rep2_tech1", #DONE* ∆ rep1 → rep2
"r6n_DSp2_SS_rep1_tech1", #DONE* ∆ rep2 → rep1
"r6n_DSp48_SS_rep2_tech1", #DONE* ∆ rep1 → rep2
"r6n_DSp48_SS_rep1_tech2", #DONE* ∆ rep2 → rep1 #DONE‡ ∆ tech1 → tech2
"r6n_G1_SS_rep2_tech2", #DONE* ∆ rep1 → rep2 #DONE‡ ∆ tech1 → tech2
"r6n_G1_SS_rep1_tech2", #DONE* ∆ rep2 → rep1 #DONE‡ ∆ tech1 → tech2
"r6n_Q_N_rep2_tech1", #DONE* ∆ rep1 → rep2
"r6n_Q_N_rep1_tech1", #DONE* ∆ rep2 → rep1
"r6n_Q_SS_rep2_tech1", #DONE* ∆ rep1 → rep2 #OK
"r6n_Q_SS_rep2_tech2", #DONE* ∆ rep1 → rep2 #OK
"r6n_Q_SS_rep1_tech1", #DONE* ∆ rep2 → rep1
"t4n_DSm2_SS_rep1_tech1",
"t4n_DSm2_SS_rep2_tech1",
"t4n_DSp24_SS_rep1_tech1",
"t4n_DSp24_SS_rep2_tech1",
"t4n_DSp2_SS_rep1_tech1",
"t4n_DSp2_SS_rep2_tech1",
"t4n_DSp48_SS_rep1_tech1",
"t4n_DSp48_SS_rep2_tech1",
"WT_DSm2_SS_rep1_tech1",
"WT_DSm2_SS_rep2_tech1",
"WT_DSp24_SS_rep1_tech1",
"WT_DSp24_SS_rep2_tech1",
"WT_DSp2_SS_rep1_tech1",
"WT_DSp2_SS_rep2_tech1",
"WT_DSp48_SS_rep1_tech1", #OK
"WT_DSp48_SS_rep1_tech2", #OK
"WT_DSp48_SS_rep2_tech1",
"WTovn_G1_N_rep1_tech1",
"WTovn_G1_N_rep2_tech1",
"WTovn_G1_SS_rep1_tech1",
"WTovn_G1_SS_rep2_tech1",
"WT_G1_SS_rep1_tech2", #DONE‡ ∆ tech1 → tech2
"WT_G1_SS_rep2_tech2", #DONE‡ ∆ tech1 → tech2
"WTovn_Q_N_rep1_tech1",
"WTovn_Q_N_rep2_tech1",
"WTovn_Q_SS_rep1_tech1",
"WTovn_Q_SS_rep2_tech1",
"WTtest_Q_N_rep2_tech1", #DONE† Duplicated #1
"WTtest_Q_SS_rep2_tech1", #DONE† Duplicated #2
"WT_Q_N_rep1_tech1",
"WT_Q_N_rep2_tech1", #DONE† Duplicated #1
"WT_Q_SS_rep1_tech1",
"WT_Q_SS_rep2_tech1" #DONE† Duplicated #2
)
)
run <- FALSE
if(base::isTRUE(run)) {
t_cm.bak <- t_cm
# t_cm <- t_cm.bak
}
t_cm <- filter_process_counts_matrix(t_cm, col_cor)
# To associate features with metadata, load gff3 or gtf file -----------------
run <- FALSE
if(base::isTRUE(run)) {
paste(p_gtf, f_gtf, sep = "/") %>% file.exists() # [1] TRUE
}
# Load in, subset, and "clean up" gff3
if(type == "mRNA") {
t_gtf <- paste(p_gtf, f_gtf, sep = "/") %>%
rtracklayer::import() %>%
as.data.frame() %>%
dplyr::as_tibble() %>%
dplyr::filter(type == "mRNA") %>%
dplyr::mutate(
ID = ID %>%
gsub("^transcript\\:", "", .) %>%
gsub("_mRNA", "", .)
) %>%
dplyr::rename(
c(chr = seqnames, names = Name, features = ID)
)
} else {
t_gtf <- paste(p_gtf, f_gtf, sep = "/") %>%
rtracklayer::import() %>%
as.data.frame() %>%
dplyr::as_tibble() %>%
dplyr::select(-c(score, phase))
if(stringr::str_detect(type, "unique")) {
# Load dataframe for custom annotations that do not overlap R64 or
#+ pa-ncRNA collapsed/merged annotations:
#+ "Trinity_putative-transcripts.2023-0620.unique"
p_df <- "notebook/KA.2023-0620.Trinity_putative-transcripts.Q_G1"
if(stringr::str_detect(type, "Q")) {
f_df <- "Trinity_putative-transcripts.2023-0620.unique.Q.tsv"
} else if(stringr::str_detect(type, "G1")) {
f_df <- "Trinity_putative-transcripts.2023-0620.unique.G1.tsv"
}
df <- readr::read_tsv(
paste(p_df, f_df, sep = "/"), show_col_types = FALSE
)
# Filter gtf to retain only "unique" custom annotations
t_gtf <- t_gtf[t_gtf$locus_id %in% df$feature, ]
t_cm <- t_cm[t_cm$features %in% df$feature, ]
rm(p_df, f_df, df)
}
}
# Subset gff3 tibble to keep only relevant columns
if(type == "mRNA") {
keep <- c(
"chr", "start", "end",
"width", "strand", "type",
"features", "biotype", "names"
)
t_gtf <- t_gtf[, colnames(t_gtf) %in% keep]
}
# Convert column names from list to character vector, and replace empty fields
#+ with NA character values
t_gtf$names <- ifelse(
as.character(t_gtf$names) == "character(0)",
NA_character_,
as.character(t_gtf$names)
)
# Combine "counts matrix tibble" and "gff3 tibble" ---------------------------
t_mat <- dplyr::full_join(t_gtf, t_cm, by = "features")
# Add column of "thorough" names
t_mat$thorough <- ifelse(!is.na(t_mat$names), t_mat$names, t_mat$features)
t_mat <- t_mat %>% dplyr::relocate(thorough, .after = names)
# Sort counts columns by column names
tmp_A <- t_mat[, 1:10]
tmp_B <- t_mat[, 11:ncol(t_mat)][, order(names(t_mat[, 11:ncol(t_mat)]))]
t_mat <- dplyr::bind_cols(tmp_A, tmp_B)
# Remove unneeded variables
rm(list = ls(pattern = "tmp_"))
rm(f_gtf, f_cm, p_base, p_exp, p_gtf, p_cm, t_gtf, t_cm)
# Order and categorize the combined counts matrix/gff3 tibble ----------------
# Order tibble by chromosome names and feature start positions
chr_SC <- c(
"I", "II", "III", "IV", "V", "VI",
"VII", "VIII", "IX", "X", "XI", "XII",
"XIII", "XIV", "XV", "XVI", "Mito"
)
chr_KL <- c("A", "B", "C", "D", "E", "F")
chr_20S <- "20S"
chr_order <- c(chr_SC, chr_KL, chr_20S)
t_mat$chr <- t_mat$chr %>% as.factor()
t_mat$chr <- ordered(t_mat$chr, levels = chr_order)
t_mat <- t_mat %>% dplyr::arrange(chr, start)
# Categorize chromosomes by genome of origin
t_mat$genome <- ifelse(
t_mat$chr %in% chr_SC,
"S_cerevisiae",
ifelse(
t_mat$chr %in% chr_KL,
"K_lactis",
ifelse(
# t_mat$chr %in% chr_20S,
t_mat$features %in% chr_20S,
"20S",
NA
)
)
) %>%
as.factor()
t_mat <- t_mat %>% dplyr::relocate(genome, .before = chr)
# (Optional) Give feature "20S" certain placeholder values
run <- FALSE
if(base::isTRUE(run)) {
t_mat$start[which(t_mat$genome == "20S")] <-
t_mat$end[which(t_mat$genome == "20S")] <-
0
t_mat$chr[which(t_mat$genome == "20S")] <- "20S"
}
# Remove unneeded variables
rm(chr_20S, chr_KL, chr_SC, chr_order)
# For analyses of non-"unique" feat., extract htseq-count summary metrics ----
if(!stringr::str_detect(type, "unique")) {
# They are at the end of the matrices and have names that begin with two
#+ underscore characters
underscore <- t_mat[
stringr::str_detect(t_mat$features, "^__[a-zA-Z0-9_]*$"),
]
# Exclude htseq-count summary metrics from t_mat
t_mat <- t_mat[!stringr::str_detect(t_mat$features, "^__[a-zA-Z0-9_]*$"), ]
}
run <- FALSE
if(base::isTRUE(run)) t_mat %>% tail(10)
# Subset t_mat to include counts only for samples of interest ----------------
run <- TRUE
if(base::isTRUE(run)) {
t_mat.bak <- t_mat
# t_mat <- t_mat.bak
# colnames(t_mat)
}
if(samples %notin% c("Ovation")) {
stop(paste("Variable \"samples\" must be \"Ovation\""))
}
tmp_A <- t_mat[, 1:11]
tmp_B <- t_mat[, 12:ncol(t_mat)]
if(samples == "Ovation") {
tmp_C <- tmp_B[, stringr::str_detect(
colnames(tmp_B), "ovn"
)]
}
t_mat <- dplyr::bind_cols(tmp_A, tmp_C)
rm(list = ls(pattern = "tmp_"))
# (Optional) Filter counts matrix --------------------------------------------
#+ ...to exclude rows without a minimum of 10 counts in n - 1 samples
if(filtering %notin% c("none", "min-4-cts-all-but-1-samps", "min-4-cts")) {
stop(paste(
"Argument for \"filtering\" must be \"none\",",
"\"min-10-cts-all-but-1-samps\", or \"min-4-cts\""
))
}
t_sub <- t_mat[t_mat$genome == "S_cerevisiae", ]
if(filtering == "none") {
# Do nothing...
} else if(filtering == "min-4-cts-all-but-1-samps") {
counts <- sapply(t_sub[, 12:ncol(t_mat)], as.numeric)
keep <- rowSums(counts >= 4) >= ncol(counts) - 1
t_sub <- t_sub[keep, ]
run <- TRUE
if(base::isTRUE(run)) rm(counts, keep)
} else if(filtering == "min-4-cts") {
counts <- sapply(t_sub[, 12:ncol(t_mat)], as.numeric)
sketch_1 <- FALSE
if(base::isTRUE(sketch_1)) {
# Sketch 1 ---------------------------------
counts_G1_1 <- counts[, c(1, 3)]
counts_G1_2 <- counts[, c(2, 4)]
counts_Q_1 <- counts[, c(5, 7)]
counts_Q_2 <- counts[, c(6, 8)]
keep_G1_1 <- rowSums(counts_G1_1 >= 4) >= ncol(counts_G1_1)
keep_G1_2 <- rowSums(counts_G1_2 >= 4) >= ncol(counts_G1_2)
keep_Q_1 <- rowSums(counts_Q_1 >= 4) >= ncol(counts_Q_1)
keep_Q_2 <- rowSums(counts_Q_2 >= 4) >= ncol(counts_Q_2)
# table(keep_G1_1)
# table(keep_G1_2)
# table(keep_Q_1)
# table(keep_Q_2)
}
sketch_2 <- TRUE
if(base::isTRUE(sketch_2)) {
# Sketch 2 ---------------------------------
counts_G1 <- counts[, c(1, 3, 2, 4)]
counts_Q <- counts[, c(5, 7, 6, 8)]
keep_G1 <- rowSums(counts_G1 >= 4) >= ncol(counts_G1)
keep_Q <- rowSums(counts_Q >= 4) >= ncol(counts_Q)
# table(keep_G1)
# table(keep_Q)
df_G1 <- dplyr::bind_cols(t_sub[keep_G1, 1:11], counts_G1[keep_G1, ])
df_Q <- dplyr::bind_cols(t_sub[keep_Q, 1:11], counts_G1[keep_Q, ])
}
sketch_3 <- TRUE
if(base::isTRUE(sketch_3)) {
# Sketch 3 ---------------------------------
counts_all <- counts[, c(1, 3, 2, 4, 5, 7, 6, 8)]
keep_all <- rowSums(counts_all >= 4) >= ncol(counts_all)
# table(keep_all)
df_all <- dplyr::bind_cols(
t_sub[keep_all, 1:11],
counts_all[keep_all, ]
)
}
}
# Calculate k = -ln(1 - θ)/t -------------------------------------------------
#+ ...where
#+ - θ is the fraction of new transcription; we estimate this value by
#+ dividing feature_N ÷ feature_SS
#+ - k is RNA degradation in absolute units of inverse time, since θ is
#+ dimensionless
#+ - t is labeling time, which we set to 6 minutes per Alison's benchwork
calculate_k <- function(N, SS, t = 6) {
# Perform debugging
debug <- FALSE
if(base::isTRUE(debug)) {
N <- G_N_1
SS <- G_S_1
}
k <- -log(1 - N/SS)/t
return(k)
}
calculate_k_updated <- function(N, SS, t = 6) {
# Perform debugging
debug <- FALSE
if(base::isTRUE(debug)) {
N <- G_N_1
SS <- G_S_1
}
k <- -log(1 - N/(N + SS))/t
return(k)
}
df_all[, 12:ncol(df_all)] <- sapply(df_all[, 12:ncol(df_all)], as.numeric)
G_N_1 <- df_all$WTovn_G1_N_rep1_tech1
G_S_1 <- df_all$WTovn_G1_SS_rep1_tech1
G_N_2 <- df_all$WTovn_G1_N_rep2_tech1
G_S_2 <- df_all$WTovn_G1_SS_rep2_tech1
Q_N_1 <- df_all$WTovn_Q_N_rep1_tech1
Q_S_1 <- df_all$WTovn_Q_SS_rep1_tech1
Q_N_2 <- df_all$WTovn_Q_N_rep2_tech1
Q_S_2 <- df_all$WTovn_Q_SS_rep2_tech1
t <- 6
# k_G_1 <- calculate_k(N = G_N_1, SS = G_S_1, t = t)
# k_G_2 <- calculate_k(N = G_N_2, SS = G_S_2, t = t)
# k_Q_1 <- calculate_k(N = Q_N_1, SS = Q_S_1, t = t)
# k_Q_2 <- calculate_k(N = Q_N_2, SS = Q_S_2, t = t)
k_G_1 <- calculate_k_updated(N = G_N_1, SS = G_S_1, t = t)
k_G_2 <- calculate_k_updated(N = G_N_2, SS = G_S_2, t = t)
k_Q_1 <- calculate_k_updated(N = Q_N_1, SS = Q_S_1, t = t)
k_Q_2 <- calculate_k_updated(N = Q_N_2, SS = Q_S_2, t = t)
k_all <- tibble::tibble(
k_G_1 = k_G_1,
k_G_2 = k_G_2,
k_Q_1 = k_Q_1,
k_Q_2 = k_Q_2
)
min(k_all)
max(k_all)
k_all_long <- reshape2::melt(k_all)
colnames(k_all_long) <- c("sample", "k")
ggplot2::ggplot(k_all_long, aes(x = sample, y = k, fill = sample)) +
geom_violin(trim = FALSE, color = "black") +
geom_boxplot(
width = 0.2,
fill = "white",
color = "black",
outlier.shape = NA
) +
labs(x = "", y = "k_deg") +
ggtitle(
"Distributions of k_deg values",
subtitle = "theta = N / (N + SS)"
) +
theme(plot.title = element_text(hjust = 0.5)) +
theme_minimal()
# Minimal reproducible example ###############################################
suppressMessages(library(tidyverse))
# Load functions for two ways to calculate k
calculate_k <- function(N, SS, t = 6) {
k <- -log(1 - (N/SS))/t # Only SS in theta denominator
return(k)
}
calculate_k_updated <- function(N, SS, t = 6) {
k <- -log(1 - (N/(N + SS)))/t # N plus SS in theta denominator
return(k)
}
# Fill 100-row dataframe with random integers between 0 and 1000
num_rows <- 100
max_value <- 1000
df <- data.frame(matrix(ncol = 8, nrow = num_rows))
set.seed(24)
for (col in 1:8) {
df[, col] <- sample(0:max_value, num_rows, replace = TRUE)
}
# Give dataframe sample-like column names
colnames(df) <- c(
paste(rep(c("G1_N", "G1_SS"), 2), c(1, 1, 2, 2), sep = "_"),
paste(rep(c("Q_N", "Q_SS"), 2), c(1, 1, 2, 2), sep = "_")
)
# Make dataframe of k values when only feature_SS is in the denominator
df_k_denom_SS <- data.frame(
k_G1_1 = calculate_k(df$G1_N_1, df$G1_SS_1, 6),
k_G1_2 = calculate_k(df$G1_N_2, df$G1_SS_2, 6),
k_Q_1 = calculate_k(df$Q_N_1, df$Q_SS_1, 6),
k_Q_2 = calculate_k(df$Q_N_2, df$Q_SS_2, 6)
) # Get NaN warnings
# Make dataframe of k values when feature_N + feature_SS is in the denominator
df_k_denom_N_SS <- data.frame(
k_G1_1 = calculate_k_updated(df$G1_N_1, df$G1_SS_1, 6),
k_G1_2 = calculate_k_updated(df$G1_N_2, df$G1_SS_2, 6),
k_Q_1 = calculate_k_updated(df$Q_N_1, df$Q_SS_1, 6),
k_Q_2 = calculate_k_updated(df$Q_N_2, df$Q_SS_2, 6)
)
head(df_k_denom_SS)
head(df_k_denom_N_SS)
# Plot the distributions of the simulated samples
df_k_denom_N_SS_long <- reshape2::melt(df_k_denom_N_SS)
colnames(df_k_denom_N_SS_long) <- c("sample", "k")
ggplot2::ggplot(df_k_denom_N_SS_long, aes(x = sample, y = k, fill = sample)) +
geom_violin(trim = FALSE, color = "black") +
geom_boxplot(
width = 0.2,
fill = "white",
color = "black",
outlier.shape = NA
) +
labs(x = "", y = "k_deg") +
ggtitle(
"Distributions of k_deg values",
subtitle = "theta = N / (N + SS)"
) +
theme(plot.title = element_text(hjust = 0.5)) +
theme_minimal()