-
Notifications
You must be signed in to change notification settings - Fork 0
/
manuscript_AD_figures.R
2000 lines (1409 loc) · 71.7 KB
/
manuscript_AD_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(DBI)
library(doParallel)
####################################################
## CONNECT TO THE SPLICING DATABASE ################
####################################################
## source("/home/sruiz/PROJECTS/splicing-accuracy-manuscript/manuscript_AD_figures.R")
## CONNECT TO THE DATABASE ------------------------------
base_folder <- here::here()
# base_folder <- "/mnt/PROJECTS/splicing-accuracy-manuscript"
gtf_version <- 105
#SRP058181
#case_type <- "PD"
#control_type <- "control"
# project_id <- "SRP100948"
# case_type <- "AD"
# control_type <- "control"
# project_id <- "OV"
#main_project_identifier <- "TCGA"
main_project_identifier <- "SRP100948"
supportive_reads <- 1
data_subsample <- F
#project_name <- paste0(main_project_identifier, "_", supportive_reads, "read_subsample", data_subsample) #SRP100948_
project_name <- paste0(main_project_identifier, "_", supportive_reads, "read")
args <-
list(
project_id = "SRP100948",
case_type = "AD",
control_type = "control",
database_folder = file.path(base_folder, "database", project_name, gtf_version),
results_folder = file.path(base_folder, "results", project_name, gtf_version, "_paper_review", "results"),
figures_folder = file.path(base_folder, "results", project_name, gtf_version, "_paper_review", "figures"),
data_folder = file.path(base_folder, "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)
database_path <- file.path(args$database_folder, "/", paste0(project_name, ".sqlite"))
con <- dbConnect(RSQLite::SQLite(), database_path)
dbListTables(con)
## QUERY MASTER TABLES
query = paste0("SELECT * FROM 'metadata'")
df_metadata <- dbGetQuery(con, query) %>% distinct(sample_id, .keep_all = T) %>% as_tibble()
all_projects <- df_metadata$SRA_project %>% unique
## GET INTRON DATA
query <- paste0("SELECT * FROM 'intron'")
master_introns <- dbGetQuery(con, query) %>% as_tibble()
## GET NOVEL DATA
query <- paste0("SELECT * FROM 'novel'")
master_novel_junctions <- dbGetQuery(con, query) %>% as_tibble()
## GET GENE & TRANSCRIPT TABLES DATA
query <- paste0("SELECT * FROM 'transcript'")
master_transcripts <- dbGetQuery(con, query) %>% as_tibble()
query <- paste0("SELECT * FROM 'gene'")
master_gene <- dbGetQuery(con, query) %>% as_tibble()
all_projects <- df_metadata$SRA_project %>% unique
all_clusters <- df_metadata$cluster %>% unique()
## UTILS
get_mode <- function(data) {
uniqv <- unique(data)
uniqv[which.max(tabulate(match(data, uniqv)))]
}
custom_ggtheme <- theme(text = element_text(size = 9, family="Arial", colour = "black"),
axis.ticks = element_line(colour = "black"),
axis.text = element_text(size = 9, family="Arial", colour = "black"),
axis.line = element_line(colour = "black"),
axis.title = element_text(size = 9, family="Arial", colour = "black"),
axis.text.y = element_text(size = 9, family="Arial", colour = "black"),
axis.text.x = element_text(size = 9, family="Arial", colour = "black", hjust = 0.5, vjust = 0.5),
strip.text = element_text(size = 9, family="Arial", colour = "black"),
legend.text = element_text(size = 8, family="Arial", colour = "black"),
legend.position = "top",
legend.box = "vertical")
########################################
## FUNCTIONS
########################################
## SECTION 0 - GET COMMON INTRONS WITH SIMILAR EXPRESSION LEVELS -----------------------------
get_common_introns <- function() {
all_introns <- map_df(c(args$control_type, args$case_type), function(cluster_id) {
print(paste0(Sys.time(), " - ", args$project_id, " - ", cluster_id))
query <- paste0("SELECT ref_junID, MSR_D, MSR_A, ref_type, ref_sum_counts, ref_n_individuals
FROM '", cluster_id, "_", args$project_id, "_nevermisspliced'")
introns_accurate_splicing <- dbGetQuery(con, query) %>% as_tibble()
query <- paste0("SELECT ref_junID, MSR_D, MSR_A, ref_type, ref_sum_counts, ref_n_individuals
FROM '", cluster_id, "_", args$project_id, "_misspliced'")
introns_inaccurate_splicing <- dbGetQuery(con, query) %>% as_tibble()
rbind(introns_inaccurate_splicing, introns_accurate_splicing) %>% mutate(sample_type = cluster_id) %>% distinct(ref_junID, .keep_all = T)
})
all_introns %>%
dplyr::group_by(ref_junID) %>%
filter(n() > 1) %>%
ungroup %>%
group_by(sample_type) %>%
mutate(mean_coverage = (ref_sum_counts/ref_n_individuals) %>% log10()) %>%
ungroup() %>%
return()
}
subsample_introns <- function(df_introns) {
set.seed(1000)
print(paste0(Sys.time(), " - start subsampling ... "))
## Subsampling introns to control by similarity in mean read coverage
m.out <- MatchIt::matchit(sample_type ~ mean_coverage,
data = df_introns %>% mutate(sample_type = sample_type %>% as.factor()),
distance = df_introns$mean_coverage,
method = "nearest",
caliper = c(mean_coverage = .005),
std.caliper = FALSE)
subsample <- MatchIt::match.data(m.out)
subsample %>% dplyr::count(sample_type)
subsample %>% group_by(sample_type) %>% distinct(ref_junID) %>% ungroup() %>% dplyr::count(sample_type)
saveRDS(object = subsample, file = file.path(args$results_folder, "common_subsampled_introns_seed1000.rds"))
print(paste0(Sys.time(), " - subsampling finished!"))
}
## SECTION 1 - GENERAL TESTS -----------------------------------------------------------------
index_database <- function() {
## INTRON TABLE -----------------------------------------------------------------
## verify indexes exist on 'intron' master table
query <- paste0("SELECT * FROM 'sqlite_master'
WHERE tbl_name = 'intron'
AND name = 'index_intron_coord'")
if (nrow(dbGetQuery(con, query)) == 0) {
query <- paste0("CREATE UNIQUE INDEX 'index_intron_coord' ON 'intron'(ref_coordinates)");
res <- DBI::dbSendQuery(conn = con, statement = query)
DBI::dbClearResult(res)
}
## NOVEL TABLE -----------------------------------------------------------------
## verify indexes exist on 'novel' master table
query <- paste0("SELECT * FROM 'sqlite_master' WHERE tbl_name = 'novel' AND name = 'index_novel'")
if ( nrow(dbGetQuery(con, query)) == 0 ) {
query <- paste0("CREATE UNIQUE INDEX 'index_novel' ON 'novel'(ref_junID,novel_junID)");
res <- DBI::dbSendQuery(conn = con, statement = query)
DBI::dbClearResult(res)
}
## verify indexes exist on 'novel' master table
query <- paste0("SELECT * FROM 'sqlite_master' WHERE tbl_name = 'novel' AND name = 'index_novel_coord'")
if ( nrow(dbGetQuery(con, query)) == 0 ) {
query <- paste0("CREATE UNIQUE INDEX 'index_novel_coord' ON 'novel'(novel_coordinates)");
res <- DBI::dbSendQuery(conn = con, statement = query)
DBI::dbClearResult(res)
}
## TRANSCRIPT TABLE -----------------------------------------------------------------
query <- paste0("SELECT * FROM 'sqlite_master' WHERE tbl_name = 'transcript' AND name = 'index_transcript_id'")
if ( nrow(dbGetQuery(con, query)) == 0 ) {
query <- paste0("CREATE UNIQUE INDEX 'index_transcript_id' ON 'transcript'(id)");
res <- DBI::dbSendQuery(conn = con, statement = query)
DBI::dbClearResult(res)
}
query <- paste0("SELECT * FROM 'sqlite_master' WHERE tbl_name = 'transcript' AND name = 'index_transcript_ensembl_id'")
if ( nrow(dbGetQuery(con, query)) == 0 ) {
query <- paste0("CREATE UNIQUE INDEX 'index_transcript_ensembl_id' ON 'transcript'(transcript_id)");
res <- DBI::dbSendQuery(conn = con, statement = query)
DBI::dbClearResult(res)
}
## GENE TABLE -----------------------------------------------------------------
## verify indexes exist on 'gene' master tableg
query <- paste0("SELECT * FROM 'sqlite_master' WHERE tbl_name = 'gene' AND name = 'index_gene_id'")
if ( nrow(dbGetQuery(con, query)) == 0 ) {
query <- paste0("CREATE UNIQUE INDEX 'index_gene_id' ON 'gene'(id)");
res <- DBI::dbSendQuery(conn = con, statement = query)
DBI::dbClearResult(res)
}
## verify indexes exist on 'gene' master tableg
query <- paste0("SELECT * FROM 'sqlite_master' WHERE tbl_name = 'gene' AND name = 'index_gene_ensembl_id'")
if ( nrow(dbGetQuery(con, query)) == 0 ) {
query <- paste0("CREATE UNIQUE INDEX 'index_gene_ensembl_id' ON 'gene'(gene_id)");
res <- DBI::dbSendQuery(conn = con, statement = query)
DBI::dbClearResult(res)
}
}
get_database_stats <- function() {
df_metadata %>% nrow()
if ( any( names(df_metadata) == "rin" ) ) {
df_metadata %>%
filter(rin >= 8) %>% nrow()
df_metadata %>%
filter(rin >= 7) %>% nrow()
df_metadata %>%
filter(rin >= 6) %>% nrow()
}
## This database included a set of 245,738 annotated introns (Ensembl-v105=)
## 149,649 of them with no evidence of mis-splicing and 96,089 introns with at least one linked novel split read)
master_introns %>% distinct(ref_junID) %>% nrow()
master_introns %>% dplyr::count(misspliced)
## and a linked set of 219,658 novel junctions (125,085 novel acceptor and 94,573 novel donor junctions),
master_novel_junctions %>% nrow()
master_novel_junctions %>% dplyr::count(novel_type)
## originating from 23,999 genes and 181,284 transcripts
master_transcripts %>% distinct(transcript_id) %>% nrow()
master_gene %>% distinct(gene_id) %>% nrow()
}
## SECTION 2 - PAPER FIGURES -----------------------------------------------------------------
## Main figures
#' Title
#' Visualise differences in number of unique novel junctions from common introns from AD vs control samples
#' Only using common, subsampled introns
#' @return
#' @export
#'
#' @examples
main_figure8_a <- function() {
## Load common introns
common_introns_subsample <- readRDS(file = file.path(args$results_folder, "/common_subsampled_introns_seed1000.rds"))
common_introns_subsample %>% dplyr::count(sample_type)
df_unique_junctions <- map_df(c(args$case_type,args$control_type), function(cluster_id) {
# cluster_id <- all_clusters[1]
print(cluster_id)
####################
## GET THE INTRONS
####################
query <- paste0("SELECT DISTINCT ref_junID FROM '", cluster_id, "_", args$project_id, "_nevermisspliced'")
introns <- dbGetQuery(con, query) %>% as_tibble()
query <- paste0("SELECT DISTINCT ref_junID FROM '", cluster_id, "_", args$project_id, "_misspliced'")
introns <- rbind(introns, dbGetQuery(con, query) %>% as_tibble())
## Only common, subsampled introns
introns <- introns %>% filter(ref_junID %in% (common_introns_subsample %>% filter(sample_type == cluster_id) %>% pull(ref_junID)))
############################
## GET THE NOVEL JUNCTIONS
############################
query <- paste0("SELECT * FROM '", cluster_id, "_", args$project_id, "_misspliced'")
novel_junctions <- dbGetQuery(con, query) %>% as_tibble()
novel_junctions <- novel_junctions %>%
inner_join(y = master_novel_junctions %>% dplyr::select(novel_junID,novel_type), by = "novel_junID") %>%
filter(ref_junID %in% (common_introns_subsample %>% filter(sample_type == cluster_id) %>% pull(ref_junID)))
###########################
## GET THE PROPORTIONS
###########################
annotated_junc <- introns %>% distinct(ref_junID) %>% nrow()
donor_junc <- novel_junctions %>% filter(novel_type == "novel_donor") %>% distinct(novel_junID) %>% nrow()
acceptor_junc <- novel_junctions %>% filter(novel_type == "novel_acceptor") %>% distinct(novel_junID) %>% nrow()
annotated_prop <- annotated_junc/(annotated_junc + donor_junc + acceptor_junc)
donor_prop <- donor_junc/(annotated_junc + donor_junc + acceptor_junc)
acceptor_prop <- acceptor_junc/(annotated_junc + donor_junc + acceptor_junc)
## Return the data.frame
return(data.frame(cluster = cluster_id,
annotated_junc = annotated_junc ,
donor_junc = donor_junc,
acceptor_junc = acceptor_junc,
annotated_prop = annotated_prop,
donor_prop = donor_prop,
acceptor_prop = acceptor_prop))
})
df_unique_junctions_tidy <- df_unique_junctions %>%
dplyr::select(cluster, donor = donor_prop, acceptor = acceptor_prop, annotated_intron = annotated_prop) %>%
tidyr::gather(key = "type", value = "prop", -cluster ) %>%
mutate(prop = prop * 100)
## Save source data
write_csv(x = df_unique_junctions_tidy, file = file.path(args$data_folder, "figure8_a.csv"), col_names = T)
######################
## BAR PLOT
######################
ggplot(data = df_unique_junctions_tidy %>%
mutate(type = factor(type, levels = c("donor", "acceptor", "annotated_intron")),
prop_label = paste0(round(x = prop, digits = 2), "%")),
aes(x = cluster, y = prop, fill = type)) +
geom_bar(stat="identity", position = "stack")+
geom_text(aes(label=prop_label), vjust=2, position = "stack",
color="white", size=4, fontface = "bold") +
theme_light() +
ylab("% unique junctions") +
xlab("") +
theme(axis.line = element_line(colour = "black"),
#text = element_text(colour = "black", size = 12),
legend.position = "top") +
scale_fill_manual(values = c( "#999999","#64037d", "#35B779FF"),
breaks = c("annotated_intron","acceptor", "donor" ),
label = c("Annotated","Acceptor", "Donor" )) +
guides(fill = guide_legend(title = "", ncol = 3, nrow = 1 )) +
custom_ggtheme +
theme( plot.margin = margin(t = 5,r = 5,l = 5, b = -5),
legend.box.margin=margin(b = -10, t = -5))#+
ggplot2::ggsave(file.path(args$figures_folder, "/figure8_a.png"), width = 80, height = 70, units = "mm", dpi = 300)
}
#' Title
#' Visualise differences in the cummulative number of novel reads in common introns from AD vs control samples
#' Only using common, subsampled introns
#' @return
#' @export
#'
#' @examples
main_figure8_b <- function() {
## Load common introns
common_introns_subsample <- readRDS(file = file.path(args$results_folder, "/common_subsampled_introns_seed1000.rds"))
df_mean_counts <- map_df(all_clusters, function(cluster_id) {
# cluster_id <- all_clusters[1]
print(cluster_id)
####################
## GET EXPRESSION LEVELS FROM THE ANNOTATED INTRONS
####################
query <- paste0("SELECT DISTINCT ref_junID, ref_sum_counts, ref_n_individuals FROM '", cluster_id, "_", args$project_id, "_nevermisspliced'")
introns <- dbGetQuery(con, query) %>% as_tibble()
query <- paste0("SELECT DISTINCT ref_junID, ref_sum_counts, ref_n_individuals FROM '", cluster_id, "_", args$project_id, "_misspliced'")
introns <- rbind(introns, dbGetQuery(con, query) %>% as_tibble())
## Only common, subsampled introns
introns <- introns %>%
filter(ref_junID %in% (common_introns_subsample %>% filter(sample_type == cluster_id) %>% pull(ref_junID)))
###########################
## GET THE NOVEL JUNCTIONS
###########################
query <- paste0("SELECT ref_junID, novel_junID, novel_sum_counts, novel_n_individuals FROM '", cluster_id, "_", args$project_id, "_misspliced'")
novel_junctions <- dbGetQuery(con, query) %>% as_tibble()
novel_junctions <- novel_junctions %>%
inner_join(y = master_novel_junctions %>% dplyr::select(novel_junID, novel_type) %>% as_tibble(), by = "novel_junID") %>%
as_tibble() %>%
## Only NOVEL READS FROM common, subsampled introns
filter(ref_junID %in% introns$ref_junID)
###########################
## GET THE PROPORTIONS
###########################
annotated <- introns %>%
dplyr::distinct(ref_junID, .keep_all = T) %>%
pull(ref_sum_counts) %>%
sum()
acceptor <- novel_junctions %>%
filter(novel_type == "novel_acceptor") %>%
dplyr::distinct(novel_junID, .keep_all = T) %>%
pull(novel_sum_counts) %>%
sum()
donor <- novel_junctions %>%
filter(novel_type == "novel_donor") %>%
dplyr::distinct(novel_junID, .keep_all = T) %>%
pull(novel_sum_counts) %>%
sum()
annotated_p = annotated * 100 / (annotated + acceptor + donor)
acceptor_p = acceptor * 100 / (annotated + acceptor + donor)
donor_p = donor * 100 / (annotated + acceptor + donor)
return(data.frame(cluster = cluster_id,
type = c("annotated","acceptor", "donor"),
prop = c(annotated_p, acceptor_p, donor_p),
sum_counts = c(annotated, acceptor, donor)))
})
df_mean_counts <- df_mean_counts %>%
mutate(type = factor(type, levels = c("donor", "acceptor", "annotated")),
prop_label = paste0(round(x = prop, digits = 2), "%"))
## Save source data
write_csv(x = df_mean_counts, file = file.path(args$data_folder, "figure8_b.csv"), col_names = T)
######################
## BAR PLOT
######################
p <- ggplot( data = df_mean_counts,
aes(x = cluster, y = prop, fill = type)) +
geom_bar(stat = "identity") +
ggforce::facet_zoom(ylim = c(95,100)) +
geom_text(aes(label=prop_label), vjust=1.3, position = "stack",
color="white", size=3, fontface = "bold") +
theme_light() +
ylab("% cumulative split read counts") +
xlab("") +
custom_ggtheme +
theme(axis.line = element_line(colour = "black"),
legend.position = "top") +
scale_fill_manual(values = c( "#999999","#64037d", "#35B779FF"),
breaks = c( "annotated","acceptor", "donor"),
label = c("Annotated","Acceptor", "Donor")) +
guides(fill = guide_legend(title = "", ncol = 3, nrow = 1 )) +
theme( plot.margin = margin(t = 5,r = 5,l = 5,
b = -5),
legend.box.margin=margin(b = -10, t = -5))#+
## Only annotated zoomed graph
pb <- ggplot_build(p)
pb$data[[2]][1:6, 'alpha'] <- 0
pg <- ggplot_gtable(pb)
## save plot
png(filename = file.path(args$figures_folder, "figure8_b.png"), width = 120, height = 80, units = "mm", res = 300)
plot(pg)
dev.off()
}
#' Title
#' Visualise differences in modulo3 values in introns from AD vs control samples
#' Only using common, subsampled introns
#' @return
#' @export
#'
#' @examples
main_figure8_c <- function() {
common_introns_subsample <- readRDS(file = file.path(args$results_folder, "/common_subsampled_introns_seed1000.rds"))
## Calculate modulo3 from MANE transcripts in 100bp distance
df_modulo <- map_df(all_clusters, function(cluster_id) {
# cluster_id <- all_clusters[1]
print(paste0(Sys.time(), " - ", cluster_id))
#####################################
## GET NOVEL JUNCTIONS
#####################################
query <- paste0("SELECT novel_junID FROM '", cluster_id, "_", args$project_id, "_misspliced'")
novel_jnx <- dbGetQuery(con, query)
query <- paste0("SELECT ref_junID, novel_junID, distance FROM 'novel' WHERE novel_junID IN (", paste(novel_jnx$novel_junID, collapse = ","),")")
novel_jnx <- novel_jnx %>% left_join(y = dbGetQuery(con, query), by = "novel_junID") %>% as_tibble()
#####################################
## GET INTRON AND TRANSCRIPT INFO
#####################################
## Add the transcript and MANE info
query <- paste0("SELECT intron.ref_junID, intron.protein_coding, transcript.MANE
FROM 'intron' INNER JOIN transcript ON intron.transcript_id = transcript.id
WHERE ref_junID IN (", paste(novel_jnx$ref_junID, collapse = ","),")")
all_jxn <- novel_jnx %>%
inner_join(y = dbGetQuery(con, query), by = "ref_junID") %>% as_tibble()
df_novel_tidy <- all_jxn %>%
distinct(novel_junID, .keep_all = T) %>%
filter(abs(distance) <= 100, MANE == 1, ref_junID %in% (common_introns_subsample$ref_junID)) %>%
mutate(modulo = abs(distance) %% 3)
df_novel_tidy %>% distinct(ref_junID) %>% nrow %>% print
df_novel_tidy <- df_novel_tidy %>%
group_by(modulo) %>%
summarise(n = n()) %>%
mutate(freq = n / sum(n)) %>%
mutate(cluster = cluster_id)
return(df_novel_tidy)
})
df_modulo_tidy <- df_modulo %>% mutate(freq = freq * 100)
df_modulo_tidy$modulo = factor(df_modulo_tidy$modulo, levels = c( "0", "1", "2"))
## Save Source Data
write_csv(x = df_modulo_tidy, file = file.path(args$data_folder, "figure8_c.csv"), col_names = T)
################
## BAR PLOT
################
ggplot(data = df_modulo_tidy %>%
mutate(freq_label = paste0(round(x = freq, digits = 2), "%"),
modulo = factor(modulo, levels = c("2", "1", "0"))),
aes(x = cluster, y = freq, fill = modulo)) +
geom_bar(stat = "identity") +
xlab("") +
ylab("% of junctions") +
geom_text(aes(label=freq_label), vjust=2, position = "stack", color="white", size=4, fontface = "bold") +
ggsci::scale_fill_npg(breaks=c('0', '1', '2')) +
theme_light() +
custom_ggtheme +
guides(fill = guide_legend(title = "Modulo: ", ncol = 3, nrow = 1 )) +
theme( plot.margin = margin(t = 5, r = 5, l = 5, b = -3),
legend.box.margin=margin(b = -10, t = -5))
ggplot2::ggsave(file.path(args$figures_folder, "figure8_c.png"), width = 80, height = 90, units = "mm", dpi = 300)
}
#' Title
#' GO, KEGG and REACTOME ENRICHMENT analysis of the introns showing increasing MSR values in AD compared to control samples
#' Only using common, subsampled introns
#' @return
#' @export
#'
#' @examples
main_figure8_d_e <- function() {
## Load common introns
common_introns_subsample <- readRDS(file = file.path(args$results_folder, "/common_subsampled_introns_seed1000.rds"))
#############################
# Select and arrange necessary columns
MSR_introns <- map(c("MSR_D", "MSR_A"), function(MSR_type) {
common_introns_subsample_MSR <- common_introns_subsample %>%
dplyr::select(MSR = all_of(MSR_type), subclass, sample_type) %>%
arrange(subclass)
# Filter and merge data for 'control' and 'AD' sample types in one step
common_introns_subsample_MSR_tidy <- common_introns_subsample_MSR %>%
filter(sample_type %in% c("control", "AD")) %>%
spread(key = sample_type, value = MSR) %>%
mutate(MSR_difference = AD - control)
# Separate introns based on MSR difference
introns_MSR_increasing <- common_introns_subsample_MSR_tidy %>%
filter(MSR_difference > 0)
introns_MSR_decreasing <- common_introns_subsample_MSR_tidy %>%
filter(MSR_difference < 0)
list(MSR_type, introns_increasing = introns_MSR_increasing, introns_decreasing = introns_MSR_decreasing)
})
#############################
## Get introns with increasing MSR values (these introns were also paired by similar expression levels)
MSR_D_introns_increasing <- MSR_introns[[1]]$introns_increasing %>%
left_join(y = common_introns_subsample %>% dplyr::select(ref_junID,subclass), by = "subclass")
MSR_A_introns_increasing <- MSR_introns[[2]]$introns_increasing %>%
left_join(y = common_introns_subsample %>% dplyr::select(ref_junID,subclass), by = "subclass")
MSR_introns_increasing <- rbind(MSR_D_introns_increasing, MSR_A_introns_increasing) %>% distinct(ref_junID)
## GET MASTER INTRON DATA
df_master_intron_tidy <- master_introns %>%
dplyr::select(ref_junID, transcript_id) %>%
inner_join(y = master_transcripts %>% dplyr::select(id, gene_id),
by =c("transcript_id" = "id")) %>%
inner_join(y = master_gene %>% dplyr::select(id, gene_name, gene_id),
by =c("gene_id" = "id"))
## Add gene name to introns with increasing MSR_A values in AD compared to control
genes_increasing_MSR <- MSR_introns_increasing %>%
inner_join(y = df_master_intron_tidy, by = c("ref_junID")) %>%
distinct(gene_name) %>% pull()
genes_increasing_MSR %>% length()
## Get gene background data
bg_genes <- common_introns_subsample %>%
inner_join(y = df_master_intron_tidy, by =c("ref_junID")) %>%
distinct(gene_name) %>% pull()
################################
## KEGG ENRICHMENT
################################
library('org.Hs.eg.db')
category_terms <- 50
entrez_genes_increasing <- AnnotationDbi::mapIds(org.Hs.eg.db::org.Hs.eg.db, genes_increasing_MSR, 'ENTREZID', 'SYMBOL')#
entrez_genes_bg <- AnnotationDbi::mapIds(org.Hs.eg.db::org.Hs.eg.db, bg_genes, 'ENTREZID', 'SYMBOL')
# mapIds(org.Hs.eg.db, introns_increasing_msra, 'ENTREZID', 'SYMBOL')
ekegg_MSR <- clusterProfiler::enrichKEGG(
gene = entrez_genes_increasing,
organism = "hsa",
keyType = "kegg",
universe = entrez_genes_bg,
pAdjustMethod = "fdr")
plotKEGG <- clusterProfiler::dotplot(ekegg_MSR %>% mutate(ONTOLOGY = "KEGG") %>%
filter(!(str_detect(string = Description, pattern = "tyrosine"))) %>%
filter(!(str_detect(string = Description, pattern = "Chemical carcino"))) %>%
filter(!(str_detect(string = Description, pattern = "Protein processing in"))) %>%
filter(!(str_detect(string = Description, pattern = "Amino sugar"))),
showCategory = category_terms,
split="ONTOLOGY")
plotKEGG +
scale_y_discrete(labels =
function(x) stringr::str_wrap(x, width = 40)) +
xlab("Gene Ratio") +
ggforce::facet_row(ONTOLOGY~., scales = "free", space = "free") +
#Coord_flip() +
custom_ggtheme +
theme(text = element_text(colour = "black", size = 7),
legend.position = "top",
legend.box="horizontal",
plot.margin = margin(t = 0, b =0, r = 5, 0),
legend.margin = margin(t = -5, b = -5, r = 5, l = 1),
legend.box.margin=margin(t = -10,b = -5, r = 0,l = 0)) +
scale_size(range = c(1, 5))+
guides(colour = guide_legend(title = "q: "),
size = guide_legend(title = "Gene count: "),
nrow = 2, ncol = 2 )
ggplot2::ggsave(file.path(args$figures_folder, "figure8_d.png"), width = 120, height = 70, units = "mm", dpi = 300)
## Save Source Data
write_csv(x = ekegg_MSR %>% as.data.frame(), file = file.path(args$data_folder, "figure8_d.csv"), col_names = T)
################################
## GO ENRICHMENT
################################
ego_MSR <- clusterProfiler::enrichGO(
gene = genes_increasing_MSR,
universe = bg_genes,
keyType = "SYMBOL",
OrgDb = "org.Hs.eg.db", ##Genome wide annotation for Human, primarily based on mapping using Entrez Gene identifiers.
ont = "ALL",
pAdjustMethod = "fdr",
pvalueCutoff = 0.05,
qvalueCutoff = 0.05
)
category_terms <- 20
clusterProfiler::dotplot(ego_MSR %>% filter(ONTOLOGY == "CC"),
x = "GeneRatio",
showCategory = category_terms,
split="ONTOLOGY") +
scale_y_discrete(labels = function(x) stringr::str_wrap(x, width = 60)) +
xlab("Gene Ratio") +
ggforce::facet_col(ONTOLOGY~., scales = "free_y", space = "free") +
custom_ggtheme +
theme(legend.position = "top",legend.box = "horizontal",
plot.margin = margin(t = -5,b = 0,l = 5,r = 5),
legend.margin=margin(t = -5,b = -5, r = 5, 0),
legend.box.margin=margin(t = -5,r = 10,b = -5,l = 0)) +
scale_size(range = c(1, 5)) +
guides(size = guide_legend(title = "Gene Count: "),
colour = guide_legend(title = "q: "))
ggplot2::ggsave(file.path(args$figures_folder, "figure8_e.png"), width = 180, height = 75, units = "mm", dpi = 300)
## Save Source Data
write_csv(x = ego_MSR %>% as.data.frame(), file = file.path(args$data_folder, "figure8_e.csv"), col_names = T)
################################
## DISEASE ENRICHMENT
################################
x <- DOSE::enrichDO(gene = entrez_genes_increasing,
ont = "DO",
pvalueCutoff = 0.05,
pAdjustMethod = "BH",
minGSSize = 10,
#universe = names(entrez_genes_bg),
qvalueCutoff = 0.05,
readable = F)
head(x)
}
#' Title
#' Test for differences in MSR_D and MSR_A between the introns from AD vs control samples.
#' Using one-tailed paired Wilcoxon test
#' @return
#' @export
#'
#' @examples
paper_stats_MSR <- function() {
common_introns_subsample <- readRDS(file = file.path(args$results_folder, "/common_subsampled_introns_seed1000.rds"))
## Pair introns by subsampling criteria
## donor
common_introns_subsample_MSRD <- common_introns_subsample %>%
dplyr::select(ref_junID, MSR_D, subclass, mean_coverage, sample_type) %>%
arrange(subclass)
## acceptor
common_introns_subsample_MSRA <- common_introns_subsample %>%
dplyr::select(ref_junID, MSR_A, subclass, mean_coverage, sample_type) %>%
arrange(subclass)
#########################################
## TEST DONOR
#########################################
wilcox.test(x = common_introns_subsample_MSRD %>% filter(sample_type == args$case_type) %>% pull(MSR_D),
y = common_introns_subsample_MSRD %>% filter(sample_type == args$control_type) %>% pull(MSR_D),
alternative = "greater", paired = T, correct = T)
rstatix::wilcox_effsize(data = common_introns_subsample_MSRD,
formula = MSR_D ~ sample_type,
paired = T)
########################################
## TEST ACCEPTOR
########################################
wilcox.test(x = common_introns_subsample_MSRA %>% filter(sample_type == args$case_type) %>% pull(MSR_A),
y = common_introns_subsample_MSRA %>% filter(sample_type == args$control_type) %>% pull(MSR_A),
alternative = "greater", paired = T, correct = T)
rstatix::wilcox_effsize(data = common_introns_subsample_MSRA, formula = MSR_A ~ sample_type, paired = T)
}
## Supplementary figures
#' Title
#' Get an overview of the metadata of the project
#' @return
#' @export
#'
#' @examples
supplementary_figure27_a_to_d <- function() {
## Num samples
plot_num_samples <- ggplot(df_metadata %>% dplyr::count(SRA_project, cluster)) +
geom_bar(aes(x = n, y = SRA_project, fill = cluster),
stat = "identity", position = position_dodge()) +
theme_light() +
scale_fill_hue() +
labs(y = "", x = "Num. samples" ) +
guides(fill = guide_legend(title = NULL, ncol = 2, nrow = 1)) +
theme(axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) +
scale_fill_manual(values = c("#bfbfbf","#666666"),
breaks = c("control", args$case_type),
label = c("Control", args$case_type)) +
custom_ggtheme
plot_num_samples
if ( any(names(df_metadata) == "gender") ) {
## Gender
plot_gender <- ggplot(df_metadata %>%
mutate(gender = gender %>% as.character()) %>%
dplyr::count(cluster, gender)) +
geom_bar(aes(y = cluster, x = n, group = gender, fill = gender), alpha = 0.8,
stat = "identity", position = "dodge") +
theme_light() +
labs(y = "", x = "Num. samples" ) +
scale_fill_manual(labels = c("Male", "Female"), values = c("1", "2"), palette=scales::hue_pal()) +
guides(fill = guide_legend(title = "Gender: ", ncol = 2, nrow = 1)) +
theme(axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) +
custom_ggtheme
plot_gender
}
if ( any(names(df_metadata) == "age") ) {
## AGE
plot_age <- ggplot(df_metadata ) +
geom_density(aes(x = age, fill = cluster), alpha = 0.7) +
theme_light() +
labs(x = "AGE" ) +
scale_fill_hue() +
guides(fill = guide_legend(title = NULL, ncol = 3, nrow = 1)) +
scale_fill_manual(values = c("#bfbfbf","#666666"),
breaks = c("control", args$case_type),
label = c("Control", args$case_type)) +
custom_ggtheme
}
if ( any(names(df_metadata) == "rin") ) {
## RIN
plot_rin <- ggplot(df_metadata ) +
geom_density(aes(x = rin, fill = cluster), alpha = 0.7) +
theme_light() +
labs(x = "RIN" ) +
scale_fill_hue() +
guides(fill = guide_legend(title = NULL, ncol = 3, nrow = 1)) +
scale_fill_manual(values = c("#bfbfbf","#666666"),
breaks = c("control", args$case_type),
label = c("Control", args$case_type)) +
custom_ggtheme
}
## Mapped read depth
plot_read_depth <- ggplot(df_metadata ) +
geom_density(aes(x = all_mapped_reads, fill = cluster), alpha = 0.6) +
theme_light() +
scale_fill_hue() +
labs(x = "Mapped Read Count" ) +
guides(fill = guide_legend(title = NULL, ncol = 3, nrow = 1)) +
scale_fill_manual(values = c("#bfbfbf","#666666"),
breaks = c("control", args$case_type),
label = c("Control", args$case_type)) +
custom_ggtheme
plot_read_depth
if ( exists("plot_age") ) {
ggpubr::ggarrange(plot_num_samples, plot_gender, plot_age, plot_read_depth,
labels = c("a","b","c","d"))
} else if ( exists("plot_rin") ) {
ggpubr::ggarrange(plot_num_samples, plot_gender, plot_rin, plot_read_depth,
labels = c("a","b","c","d"))
} else {
ggpubr::ggarrange(plot_num_samples, plot_read_depth,
labels = c("a","b"))
}
ggplot2::ggsave(file.path(args$figures_folder, "supplementary_figure27_ad.png"), width = 180, height = 100, units = "mm", dpi = 300)
}
#' Title
#' Get common introns between cases and controls (ONLY FRONTAL CORTEX) and subsample them by read coverage
#' @return