-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSplicingRates_code.Rmd
3642 lines (3088 loc) · 245 KB
/
SplicingRates_code.Rmd
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
---
title: "Code for: Drosophila Splicing Rates"
author: "Athma A. Pai"
output: pdf_document
---
# Contents
- [Set up of workspace](#setup)
- [Functions](#functions)
- [Expression](#expression)
[Table of Contents](#contents)
# Setup
## Set-up workspace and libraries
```{r, message=FALSE, warning=FALSE}
setwd("~/Dropbox (MIT)/Projects/Adelman/timecourse")
setdir="~/Dropbox (MIT)/Projects/Adelman/timecourse/"
#load libraries
library(ggplot2)
library(wesanderson)
library(GGally)
library(corrplot)
library(cowplot)
library(scales)
library(gridExtra)
library(ggrepel)
library(MatchIt)
library(seqLogo)
library(png)
library(grid)
library(RColorBrewer)
library(relaimpo)
load("~/Desktop/Dropbox (MIT)/Projects/Adelman/timecourse/FinalCode.Rdata")
save.image("~/Desktop/Dropbox (MIT)/Projects/Adelman/timecourse/FinalCode.Rdata")
```
[Table of Contents](#contents)
# Functions
## Functions to use during script
```{r, echo=TRUE}
getLM_throughzero <- function(vals){
times <- c(5,5,5,10,10,10,20,20,20)
lm.row = c(NA,NA,NA)
try(lm.hold <- lm(log(vals)~0+times))
try(lm.row <- c(as.numeric(lm.hold$coefficients[1]),summary(lm.hold)$coefficients[1,4],
summary(lm.hold)$adj.r.squared))
return(lm.row)
}
getLM_throughzero_tau <- function(input){
vals <- input[1:9]
distance <- input[10]
# assume txn rate is 1.5kb/minute
tau = distance/1500
# get corrected time
times <- c(5,5,5,10,10,10,20,20,20)
times_with <- (times+tau)/2
lm.row = c(NA,NA,NA)
try(lm.hold <- lm(log(vals)~0+times_with))
try(lm.row <- c(as.numeric(lm.hold$coefficients[1]),summary(lm.hold)$coefficients[1,4],
summary(lm.hold)$adj.r.squared))
return(lm.row)
}
getLM_throughzero_tau_lowadj <- function(input){
vals <- as.numeric(input[1:9])
distance <- as.numeric(input[10])
# assume txn rate is 1.5kb/minute
tau = distance/1500
# get corrected time
times <- c(5,5,5,10,10,10,20,20,20)
times_with <- (times+tau)/2
lm.row = c(NA,NA,NA, NA)
try(lm.hold <- lm(log(vals)~0+times_with))
try(lm.row <- c(as.numeric(lm.hold$coefficients[1]),summary(lm.hold)$coefficients[1,4],
summary(lm.hold)$adj.r.squared, "fit"))
# correct for low half-life; if second effective timepoint should have PSI < 0.1
meaninitial <- mean(vals[1:3],na.rm=T)
k <- -log(meaninitial)/times_with[1]
meansecond <- exp(-k*times_with[4])
try(if(meansecond <= 0.1){ lm.row <- c(-k, NA, NA, "corrected") })
return(lm.row)
}
writeSSbeds <- function(name,introns){
out.names <- c()
# name = outbase name
# introns = SEs[gene,chr,start,end,strand]
file.3ss <- paste0(name,".threeSS.bed")
file.5ss <- paste0(name,".fiveSS.bed")
for(i in 1:nrow(introns)){
print(i)
gene <- as.character(introns[i,1])
chr <- as.character(introns[i,2])
# introns lines
intron3 <- c(chr,as.numeric(as.character(introns[i,4]))-21,as.numeric(as.character(introns[i,4]))+2,paste(gene,"3ss",sep=";"),".","+")
intron5 <- c(chr,as.numeric(as.character(introns[i,3]))-3,as.numeric(as.character(introns[i,3]))+6,paste(gene,"5ss",sep=";"),".","+")
if(introns[i,5] == "-"){
#print("minus!")
intron3 <- c(chr,as.numeric(as.character(introns[i,3]))-3,as.numeric(as.character(introns[i,3]))+20,paste(gene,"3ss",sep=";"),".","-")
intron5 <- c(chr,as.numeric(as.character(introns[i,4]))-7,as.numeric(as.character(introns[i,4]))+2,paste(gene,"5ss",sep=";"),".","-")
}
out.names <- rbind(out.names,c(intron5[4],intron3[4]))
write.table(rbind(intron3),file=file.3ss,append=T,sep="\t",quote=F,row.names=F,col.names=F)
write.table(rbind(intron5),file=file.5ss,append=T,sep="\t",quote=F,row.names=F,col.names=F)
}
colnames(out.names) <- c("fivess","threess")
return(out.names)
}
Tcomplete <- function(x){
#x = [constant, intercept, total]
return((x[2] - log(x[3]))/(-x[1]))
}
halflife <- function(x){
#x = [constant]
#half.val <- 1-x[3]
#return((x[2] + log(half.val))/x[1])
return(log(2)/(-x[1]))
}
# write out beds of flanking exons to get nucleotide
flankingExonBeds <- function(x,filename){
# x =[name]
# filename = basename.flankexon.[up/down].bed
name = strsplit(as.character(x),split=":")[[1]]
upexon <- c(name[1],name[2],name[3],as.character(x),"up",name[7])
downexon <- c(name[1],name[5],name[6],as.character(x),"down",name[7])
if(name[7] == "-"){
upexon <- c(name[1],name[5],name[6],as.character(x),"up",name[7])
downexon <- c(name[1],name[2],name[3],as.character(x),"down",name[7])
}
write.table(rbind(upexon),paste0(filename,".flankexon.up.bed"),append=T,sep="\t",quote=F,row.names=F,col.names=F)
write.table(rbind(downexon),paste0(filename,".flankexon.down.bed"),append=T,sep="\t",quote=F,row.names=F,col.names=F)
}
binVec <- function(vec,x){
# takes in: (1) vector to bin based on [vec], (2) quantile bins (ie. 5% bins) [x]
# returns vector indicating which bin the value falls in (x*#bin)
binned <- rep("0%",length(vec))
seps <- seq(100,x,by=-x)/100
quant.sep <- quantile(vec,seps,na.rm=T)
for(i in 1:length(quant.sep)){
# print(i)
binned[which(vec <= quant.sep[[i]][1])] <- names(quant.sep[i])
}
return(binned)
# to reorder levels properly: paste(seq(x,100,x),"%",sep=""), where x is the quantile bin
}
getBPbins <- function(x, num){
minx <- min(x)
maxx <- max(x)
bins <- seq(minx, maxx-num, num)
binout <- c()
binout[which(x >= bins[1] & x < bins[2])] <- bins[1]
binout[which(x >= bins[length(bins)])] <- bins[length(bins)]
for(i in 2:(length(bins)-1)){
binout[which(x >= bins[i] & x < bins[i+1])] <- bins[i]
}
return(binout)
}
get_runningmedians <- function(datahere, bins, bin.min, colnum){
run.half <- c()
for(i in 1:nrow(datahere)){
if(i <= bins){
size <- max(bin.min, i)
run.half[i] <- median(datahere[c(1:size),colnum],na.rm=T)
}
if(i > bins & i < nrow(datahere)-bins){
run.half[i] <- median(datahere[c((i-bins):(i+bins)),colnum],na.rm=T)
}
if(i >= nrow(datahere)-bins){
size <- min(i, nrow(datahere)-bin.min)
run.half[i] <- median(datahere[c(size:nrow(datahere)),colnum],na.rm=T)
}
}
median.data <- data.frame(length = datahere$intronlen, half=datahere$fitvalue, pos = datahere$intronnum, fit_half = run.half, IEratio=datahere$IEratio)
return(median.data)
}
```
# Expression
Read in gene expression data. TPMs were obtained by running Kallisto on each fastq file and then combining transcript-level TPMs to gene-level TPMs with tximport package
```{r}
adelman.txi <- read.table(file=paste0(setdir,"adelman_abundance.gene"),sep="\t",header=T)
adelman.txi <- adelman.txi[,c(3,1,2,4)]
colnames(adelman.txi) <- c("5m","10m","20m","total")
```
# Splicing
Read in splicing information across timepoints. PSIs were obtained by running MISO on each mapped bam file (mapped with Tophat)
```{r}
all.combo <- read.table(paste0(setdir,"4sU_all.psi"),header=T)
psis <- all.combo[,c(2:12)]
rownames(psis) <- all.combo[,1]
# take mean across replicates per timepoints
miso.meanPsi <- as.data.frame(cbind(rowMeans(all.combo[,2:4],na.rm=T),rowMeans(all.combo[,5:7],na.rm=T),rowMeans(all.combo[,8:10],na.rm=T),rowMeans(all.combo[,11:12],na.rm=T)))
rownames(miso.meanPsi) <- all.combo$introns
colnames(miso.meanPsi) <- c("meanPsi_5m","meanPsi_10m","meanPsi_20m","meanPsi_total")
miso.names <- matrix(unlist(strsplit(rownames(miso.meanPsi),split=":")),ncol=7,byrow=T)
```
Read in junction-based splicing information. PSIs were obtained using only junction reads - with intron-exon | exon-intron reads indicating incomplete splicing and exon-exon reads indicating completed splicing.
```{r}
altpsi.names <- c("5min_rep1", "5min_rep2", "5min_rep3","10min_rep1","10min_rep2","10min_rep3","20min_rep1","20min_rep2","20min_rep3","total_rep1","total_rep2")
altpsi.init <- read.table(paste0(setdir,"alt_psi/Adelman_4sU_RNA-seq_5min_rep1.intron.psi"),header=T)
altpsi <- data.frame(name=altpsi.init$name)
for(i in 1:length(altpsi.names)){
print(i)
altpsi.hold <- read.table(paste0(setdir,"alt_psi/Adelman_4sU_RNA-seq_",altpsi.names[i],".intron.psi"),header=T)
readcount = rowSums(altpsi.hold[,c(2:4)])
altpsi.hold$PSI[which(readcount < 20)] <- NA
altpsi <- cbind(altpsi, altpsi.hold$PSI)
}
colnames(altpsi)[2:12] <- c("psi_5m_r1","psi_5m_r2","psi_5m_r3","psi_10m_r1","psi_10m_r2","psi_10m_r3","psi_20m_r1","psi_20m_r2","psi_20m_r3","psi_total_r1","psi_total_r2")
```
# Simulations to assess methods
Read in simulated ratios
```{r}
Ddists = seq(1,5,1)
exprlevels = seq(1,50,5)
ratiofit.dir <- "~/Dropbox (MIT)/Projects/Adelman/timecourse/simulations/ratioSIM"
ratiosims <- c()
for(D in Ddists){
for(X in exprlevels){
print(paste0(D," - ",X))
hold <- read.table(paste0(ratiofit.dir,"/ratioSIM_D",D,"_X",X,".txt"),header=T)
ratiosims <- rbind(ratiosims, hold)
}
}
# make NA those without any reads in either the 5 or 60m timepoints
ratiosims$splicingratio[which(ratiosims$intron_5 ==0 | ratiosims$intron_60 == 0)] <- NA
```
Read in simulated junc fits
```{r}
Ddists = seq(1,5,1)
exprlevels = seq(1,50,5)
juncfit.dir <- "~/Dropbox (MIT)/Projects/Adelman/timecourse/simulations/juncSIM"
juncsims <- c()
for(D in Ddists){
for(X in exprlevels){
print(paste0(D," - ",X))
hold <- read.table(paste0(juncfit.dir,"/juncSIM_D",D,"_X",X,".txt"),header=T)
juncsims <- rbind(juncsims, hold)
}
}
# make NA:
# those where fit failed: already in dataframe
# those without exon-exon junction reads
juncsims$root[which(is.infinite(juncsims$ratio_5) | is.infinite(juncsims$ratio_10) | is.infinite(juncsims$ratio_20))] <- NA
# those without intron-exon junction coverage in 5m timepoint
juncsims$root[which(juncsims$unspliced_5 == 0)] <- NA
# those with coverage in at least two timepoints
juncsims$root[which(juncsims$unspliced_10 ==0 & juncsims$unspliced_20==0)] <- NA
```
Read in simulated miso fits
```{r}
Ddists = seq(1,5,1)
psifit.dir = "~/Dropbox (MIT)/Projects/Adelman/timecourse/simulations/psiSIM"
psisims <- c()
for(D in Ddists){
print(D)
hold <- read.table(paste0(psifit.dir,"/psiSIM_D",D,".txt"),header=T)
psisims <- rbind(psisims,hold)
}
# make NA:
# any timepoint is NA
psisims$halflife[which(is.na(psisims$psi5) | is.na(psisims$psi10) | is.na(psisims$psi20))] <- NA
# 10m | 20m PSI > 5m PSI
psisims$halflife[which((psisims$psi10 > psisims$psi5) | (psisims$psi20 > psisims$psi5))] <- NA
# all psis == 0
psisims$halflife[which(psisims$psi5 == 1 & psisims$psi10 == 1 & psisims$psi20 == 1)] <- NA
# 20m & 10m >= 5m
psisims$halflife[which((psisims$psi20 >= psisims$psi5) & (psisims$psi20 >= psisims$psi5))] <- NA
```
Combine simulated metrics
```{r}
Ddists = seq(1,5,1)
exprlevels = seq(1,50,5)
half_lives = unique(ratiosims$half_life)
introns = unique(ratiosims$intron)
fullsims <- c()
fullsims.cor <- c()
for(D in Ddists){
d.ratio = subset(ratiosims, D_dist == D*1000)
d.junc = subset(juncsims, D_dist == D*1000)
d.psi = subset(psisims, D_dist == D)
for(exp in exprlevels){
d.e.ratio = subset(d.ratio, expression_level == exp)
d.e.junc = subset(d.junc, expression_level == exp)
d.e.psi = subset(d.psi, expression == exp)
for(i in introns){
print(paste(D,exp,i,sep=" - "))
d.e.i.ratio = subset(d.e.ratio, intron == i)
d.e.i.junc = subset(d.e.junc, intron == i)
d.e.i.psi = subset(d.e.psi, intron_len == as.numeric(i)/1000)
# get match inds
d.e.i.h.ratio.match = match(half_lives, d.e.i.ratio$half_life)
d.e.i.h.junc.match = match(half_lives, d.e.i.junc$half_life)
d.e.i.h.psi.match = match(half_lives, d.e.i.psi$sim_half_life)
# create temp dataframe
hold.data <- data.frame(D_dist = D,
expression_level = exp,
intron = i,
half_life = rep(half_lives, 3),
type = rep(c("ratio","psi","junc"),each=length(half_lives)),
sim_hl = c(d.e.i.ratio$splicingratio[d.e.i.h.ratio.match],
d.e.i.psi$halflife[d.e.i.h.psi.match],
d.e.i.junc$root[d.e.i.h.junc.match]))
hold.row <- data.frame(D_dist = D,
expression_level = exp,
intron = i,
type = rep(c("ratio","psi","junc"),2),
cor_type = rep(c("spearman","ME"),each=3),
relate = c(cor(subset(hold.data, type=="ratio")$half_life, subset(hold.data, type=="ratio")$sim_hl, use="na.or.complete", method="spearman"),
cor(subset(hold.data, type=="psi")$half_life, subset(hold.data, type=="psi")$sim_hl, use="na.or.complete", method="spearman"),
cor(subset(hold.data, type=="junc")$half_life, subset(hold.data, type=="junc")$sim_hl, use="na.or.complete", method="spearman"),
sum((subset(hold.data, type=="ratio")$sim_hl - subset(hold.data, type=="ratio")$half_life), na.rm=T)/sum(!is.na(subset(hold.data, type=="ratio")$sim_hl)),
sum((subset(hold.data, type=="psi")$sim_hl - subset(hold.data, type=="psi")$half_life), na.rm=T)/sum(!is.na(subset(hold.data, type=="ratio")$sim_hl)),
sum((subset(hold.data, type=="junc")$sim_hl - subset(hold.data, type=="junc")$half_life), na.rm=T)/sum(!is.na(subset(hold.data, type=="ratio")$sim_hl))),
cor_hl20 = c(cor(subset(hold.data, type=="ratio" & half_life <=20)$half_life, subset(hold.data, type=="ratio" & half_life <=20)$sim_hl, use="na.or.complete", method="spearman"),
cor(subset(hold.data, type=="psi" & half_life <=20)$half_life, subset(hold.data, type=="psi" & half_life <=20)$sim_hl, use="na.or.complete", method="spearman"),
cor(subset(hold.data, type=="junc" & half_life <=20)$half_life, subset(hold.data, type=="junc" & half_life <=20)$sim_hl, use="na.or.complete", method="spearman"),
sum((subset(hold.data, type=="ratio" & half_life <= 20)$sim_hl - subset(hold.data, type=="ratio" & half_life<=20)$half_life), na.rm=T)/
sum(!is.na(subset(hold.data, type=="ratio" & half_life <=20)$sim_hl)),
sum((subset(hold.data, type=="psi" & half_life <= 20)$sim_hl - subset(hold.data, type=="psi" & half_life<=20)$half_life)^2, na.rm=T)/
sum(!is.na(subset(hold.data, type=="ratio" & half_life <=20)$sim_hl)),
sum((subset(hold.data, type=="junc" & half_life <=20)$sim_hl - subset(hold.data, type=="junc" & half_life<=20)$half_life)^2, na.rm=T)/
sum(!is.na(subset(hold.data, type=="ratio" & half_life <=20)$sim_hl))))
# add to full dataframe
fullsims <- rbind(fullsims, hold.data)
fullsims.cor <- rbind(fullsims.cor, hold.row)
}
}
}
fullsims.cor$relate <- as.numeric(fullsims.cor$relate)
fullsims.cor$cor_hl20 <- as.numeric(fullsims.cor$cor_hl20)
# percentage with detection power
nrow(subset(fullsims, type=="ratio" & !is.na(sim_hl)))/(nrow(fullsims)/3)
nrow(subset(fullsims, type=="psi" & !is.na(sim_hl)))/(nrow(fullsims)/3)
nrow(subset(fullsims, type=="junc" & !is.na(sim_hl)))/(nrow(fullsims)/3)
# percentage with detection power
nrow(subset(fullsims, type=="ratio" & !is.na(sim_hl) & expression_level > 5))/(nrow(fullsims)/3)
nrow(subset(fullsims, type=="psi" & !is.na(sim_hl) & expression_level > 5))/(nrow(fullsims)/3)
nrow(subset(fullsims, type=="junc" & !is.na(sim_hl) & expression_level > 5))/(nrow(fullsims)/3)
```
Look at simulation correlations
```{r}
ggplot(fullsims, aes(x=half_life, y=sim_hl)) + geom_point(alpha=0.05) + geom_abline() + facet_wrap(~type)
ggplot(subset(fullsims, half_life<=10 & expression_level >= 10), aes(x=half_life, y=sim_hl)) + geom_point() + geom_abline() + facet_wrap(~type) + scale_x_log10() + scale_y_log10()
ggplot(fullsims, aes(x=half_life,y=sim_hl,fill=factor(type))) + geom_boxplot(notch=T) + facet_grid(D_dist~expression_level)
full.sim.cors.data <- data.frame(cor_type = "spearman",
type = c("ratio","psi","junc"),
meancor = c(mean(subset(fullsims.cor, cor_type=="spearman" & type=="ratio")$relate, na.rm=T),
mean(subset(fullsims.cor, cor_type=="spearman" & type=="psi")$relate, na.rm=T),
mean(subset(fullsims.cor, cor_type=="spearman" & type=="junc")$relate, na.rm=T)),
secor = c(sd(subset(fullsims.cor, cor_type=="spearman" & type=="ratio")$relate, na.rm=T)/sqrt(nrow(fullsims.cor)/6),
sd(subset(fullsims.cor, cor_type=="spearman" & type=="psi")$relate, na.rm=T)/sqrt(nrow(fullsims.cor)/6),
sd(subset(fullsims.cor, cor_type=="spearman" & type=="junc")$relate, na.rm=T)/sqrt(nrow(fullsims.cor)/6)),
meancor_hl20 = c(mean(subset(fullsims.cor, cor_type=="spearman" & type=="ratio")$cor_hl20, na.rm=T),
mean(subset(fullsims.cor, cor_type=="spearman" & type=="psi")$cor_hl20, na.rm=T),
mean(subset(fullsims.cor, cor_type=="spearman" & type=="junc")$cor_hl20, na.rm=T)),
secor_hl20 = c(sd(subset(fullsims.cor, cor_type=="spearman" & type=="ratio")$cor_hl20, na.rm=T)/sqrt(nrow(fullsims.cor)/6),
sd(subset(fullsims.cor, cor_type=="spearman" & type=="psi")$cor_hl20, na.rm=T)/sqrt(nrow(fullsims.cor)/6),
sd(subset(fullsims.cor, cor_type=="spearman" & type=="junc")$cor_hl20, na.rm=T)/sqrt(nrow(fullsims.cor)/6)))
full.sim.cors.data$type <- factor(full.sim.cors.data$type, levels=c("ratio","psi","junc"))
fullsims.cor$type <- factor(fullsims.cor$type, levels=c("ratio","psi","junc"))
ggplot(subset(fullsims.cor, cor_type=="ME"), aes(x=factor(cor_type),y=relate, fill=factor(type))) + geom_boxplot(notch=T)
ggplot(subset(fullsims.cor, expression_level>=5 & cor_type=="SSE"), aes(x=factor(cor_type),y=cor_hl10, fill=factor(type))) + geom_boxplot(notch=T) + scale_y_log10()
```
Simulation errors
```{r}
fullsims$percenterror <- fullsims$sim_hl/fullsims$half_life
fullsims$logpercenterror <- log2(fullsims$percenterror)
fullsims$abslogpercenterror <- abs(fullsims$logpercenterror)
ggplot(fullsims, aes(x=factor(type), y=abslogpercenterror)) + geom_boxplot()
fullsims.error <- data.frame(type = c("ratio","psi","junc"),
abslogpererror_mean = c(NA, mean(subset(fullsims, type=="psi")$abslogpercenterror, na.rm=T),
mean(subset(fullsims, type=="junc")$abslogpercenterror, na.rm=T)),
abslogpererror_sd = c(NA, sd(subset(fullsims, type=="psi")$abslogpercenterror, na.rm=T)/nrow(subset(fullsims, type=="psi")),
sd(subset(fullsims, type=="junc")$abslogpercenterror, na.rm=T)/nrow(subset(fullsims, type=="junc"))),
abslogpererror_hl20_mean = c(NA, mean(subset(fullsims, type=="psi" & half_life<=20)$abslogpercenterror, na.rm=T),
mean(subset(fullsims, type=="junc" & half_life<=20)$abslogpercenterror, na.rm=T)),
abslogpererror_hl20_se = c(NA, sd(subset(fullsims, type=="psi" & half_life<=20)$abslogpercenterror, na.rm=T)/nrow(subset(fullsims, type=="psi" & half_life<=20)),
sd(subset(fullsims, type=="junc" & half_life<=20)$abslogpercenterror, na.rm=T)/nrow(subset(fullsims, type=="junc" & half_life<=20))))
fullsims.error$type <- factor(fullsims.error$type, levels=c("ratio","psi","junc"))
### 10,000 random comparisons to get relative percent estimation
fullsims.ratio <- subset(fullsims, type=="ratio")
fullsims.psi <- subset(fullsims, type=="psi")
fullsims.junc <- subset(fullsims, type=="junc")
fullsims.ratio.hl20 <- subset(fullsims, type=="ratio" & half_life<=20)
fullsims.psi.hl20 <- subset(fullsims, type=="psi" & half_life<=20)
fullsims.junc.hl20 <- subset(fullsims, type=="junc" & half_life<=20)
choose.first <- sample((which(!is.na(fullsims.ratio$sim_hl) & !is.na(fullsims.psi$sim_hl) & !is.na(fullsims.junc$sim_hl))), 10000, replace=F)
choose.second <- sample((which(!is.na(fullsims.ratio$sim_hl) & !is.na(fullsims.psi$sim_hl) & !is.na(fullsims.junc$sim_hl))), 10000, replace=F)
choose.first.hl20 <- sample((which(!is.na(fullsims.ratio.hl20$sim_hl) & !is.na(fullsims.psi.hl20$sim_hl) & !is.na(fullsims.junc.hl20$sim_hl))), 10000, replace=F)
choose.second.hl20 <- sample((which(!is.na(fullsims.ratio.hl20$sim_hl) & !is.na(fullsims.psi.hl20$sim_hl) & !is.na(fullsims.junc.hl20$sim_hl))), 10000, replace=F)
fullsims.ratio.logpercenterror <- log2((fullsims.ratio$sim_hl[choose.first]/fullsims.ratio$sim_hl[choose.second])/(fullsims.ratio$half_life[choose.first]/fullsims.ratio$half_life[choose.second]))
fullsims.psi.logpercenterror <- log2((fullsims.psi$sim_hl[choose.first]/fullsims.psi$sim_hl[choose.second])/(fullsims.psi$half_life[choose.first]/fullsims.psi$half_life[choose.second]))
fullsims.junc.logpercenterror <- log2((fullsims.junc$sim_hl[choose.first]/fullsims.junc$sim_hl[choose.second])/(fullsims.junc$half_life[choose.first]/fullsims.junc$half_life[choose.second]))
fullsims.ratio.logpercenterror.hl20 <- log2((fullsims.ratio.hl20$sim_hl[choose.first.hl20]/fullsims.ratio.hl20$sim_hl[choose.second.hl20])/
(fullsims.ratio.hl20$half_life[choose.first.hl20]/fullsims.ratio.hl20$half_life[choose.second.hl20]))
fullsims.psi.logpercenterror.hl20 <- log2((fullsims.psi.hl20$sim_hl[choose.first.hl20]/fullsims.psi.hl20$sim_hl[choose.second.hl20])/
(fullsims.psi.hl20$half_life[choose.first.hl20]/fullsims.psi.hl20$half_life[choose.second.hl20]))
fullsims.junc.logpercenterror.hl20 <- log2((fullsims.junc.hl20$sim_hl[choose.first.hl20]/fullsims.junc.hl20$sim_hl[choose.second.hl20])/
(fullsims.junc.hl20$half_life[choose.first.hl20]/fullsims.junc.hl20$half_life[choose.second.hl20]))
fullsims.relative.error <- data.frame(type=c("ratio", "psi","junc"),
logpererror_mean = c(mean(fullsims.ratio.logpercenterror, na.rm=T),
mean(fullsims.psi.logpercenterror, na.rm=T),
mean(fullsims.junc.logpercenterror, na.rm=T)),
logpererror_se = c(sd(fullsims.ratio.logpercenterror, na.rm=T)/length(choose.first),
sd(fullsims.psi.logpercenterror, na.rm=T)/length(choose.first),
sd(fullsims.junc.logpercenterror, na.rm=T)/length(choose.first)),
abslogpererror_mean = c(mean(abs(fullsims.ratio.logpercenterror), na.rm=T),
mean(abs(fullsims.psi.logpercenterror), na.rm=T),
mean(abs(fullsims.junc.logpercenterror), na.rm=T)),
abslogpererror_se = c(sd(abs(fullsims.ratio.logpercenterror), na.rm=T)/length(choose.first),
sd(abs(fullsims.psi.logpercenterror), na.rm=T)/length(choose.first),
sd(abs(fullsims.junc.logpercenterror), na.rm=T)/length(choose.first)))
fullsims.relative.error$type <- factor(fullsims.relative.error$type, levels=c("ratio","psi","junc"))
fullsims.relative.error.hl20 <- data.frame(type=c("ratio", "psi","junc"),
logpererror_mean = c(mean(fullsims.ratio.logpercenterror.hl20, na.rm=T),
mean(fullsims.psi.logpercenterror.hl20, na.rm=T),
mean(fullsims.junc.logpercenterror.hl20, na.rm=T)),
logpererror_se = c(sd(fullsims.ratio.logpercenterror.hl20, na.rm=T)/length(choose.first.hl20),
sd(fullsims.psi.logpercenterror.hl20, na.rm=T)/length(choose.first.hl20),
sd(fullsims.junc.logpercenterror.hl20, na.rm=T)/length(choose.first.hl20)),
abslogpererror_mean = c(mean(abs(fullsims.ratio.logpercenterror.hl20), na.rm=T),
mean(abs(fullsims.psi.logpercenterror.hl20), na.rm=T),
mean(abs(fullsims.junc.logpercenterror.hl20), na.rm=T)),
abslogpererror_se = c(sd(abs(fullsims.ratio.logpercenterror.hl20), na.rm=T)/length(choose.first.hl20),
sd(abs(fullsims.psi.logpercenterror.hl20), na.rm=T)/length(choose.first.hl20),
sd(abs(fullsims.junc.logpercenterror.hl20), na.rm=T)/length(choose.first.hl20)))
fullsims.relative.error.hl20$type <- factor(fullsims.relative.error.hl20$type, levels=c("ratio","psi","junc"))
```
Correlation across half-lifes
```{r}
fullsims$type <- factor(fullsims$type, levels=c("ratio","psi","junc"))
ggplot(fullsims, aes(x=half_life, y=sim_hl,fill=factor(type),color=factor(type))) +
geom_point(data=subset(fullsims, type=="ratio"),alpha=0.05,shape=21,color=NA) +
geom_point(data=subset(fullsims, type=="psi"),alpha=0.05,shape=21,color=NA) +
geom_point(data=subset(fullsims, type=="junc"),alpha=0.05,shape=21,color=NA) +
stat_smooth(data=subset(fullsims, type=="ratio"),method="lm") + stat_smooth(data=subset(fullsims, type=="psi"),method="lm") + stat_smooth(data=subset(fullsims, type=="junc"),method="lm") +
scale_fill_manual(values=c(brewer.pal(9,"BuPu")[6], brewer.pal(9,"Oranges")[3], brewer.pal(9,"RdPu")[3])) +
scale_color_manual(values=c(brewer.pal(9,"BuPu")[8], brewer.pal(9,"Oranges")[5], brewer.pal(9,"RdPu")[5])) +
geom_abline(linetype="longdash",color="grey50",size=2) +xlim(0,10) + ylim(0,40)
```
# Model splicing
Read in only junction reads (exon-exon and intron-exon) to model splicing rates jointly with continuing transcription
```{r}
ratio.names <- c("5min_rep1", "5min_rep2", "5min_rep3","10min_rep1","10min_rep2","10min_rep3","20min_rep1","20min_rep2","20min_rep3","total_rep1","total_rep2")
juncratio.data <- c()
for(i in ratio.names){
print(i)
junccombo <- read.table(paste0("junccombo/Adelman_4sU_RNA-seq_",i,"_startsites_junc.combo"),header=T)
hold.data <- data.frame(time = strsplit(i, split="_")[[1]][1],
rep = strsplit(i, split="_")[[1]][2],
intron = junccombo$intron,
ee_count = junccombo$ee_count,
ie_count = junccombo$ie_count)
juncratio.data <- rbind(juncratio.data, hold.data)
}
juncratio.data$ratio <- (juncratio.data$ie_count/40)/(juncratio.data$ee_count/40)
ggplot(juncratio.data, aes(x=factor(time),y=ratio,fill=factor(rep))) + geom_boxplot(notch=T) + scale_y_log10(limits=c(0.05,7.5),breaks=c(0.1, 0.25, 0.5, 0.75, 1, 2.5, 5),labels=comma)
```
Get information for introns
```{r}
fullintronlist <- as.character(unique(juncratio.data$intron))
# gene
dm3_introns <- read.table("~/Dropbox (MIT)/Annotations/dm3_ensGene_annot/dm3.genes.gff",sep="\t",header=F)
gene1 <- unlist(lapply(strsplit(as.character(dm3_introns$V9),split=";"),"[",2))
gene2 <- sub("Gene:","",unlist(lapply(strsplit(gene1, split=","),function(x){return(x[grep("Gene:",x)])})))
dm3_introns$gene <- gene2
name <- sub("ID=","",unlist(lapply(strsplit(as.character(dm3_introns$V9),split=";"),"[",1)))
dm3_introns$name <- name
full.intron.genes <- dm3_introns$gene[match(fullintronlist, dm3_introns$name)]
# TPM
full.intron.TPM <- adelman.txi[match(full.intron.genes, rownames(adelman.txi)),4]
# intron length
full.names <- matrix(unlist(strsplit(fullintronlist,split=":")),ncol=7,byrow=T)
full.intron.length <- as.numeric(full.names[,5])-as.numeric(full.names[,3])
# three distance
dm3.gff <- read.table("~/Dropbox (MIT)/Annotations/dm3_ensGene_annot/dmel-all-r5.57-genes_parsed.gff")
dm3.gff$V9 <- sub("ID=","",dm3.gff$V9)
dm3.gff$genelength <- dm3.gff$V5 - dm3.gff$V4
dm3.gff$TES <- dm3.gff$V5
dm3.gff$TES[which(dm3.gff$V7 == "-")] <- dm3.gff$V4[which(dm3.gff$V7=="-")]
getpolyAdist <- function(x){
xhere <- strsplit(as.character(x), split=":")[[1]]
threess <- as.numeric(xhere[5])
if(xhere[7] == "-"){ threess <- as.numeric(xhere[3]) }
return(threess)
}
full.intron.TES <- sapply(fullintronlist, getpolyAdist)
full.intron.threelength <- abs(dm3.gff$TES[match(full.intron.genes, dm3.gff$V9)] - full.intron.TES)
three.data <- data.frame(intron = fullintronlist, threelength = full.intron.threelength)
write.table(three.data, file="threeprime_distance.txt",sep="\t",quote=F,row.names=F,col.names=T)
# total MISO PSI
full.intron.PSI <- miso.meanPsi$meanPsi_total[match(fullintronlist, rownames(miso.meanPsi))]
# intron num
intron.bed<- read.table("~/Dropbox (MIT)/Annotations/dm3_ensGene_annot/introns.sense.nochr.bed")
intron.names <- unlist(lapply(strsplit(as.character(intron.bed$V4),split=";"),"[",1))
intron.nums <- as.numeric(unlist(lapply(strsplit(unlist(lapply(strsplit(as.character(intron.bed$V4),split=";"),"[",3)),split="="),"[",2)))
full.intron.num <- intron.nums[match(fullintronlist, intron.names)]
# splice sites
introns.3ss <- read.table("~/Dropbox (MIT)/Annotations/dm3_ensGene_annot/introns.sense.nochr.threeSS.maxEnt.tab.out",fill=T)
names.3ss <- unlist(lapply(strsplit(as.character(introns.3ss$V1),split=";"),"[",1))
names.3ss <- sub(">","",names.3ss)
full.intron.3ss <- introns.3ss[match(fullintronlist,names.3ss),4]
introns.5ss <- read.table("~/Dropbox (MIT)/Annotations/dm3_ensGene_annot/introns.sense.nochr.fiveSS.maxEnt.tab.out",fill=T)
names.5ss <- unlist(lapply(strsplit(as.character(introns.5ss$V1),split=";"),"[",1))
names.5ss <- sub(">","",names.5ss)
full.intron.5ss <- introns.5ss[match(fullintronlist,names.5ss),4]
# intron type
RI.gff <- read.table("~/Dropbox (MIT)/Annotations/dm3_ensGene_annot/MISO_annots/modENCODE_RI_MISO.gff3")
RI.gff <- RI.gff[RI.gff$V3 == "gene",c(7,9)]
RI.names <- sub("ID=","",unlist(lapply(strsplit(as.character(RI.gff$V9),split=";"),"[",1)))
flipRInames <- function(x){
# x = [name, strand]
name = as.character(x[1])
if(as.character(x[2]) == "-"){
name.split = strsplit(as.character(x[1]),split="@")[[1]]
name = paste(name.split[2],name.split[1],sep="@")
}
return(name)
}
RI.names.flip <- apply(cbind(RI.names,as.character(RI.gff[,1])),1,flipRInames)
SE.gff <- read.table("~/Dropbox (MIT)/Annotations/dm3_ensGene_annot/MISO_annots/modENCODE_SE_MISO.gff3")
SE.gff <- SE.gff[SE.gff$V3 == "gene",c(7,9)]
SE.names <- sub("ID=","",unlist(lapply(strsplit(as.character(SE.gff$V9),split=";"),"[",1)))
flankingSEnames <- function(x){
name = strsplit(as.character(x[1]),split="@")[[1]]
flanks = c(paste(name[1],name[2],sep="@"),paste(name[2],name[3],sep="@"))
if(as.character(x[2] == "-")){
flanks = c(paste(name[3],name[2],sep="@"),paste(name[2],name[1],sep="@"))
}
return(flanks)
}
SE.names.flanks <- t(apply(cbind(SE.names,as.character(SE.gff[,1])),1,flankingSEnames))
SE.names.flanks <- c(SE.names.flanks[,1],SE.names.flanks[,2])
SE.names.combo <- c(SE.names, SE.names)
containingSEnames <- function(x){
name = strsplit(as.character(x[1]),split="@")[[1]]
containing = paste(name[1],name[3],sep="@")
if(as.character(x[2] == "-")){
containing = paste(name[3],name[1],sep="@")
}
return(containing)
}
SE.names.containing <- apply(cbind(SE.names, as.character(SE.gff[,1])),1,containingSEnames)
full.intron.type <- rep("CI", length(fullintronlist))
full.intron.type[which(!is.na(match(fullintronlist, RI.names.flip)))] <- "RI"
full.intron.type[which(!is.na(match(fullintronlist, SE.names.flanks)))] <- "SEflanking"
full.intron.type[which(!is.na(match(fullintronlist, SE.names.containing)))] <- "SEcontaining"
full.intron.SEname <- rep(NA, length(fullintronlist))
full.intron.SEname[which(full.intron.type=="SEflanking")] <- SE.names.combo[match(fullintronlist[which(full.intron.type == "SEflanking")], SE.names.flanks)]
full.intron.SEname[which(full.intron.type=="SEcontaining")] <- SE.names[match(fullintronlist[which(full.intron.type == "SEcontaining")], SE.names.containing)]
fullintronlist.data <- data.frame(intron = fullintronlist, gene = full.intron.genes, TPM_total = full.intron.TPM, PSI_total = full.intron.PSI,
threelength = full.intron.threelength, intronlen = full.intron.length, intronnum = full.intron.num,
ss3 = full.intron.3ss, ss5 = full.intron.5ss, type = full.intron.type, SEname = full.intron.SEname)
```
Re-structure dataframe to (1) combined across replicates and (2) allow easy access to the same intron from different timepoints. Simultaneously, calculate ratio of intron-exon and exon-exon junction reads and other parameters necessary to model half-lives. Transcription rate is set at 1500 nt/min.
```{r}
txnrate = 1500
mappingnt = 40
# 5min
minute=5
min5.juncratio.data <- data.frame(intron = fullintronlist, threelength = full.intron.threelength,
ee_count_combo = rowSums(cbind(subset(juncratio.data, time=="5min" & rep=="rep1")$ee_count[match(fullintronlist, subset(juncratio.data, time=="5min" & rep=="rep1")$intron)],
subset(juncratio.data, time=="5min" & rep=="rep2")$ee_count[match(fullintronlist, subset(juncratio.data, time=="5min" & rep=="rep2")$intron)],
subset(juncratio.data, time=="5min" & rep=="rep3")$ee_count[match(fullintronlist, subset(juncratio.data, time=="5min" & rep=="rep3")$intron)]),na.rm=T),
ie_count_combo = rowSums(cbind(subset(juncratio.data, time=="5min" & rep=="rep1")$ie_count[match(fullintronlist, subset(juncratio.data, time=="5min" & rep=="rep1")$intron)],
subset(juncratio.data, time=="5min" & rep=="rep2")$ie_count[match(fullintronlist, subset(juncratio.data, time=="5min" & rep=="rep2")$intron)],
subset(juncratio.data, time=="5min" & rep=="rep3")$ie_count[match(fullintronlist, subset(juncratio.data, time=="5min" & rep=="rep3")$intron)]),na.rm=T))
min5.juncratio.data$ratio <- (min5.juncratio.data$ie_count_combo/mappingnt)/(min5.juncratio.data$ee_count_combo/mappingnt)
min5.juncratio.data$D_prime <- (min5.juncratio.data$threelength) + (minute * txnrate)
min5.juncratio.data$R_prime <- (min5.juncratio.data$D_prime*log(2))/(txnrate*((1/min5.juncratio.data$ratio) + 1))
# 10min
minute=10
min10.juncratio.data <- data.frame(intron = fullintronlist, threelength = full.intron.threelength,
ee_count_combo = rowSums(cbind(subset(juncratio.data, time=="10min" & rep=="rep1")$ee_count[match(fullintronlist, subset(juncratio.data, time=="10min" & rep=="rep1")$intron)],
subset(juncratio.data, time=="10min" & rep=="rep2")$ee_count[match(fullintronlist, subset(juncratio.data, time=="10min" & rep=="rep2")$intron)],
subset(juncratio.data, time=="10min" & rep=="rep3")$ee_count[match(fullintronlist, subset(juncratio.data, time=="10min" & rep=="rep3")$intron)]),na.rm=T),
ie_count_combo = rowSums(cbind(subset(juncratio.data, time=="10min" & rep=="rep1")$ie_count[match(fullintronlist, subset(juncratio.data, time=="10min" & rep=="rep1")$intron)],
subset(juncratio.data, time=="10min" & rep=="rep2")$ie_count[match(fullintronlist, subset(juncratio.data, time=="10min" & rep=="rep2")$intron)],
subset(juncratio.data, time=="10min" & rep=="rep3")$ie_count[match(fullintronlist, subset(juncratio.data, time=="10min" & rep=="rep3")$intron)]),na.rm=T))
min10.juncratio.data$ratio <- (min10.juncratio.data$ie_count_combo/mappingnt)/(min10.juncratio.data$ee_count_combo/mappingnt)
min10.juncratio.data$D_prime <- (min10.juncratio.data$threelength) + (minute * txnrate)
min10.juncratio.data$R_prime <- (min10.juncratio.data$D_prime*log(2))/(txnrate*((1/min10.juncratio.data$ratio) + 1))
# 20min
minute=20
min20.juncratio.data <- data.frame(intron = fullintronlist, threelength = full.intron.threelength,
ee_count_combo = rowSums(cbind(subset(juncratio.data, time=="20min" & rep=="rep1")$ee_count[match(fullintronlist, subset(juncratio.data, time=="20min" & rep=="rep1")$intron)],
subset(juncratio.data, time=="20min" & rep=="rep2")$ee_count[match(fullintronlist, subset(juncratio.data, time=="20min" & rep=="rep2")$intron)],
subset(juncratio.data, time=="20min" & rep=="rep3")$ee_count[match(fullintronlist, subset(juncratio.data, time=="20min" & rep=="rep3")$intron)]),na.rm=T),
ie_count_combo = rowSums(cbind(subset(juncratio.data, time=="20min" & rep=="rep1")$ie_count[match(fullintronlist, subset(juncratio.data, time=="20min" & rep=="rep1")$intron)],
subset(juncratio.data, time=="20min" & rep=="rep2")$ie_count[match(fullintronlist, subset(juncratio.data, time=="20min" & rep=="rep2")$intron)],
subset(juncratio.data, time=="20min" & rep=="rep3")$ie_count[match(fullintronlist, subset(juncratio.data, time=="20min" & rep=="rep3")$intron)]),na.rm=T))
min20.juncratio.data$ratio <- (min20.juncratio.data$ie_count_combo/mappingnt)/(min20.juncratio.data$ee_count_combo/mappingnt)
min20.juncratio.data$D_prime <- (min20.juncratio.data$threelength) + (minute * txnrate)
min20.juncratio.data$R_prime <- (min20.juncratio.data$D_prime*log(2))/(txnrate*((1/min20.juncratio.data$ratio) + 1))
# total
minute=12*60
total.juncratio.data <- data.frame(intron = fullintronlist, threelength = full.intron.threelength,
ee_count_combo = rowSums(cbind(subset(juncratio.data, time=="total" & rep=="rep1")$ee_count[match(fullintronlist, subset(juncratio.data, time=="total" & rep=="rep1")$intron)],
subset(juncratio.data, time=="total" & rep=="rep2")$ee_count[match(fullintronlist, subset(juncratio.data, time=="total" & rep=="rep2")$intron)])),
ie_count_combo = rowSums(cbind(subset(juncratio.data, time=="total" & rep=="rep1")$ie_count[match(fullintronlist, subset(juncratio.data, time=="total" & rep=="rep1")$intron)],
subset(juncratio.data, time=="total" & rep=="rep2")$ie_count[match(fullintronlist, subset(juncratio.data, time=="total" & rep=="rep2")$intron)])))
total.juncratio.data$ratio <- (total.juncratio.data$ie_count_combo/mappingnt)/(total.juncratio.data$ee_count_combo/mappingnt)
total.juncratio.data$D_prime <- (total.juncratio.data$threelength) + (minute * txnrate)
total.juncratio.data$R_prime <- (total.juncratio.data$D_prime*log(2))/(txnrate*((1/total.juncratio.data$ratio) + 1))
# combine
combo.juncratio.data <- data.frame(fullintronlist.data,
ee_count_5 = min5.juncratio.data$ee_count_combo, ee_count_10 = min10.juncratio.data$ee_count_combo,
ee_count_20 = min20.juncratio.data$ee_count_combo, ee_count_total = total.juncratio.data$ee_count_combo,
ie_count_5 = min5.juncratio.data$ie_count_combo, ie_count_10 = min10.juncratio.data$ie_count_combo,
ie_count_20 = min20.juncratio.data$ie_count_combo, ie_count_total = total.juncratio.data$ie_count_combo,
ratio_5 = min5.juncratio.data$ratio, ratio_10 = min10.juncratio.data$ratio, ratio_20 = min20.juncratio.data$ratio, ratio_total = total.juncratio.data$ratio,
Dprime_5 = min5.juncratio.data$D_prime, Dprime_10 = min10.juncratio.data$D_prime, Dprime_20 = min20.juncratio.data$D_prime,
Rprime_5 = min5.juncratio.data$R_prime, Rprime_10 = min10.juncratio.data$R_prime, Rprime_20 = min20.juncratio.data$R_prime)
```
Function to jointly model half-lives from all timepoints
```{r}
sumsqequationsolve <- function(atts, txnrate){
# atts[1:3] are Dprimes
# atts[4:6] are Rprimes
print(atts)
D_prime = atts[1:3]
R_prime = atts[4:6]
hold.row <- c(NA, NA)
# f <- function(h) { (h*(1 - 2^(-D_prime/(h*txnrate)))) - R_prime }
f <- function(h){ ((h*(1 - 2^(-D_prime[1]/(h*txnrate)))) - R_prime[1])^2 + ((h*(1 - 2^(-D_prime[2]/(h*txnrate)))) - R_prime[2])^2 + ((h*(1 - 2^(-D_prime[3]/(h*txnrate)))) - R_prime[3])^2 }
starth = 0
if(sum(is.na(R_prime))==3){ return(hold.row) }
try(fit.hold <- optim(starth, f))
try(hold.row <- c(fit.hold$par, fit.hold$value))
return(hold.row)
}
```
Run function across all introns in dataset and add results to dataframe
```{r}
sumsqfit.data <- t(apply(combo.juncratio.data[,c(24:29)], 1, sumsqequationsolve, 1500))
combo.juncratio.data$fitvalue <- sumsqfit.data[,1]
combo.juncratio.data$yvalue <- sumsqfit.data[,2]
write.table(combo.juncratio.data, file="combo.juncratio.data.txt",sep="\t",quote=F,row.names=F,col.names=F)
```
Parse modeled introns to a set with optimal power to confidently detect half-lives
```{r}
# Remove those where fit failed
combo.juncratio.data.parsed <- subset(combo.juncratio.data, !is.na(fitvalue))
# Remove those without exon-exon junction reads
combo.juncratio.data.parsed <- subset(combo.juncratio.data.parsed, !is.infinite(ratio_5) & !is.infinite(ratio_10) & !is.infinite(ratio_20))
# Remove those without intron-exon junction coverage in the 5m timepoint
combo.juncratio.data.parsed <- subset(combo.juncratio.data.parsed, ie_count_5!=0)
# Keep only those with coverage in at least two timepoints
combo.juncratio.data.parsed <- subset(combo.juncratio.data.parsed, ie_count_10>0 | ie_count_20>0)
# Remove those not expressed (TPM < 5)
combo.juncratio.data.parsed <- subset(combo.juncratio.data.parsed, TPM_total >= 5)
# Remove those that are retained in total timepoint
combo.juncratio.data.parsed <- subset(combo.juncratio.data.parsed, PSI_total <= 0.2)
# Remove those that contain SEs
combo.juncratio.data.parsed <- subset(combo.juncratio.data.parsed, type!="SEcontaining")
write.table(combo.juncratio.data.parsed, file="combo.juncratio.data.parsed.txt",sep="\t",quote=F,row.names=F,col.names=F)
parsed.introns <- match(combo.juncratio.data.parsed$intron, combo.juncratio.data$intron)
```
Fit model to each replicate individually for subset of 1000 most expressed genes
```{r}
# add necessary information to replicate divided dataframe
juncratio.data$gene <- fullintronlist.data$gene[match(juncratio.data$intron, fullintronlist.data$intron)]
juncratio.data$TPM <- fullintronlist.data$TPM_total[match(juncratio.data$intron, fullintronlist.data$intron)]
juncratio.data$threelength <- fullintronlist.data$threelength[match(juncratio.data$intron, fullintronlist.data$intron)]
juncratio.data$min <- as.numeric(sub("min","",juncratio.data$time))
juncratio.data$min[which(is.na(juncratio.data$min))] <- 12*60
juncratio.data$D_prime <- juncratio.data$threelength + (juncratio.data$min * 1500)
juncratio.data$R_prime <- (juncratio.data$D_prime*log(2))/(1500*((1/juncratio.data$ratio) + 1))
getroot <- function(atts, txnrate){
#print(atts)
# assigns parameters to variables
D_prime <- as.numeric(atts[1])
R_prime <- as.numeric(atts[2])
# makes the lowerbound for fitting into the R_prime
lowerbound = 0
#lowerbound = R_prime
#try(if(R_prime < 0){ lowerbound = 0 })
# dummy variable for results
root.hold <- NA
hold.row <- c(NA, NA, NA)
if(is.na(R_prime)){ return(hold.row) }
# actual function to get the root for
#f <- function(h) { ((D_prime)/(h*txnrate)) - log(1 - (R_prime/h))/log(2) }
f <- function(h) { (h*(1 - 2^(-D_prime/(h*txnrate)))) - R_prime }
try(root.hold <- uniroot(f, lower=lowerbound, upper=7500),silent=T)
try(hold.row <- c(root.hold$root, root.hold$f.root, root.hold$estim.prec),silent=T)
return(hold.row)
}
juncroot.data <- t(apply(juncratio.data[,c(11,12)], 1, getroot, 1500))
juncratio.data$root_value <- juncroot.data[,1]
write.table(juncratio.data, file="juncratio.data.txt",sep="\t",quote=F,row.names=F,col.names=T)
juncratio.data <- read.table(file="juncratio.data.txt",header=F)
colnames(juncratio.data)
ggplot(juncratio.data, aes(x=factor(time),y=ratio,fill=factor(rep))) + geom_boxplot(notch=T) + scale_y_log10(limits=c(0.001,125),breaks=c(0.001,0.01,0.1, 1,10,100),labels=comma) +
scale_fill_manual(values=c("dodgerblue3","dodgerblue2","dodgerblue1")) + labs(x="labeling period",y="ratio of ie/ee junction counts",fill="replicates") + theme(legend.position="bottom")
juncratio.data.combo <- data.frame(fullintronlist.data,
fit_5r1 = subset(juncratio.data, time=="5min" & rep=="rep1")$root_value[match(fullintronlist,subset(juncratio.data, time=="5min" & rep=="rep1")$intron)],
fit_5r2 = subset(juncratio.data, time=="5min" & rep=="rep2")$root_value[match(fullintronlist,subset(juncratio.data, time=="5min" & rep=="rep2")$intron)],
fit_5r3 = subset(juncratio.data, time=="5min" & rep=="rep3")$root_value[match(fullintronlist,subset(juncratio.data, time=="5min" & rep=="rep3")$intron)],
fit_10r1 = subset(juncratio.data, time=="10min" & rep=="rep1")$root_value[match(fullintronlist,subset(juncratio.data, time=="10min" & rep=="rep1")$intron)],
fit_10r2 = subset(juncratio.data, time=="10min" & rep=="rep2")$root_value[match(fullintronlist,subset(juncratio.data, time=="10min" & rep=="rep2")$intron)],
fit_10r3 = subset(juncratio.data, time=="10min" & rep=="rep3")$root_value[match(fullintronlist,subset(juncratio.data, time=="10min" & rep=="rep3")$intron)],
fit_20r1 = subset(juncratio.data, time=="20min" & rep=="rep1")$root_value[match(fullintronlist,subset(juncratio.data, time=="20min" & rep=="rep1")$intron)],
fit_20r2 = subset(juncratio.data, time=="20min" & rep=="rep2")$root_value[match(fullintronlist,subset(juncratio.data, time=="20min" & rep=="rep2")$intron)],
fit_20r3 = subset(juncratio.data, time=="20min" & rep=="rep3")$root_value[match(fullintronlist,subset(juncratio.data, time=="20min" & rep=="rep3")$intron)])
library(gplots)
heatmap.2(cor(as.matrix(juncratio.data.combo[,c(12:20)]),use="complete.obs", method="spearman"))
heatmap.2(cor(as.matrix(subset(juncratio.data.combo, TPM_total > 514)[,c(12:20)]),use="complete.obs", method="pearson"))
withincors <- function(x){
# each column in x is a vector to be correlated to all other vectors in x
cor.vec <- c()
for(i in 1:ncol(x)){
for(j in i:ncol(x)){
if(i == j){ next }
else{ cor.vec <- c(cor.vec, cor(x[,i], x[,j], use="complete.obs")) }
}
}
return(cor.vec)
}
acrosstimecors <- function(x, y){
#each column in x is a vector to be correlated to all the column vectors in y
cor.vec <- c()
for(i in 1:ncol(x)){
for(j in 1:ncol(y)){
cor.vec <- c(cor.vec, cor(x[,i], y[,j], use="complete.obs"))
}
}
return(cor.vec)
}
coefvar <- function(x){ sd(x)/mean(x) }
withintime.5 <- apply(juncratio.data.combo[,c('fit_5r1', 'fit_5r2', 'fit_5r3')], 1, coefvar)
withintime.10 <- apply(juncratio.data.combo[,c('fit_10r1', 'fit_10r2', 'fit_10r3')], 1, coefvar)
withintime.20 <- apply(juncratio.data.combo[,c('fit_20r1', 'fit_20r2', 'fit_20r3')], 1, coefvar)
acrosstime.5.10 <- apply(juncratio.data.combo[,c('fit_5r1', 'fit_5r2', 'fit_5r3', 'fit_10r1', 'fit_10r2', 'fit_10r3')], 1, coefvar)
acrosstime.5.20 <- apply(juncratio.data.combo[,c('fit_5r1', 'fit_5r2', 'fit_5r3', 'fit_20r1', 'fit_20r2', 'fit_20r3')], 1, coefvar)
acrosstime.10.20 <- apply(juncratio.data.combo[,c('fit_10r1', 'fit_10r2', 'fit_10r3', 'fit_20r1', 'fit_20r2', 'fit_20r3')], 1, coefvar)
acrossall <- apply(juncratio.data.combo[,c('fit_5r1', 'fit_5r2', 'fit_5r3', 'fit_10r1', 'fit_10r2', 'fit_10r3','fit_20r1', 'fit_20r2', 'fit_20r3')], 1, coefvar)
coef.data <- data.frame(type = c(rep("withintime", length(withintime.5)*3), rep("acrosstime", length(withintime.5)*3), rep("acrossall", length(acrossall))),
time = c(rep(c("5","10","20","5-10","5-20","10-20","all"),each=length(withintime.5))),
coef = c(withintime.5, withintime.10, withintime.20, acrosstime.5.10, acrosstime.5.20, acrosstime.10.20, acrossall))
coef.data$type <- factor(coef.data$type, levels=c("withintime","acrosstime","acrossall"))
coef.data$time <- factor(coef.data$time, levels=c("5","10","20","5-10","5-20","10-20","all"))
expinds <- which(juncratio.data.combo$TPM_total >= 514)
coef.data.exp <- data.frame(type = c(rep("withintime", length(expinds)*3), rep("acrosstime", length(expinds)*3), rep("acrossall", length(expinds))),
time = c(rep(c("5","10","20","5-10","5-20","10-20","all"),each=length(expinds))),
coef = c(withintime.5[expinds], withintime.10[expinds], withintime.20[expinds], acrosstime.5.10[expinds], acrosstime.5.20[expinds], acrosstime.10.20[expinds], acrossall[expinds]))
coef.data.exp$type <- factor(coef.data.exp$type, levels=c("withintime","acrosstime","acrossall"))
coef.data.exp$time <- factor(coef.data.exp$time, levels=c("5","10","20","5-10","5-20","10-20","all"))
ggplot(coef.data.exp, aes(x=factor(type),y=coef, fill=factor(time))) + geom_boxplot(notch=T) + ylim(0,1) +
scale_fill_manual(values=c(brewer.pal(11,"BrBG")[8:10], rev(brewer.pal(11,"BrBG")[2:4]), "darkgrey")) +
labs(x="comparison",y="coefficient of variation",fill="labeling periods") + theme(legend.position="bottom")
```
Get confidence intervals for each intron
```{r}
test1 <- read.table("subsampling/juncrates_sub1.txt", header=T)
test2 <- read.table("subsampling/juncrates_sub2.txt", header=T)
test3 <- read.table("subsampling/juncrates_sub3.txt", header=T)
sampling.dist <- data.frame(intron=combo.juncratio.data.parsed$intron)
for(i in c(1:10)){
print(i)
sample.hold <- read.table(paste0("subsampling/juncrates_sub",i,".txt"),header=T)
sample.hold.parsed <- subset(sample.hold, !is.na(root))
sample.hold.parsed <- subset(sample.hold.parsed, !is.infinite(ratio_5) & !is.infinite(ratio_10) & !is.infinite(ratio_20))
sample.hold.parsed <- subset(sample.hold.parsed, ie_count_5!=0)
sample.hold.parsed <- subset(sample.hold.parsed, ie_count_10 > 0 | ie_count_20 > 0)
match.inds <- match(combo.juncratio.data.parsed$intron, sample.hold.parsed$intron)
sampling.dist <- cbind(sampling.dist, sample.hold.parsed$root[match.inds])
}
colnames(sampling.dist)[2:11] <- paste("sample",seq(1:10),sep="")
sampling.dist$se <- apply(sampling.dist[,c(2:11)], 1, sd, na.rm=T)
combo.juncratio.data.parsed$se <- sampling.dist$se
```
Get rates from different transcription rates
```{r}
sumsqfit.data.matrix <- combo.juncratio.data.parsed[,c(1:11)]
sumsqfit.data.matrix$fitvalue_500 <- sumsqfit.500$halflife[match(sumsqfit.data.matrix$intron, rownames(sumsqfit.500))]
sumsqfit.data.matrix$fitvalue_1000 <- sumsqfit.1000$halflife[match(sumsqfit.data.matrix$intron, rownames(sumsqfit.1000))]
sumsqfit.data.matrix$fitvalue_1500 <- sumsqfit.1500$halflife[match(sumsqfit.data.matrix$intron, rownames(sumsqfit.1500))]
sumsqfit.data.matrix$fitvalue_2000 <- sumsqfit.2000$halflife[match(sumsqfit.data.matrix$intron, rownames(sumsqfit.2000))]
sumsqfit.data.matrix$fitvalue_2500 <- sumsqfit.2500$halflife[match(sumsqfit.data.matrix$intron, rownames(sumsqfit.2500))]
sumsqfit.data.matrix$fitvalue_3000 <- sumsqfit.3000$halflife[match(sumsqfit.data.matrix$intron, rownames(sumsqfit.3000))]
sumsqfit.data.matrix$fitvalue_3500 <- sumsqfit.3500$halflife[match(sumsqfit.data.matrix$intron, rownames(sumsqfit.3500))]
sumsqfit.data.matrix$fitvalue_4000 <- sumsqfit.4000$halflife[match(sumsqfit.data.matrix$intron, rownames(sumsqfit.4000))]
randomtxn <- c()
for(i in 1:nrow(sumsqfit.data.matrix)){
print(i)
randomtxn[i] <- as.numeric(sample(sumsqfit.data.matrix[i,c(12:19)],1))
}
sumsqfit.data.matrix$fitvalue <- randomtxn
```
Redo with 4sU incorporation lag times
```{r}
# use getroot function to get individual labeling period half-lives
# recalculate D_prime and R_prime with each labeling period
get_r2 <- function(min5, min10, min20, lagtime){
# min5 is dataframe: intron, threelength, ee_count_combo, ie_count_combo, ratio, D_prime, R_prime
# min10 is dataframe: intron, threelength, ee_count_combo, ie_count_combo, ratio, D_prime, R_prime
# min20 is dataframe: intron, threelength, ee_count_combo, ie_count_combo, ratio, D_prime, R_prime
# lagtime is offset in minutes
txnrate = 1500
newtime <- c(5, 10, 20) + lagtime
# calculate new D_prime & R_prime values
min5.Dprime <- min5$threelength + (newtime[1] * txnrate)
min5.Rprime <- (min5.Dprime*log(2))/(txnrate*((1/min5$ratio) + 1))
min10.Dprime <- min10$threelength + (newtime[2] * txnrate)
min10.Rprime <- (min10.Dprime*log(2))/(txnrate*((1/min10$ratio) + 1))
min20.Dprime <- min20$threelength + (newtime[3] * txnrate)
min20.Rprime <- (min20.Dprime*log(2))/(txnrate*((1/min20$ratio) + 1))
# get half lives
print("CALCULATING 5 MIN...")
min5.half <- t(apply(cbind(min5.Dprime, min5.Rprime), 1, getroot, txnrate))[,1]
print("CALCULATING 10 MIN...")
min10.half <- t(apply(cbind(min10.Dprime, min10.Rprime), 1, getroot, txnrate))[,1]
print("CALCULATING 20 MIN...")
min20.half <- t(apply(cbind(min20.Dprime, min20.Rprime), 1, getroot, txnrate))[,1]
# recalculate ratio
getratio <- function(half, Dprime, txnrate){
Hprime <- (half*txnrate)/log(2)
DHratio <- Dprime/Hprime
expR <- 1/((DHratio*(1 - exp(-DHratio))) - 1)
return(expR)
}
min5.expR <- getratio(min5.half, min5.Dprime, txnrate)
min10.expR <- getratio(min10.half, min10.Dprime, txnrate)
min20.expR <- getratio(min20.half, min20.Dprime, txnrate)
# calculate R2
SSres <- (min5$ratio - min5.expR)^2 + (min10$ratio - min10.expR)^2 + (min20$ratio - min20.expR)^2
#ratiomean <- rowMeans(cbind(min5$ratio, min10$ratio, min20$ratio))
#SStot <- (min5$ratio - ratiomean)^2 + (min10$ratio - ratiomean)^2 + (min20$ratio - ratiomean)^2
#r_squared <- 1 - (SSres / SStot)
return(SSres)
}
#rsq.minus3 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, -3.0)[parsed.introns]
#rsq.minus2 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, -2.0)[parsed.introns]
rsq.minus1 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, -1.0)[parsed.introns]
rsq.minus08 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, -0.8)[parsed.introns]
rsq.minus06 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, -0.6)[parsed.introns]
rsq.minus04 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, -0.4)[parsed.introns]
rsq.minus02 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, -0.2)[parsed.introns]
rsq.zero <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, 0)[parsed.introns]
rsq.plus02 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, 0.2)[parsed.introns]
rsq.plus04 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, 0.4)[parsed.introns]
rsq.plus06 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, 0.6)[parsed.introns]
rsq.plus08 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, 0.8)[parsed.introns]
rsq.plus1 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, 1)[parsed.introns]
#rsq.plus2 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, 2)[parsed.introns]
#rsq.plus3 <- get_r2(min5.juncratio.data, min10.juncratio.data, min20.juncratio.data, 3)[parsed.introns]
rsq.data <- data.frame(offset = rep(c(-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1), each=length(parsed.introns)),
rsq = c(rsq.minus1, rsq.minus08, rsq.minus06, rsq.minus04, rsq.minus02,
rsq.zero,rsq.plus02, rsq.plus04, rsq.plus06, rsq.plus08, rsq.plus1))
pdf("~/Desktop/RSS_lagtime.pdf")
ggplot(rsq.data, aes(y=rsq, x=factor(offset))) + geom_boxplot(notch=T) + ylim(0,10) +
labs(x="lag time (minutes)",y="residual sum of squares")
def.off()