-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanuscript_ENCODE_figures.R
2075 lines (1562 loc) · 86.5 KB
/
manuscript_ENCODE_figures.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
library(tidyverse)
library(GenomicRanges)
library(SummarizedExperiment)
library(biomaRt)
library(DBI)
library(doParallel)
library(ggridges)
####################################################
## CONNECT TO THE SPLICING DATABASE ################
####################################################
# source("/home/sruiz/PROJECTS/splicing-accuracy-manuscript/manuscript_ENCODE_figures.R")
## CONNECT TO THE DATABASE ------------------------------
supportive_reads <- 1
gtf_version <- 105
analysis_type = "shRNA"
project_name <- paste0("ENCODE_SR_", supportive_reads, "read_", analysis_type)
database_folder <- here::here(file.path("database/", project_name, gtf_version))
database_path <- paste0(database_folder, "/", project_name, ".sqlite")
con <- dbConnect(RSQLite::SQLite(), database_path)
tables <- dbListTables(con)
## SET PATHS TO FOLDERS
base_folder <- here::here()
args <-
list(
dependencies_folder = file.path(here::here(), "dependencies"),
results_folder = file.path(here::here(), "results", project_name, gtf_version, "_paper_review", "results"),
figures_folder = file.path(here::here(), "results", project_name, gtf_version, "_paper_review", "figures"),
data_folder = file.path(here::here(), "results", project_name, gtf_version, "_paper_review", "data")
)
dir.create(file.path(args$results_folder), recursive = TRUE, showWarnings = F)
dir.create(file.path(args$figures_folder), recursive = TRUE, showWarnings = F)
dir.create(file.path(args$data_folder), recursive = TRUE, showWarnings = F)
## QUERY MASTER TABLES
query = paste0("SELECT * FROM 'metadata'")
master_metadata <- dbGetQuery(con, query) %>% as_tibble()
all_projects <- master_metadata$SRA_project %>% unique
all_projects %>% length
query <- paste0("SELECT * FROM 'intron'")
master_introns <- dbGetQuery(con, query) %>% as_tibble()
query <- paste0("SELECT * FROM 'novel'")
master_novel_junctions <- dbGetQuery(con, query) %>% as_tibble()
query <- paste0("SELECT * FROM 'transcript'")
master_transcript <- dbGetQuery(con, query) %>% as_tibble()
query <- paste0("SELECT * FROM 'gene'")
master_gene <- dbGetQuery(con, query) %>% as_tibble()
## UTILS FUNCTION
get_mode <- function(data) {
uniqv <- unique(data)
uniqv[which.max(tabulate(match(data, uniqv)))]
}
custom_ggtheme <- theme(text = element_text(size = 7, colour = "black"),
axis.ticks = element_line(colour = "black", linewidth = 2),
axis.text = element_text(size = 7, colour = "black"),
axis.line = element_line(colour = "black"),
axis.title = element_text(size = 7, colour = "black"),
axis.text.y = element_text(size = 7, colour = "black"),
axis.text.x = element_text(size = 7, colour = "black", hjust = 0.5, vjust = 0.5),
strip.text = element_text(size = 7, colour = "black"),
legend.text = element_text(size = "7", colour = "black"),
legend.title = element_blank(),
legend.position = "top")
##############################################
## FUNCTIONS - Figures for the Splicing paper
##############################################
## SECTION 1 ---------------------------------------------
get_database_stats <- function() {
tables <- dbListTables(con)
## Methods: number of samples by RIN number
master_metadata %>% nrow()
master_metadata %>%
filter(rin >= 8) %>% nrow()
master_metadata %>%
filter(rin >= 7) %>% nrow()
master_metadata %>%
filter(rin >= 6) %>% nrow()
if ( master_metadata %>% filter(rin < 6) %>% nrow() > 1 ) {
print("ERROR! Some of the samples considered present a RIN number lower than 6!")
break;
}
master_metadata %>%
dplyr::count(cluster) %>%
print(n = 50)
## We found that 268,988 (82.8%) annotated introns had at least a single associated novel donor or acceptor junction,
## with only 55,968 annotated introns appearing to be precisely spliced across all the samples and tissues studied.
master_introns %>% head()
master_introns %>% distinct(ref_junID) %>% nrow()
master_introns %>%
dplyr::count(misspliced)
## Collectively, we detected 3,865,268 unique novel junctions, equating to 14 novel junctions per annotated intron.
master_novel_junctions %>% head
master_novel_junctions %>% nrow()
master_novel_junctions %>%
dplyr::count(novel_type)
(master_novel_junctions %>% nrow()) / (master_introns %>% filter(misspliced==1) %>% nrow())
master_novel_junctions$seqnames %>% unique
## Collectively, we detected 31,811 genes and 199,551 transcripts
query <- paste0("SELECT * FROM 'transcript'")
master_transcripts <- dbGetQuery(con, query) %>% as_tibble()
master_transcripts %>% nrow()
## JOIN WITH GENE DATA
query <- paste0("SELECT * FROM 'gene'")
master_genes <- dbGetQuery(con, query) %>% as_tibble()
master_genes %>% distinct(gene_id) %>% nrow()
## Novel junctions exceed in X fold to annotated introns
(master_novel_junctions %>% distinct(novel_junID) %>% nrow()) / (master_introns %>% distinct(ref_junID) %>% nrow())
## Percentage of mis-spliced introns
(((master_introns %>%
dplyr::count(misspliced) %>%
filter(misspliced == 1) %>%
pull(n)) * 100 )) /
((master_introns %>% distinct(ref_junID) %>% nrow()) %>%
round(digits = 1))
## Collectively, we detected X novel junctions
master_novel_junctions %>% distinct(novel_junID) %>% nrow()
## equating to 14 novel junctions per an annotated junction.
((master_novel_junctions %>% distinct(novel_junID) %>% nrow()) / (master_introns %>%
dplyr::count(misspliced) %>%
filter(misspliced == "Yes") %>%
pull(n))) %>%
round()
## After accounting for sample number, we found that the highest numbers of unique
## novel junctions were identified in X tissue with the lowest numbers in Y tissue,
db_metadata_tidy <- master_metadata %>%
dplyr::group_by(cluster) %>%
mutate(n=n()) %>%
dplyr::select(SRA_project,cluster,n) %>%
distinct( SRA_project, cluster, .keep_all = T)
df_novel_jxn_count <- map_df( (db_metadata_tidy$SRA_project %>% unique()), function (project_id) {
# project_id <- (db_metadata_tidy$SRA_project %>% unique())[1]
all_clusters <- db_metadata_tidy %>%
filter(SRA_project == project_id) %>%
pull(cluster) %>%
unique()
map_df(all_clusters, function (cluster_id) {
# cluster_id <- all_clusters[1]
print(paste0(cluster_id))
query <- paste0("SELECT COUNT(DISTINCT novel_junID), AVG(MSR_D), AVG(MSR_A)
FROM '", cluster_id, "_", project_id, "_misspliced'")
novel_junctions <- dbGetQuery(con, query) %>% as.double()
return(data.frame(cluster = cluster_id,
n_novel = novel_junctions[1],
mean_msrd = novel_junctions[2],
mean_msra = novel_junctions[3]))
})
})
## The detection of unique novel donor and acceptor junctions was a common finding across all tissues,
## with the highest numbers found in “Cells - EBV-transformed lymphocytes” tissue and the lowest in “Whole Blood”.
df_n_samples_msr <- db_metadata_tidy %>%
dplyr::rename(n_sample = n) %>%
inner_join(y = df_novel_jxn_count, by = "cluster") %>%
mutate(prop_novel = n_novel/n_sample)
df_n_samples_msr %>%
arrange(desc(prop_novel)) %>%
as.data.frame()
df_n_samples_msr$cluster <- df_n_samples_msr$cluster %>% as.factor()
plot_data <- df_n_samples_msr %>%
mutate(avg_n_novel_sample = n_novel/n_sample) %>%
ungroup() %>%
arrange(desc(avg_n_novel_sample))%>%
mutate(cluster = fct_inorder(cluster))
ggplot(data = plot_data) +
geom_bar(mapping = aes(x = cluster,
y = avg_n_novel_sample),
stat = "identity") +
ylab("Average number of unique novel\njunctions across samples") +
xlab("GTEx tissue") +
theme_light() +
custom_ggtheme +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
file_name <- paste0(args$figures_folder, "/avg_novel_jnx_per_tissue")
ggplot2::ggsave(paste0(file_name, ".png"), width = 180, height = 100, units = "mm", dpi = 300)
}
get_common_introns_across_experiments <- function (RBPs = NULL,
required_clusters = NULL) {
## Get list of RBPs
if ( is.null(RBPs) || RBPs == "all" ) {
all_projects <- master_metadata$SRA_project %>% unique()
RBPs <- "all"
} else {
all_projects <- RBPs
}
if ( is.null(required_clusters) ) {
required_clusters <- c("case", "control")
}
print(paste0(Sys.time(), " - getting unique and common junctions across ENCODE experiments..."))
## Getting all introns that are common across all ENCODE experiments -------------------------------------------
all_introns <- list()
for (project_id in all_projects) {
# project_id <- all_projects[1]
print(paste0(Sys.time(), " - ", project_id))
## GET THE CLUSTERS
all_clusters <- master_metadata %>%
filter(SRA_project == project_id,
cluster %in% required_clusters) %>%
distinct(cluster) %>%
pull()
print(all_clusters)
for(cluster_id in all_clusters) {
query <- paste0("SELECT DISTINCT ref_junID
FROM '", cluster_id, "_", project_id, "_nevermisspliced'")
introns <- dbGetQuery(con, query) %>% as_tibble()
query <- paste0("SELECT DISTINCT ref_junID
FROM '", cluster_id, "_", project_id, "_misspliced'")
introns <- rbind(introns, dbGetQuery(con, query) %>% as_tibble())
all_introns[[paste(c(project_id,cluster_id), collapse = "_")]] <- introns$ref_junID %>% unique()
print(paste0(Sys.time(), " - ", introns$ref_junID %>% unique() %>% length(),
" unique introns collected from '", project_id, "'"))
}
}
common_introns <- data.frame(ref_junID = Reduce(intersect, all_introns))
common_introns %>% head()
common_introns %>% nrow()
## Getting splicing data from common introns across all ENCODE experiments -------------------------------------------
common_introns_all_experiments <- map_df(all_projects, function(project_id) {
# project_id <- all_projects[1]
print(paste0(Sys.time(), " - ", project_id))
all_clusters <- master_metadata %>%
filter(SRA_project == project_id,
cluster %in% required_clusters) %>%
distinct(cluster) %>%
pull()
map_df(all_clusters, function(cluster_id) {
print(paste0(" --> ", cluster_id))
query <- paste0("SELECT *
FROM '", cluster_id, "_", project_id, "_nevermisspliced'")
introns <- dbGetQuery(con, query) %>% as_tibble()
query <- paste0("SELECT *
FROM '", cluster_id, "_", project_id, "_misspliced'")
introns <- plyr::rbind.fill(introns, dbGetQuery(con, query) %>% as_tibble())
introns %>%
filter(ref_junID %in% common_introns$ref_junID) %>%
mutate(RBP = project_id,
cluster = cluster_id) %>%
as_tibble() %>%
return()
})
})
## The number of unique introns should be the same across experiments
common_introns_all_experiments %>%
dplyr::group_by(RBP, cluster) %>%
distinct(ref_junID) %>%
dplyr::count() %>%
ungroup() %>%
print
## The number of novel junctions linked to the unique introns studied can differ across experiments
common_introns_all_experiments %>%
dplyr::group_by(RBP, cluster)%>%
distinct(novel_junID) %>%
dplyr::count() %>%
ungroup()%>%
print
file_name <- paste0(args$results_folder, "/common_introns_details_",
paste(RBPs,collapse = "_"), "_experiments_",
paste(required_clusters,collapse = "_"), ".rds")
saveRDS(object = common_introns_all_experiments, file = file_name)
return(common_introns_all_experiments)
}
##################################
## MAIN FIGURE 5
##################################
get_data_main_figure5_c <- function(replace = T) {
## LOAD DATABASE METADATA
metadata_RBPs <- if (analysis_type=="shRNA") {
master_metadata %>%
pivot_longer(c("Splicing regulation",
"Spliceosome",
"Exon Junction Complex",
"NMD",
"Novel_RBP",
"RNA modification"), names_to = "Category") %>%
filter(value == 1) %>%
dplyr::select(-value) %>%
distinct(target_gene, sample_id, .keep_all = T)
} else { master_metadata }
metadata_RBPs$Category %>% unique
## Load common introns ---------------------------------------
target_genes = "all"
required_clusters <- c("case", "control")
common_introns_path <- paste0(args$results_folder, "/common_introns_details_",
paste(target_genes, collapse = "_"), "_experiments_",
paste(required_clusters, collapse = "_"), ".rds")
common_introns_all_experiments <- if (!file.exists(common_introns_path)) {
message("Calculating common introns...")
get_common_introns_across_experiments(RBPs = target_genes, required_clusters = c("case", "control"))
} else {
message("Loading common introns across '", target_genes, "' experiments...")
readRDS(file = common_introns_path)
}
## Run the wilcox test on MSR_D --------------------------
if (!file.exists(paste0(args$results_folder, "/ENCODE_effectsize_MSRD.rds")) || replace) {
message("Calculating MSR_D effect sizes...")
# Load the MSR tables
MSR_D <- common_introns_all_experiments %>%
group_by(RBP, cluster) %>%
distinct(ref_junID, .keep_all = T) %>%
ungroup() %>%
dplyr::select(ref_junID, MSR_D, RBP, cluster) %>%
pivot_wider(id_cols = ref_junID,
names_from = c("RBP", "cluster"),
values_from = c("MSR_D") )
MSR_D %>% head()
MSR_D_tests <- GenerateMSRtests(target_RBPs = all_projects,
MSR_Table = MSR_D,
file_output = paste0(args$results_folder, "/ENCODE_effectsize_MSRD.rds"),
overwrite = T,
num_cores = 8)
## Add the categories
if (analysis_type=="shRNA") {
MSR_D_tests <- MSR_D_tests %>%
left_join(y = metadata_RBPs %>% distinct(target_gene, Category), by = "target_gene")
}
## Add bonferroni correction
MSR_D_tests <- MSR_D_tests %>% mutate(FDR = NA, .after = p.value)
MSR_D_tests$FDR <- p.adjust(MSR_D_tests$p.value, method = "fdr")
## Save results
saveRDS(object = MSR_D_tests, file = paste0(args$results_folder, "/ENCODE_effectsize_MSRD.rds"))
write_csv(x = MSR_D_tests %>%
distinct(target_gene, .keep_all=T) %>%
mutate(statistical_test = "Wilcoxon Rank text: rstatix::wilcox_test(data, formula, paired = TRUE, correct = TRUE, alternative = 'greater')",
H0 = "The MSR_D observations in case and control samples are symmetric about their median value.",
H1 = "The MSR_D observations in case samples are greater at their median value than the MSR_D observations in control samples."),
file = paste0(args$results_folder, "/ENCODE_effectsize_MSRD.csv"), col_names = T)
} else {
message("Loading 'ENCODE_effectsize_MSRD.rds' file ...")
MSR_D_tests <- readRDS(file = paste0(args$results_folder, "/ENCODE_effectsize_MSRD.rds"))
}
if (!file.exists(paste0(args$results_folder, "/ENCODE_effectsize_MSRA.rds"))) {
message("Calculating MSR_A effect sizes...")
MSR_A <- common_introns_all_experiments %>%
group_by(RBP, cluster) %>%
distinct(ref_junID, .keep_all = T) %>%
ungroup() %>%
dplyr::select(ref_junID, MSR_A, RBP, cluster) %>%
pivot_wider(id_cols = ref_junID,
names_from = c("RBP", "cluster"),
values_from = c("MSR_A"))
MSR_A %>% head()
MSR_A_tests <- GenerateMSRtests(target_RBPs = all_projects,
MSR_Table = MSR_A,
file_output = paste0(args$results_folder, "/ENCODE_effectsize_MSRA.rds"),
overwrite = T,
num_cores = 8)
## Add the categories
if (analysis_type=="shRNA") {
MSR_A_tests <- MSR_A_tests %>%
left_join(y = metadata_RBPs %>% distinct(target_gene, Category), by = "target_gene")
}
## Add bonferroni correction
MSR_A_tests <- MSR_A_tests %>% distinct(target_gene, .keep_all=T) %>% mutate(FDR = NA, .after = p.value)
MSR_A_tests$FDR <- p.adjust(MSR_A_tests$p.value, method = "fdr")
## Save results
saveRDS(object = MSR_A_tests, file = paste0(args$results_folder, "/ENCODE_effectsize_MSRA.rds"))
write_csv(x = MSR_A_tests %>%
distinct(target_gene, .keep_all=T) %>%
mutate(statistical_test = "Wilcoxon Rank text: rstatix::wilcox_test(data, formula, paired = TRUE, correct = TRUE, alternative = 'greater')",
H0 = "The observations MSR_A in case samples vs control samples are symmetric about their median value.",
H1 = "The observations MSR_A in case samples are greater at their median value than the observations MSR_A in control samples."),
file = paste0(args$results_folder, "/ENCODE_effectsize_MSRA.csv"))
} else {
message("Loading 'ENCODE_effectsize_MSRA.rds' file ...")
MSR_A_tests <- readRDS(file = paste0(args$results_folder, "/ENCODE_effectsize_MSRA.rds"))
}
##########################
## PAPER STATS
##########################
## 1. TEST ---------------------------------------------------------------------------------------------------------
## MSR_D
## Firstly, it revealed a significant increase in mis-splicing rates in samples with gene knockdowns compared to untreated controls for 90% of the 54 genes considered
(MSR_D_tests %>%
distinct(target_gene, .keep_all = T) %>%
filter(FDR <= 0.05) %>%
distinct(target_gene) %>%
nrow() * 100) / ( MSR_D_tests %>%
distinct(target_gene, .keep_all = T) %>%
distinct(target_gene)%>%
nrow() )
MSR_D_tests %>% distinct(target_gene, .keep_all = T) %>% filter(FDR <= 0.05) %>% distinct(target_gene, .keep_all=T)
MSR_D_tests %>% distinct(target_gene, .keep_all = T) %>% filter(FDR <= 0.05) %>% distinct(target_gene, .keep_all=T) %>% pull(FDR) %>% summary
## MSR_A
## increase in mis-splicing rates in samples with gene knockdowns compared to untreated controls for 90% of the 54 genes considered
(MSR_A_tests %>%
filter(FDR <= 0.05) %>%
distinct(target_gene) %>%
nrow() * 100) / ( MSR_A_tests %>%
distinct(target_gene)%>%
nrow() )
MSR_A_tests %>% distinct(target_gene, .keep_all = T) %>% filter(FDR <= 0.05) %>% distinct(target_gene, .keep_all=T)
MSR_A_tests %>% distinct(target_gene, .keep_all = T) %>% filter(FDR <= 0.05) %>% distinct(target_gene, .keep_all=T) %>% pull(FDR) %>% summary
## 2ND TEST ------------------------------------------------------------------------------------
## Knockdowns of the splicing machinery components tended to have a greater effect on 3’ss than 5’ss mis-splicing
## MSR_D
MSR_D_tests %>%
distinct(target_gene, .keep_all = T) %>%
filter(FDR <= 0.05,
effect_size==max(effect_size)) %>%
distinct(target_gene, .keep_all=T)
MSR_D_tests %>%
distinct(target_gene, .keep_all = T) %>%
filter(FDR <= 0.05) %>%
distinct(target_gene, .keep_all=T) %>%
pull(effect_size) %>%
summary
## MSR_A
MSR_A_tests %>%
distinct(target_gene, .keep_all = T) %>%
filter(FDR <= 0.05,
Category == "Spliceosome") %>%
#distinct(target_gene, .keep_all=T) %>%
pull(effect_size) %>%
summary
## (...) except for 6 genes (including SAFB2 which is not thought to impact on splicing and so was used as a negative control)
c(MSR_D_tests %>%
filter(FDR > 0.05) %>%
distinct(target_gene, .keep_all=T) %>% pull(target_gene),
MSR_A_tests %>%
filter(FDR > 0.05) %>%
distinct(target_gene, .keep_all=T) %>% pull(target_gene)) %>% unique
## Notably, AQR, EIF4A3, SF3A3, U2AF1, U2AF2, and MAGOH knockdowns resulted in the highest increases in 5’ss
MSR_D_tests %>%
distinct(target_gene, .keep_all = T) %>%
filter(FDR <= 0.05) %>%
arrange(desc(effect_size) )
## Notably, AQR, EFTUD2, HNRNPC, MAGOH, SF3A3, SF3B4 and U2AF1 knockdowns resulted in the highest increases in 3’ss mis-splicing
MSR_A_tests %>%
distinct(target_gene, .keep_all = T) %>%
filter(FDR <= 0.05) %>%
arrange(desc(effect_size) )
}
main_figure5_c <- function() {
## Run the wilcox test on MSR_A --------------------------
message("Loading 'ENCODE_effectsize_MSRD.rds' file ...")
MSR_D_tests <- readRDS(file = paste0(args$results_folder, "/ENCODE_effectsize_MSRD.rds")) %>% distinct(target_gene, Category, .keep_all =T)
message("Loading 'ENCODE_effectsize_MSRA.rds' file ...")
MSR_A_tests <- readRDS(file = paste0(args$results_folder, "/ENCODE_effectsize_MSRA.rds")) %>% distinct(target_gene, Category, .keep_all =T)
RBPs_subgroups <- readxl::read_excel("dependencies/RBPs_subgroups.xlsx")
#Tidy
MSR_D_tests <- MSR_D_tests %>%
left_join(y = RBPs_subgroups %>% gather(category, type, -id,-name) %>% filter(type == 1),
by = c("target_gene" = "name")) %>%
mutate(category = ifelse(is.na(category), Category, category)) %>%
dplyr::select(-c(id, Category, type)) %>% dplyr::rename(Category=category)
MSR_A_tests <- MSR_A_tests %>%
left_join(y = RBPs_subgroups %>% gather(category, type, -id,-name) %>% filter(type == 1),
by = c("target_gene" = "name")) %>%
mutate(category = ifelse(is.na(category), Category, category)) %>%
dplyr::select(-c(id, Category, type)) %>% dplyr::rename(Category=category)
##########################
## TIDY EFFECT SIZES
##########################
# Combine both MSR_A and MSR_D -------------------
MSR_combined = rbind(MSR_A_tests %>% mutate(MSR_type = "MSR_A"),
MSR_D_tests %>% mutate(MSR_type = "MSR_D")) %>%
filter(FDR <= 0.05 | target_gene == "SAFB2")## 'SAFB2' is a novel RBP that will be used as control
#MSR_combined[is.na(MSR_combined$Category),] %>% print(n=100)
#MSR_combined$Category[is.na(MSR_combined$Category)] <- "Novel_RBP"
max_genes <- 10
# Filter the Splicing regulation category
filter_splicing_regulation <- MSR_combined %>%
distinct(target_gene, .keep_all = T) %>%
arrange(-effect_size) %>%
filter(Category == "Splicing regulation") %>%
head(max_genes) %>%
pull(target_gene)
filter_spliceosome <- MSR_combined %>%
distinct(target_gene, .keep_all = T) %>%
arrange(-effect_size) %>%
filter(Category == "Spliceosome") %>%
head(max_genes) %>%
pull(target_gene)
MSR_graph_data <- MSR_combined %>%
filter(target_gene %in% c(filter_splicing_regulation, filter_spliceosome, "UPF1", "UPF2", "MAGOH", "SAFB2")) %>% #| Category != c("Splicing regulation") ) %>%
arrange(-effect_size) %>%
mutate(target_gene = factor(target_gene, levels = .$target_gene %>% unique)) %>%
distinct(target_gene, MSR_type, .keep_all = T)
if (analysis_type == "shRNA") {
MSR_graph_data <- MSR_graph_data %>%
mutate(# Use factors to sort the graph
Category = factor(Category, levels = c("Splicing regulation",
"Spliceosome",
"NMD",
"Exon Junction Complex",
"Novel_RBP"))) # Use factors to sort the graph)
print(MSR_graph_data$Category %>% unique())
}
##########################
## METADATA KEFF
##########################
metadata_kEff_path <- file.path("ENCODE_SR/metadata_WB_kEff.tsv")
metadata_kEff <- readr::read_delim(metadata_kEff_path, show_col_types = F) %>%
mutate(kEff_text = ifelse(is.na(kEff_avg), kEff_avg, paste0(round(kEff_avg), "%"))) %>%
mutate(kEff_text = kEff_text %>% as.factor())
MSR_graph_data <- MSR_graph_data %>%
left_join(y = metadata_kEff, by = "target_gene") %>%
dplyr::select(-c(statistical_test, H0,H1)) %>%
distinct(target_gene, Category, MSR_type, .keep_all = T)
MSR_graph_data %>% head()
## Save data
write.csv(x = MSR_graph_data, file = file.path(args$data_folder,"figure5_c.csv"), row.names = T)
##########################
## PLOT
##########################
MSR_graph_data <- MSR_graph_data %>%
mutate(Category = as.character(Category)) %>%
mutate(Category = ifelse(Category == "Exon Junction Complex", "EJC", Category)) %>%
mutate(Category = ifelse(Category == "Novel_RBP", "Control", Category)) %>%
mutate(Category = str_replace(string = Category, pattern = "_", replacement = " ")) %>%
mutate(Category = ifelse(Category == "Splicing regulation", str_to_title(string = Category), Category)) %>%
mutate(Category = factor(Category,
levels = c("Splicing Regulation", "Spliceosome", "NMD", "EJC", "Control" ))) %>%
arrange(Category, desc(effect_size)) %>% # First by 'type', then by 'prop' descending
mutate(target_gene = factor(target_gene, levels = unique(target_gene))) # Reorder target_gene
MSR_graph_data$MSR_type = factor(MSR_graph_data$MSR_type, levels = c( "MSR_A","MSR_D"))
# Plot the graph
plot_effectsize <- ggplot(MSR_graph_data, aes(x = target_gene, y = effect_size)) +
geom_bar(aes(fill = MSR_type),
stat = "identity", color = "black",
linewidth = 0.25, width = 0.80, position = "dodge") +
scale_y_continuous(limits = c(0,0.8),
expand = expansion(mult = c(0, 0.02)),
breaks = seq(0, 0.8, 0.1),
labels = c("0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "", "")) +
scale_x_discrete(expand = expansion(add = c(0.7, 0.7))) +
labs(x = "Target gene shRNA knockdown",
y = "Probability of superior MSR in\ngene knockdown vs. untreated samples") +
scale_fill_manual(values = c("#35B779FF","#64037d"),
labels = c("MSR_A" = "MSR Acceptor", "MSR_D" = "MSR Donor"),
breaks = c("MSR_D", "MSR_A")) +
guides(fill = guide_legend(title = NULL, order = 2, ncol = 2, nrow = 1 )) +
theme_light() +
ggforce::facet_row(facets = vars(Category),
scales = "free_x", space = "free",
#labeller = labeller(Category = category_labels),
drop = T,
shrink = T) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1),
legend.box = "horizontal") +
guides(colour = guide_legend(override.aes = list(fill = '#999999'), title = NULL, label.position = "bottom", order = 1)) +
ggnewscale::new_scale_fill() +
geom_tile(stat = "identity",
aes(y = 0.68, fill = kEff_avg, color = "No data\navailable"),
linewidth = 0.5, width = 1, height = 0.045) +
geom_text(aes(y = 0.76, label = kEff_text), color = "black", size = 2.5) +
viridis::scale_fill_viridis(option = "inferno",
# na.value = "#999999",
name = "Knockdown\nEfficiency",
limits = c(0, 100),
breaks = c(NA, seq(0, 100, 25)),
labels = c("none", paste0(seq(0, 100, 25), "%")),
guide = guide_colourbar(frame.colour = "black",
frame.linewidth = 0.4,
order = 1,
ticks.colour = "black",
barwidth = 10,
barheight = 1.5)) +
scale_colour_manual(values = c( "No data\navailable" = "black")) +
custom_ggtheme +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1), legend.box = "horizontal") +
guides(colour = guide_legend(override.aes = list(fill = '#999999'), title = NULL, label.position = "bottom", order = 1))
plot_effectsize
# Save the graph
figure_name <- file.path(args$figures_folder, "main_figure5c")
ggsave(file = paste0(figure_name, ".png"), plot = plot_effectsize, width = 180, height = 90, units = "mm", dpi = 300)
ggsave(file = paste0(figure_name, ".svg"), plot = plot_effectsize, width = 180, height = 90, units = "mm", dpi = 300)
}
##################################
## MAIN FIGURE 6
##################################
main_figure6_a <- function() {
## LOAD COMMON INTRONS AND NOVEL FOR AQR AND U2AF2
target_RBPs <- c("AQR", "U2AF2")
required_clusters <- c("case", "control")
common_introns_path <- paste0(args$results_folder, "/common_introns_details_",
paste(target_RBPs, collapse = "_"), "_experiments_",
paste(required_clusters, collapse = "_"), ".rds")
## LOAD COMMON INTRONS
common_introns <- if (file.exists(common_introns_path)) {
readRDS(file = common_introns_path) %>% as_tibble()
} else {
get_common_introns_across_experiments(RBPs = target_RBPs,
required_clusters = required_clusters)
}
limit_bp = 30
RBP_novel <- common_introns %>%
filter(RBP %in% target_RBPs) %>%
inner_join(master_introns %>% dplyr::select(ref_junID), by = "ref_junID") %>%
left_join(master_novel_junctions %>% dplyr::select(novel_junID, novel_type, distance), by = "novel_junID") %>%
group_by(RBP, cluster) %>%
distinct(novel_junID, .keep_all = TRUE) %>%
ungroup() %>%
mutate(novel_type = str_replace(string = novel_type, pattern = "_", replacement = " ")) %>%
filter(abs(distance) < limit_bp) %>%
mutate(RBP = factor(x = RBP, levels = target_RBPs)) %>%
dplyr::select(distance, cluster, novel_type, RBP) %>%
mutate(cluster = factor(ifelse(cluster == "case" & novel_type == "novel donor", "gene knockdown donor",
ifelse(cluster == "case" & novel_type == "novel acceptor", "gene knockdown acceptor", "control")),
levels = c("control", "gene knockdown donor", "gene knockdown acceptor"))) %>%
mutate(novel_type = str_to_title(novel_type))
## Save source data
write_csv(x = RBP_novel, file = file.path(args$data_folder, "figure6_a.csv"), col_names = T)
#################################################
## PLOT AQR AND U2AF1 DISTANCES
#################################################
distances_plot <- ggplot(data = RBP_novel) +
geom_histogram(aes(x = distance, fill = fct_rev(cluster)),
bins = 60, binwidth = 1, position = "identity", alpha = 1, color = "black", linewidth = 0.1) +
scale_x_continuous(breaks = seq(-limit_bp, limit_bp, length.out = 5)) +
scale_fill_manual(values = c("#35B779FF", "#8d03b0", "#666666"),
breaks = c("gene knockdown donor", "gene knockdown acceptor", "control"),
labels = c("gene knockdown donor", "gene knockdown acceptor", "control")) +
labs(x = "Distance (bp)", y = "Number of unique novel junctions") +
facet_grid(fct_rev(novel_type)~RBP) +
guides(fill = guide_legend(title = "Sample type: ", ncol = 2, nrow = 1 )) +
theme_light() +
custom_ggtheme +
theme(legend.position = "none")
distance_rectangle <- ggplot() +
geom_rect(aes(xmin = 0, xmax = limit_bp, ymin = 1, ymax = 60), fill = "grey", color = "black") +
geom_text(aes(x = 15, y = 33), size = 3, label = "exon") +
geom_rect(aes(xmin = (limit_bp)*-1, xmax = 0, ymin = 30, ymax = 31), fill = "grey", alpha = 1, color = "black") +
geom_text(aes(x = -15, y = 48), size = 3, label = "intron") +
theme_void()
distances_plot <- distances_plot / (distance_rectangle + distance_rectangle) + patchwork::plot_layout(heights = c(8, 1))
distances_plot
figure_name <- file.path(args$figures_folder, "main_figure6_a")
ggsave(file = paste0(figure_name, ".png"), width = 180, height = 80, dpi = 300, units = "mm")
ggsave(file = paste0(figure_name, ".svg"), width = 180, height = 80, dpi = 300, units = "mm")
}
main_figure6_b <- function() {
## LOAD COMMON INTRONS AND NOVEL FOR AQR AND U2AF2
target_RBPs <- c("AQR", "U2AF2")
required_clusters <- c("case", "control")
common_introns_path <- paste0(args$results_folder, "/common_introns_details_",
paste(target_RBPs, collapse = "_"), "_experiments_",
paste(required_clusters, collapse = "_"), ".rds")
## LOAD COMMON INTRONS
common_introns <- if (file.exists(common_introns_path)) {
readRDS(file = common_introns_path) %>% as_tibble()
} else {
get_common_introns_across_experiments(RBPs = target_RBPs,
required_clusters = required_clusters)
}
## Add MES 5' and 3' info to the common introns and join with novel junctions
RBP_novel <- common_introns %>%
filter(RBP %in% target_RBPs) %>%
mutate(cluster = factor(ifelse(cluster == "case", "gene knockdown", "control"),
levels = c("control", "gene knockdown"))) %>%
inner_join(master_introns %>% dplyr::select(ref_junID, ref_mes5ss, ref_mes3ss), by = "ref_junID") %>%
left_join(master_novel_junctions %>%
dplyr::select(novel_junID, novel_type, distance, novel_mes5ss, novel_mes3ss),
by = "novel_junID") %>%
group_by(RBP, cluster) %>%
distinct(novel_junID, .keep_all = TRUE) %>%
ungroup() %>%
mutate(delta_ss5score = ref_mes5ss - novel_mes5ss,
delta_ss3score = ref_mes3ss - novel_mes3ss)
## Statistical Tests
run_wilcox_test <- function(data, RBP) {
wilcox.test(x = data %>% filter(RBP == RBP, cluster == "gene knockdown") %>% pull(delta_ss3score),
y = data %>% filter(RBP == RBP, cluster == "control") %>% pull(delta_ss3score),
paired = FALSE,
alternative = "greater")
}
RBP_stats <- map(target_RBPs, ~run_wilcox_test(RBP_novel, .))
#################################################
## PREPARE DATA TO PLOT
#################################################
RBP_novel_acceptor_delta <- RBP_novel %>%
filter(novel_type == "novel_acceptor") %>%
dplyr::group_by(RBP, cluster) %>%
mutate(medianMSR = median(delta_ss3score)) %>%
ungroup() %>%
mutate(cluster = factor(cluster, levels = c("gene knockdown", "control"))) %>%
dplyr::group_by(RBP) %>%
mutate(
p.value = format(
wilcox.test(delta_ss3score ~ cluster,
data = cur_data(),
alternative = "greater",
subset = cluster %in% c("gene knockdown", "control"),
exact = T)$p.value,
digits = 2, scientific = TRUE
)
) %>%
ungroup()%>%
mutate(p.value = ifelse(as.numeric(p.value) == 0, "2.2e-16", p.value)) %>%
mutate(RBP = factor(RBP, levels = target_RBPs)) %>%
dplyr::select(delta_ss3score, cluster, medianMSR, RBP, p.value)
# RBP_novel_acceptor_delta <- RBP_novel %>%
# filter(novel_type == "novel_acceptor") %>%
#
# dplyr::group_by(RBP, cluster) %>%
# mutate(medianMSR = median(delta_ss3score)) %>%
# ungroup() %>%
# dplyr::group_by(RBP) %>%
# mutate(p.value = format(wilcox.test(delta_ss3score ~ cluster)$p.value, digits = 2, scientific = TRUE)) %>%
# ungroup %>%
# mutate(p.value = ifelse(as.numeric(p.value) == 0, "2.2e-16", p.value)) %>%
# mutate(RBP = factor(RBP, levels = target_RBPs)) %>%
# dplyr::select(delta_ss3score, cluster, medianMSR, RBP, p.value)
write.csv(RBP_novel_acceptor_delta, file = file.path(args$data_folder, "figure6_b.csv"), row.names = FALSE)
#################################################
## PLOT DELTA MES ONLY ACCEPTOR - AQR and U2AF2
#################################################
delta_mes_acceptor <- ggplot(data = RBP_novel_acceptor_delta) +
geom_density(aes(x = delta_ss3score, fill = cluster), alpha = 0.8, linewidth = 0.3, color = "black") +
geom_vline(xintercept = 0) +
geom_vline(aes(xintercept = medianMSR, color = cluster), linetype = "dashed", linewidth = 0.9) +
facet_wrap(vars(RBP)) +
geom_text(data = distinct(RBP_novel_acceptor_delta, RBP, p.value),
aes(label = paste0("P<", p.value)), x = 25, y = 0.08, size = 3, color = "#333333") +
scale_fill_manual(values = c("#64037d", "#999999"), breaks = c("gene knockdown", "control"), labels = c("shRNA knockdown", "Control")) +
scale_colour_manual(values = c("#64037d", "#333333"), breaks = c("gene knockdown", "control"), labels = c("shRNA knockdown", "Control")) +
labs(x = "Delta MES Acceptor") +
theme_light() +
custom_ggtheme
delta_mes_acceptor
figure_name <- file.path(args$figures_folder, "main_figure6_b")
ggsave(file = paste0(figure_name, ".png"), width = 180, height = 60, dpi = 300, units = "mm")
ggsave(file = paste0(figure_name, ".svg"), width = 180, height = 60, dpi = 300, units = "mm")
## Supplementary Figure for AQR distances
# supplementary_figure14(RBP_novel)
}
main_figure6_c <- function() {
################################
## LOAD METADATA & RBPs
################################
all_projects <- unique(master_metadata$SRA_project)
target_genes <- "all"
required_clusters <- c("case", "control")
file_name <- paste0(args$results_folder, "/common_introns_details_", target_genes, "_experiments_",
paste(required_clusters, collapse = "_"), ".rds")
common_introns_all_experiments <- if (file.exists(file_name)) {
message("Loading common introns across all experiments...")
readRDS(file = file_name)
} else {
get_common_introns_across_experiments(RBPs = target_genes, required_clusters = required_clusters)
}
# Summary of introns across experiments
common_introns_summary <- common_introns_all_experiments %>% group_by(RBP, cluster) %>% distinct(ref_junID) %>% dplyr::count() %>% ungroup()
####################################
## LOAD MSR VALUES
####################################
MSR_RBPs <- common_introns_all_experiments %>%
inner_join(master_introns %>% dplyr::select(ref_junID, seqnames, start, end, strand), by = "ref_junID") %>%
dplyr::select(ref_junID, MSR_D, MSR_A, RBP, cluster, seqnames, start, end, strand) %>%
group_by(RBP, cluster) %>%
distinct(ref_junID, .keep_all = TRUE) %>%
ungroup()
####################################
## MERGE ENCODE and iCLIP DATA
####################################
iclip_results_path <- file.path(args$results_folder, "/iCLIP_ENCODE_all_introns_chisq.csv")
if (!file.exists(iclip_results_path)) {
process_iCLIP_data <- function(target_RBP) {
message("Processing RBP: ", target_RBP)