-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEpicapture_annotate exp-covered regions.R
1889 lines (1481 loc) · 91.9 KB
/
Epicapture_annotate exp-covered regions.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
############################################################################
# Title: Epicapture - annotate expperimentaly covered regions
# Description: Script used to annotate Target capture coverage files across 5 platforms
# Author: Miljana Tanic ([email protected])
# Created: November 2018
# Last edited: Mart 2019
# edited: Dec 2019 # plot all annotations
#####################################################################################
#=========================================
# Load libraries and set working directory
#=========================================
library(GenomicRanges)
library(AnnotationHub)
library(Rsamtools)
library(data.table)
library(genomation)
library(rtracklayer) # not necessary for importing BED files, can do it manualy crating GRanges object from dataframe
library(dplyr)
library(readr)
library(annotatr)
library(BSgenome.Hsapiens.UCSC.hg19) # sequence of the hg19 genome
library(ggplot2)
library(HelloRanges)
library(reshape2)
library(yarrr)
library(GenomeInfoDb) # Contains data and functions that define and allow translation between different chromosome sequence naming conventions (e.g., "chr1" versus "1")
#------------------------------------------
# Set Path to files
#------------------------------------------
# set work directly on R server for increased speed
setwd("/home/[email protected]/data/EpiCapture/")
setwd("/mnt/254b78b9-76b4-422d-84b1-cc632bff60f7/regmani/Epicapture")
list.files()
getwd()
# set resource and RESULTS directory
Epicapture <- "/home/[email protected]/RDS_C2c/EpiCapture/"
list.files(Epicapture)
RESULTS <- paste0(Epicapture,"RESULTS/")
list.files(RESULTS)
BEDfiles <- paste0(Epicapture,"BEDfiles/")
list.files(BEDfiles)
path2Agilent_BED <- paste0(BEDfiles, "AgilentBEDfiles/S03770311/")
path2Roche_BED <- paste0(BEDfiles,"RocheBEDfiles/")
path2Illumina_BED <- paste0(BEDfiles,"IlluminaBEDfiles/")
path2RRBS_BED <- paste0(BEDfiles,"RRBS_in_silico_BEDfile/")
# # get working directory and paths from script "Epicapture_Sample names and Paths.R"
# platforms <- paste0(Epicapture,"PLATFORMS/")
# path2Agilent <- paste0(platforms, "Agilent_SureSelect/")
# path2Illumina <- paste0(platforms, "Illumina_EPIC/")
# path2Roche <- paste0(platforms, "Roche_Nimblegen/")
# path2Diagenode <- paste0(platforms, "Diagenode_RRBS/")
# path2Nugen <- paste0(platforms, "NuGen_RRBS/")
# # Path to each platform
# Agilent_bedcov <- paste0(path2Agilent, "bedGraphs_Coverage/")
# Illumina_bedcov <- paste0(path2Illumina, "bedGraphs_Coverage/")
# Roche_bedcov <- paste0(path2Roche, "bedGraphs_Coverage/")
# Diagenode_bedcov <- paste0(path2Diagenode, "bedGraphs_Coverage/")
# Nugen_bedcov <- paste0(path2Nugen, "bedGraphs_Coverage/")
# # Platform prefix
# agilent.prefix <- "Agilent_"
# illumina.prefix <- "Illumina_"
# roche.prefix <- "Roche_"
# diagenode.prefix <- "Diagenode_"
# nugen.prefix <- "NuGEN_"
# # The bedGraph output (optional) looks like this (tab-delimited, 0-based start, 1-based end coords):
# bedgraph.sufix <- "_R1_val_1_trimmed_bismark_bt2_pe_n6dupsRemoved_NameSorted.bedGraph"
#---------------------------------------------------
# Load annotation files and MethylKit GRanges files:
#---------------------------------------------------
# Load design files:
load(file=paste0(BEDfiles,"Platform_Design_GRanges.RData"))
# Load feature annotations:
load(file=paste0(BEDfiles,"FeatureAnnotations/Genes_annotations_GRangesList.RData"))
load(file=paste0(BEDfiles,"FeatureAnnotations/extended_annotations_GRangesList.RData"))
load(file=paste0(BEDfiles,"FeatureAnnotations/CpG_annottations_GRangesList.RData"))
load(file=paste0(BEDfiles,"FeatureAnnotations/CpG_annottations_GRanges.RData"))
load(file=paste0(BEDfiles,"FeatureAnnotations/Enhancers_annotations_GRanges.RData"))
load(file=paste0(BEDfiles,"FeatureAnnotations/lncRNA_annotations_GRanges.RData"))
load(file=paste0(BEDfiles,"FeatureAnnotations/ChromHMM-byState_GRanges.RData"))
# load(file=paste0(BEDfiles, "FeatureAnnotations/CpG_locations_hg19_GRanges.RData"))
# load(file=paste0(BEDfiles, "FeatureAnnotations/all_cpgs.RData"))
# load(file=paste0(BEDfiles,"FeatureAnnotations/hg19_CpGsites_GRanges.RData"))
# load(file=paste0(BEDfiles,"FeatureAnnotations/UCSC_CGI_GRanges.RData"))
#-----------------------------------------------------------------
# Load experimentaly covered GRangesList objects for each platform
#-----------------------------------------------------------------
# # Load GRangesList objects
# # grl.PLATFORM was generated from my.DB.PLATFORM object using MethylKit
# # CpGs covered at min 10x
# # CpGs at both strands included! start=end and separated by strand info!
# load(file=paste0(RESULTS,"5_MethylKit/","grl.Agilent.RData"))
# load(file=paste0(RESULTS,"5_MethylKit/","grl.Roche.RData"))
# load(file=paste0(RESULTS,"5_MethylKit/","grl.Illumina.RData"))
# load(file=paste0(RESULTS,"5_MethylKit/","grl.Diagenode.RData"))
# load(file=paste0(RESULTS,"5_MethylKit/","grl.Nugen.RData"))
# # Roche_Hela_1 has failed so exclud it form analysis
# grl.Roche <- grl.Roche[-6]
# #-------------------------------------------------------------
# # Find union of all covered regions - max breadth of coverage:
# #-------------------------------------------------------------
# reduce(unlist(grl.Agilent)) # this is the union
# # get number of all total CpGs covered - union
# gr.Agilent<- unlist(grl.Agilent)
# gr.Roche<- unlist(grl.Roche)
# gr.Illumina<- unlist(grl.Illumina)
# gr.Diagenode<- unlist(grl.Diagenode)
# gr.Nugen<- unlist(grl.Nugen)
# sum(width(reduce(gr.Agilent)))
# sum(width(reduce(gr.Roche)))
# sum(width(reduce(gr.Illumina)))
# sum(width(reduce(gr.Diagenode)))
# sum(width(reduce(gr.Nugen)))
# #------------------------------------------------
# # Find overlap (intersection) between several GRanges objects:
# #-----------------------------------------------
# # Find intersection of all CpGs covered by each platform -
# # using MethylKit's functionality to make a merged dataset and then convert to GRanges
# meth.Agilent=unite(my.DB.Agilent, destrand=TRUE)
# meth.Roche=unite(my.DB.Roche, destrand=TRUE)
# meth.Illumina=unite(my.DB.Illumina, destrand=TRUE)
# meth.Diagenode=unite(my.DB.Diagenode, destrand=TRUE)
# meth.Nugen=unite(my.DB.Nugen, destrand=TRUE)
# save(meth.Agilent, meth.Roche, meth.Illumina, meth.Diagenode,meth.Nugen, file="meth.PLATFORM.RData")
# # convert to GRanges
# gr.common.Agilent <- as(meth.Agilent, "GRanges")
# gr.common.Roche <- as(meth.Roche, "GRanges")
# gr.common.Illumina <- as(meth.Illumina, "GRanges")
# gr.common.Diagenode <- as(meth.Diagenode, "GRanges")
# gr.common.Nugen <- as(meth.Nugen, "GRanges")
# # get number of all common CpGs covered - intersection
# length(gr.common.Agilent)
# length(gr.common.Roche)
# length(gr.common.Illumina)
# length(gr.common.Diagenode)
# length(gr.common.Nugen)
# returns an object of overlappingPeaks, which contains there elements: venn_cnt, peaklist (a list of overlapping peaks or unique peaks), and overlappingPeaks (a list of data frame consists of the annotation of all the overlapping peaks).
# ol <- findOverlapsOfPeaks(grl.Agilent) # max 5 peak lists!
# head(ol)
#==============================
# Sample list
#==============================
all <- c("Ref-gDNA-500-1",
"Ref-gDNA-500-2",
"Ref-gDNA-recommended-1",
"Ref-gDNA-recommended-2",
"Coriell-NA12878-K12-1",
"Coriell-NA12878-K12-2",
"Hela-1",
"Hela-2",
"ZYMO-FM",
"ZYMO-UM",
"DNAm-5pct",
"DNAm-10pct",
"T24",
"253J",
"RT112",
"RT112-CP")
tech_repl <- c("Ref-gDNA-500-1",
"Ref-gDNA-500-2",
"Ref-gDNA-recommended-1",
"Ref-gDNA-recommended-2",
"Coriell-NA12878-K12-1",
"Coriell-NA12878-K12-2",
"Hela-1",
"Hela-2")
#--------------------------------------------------------------
# Import bedgraph files from each platform:
#--------------------------------------------------------------
# to import bedgraph files use rtracklayer:
# imp = import(f1, format="bedGraph")
#! bedGraph doesn't have covergae info for filtering! Better use cov.files they also have BED like info but can be filtered:
# Try imorting with Methyl Kit and use those files for annotation later :
# # List files:
# print(samples.agilent <- list.files(path=path2Agilent, pattern="bismark.cov"))
# print(samples.roche <- list.files(path=path2Roche, pattern="bismark.cov"))
# print(samples.illumina <- list.files(path=path2Illumina, pattern="bismark.cov"))
# print(samples.diagenode <- list.files(path=path2Diagenode, pattern="bismark.cov"))
# print(samples.nugen <- list.files(path=path2Nugen, pattern="bismark.cov"))
# import function from rtracklayer, which can load pretty much any kind of genomic data into the appropriate typevof Bioconductor object
#x <- import.bedGraph(paste0(path2Agilent,"/",samples.agilent[1]))
# fread()
#------------
# Agilent
#------------
# # Sample names:
# samples_names.agilent <- gsub("_R1_val_1_bismark_bt2_pe.deduplicated.bedGraph","",samples.agilent)
# # Import multiple bedGraph samples:
# grl <-GRangesList()
# sample.list.agilent <- list()
# for (i in length(samples.agilent) ) {
# sample.list.agilent[i] <- fread(paste0(path2Agilent,"/",samples.agilent[i]))
# }
# # Coerce to GrangesList object
# grl.Agilent <- GRangesList(file.list)
# class(grl.Agilent)
# names(grl.Agilent)
#==============================
# Import merged CpG coverage file
#==============================
#--------------------------------------
# Sample list
#--------------------------------------
# samples <- list.files(pattern="bismark.cov") # Total number of CpG sites for both strands independently
# Total number of CpG sites - output from coverage2cytosine with information from top and bottom strand merged into one
samples.agilent <- list.files(path=Agilent_bedcov, pattern="CpG_evidence.cov")
samples.roche <- list.files(path=Roche_bedcov, pattern="CpG_evidence.cov")
samples.illumina <- list.files(path=Illumina_bedcov, pattern="CpG_evidence.cov")
samples.diagenode <- list.files(path=Diagenode_bedcov, pattern="CpG_evidence.cov")
samples.nugen <- list.files(path=Nugen_bedcov, pattern="CpG_evidence.cov")
#--------------------------------------
# read samples
#--------------------------------------
# make a list object
list.Agilent <- list()
list.Roche <- list()
list.Illumina <- list()
list.Diagenode <- list()
list.Nugen <- list()
#test
# for (i in seq_along(samples.agilent)) {
# print(samples.agilent[i])
# }
# list.Agilent[[1]] <- fread(paste0(Agilent_bedcov,samples.agilent[1]))
# names(list.Agilent[[1]]) <- c("chr", "start", "end", "pct_meth", "count_M", "count_UM")
# read merged cov files
for (i in seq_along(samples.agilent)) {
list.Agilent[[i]] <- fread(paste0(Agilent_bedcov,samples.agilent[i]))
names(list.Agilent[[i]]) <- c("chr", "start", "end", "pct_meth", "count_M", "count_UM")
}
for (i in seq_along(samples.roche)) {
list.Roche[[i]] <- fread(paste0(Roche_bedcov,samples.roche[i]))
names(list.Roche[[i]]) <- c("chr", "start", "end", "pct_meth", "count_M", "count_UM")
}
for (i in seq_along(samples.illumina)) {
list.Illumina[[i]] <- fread(paste0(Illumina_bedcov,samples.illumina[i]))
names(list.Illumina[[i]]) <- c("chr", "start", "end", "pct_meth", "count_M", "count_UM")
}
for (i in seq_along(samples.diagenode)) {
list.Diagenode[[i]] <- fread(paste0(Diagenode_bedcov,samples.diagenode[i]))
names(list.Diagenode[[i]]) <- c("chr", "start", "end", "pct_meth", "count_M", "count_UM")
}
for (i in seq_along(samples.nugen)) {
list.Nugen[[i]] <- fread(paste0(Nugen_bedcov,samples.nugen[i]))
names(list.Nugen[[i]]) <- c("chr", "start", "end", "pct_meth", "count_M", "count_UM")
}
# make new sample names
samples.agilent<- gsub("_R1_val_1_bismark_bt2_pe.deduplicated.bismark.CpG_report.merged_CpG_evidence.cov", "", samples.agilent)
samples.roche <- gsub("_R1_val_1_bismark_bt2_pe.deduplicated.bismark.CpG_report.merged_CpG_evidence.cov", "", samples.roche)
samples.illumina <- gsub("_R1_val_1_bismark_bt2_pe.deduplicated.bismark.CpG_report.merged_CpG_evidence.cov", "", samples.illumina)
samples.diagenode <- gsub("_R1_val_1_bismark_bt2_pe.bismark.CpG_report.merged_CpG_evidence.cov", "", samples.diagenode)
samples.nugen <- gsub("_R1_val_1_trimmed_bismark_bt2_pe_n6dupsRemoved_NameSorted.bismark.CpG_report.merged_CpG_evidence.cov", "", samples.nugen)
"R1_val_1_trimmed.fq.gz_bismark_bt2_pe_n6dupsRemoved_NameSorted.bismark.CpG_report.merged_CpG_evidence.cov"
samples.nugen <- gsub("_R1_val_1_trimmed.fq.gz_bismark_bt2_pe_n6dupsRemoved_NameSorted.bismark.CpG_report.merged_CpG_evidence.cov", "", samples.nugen)
samples.diagenode <- gsub("Coriell-NA12878", "Coriell-NA12878-K12", samples.diagenode)
samples.nugen <- gsub("Nugen_", "", samples.nugen)
samples.nugen <- gsub("_100M", "", samples.nugen)
samples.nugen <- gsub("Ref.", "Ref-", samples.nugen)
names(list.Agilent) <- samples.agilent
names(list.Roche) <- samples.roche
names(list.Illumina) <- samples.illumina
names(list.Diagenode) <- samples.diagenode
names(list.Nugen) <- samples.nugen
# add a coverage column
for (i in seq_along(list.Agilent)){
list.Agilent[[i]][["cov"]] <- list.Agilent[[i]][["count_M"]] + list.Agilent[[i]][["count_UM"]]
}
for (i in seq_along(list.Roche)){
list.Roche[[i]][["cov"]] <- list.Roche[[i]][["count_M"]] + list.Roche[[i]][["count_UM"]]
}
for (i in seq_along(list.Illumina)){
list.Illumina[[i]][["cov"]] <- list.Illumina[[i]][["count_M"]] + list.Illumina[[i]][["count_UM"]]
}
for (i in seq_along(list.Diagenode)){
list.Diagenode[[i]][["cov"]] <- list.Diagenode[[i]][["count_M"]] + list.Diagenode[[i]][["count_UM"]]
}
for (i in seq_along(list.Nugen)){
list.Nugen[[i]][["cov"]] <- list.Nugen[[i]][["count_M"]] + list.Nugen[[i]][["count_UM"]]
}
# save
save(list.Agilent, list.Roche, list.Illumina, list.Diagenode, list.Nugen, file="mrg_CpGcoverage_list.PLATFORM.RData")
load(file="mrg_CpGcoverage_list.PLATFORM.RData")
#===================================================
# filter by coverage >10x and convert to GRangesList
#===================================================
#-----------------------------
# Filter by coverage >=10x
#-----------------------------
# test
# list.Nugen[[1]] %>% filter(cov >=10)
for (i in seq_along(list.Agilent)){
list.Agilent[[i]]<- list.Agilent[[i]] %>% filter(cov >=10)}
for (i in seq_along(list.Roche)){
list.Roche[[i]]<- list.Roche[[i]] %>% filter(cov >=10)}
for (i in seq_along(list.Illumina)){
list.Illumina[[i]]<- list.Illumina[[i]] %>% filter(cov >=10)}
for (i in seq_along(list.Diagenode)){
list.Diagenode[[i]]<- list.Diagenode[[i]] %>% filter(cov >=10)}
for (i in seq_along(list.Nugen)){
list.Nugen[[i]]<- list.Nugen[[i]] %>% filter(cov >=10)}
# save
save(list.Agilent, list.Roche, list.Illumina, list.Diagenode, list.Nugen, file="mrg_CpGcoverage_list.PLATFORM.RData")
load(file="mrg_CpGcoverage_list_10x.PLATFORM.RData")
#-----------------------------
# Convert to GRangesList
#-----------------------------
makeGRangesFromDataFrame(df,
keep.extra.columns=FALSE,
ignore.strand=FALSE,
seqinfo=NULL,
seqnames.field=c("seqnames", "seqname",
"chromosome", "chrom",
"chr", "chromosome_name",
"seqid"),
start.field="start",
end.field=c("end", "stop"),
strand.field="strand",
starts.in.df.are.0based=FALSE)
grl.Agilent <- GRangesList()
for (i in names(list.Agilent)) {
grl.Agilent[[i]] <- makeGRangesFromDataFrame(list.Agilent[[i]],
keep.extra.columns=TRUE,
ignore.strand=TRUE,
seqinfo=NULL,
seqnames.field="chr",
start.field="start",
end.field=c("end", "stop"),
starts.in.df.are.0based=FALSE)
}
grl.Roche <- GRangesList()
for (i in names(list.Roche)) {
grl.Roche[[i]] <- makeGRangesFromDataFrame(list.Roche[[i]],
keep.extra.columns=TRUE,
ignore.strand=TRUE,
seqinfo=NULL,
seqnames.field="chr",
start.field="start",
end.field=c("end", "stop"),
starts.in.df.are.0based=FALSE)
}
grl.Illumina <- GRangesList()
for (i in names(list.Illumina)) {
grl.Illumina[[i]] <- makeGRangesFromDataFrame(list.Illumina[[i]],
keep.extra.columns=TRUE,
ignore.strand=TRUE,
seqinfo=NULL,
seqnames.field="chr",
start.field="start",
end.field=c("end", "stop"),
starts.in.df.are.0based=FALSE)
}
grl.Diagenode <- GRangesList()
for (i in names(list.Diagenode)) {
grl.Diagenode[[i]] <- makeGRangesFromDataFrame(list.Diagenode[[i]],
keep.extra.columns=TRUE,
ignore.strand=TRUE,
seqinfo=NULL,
seqnames.field="chr",
start.field="start",
end.field=c("end", "stop"),
starts.in.df.are.0based=FALSE)
}
grl.Nugen <- GRangesList()
for (i in names(list.Nugen)) {
grl.Nugen[[i]] <- makeGRangesFromDataFrame(list.Nugen[[i]],
keep.extra.columns=TRUE,
ignore.strand=TRUE,
seqinfo=NULL,
seqnames.field="chr",
start.field="start",
end.field=c("end", "stop"),
starts.in.df.are.0based=FALSE)
}
# Roche_Hela_1 has failed so exclud it form analysis
grl.Roche <- grl.Roche[-6]
# add Platform names to Nugen
names(grl.Nugen) <- paste0("Nugen_", names(grl.Nugen))
# Get number of CpGs covered - lapply and sapply return a dataframe and a list object respectively!
sapply(grl.Agilent,length)
sapply(grl.Roche,length)
sapply(grl.Illumina,length)
sapply(grl.Diagenode,length)
sapply(grl.Nugen,length)
# add "chr" to seqnames - ALWAYS USE ENDOAPPLY() TO RETURN A GRANGESLIST OBJECT
BiocManager::install("diffloop")
library(diffloop)
grl.Agilent <- endoapply(grl.Agilent,addchr)
grl.Roche <- endoapply(grl.Roche,addchr)
grl.Illumina <- endoapply(grl.Illumina,addchr)
grl.Diagenode <- endoapply(grl.Diagenode,addchr)
grl.Nugen <- endoapply(grl.Nugen,addchr)
# save
save(grl.Agilent, grl.Roche, grl.Illumina, grl.Diagenode, grl.Nugen, file=paste0(RESULTS,"8_ExpCoveredCpGsAnnotation/mrg_CpGcoverage_grl.PLATFORM.RData"))
load(file=paste0(RESULTS,"8_ExpCoveredCpGsAnnotation/mrg_CpGcoverage_grl.PLATFORM.RData"))
#------------------------------------------------
# find union - all CpGs covered by each platform:
#------------------------------------------------
# using reduce results in consecutive CpGs being merged into one region
ugr.Agilent <- reduce(unlist(grl.Agilent))
length(ugr.Agilent)
head(ugr.Agilent)
ugr.Roche <- reduce(unlist(grl.Roche))
length(ugr.Roche)
head(ugr.Roche)
ugr.Illumina <- reduce(unlist(grl.Illumina))
length(ugr.Illumina)
head(ugr.Illumina)
ugr.Diagenode <- reduce(unlist(grl.Diagenode))
length(ugr.Diagenode)
head(ugr.Diagenode)
ugr.Nugen <- reduce(unlist(grl.Nugen))
length(ugr.Nugen)
head(ugr.Nugen)
#------------------------------------------------------------
# find intersection - commonly covered CpGs by each platform:
#-------------------------------------------------------------
# subsetByOverlaps method simply subsets the first GRanges object to include only those that overlap the second.
subsetByOverlaps(gr, g)
# use intersect instead
for (i in names(grl.Agilent)) {
cgr.Agilent <- intersect(ugr.Agilent, grl.Agilent[[i]])}
length(cgr.Agilent)
for (i in names(grl.Roche)) {
cgr.Roche <- intersect(ugr.Roche, grl.Roche[[i]])}
length(cgr.Roche)
for (i in names(grl.Illumina)) {
cgr.Illumina <- intersect(ugr.Illumina, grl.Illumina[[i]])}
length(cgr.Illumina)
for (i in names(grl.Diagenode)) {
cgr.Diagenode <- intersect(ugr.Diagenode, grl.Diagenode[[i]])}
length(cgr.Diagenode)
for (i in names(grl.Nugen)) {
cgr.Nugen <- intersect(ugr.Nugen, grl.Nugen[[i]])}
length(cgr.Nugen)
# # add "chr" to seqnames to make it compatible with annotation files
# # addchr takes a loops object or GRanges object and simply adds 'chr' to seqnames
# BiocManager::install("diffloop")
# library(diffloop)
# ## S4 method for signature 'GRanges'
# ugr.Agilent <- addchr(ugr.Agilent)
# ugr.Roche <- addchr(ugr.Roche)
# ugr.Illumina <- addchr(ugr.Illumina)
# ugr.Diagenode <- addchr(ugr.Diagenode)
# ugr.Nugen <- addchr(ugr.Nugen)
# cgr.Agilent <- addchr(cgr.Agilent)
# cgr.Roche <- addchr(cgr.Roche)
# cgr.Illumina <- addchr(cgr.Illumina)
# cgr.Diagenode <- addchr(cgr.Diagenode)
# cgr.Nugen <- addchr(cgr.Nugen)
# save objects
save(ugr.Agilent,ugr.Roche,ugr.Illumina,ugr.Diagenode, ugr.Nugen, cgr.Agilent,cgr.Roche, cgr.Illumina, cgr.Diagenode, cgr.Nugen, file=paste0(RESULTS,"8_ExpCoveredCpGsAnnotation/Total_n_common_CpGs.RData"))
load(file=paste0(RESULTS,"8_ExpCoveredCpGsAnnotation/Total_n_common_CpGs.RData"))
#==================================================
# find overlaping pairs intersection size Function:
#==================================================
# code form HelloRanges package
findOverlapSize <- function(platform, annotation){
pairs <- findOverlapPairs(platform, annotation, ignore.strand = TRUE) # find overlaping pairs
ans <- pintersect(pairs, ignore.strand=TRUE) # get actual intersectiong regions
sum(width(reduce(ans))) # Calculate overlap between targeted regions in kilobases
}
library(BSgenome.Hsapiens.UCSC.hg19)
genome <- BSgenome.Hsapiens.UCSC.hg19
# Get sequences from GRangesList object:
grl.seq <- getSeq(genome, platforms_design.grl) # The return values are DNAStringSetList objects.
findOverlapCpGCount <- function(platform, annotation){
pairs <- findOverlapPairs(platform, annotation, ignore.strand = TRUE)
ans <- pintersect(pairs, ignore.strand=TRUE)
x <- getSeq(genome, reduce(ans)) # # The return values are DNAStringSetList objects
sum(vcountPattern("CG", x)) # count number of CG occurancesin the sequence
}
countOverlaps()
# jaccard statistics
intersects <- intersect(gr_a, gr_b, ignore.strand = TRUE)
intersection <- sum(width(intersects))
union <- sum(width(union(gr_a, gr_b, ignore.strand = TRUE)))
ans <- DataFrame(intersection, union, jaccard = intersection/union, n_intersections = length(intersects))
findOverlapPairs #convenience for creating a Pairs object that matches up the overlapping ranges:
# Another way of getting at overlap information is to use %over% which returns a logical vector of which ranges in the first argument overlapped any ranges in the second.
gr1 %over% gr2
## [1] FALSE FALSE TRUE TRUE FALSE
gr1[gr1 %over% gr2]
# Note that both of these are strand-specific, although findOverlaps has an ignore.strand option.
#==========================
# Set colors for Plotting
#==========================
# Selecting colors using yarr (pirateplot)
library(yarrr)
piratepal(palette= "all")
piratepal("google")
# blue red yellow green
# "#3D79F3FF" "#E6352FFF" "#F9B90AFF" "#34A74BFF"
# platforms <- c( "Agilent", "Roche", "Illumina", "Diagenode", "Nugen")
col.platforms <- c("#E6352FFF", "#3D79F3FF", "#34A74BFF", "#7570b3" , "#F9B90AFF") # GOOD LOKING DIVERGING PALLETE
barplot(rep(1,length(col.platforms)), col= col.platforms)
legend(x = 'topleft', legend=col.platforms, fill=col.platforms, cex = 0.8)
# make color plaette transparent uing yarr transparent() function:
col.platforms <- transparent(orig.col = col.platforms, trans.val = 0.2) # BEAUTIFUL :)
# change default colors in ggplot2
opts <- options() # save old options
library(RColorBrewer)
feature.cols <- brewer.pal(n = 8, name = "Dark2")
scale_fill_discrete <- function(...) {scale_fill_manual(..., values = feature.cols)}
scale_fill_discrete <- function(...) {scale_fill_manual(..., values = platform.cols)}
#===========================================================================
# 1. Annotate Union of all CpGs coverd by each platforms for each Feature type:
#===========================================================================
#===========================
# Annotate Genomic Features:
#===========================
#----------------------------------------------------------------------------------
# Calculate overlap - intersection between platform design file and annotation file:
#----------------------------------------------------------------------------------
u_df_genes <- data.frame(Total=integer(), Agilent=integer(), Roche=integer(), Illumina=integer(), Diagenode=integer(), Nugen=integer())
for (i in (names(annotations_genes.grl))) {
u_df_genes[i,"Agilent"] <- findOverlapSize(ugr.Agilent, unlist(annotations_genes.grl[i]))}
for (i in (names(annotations_genes.grl))) {
u_df_genes[i,"Roche"] <- findOverlapSize(ugr.Roche, unlist(annotations_genes.grl[i]))}
for (i in (names(annotations_genes.grl))) {
u_df_genes[i,"Illumina"] <- findOverlapSize(ugr.Illumina, unlist(annotations_genes.grl[i]))}
for (i in (names(annotations_genes.grl))) {
u_df_genes[i,"Diagenode"] <- findOverlapSize(ugr.Diagenode, unlist(annotations_genes.grl[i]))}
for (i in (names(annotations_genes.grl))) {
u_df_genes[i,"Nugen"] <- findOverlapSize(ugr.Nugen, unlist(annotations_genes.grl[i]))}
u_df_genes$Total <- sum(width(reduce(annotations_genes.grl)))
head(u_df_genes)
save(u_df_genes, file=paste0(RESULTS,"1_PlatformDesignDiferences/u_df_genes_size.RData"))
write.table(u_df_genes, sep="\t", file=paste0(RESULTS,"8_ExpCoveredCpGsAnnotation/union_genic_size.txt"))
load(file=paste0(RESULTS,"1_PlatformDesignDiferences/u_df_genes_size.RData"))
# Transform dataframe to long format for plotting:
u_df_genes_long <- melt(t(u_df_genes[,-1]))
names(u_df_genes_long) <- c("Platform", "Feature", "Size_bp" )
head(u_df_genes_long)
u_df_genes_long$Size_Mb <- round(u_df_genes_long$Size_bp/1000000,2)
#------------------------------------------
# Plot Feature size by Platform - barplots:
#------------------------------------------
gene_annots <- ggplot(data=u_df_genes_long, aes(x=Feature, y=Size_Mb, fill=Platform)) +
geom_bar(stat = "identity", position = "dodge", colour="black", size=0.25)+
# geom_text(aes(label=Size_bp), vjust=1.6, color="white", position = position_dodge(0.9), size=2)+
scale_fill_brewer(palette="Set1") +
# scale_color_manual(values=piratepal("appletv")[1:5]) + # select colors for dots
# scale_fill_manual(values=col.platforms[1:5])+
ylab("Genomic size (Mb)") +
ggtitle("Breadth of coverage of genomic features by platform") +
theme(axis.title.x=element_blank()) +
# coord_flip()+ # make horizontal chart
scale_x_discrete(limits=c("hg19_genes_1to5kb","hg19_genes_5UTRs","hg19_genes_promoters",
"hg19_genes_exons", "hg19_genes_introns","hg19_genes_3UTRs" ), # Change the order of items
labels=c("hg19_genes_1to5kb"="1 to 5kb", "hg19_genes_3UTRs"="3'UTRs",
"hg19_genes_5UTRs"="5'UTRs", "hg19_genes_exons"="exons", "hg19_genes_introns"="introns", "hg19_genes_promoters"="promoters"
)) + # Change the name of items
theme_classic()
gene_annots
ggsave(paste0(RESULTS, "8_ExpCoveredCpGsAnnotation/u_Barplot_Genic_ByFeature_size.pdf"))
#--------------------------------------------
# find # of CpGs in each feature category
#--------------------------------------------
# Count # of CpG in each annotation category for UNION
u_df_genes_cpg <- data.frame(Total=integer(), Agilent=integer(), Roche=integer(), Illumina=integer(), Diagenode=integer(), Nugen=integer())
for (i in (names(annotations_genes.grl))) {
u_df_genes_cpg[i,"Agilent"] <- findOverlapCpGCount(ugr.Agilent, unlist(annotations_genes.grl[i]))}
for (i in (names(annotations_genes.grl))) {
u_df_genes_cpg[i,"Roche"] <- findOverlapCpGCount(ugr.Roche, unlist(annotations_genes.grl[i]))}
for (i in (names(annotations_genes.grl))) {
u_df_genes_cpg[i,"Illumina"] <- findOverlapCpGCount(ugr.Illumina, unlist(annotations_genes.grl[i]))}
for (i in (names(annotations_genes.grl))) {
u_df_genes_cpg[i,"Diagenode"] <- findOverlapCpGCount(ugr.Diagenode, unlist(annotations_genes.grl[i]))}
for (i in (names(annotations_genes.grl))) {
u_df_genes_cpg[i,"Nugen"] <- findOverlapCpGCount(ugr.Nugen, unlist(annotations_genes.grl[i]))}
for (i in seq_along(names(annotations_genes.grl))){
x <- getSeq(genome, reduce(unlist(annotations_genes.grl[i])))
u_df_genes_cpg[i,"Total"] <-sum(vcountPattern("CG", x))}
head(u_df_genes_cpg)
save(u_df_genes_cpg, file=paste0(RESULTS,"8_ExpCoveredCpGsAnnotation/u_df_genes_cpg.RData"))
write.table(u_df_genes, sep="\t", file=paste0(RESULTS,"8_ExpCoveredCpGsAnnotation/union_genic_cpg.txt"))
# Transform dataframe to long format for plotting:
u_df_genes_cpg_long <- melt(t(u_df_genes_cpg[,-1]))
names(u_df_genes_cpg_long) <- c("Platform", "Feature", "No_CpGs" )
head(u_df_genes_cpg_long)
u_df_genes_cpg_long$No_CpGs_M <- round(u_df_genes_cpg_long$No_CpGs/1000000,2)
#----------------------------------------
# Plot Features by Platform - barplots:
#----------------------------------------
gene_annots <- ggplot(data=u_df_genes_cpg_long, aes(x=Feature, y=No_CpGs, fill=Platform)) +
geom_bar(stat = "identity", position = "dodge", colour="black", size=0.25)+
# geom_text(aes(label=Size_bp), vjust=1.6, color="white", position = position_dodge(0.9), size=2)+
scale_fill_manual(values=col.platforms) +
ylab("# CpGs") +
ggtitle("Number of total CpGs per genomic feature by platform") +
theme(axis.title.x=element_blank()) +
scale_x_discrete(limits=c("hg19_genes_1to5kb","hg19_genes_5UTRs","hg19_genes_promoters",
"hg19_genes_exons", "hg19_genes_introns","hg19_genes_3UTRs" ), # Change the order of items
labels=c("hg19_genes_1to5kb"="1 to 5kb", "hg19_genes_3UTRs"="3'UTRs",
"hg19_genes_5UTRs"="5'UTRs", "hg19_genes_exons"="exons", "hg19_genes_introns"="introns", "hg19_genes_promoters"="promoters"
)) + # Change the name of items
theme_classic()
gene_annots
ggsave(paste0(RESULTS, "8_ExpCoveredCpGsAnnotation/u_Barplot_Genic_ByFeature_NoCpGs.pdf"))
#----------------------------------------
# Stacked Plot NoCpGs per Feature by Platform
#----------------------------------------
scale_fill_discrete <- function(...) {scale_fill_manual(..., values = feature.cols)}
gene_annots <- ggplot(data=u_df_genes_cpg_long, aes(x=Platform, y=No_CpGs, fill=Feature)) +
geom_bar(stat = "identity", colour="black", size=0.25)+
ylab("# CpGs") +
ggtitle("Number of CpGs per genomic feature by platform") +
scale_fill_discrete(limits=c("hg19_genes_1to5kb","hg19_genes_5UTRs","hg19_genes_promoters", "hg19_genes_exons", "hg19_genes_introns","hg19_genes_3UTRs" ), # Change the order of items
labels=c("hg19_genes_1to5kb"="1 to 5kb", "hg19_genes_3UTRs"="3'UTRs",
"hg19_genes_5UTRs"="5'UTRs", "hg19_genes_exons"="exons", "hg19_genes_introns"="introns","hg19_genes_promoters"="promoters"
)) + # Change the name of items
theme_classic()
gene_annots
ggsave(paste0(RESULTS, "8_ExpCoveredCpGsAnnotation/u_StakedBarplot_byPlatform_NoCpGs.pdf"))
#----------------------------------------------
# Percent CpGs covered per Feature by Platform
#----------------------------------------------
# Calculate percentages of total features covered by each platform
head(u_df_genes_cpg)
u_df_genes_cpg_pct <- apply(u_df_genes_cpg, 2, function(x) x/u_df_genes_cpg$Total)
head(u_df_genes_cpg_pct)
# Plot percentages:
u_df_genes_cpg_pct_long <- melt(t(u_df_genes_cpg_pct[,-1]))
names(u_df_genes_cpg_pct_long) <- c("Platform", "Feature", "PercentCpGs_covered" )
gene_annots_pct <- ggplot(data=u_df_genes_cpg_pct_long, aes(x=Feature, y=PercentCpGs_covered, fill=Platform)) +
geom_bar(stat = "identity", position = "dodge", colour="black", size=0.25)+
#geom_text(aes(label=Percent_covered), vjust=1.6, color="white",
# position = position_dodge(0.9), size=3.5)+
scale_fill_manual(values=col.platforms) +
ylab("% CpGs covered") +
ggtitle("Percent CpGs covered per features by platform") +
theme(axis.title.x=element_blank()) +
# coord_flip()+ # make horizontal chart
scale_x_discrete(limits=c("hg19_genes_1to5kb","hg19_genes_5UTRs","hg19_genes_promoters",
"hg19_genes_exons", "hg19_genes_introns","hg19_genes_3UTRs" ), # Change the order of items
labels=c("hg19_genes_1to5kb"="1 to 5kb", "hg19_genes_3UTRs"="3'UTRs",
"hg19_genes_5UTRs"="5'UTRs", "hg19_genes_exons"="exons", "hg19_genes_introns"="introns", "hg19_genes_promoters"="promoters"
)) + # Change the name of items
theme_classic()
gene_annots_pct
ggsave(paste0(RESULTS, "8_ExpCoveredCpGsAnnotation/u_Barplot_Genic_ByFeature_pctCpGs.pdf"))
#----------------------------------------
# Piechart % CpGs in features by Platform
#----------------------------------------
# Compute percentages of each features covered by each platform
u_df_genes_cpg_pct_CpGs <- apply(u_df_genes_cpg, 2, function(x) pct=x/sum(x))
head(u_df_genes_cpg_pct_CpGs)
# calculate percentage:
u_df_genes_cpg_pct_CpGs_long <- melt(t(u_df_genes_cpg_pct_CpGs[,-1]))
names(u_df_genes_cpg_pct_CpGs_long) <- c("Platform", "Feature", "pctCpGs" )
head(u_df_genes_cpg_pct_CpGs_long)
gene_annots <- ggplot(data=u_df_genes_cpg_pct_CpGs_long, aes(x="", y=pctCpGs, fill=Feature)) +
facet_grid(. ~ Platform) +
geom_bar(width = 1, stat = "identity", color="white") +
coord_polar("y", start=0) +
ylab("% CpGs covered") +
ggtitle("Percent CpGs covered per features by platform") +
scale_fill_discrete(name="Genic features",
limits=c("hg19_genes_1to5kb","hg19_genes_5UTRs",
"hg19_genes_promoters", "hg19_genes_exons", "hg19_genes_introns","hg19_genes_3UTRs" ), # Change the order of items
labels=c("hg19_genes_1to5kb"="1 to 5kb", "hg19_genes_3UTRs"="3'UTRs",
"hg19_genes_5UTRs"="5'UTRs", "hg19_genes_exons"="exons", "hg19_genes_introns"="introns", "hg19_genes_promoters"="promoters"),
guide = guide_legend(label = TRUE, label.position = "right",
legend.theme = element_text(size = 8)
)) + # Change the name of items
theme(legend.position="bottom") +
theme_void() # remove background, grid, numeric labels
gene_annots
ggsave(paste0(RESULTS, "8_ExpCoveredCpGsAnnotation/u_Piechart_byPlatform_pctCpGs.pdf"))
#================
# Annotate CpGs:
#================
# Inspect loaded object:
head(annotations_CpGs.grl)
updateObject(annotations_CpGs.grl, verbose=TRUE)
names(annotations_CpGs.grl)
#--------------------------------------------
# find # of CpGs in each feature category
#--------------------------------------------
# Count # of CpG in each annotation category for UNION
u_df_CGI_cpg <- data.frame(Total=integer(), Agilent=integer(), Roche=integer(), Illumina=integer(), Diagenode=integer(), Nugen=integer())
for (i in (names(annotations_CpGs.grl))) {
u_df_CGI_cpg[i,"Agilent"] <- findOverlapCpGCount(ugr.Agilent, unlist(annotations_CpGs.grl[i]))}
for (i in (names(annotations_CpGs.grl))) {
u_df_CGI_cpg[i,"Roche"] <- findOverlapCpGCount(ugr.Roche, unlist(annotations_CpGs.grl[i]))}
for (i in (names(annotations_CpGs.grl))) {
u_df_CGI_cpg[i,"Illumina"] <- findOverlapCpGCount(ugr.Illumina, unlist(annotations_CpGs.grl[i]))}
for (i in (names(annotations_CpGs.grl))) {
u_df_CGI_cpg[i,"Diagenode"] <- findOverlapCpGCount(ugr.Diagenode, unlist(annotations_CpGs.grl[i]))}
for (i in (names(annotations_CpGs.grl))) {
u_df_CGI_cpg[i,"Nugen"] <- findOverlapCpGCount(ugr.Nugen, unlist(annotations_CpGs.grl[i]))}
for (i in seq_along(names(annotations_CpGs.grl))){
x <- getSeq(genome, reduce(unlist(annotations_CpGs.grl[i])))
u_df_CGI_cpg[i,"Total"] <-sum(vcountPattern("CG", x))}
head(u_df_CGI_cpg)
save(u_df_CGI_cpg, file=paste0(RESULTS,"8_ExpCoveredCpGsAnnotation/u_df_CGI_cpg.RData"))
write.table(u_df_CGI_cpg, sep="\t", file=paste0(RESULTS,"8_ExpCoveredCpGsAnnotation/union_CGI_cpg.txt"))
# Transform dataframe to long format for plotting:
u_df_CGI_cpg_long <- melt(t(u_df_CGI_cpg[,-1]))
names(u_df_CGI_cpg_long) <- c("Platform", "Feature", "No_CpGs" )
head(u_df_CGI_cpg_long)
u_df_CGI_cpg_long$No_CpGs_M <- round(u_df_CGI_cpg_long$No_CpGs/1000000,2)
#----------------------------------------
# Plot Features by Platform - barplots:
#----------------------------------------
# scale_fill_discrete <- function(...) {scale_fill_manual(..., values = platform.cols)}
gene_annots <- ggplot(data=u_df_CGI_cpg_long, aes(x=Feature, y=No_CpGs, fill=Platform)) +
geom_bar(stat = "identity", position = "dodge", colour="black", size=0.25)+
# geom_text(aes(label=Size_bp), vjust=1.6, color="white", position = position_dodge(0.9), size=2)+
scale_fill_manual(values=col.platforms) +
ylab("# CpGs") +
ggtitle("Number of CpGs per feature category by platform") +
theme(axis.title.x=element_blank()) +
scale_x_discrete(limits=c("hg19_cpg_islands","hg19_cpg_shelves","hg19_cpg_shores",
"hg19_cpg_inter"), # Change the order of items
labels=c("hg19_cpg_islands"="CpG Islands", "hg19_cpg_shelves"="shelves",
"hg19_cpg_shores"="shores", "hg19_cpg_inter"="inter CGI")) + # Change the name of items
theme_classic()
gene_annots
ggsave(paste0(RESULTS, "8_ExpCoveredCpGsAnnotation/u_Barplot_CGI_ByFeature_NoCpGs.pdf"))
#---------------------------------------------
# Stacked Plot NoCpGs per Feature by Platform
#--------------------------------------------
scale_fill_discrete <- function(...) {scale_fill_manual(..., values = feature.cols)}
gene_annots <- ggplot(data=u_df_CGI_cpg_long, aes(x=Platform, y=No_CpGs, fill=Feature)) +
geom_bar(stat = "identity", colour="black", size=0.25)+
ylab("# CpGs") +
ggtitle("Number of CpGsper feature category by platform") +
scale_fill_discrete(labels=c("hg19_cpg_islands"="CpG Islands", "hg19_cpg_shelves"="shelves", "hg19_cpg_shores"="shores", "hg19_cpg_inter"="inter CGI")) + # Change the name of items
theme_classic()
gene_annots
ggsave(paste0(RESULTS, "8_ExpCoveredCpGsAnnotation/u_StakedBarplot_CGI_byPlatform_NoCpGs.pdf"))
#----------------------------------------------
# Percent CpGs covered per Feature by Platform
#----------------------------------------------
# Calculate percentages of total features covered by each platform
head(u_df_CGI_cpg)
u_df_CGI_cpg_pct <- apply(u_df_CGI_cpg, 2, function(x) x/u_df_CGI_cpg$Total)
head(u_df_CGI_cpg_pct)
# Plot percentages:
u_df_CGI_cpg_pct_long <- melt(t(u_df_CGI_cpg_pct[,-1]))
names(u_df_CGI_cpg_pct_long) <- c("Platform", "Feature", "PercentCpGs_covered" )
gene_annots_pct <- ggplot(data=u_df_CGI_cpg_pct_long, aes(x=Feature, y=PercentCpGs_covered, fill=Platform)) +
geom_bar(stat = "identity", position = "dodge", colour="black", size=0.25)+
#geom_text(aes(label=Percent_covered), vjust=1.6, color="white",
# position = position_dodge(0.9), size=3.5)+
scale_fill_manual(values=col.platforms) +
ylab("% CpGs covered") +
ggtitle("Percent CpGs covered per features by platform") +
theme(axis.title.x=element_blank()) +
# coord_flip()+ # make horizontal chart
scale_x_discrete(limits=c("hg19_cpg_islands","hg19_cpg_shelves","hg19_cpg_shores",
"hg19_cpg_inter"), # Change the order of items
labels=c("hg19_cpg_islands"="CpG Islands", "hg19_cpg_shelves"="shelves",
"hg19_cpg_shores"="shores", "hg19_cpg_inter"="inter CGI")) + # Change the name of items
theme_classic()
gene_annots_pct
ggsave(paste0(RESULTS, "8_ExpCoveredCpGsAnnotation/u_Barplot_CGI_ByFeature_pctCpGs.pdf"))
#----------------------------------------
# Piechart % CpGs in features by Platform
#----------------------------------------
# Compute percentages of each features covered by each platform
u_df_platform_CGI_pct_CpGs <- apply(u_df_CGI_cpg, 2, function(x) pct=x/sum(x))
# calculate percentage:
u_df_platform_CGI_pct_CpGs_long <- melt(t(u_df_platform_CGI_pct_CpGs[,-1]))
names(u_df_platform_CGI_pct_CpGs_long) <- c("Platform", "Feature", "pctCpGs" )
head(u_df_platform_CGI_pct_CpGs_long)
gene_annots <- ggplot(data=u_df_platform_CGI_pct_CpGs_long, aes(x="", y=pctCpGs, fill=Feature)) +
facet_grid(. ~ Platform) +
geom_bar(width = 1, stat = "identity", color="white") +
coord_polar("y", start=0) +
ylab("% CpGs covered") +
ggtitle("Percent CpGs covered per features by platform") +
scale_fill_discrete(name="CGI features",
labels=c("hg19_cpg_islands"="CpG Islands", "hg19_cpg_shelves"="shelves",
"hg19_cpg_shores"="shores", "hg19_cpg_inter"="inter CGI") ,
guide = guide_legend(label = TRUE, label.position = "right",
legend.theme = element_text(size = 8)
)) + # Change the name of items
theme(legend.position="bottom") +
theme_void() # remove background, grid, numeric labels
gene_annots
ggsave(paste0(RESULTS, "8_ExpCoveredCpGsAnnotation/u_Piechart_CGI_byPlatform_pctCpGs.pdf"))
#===============================
# Annotate Enhancers and lncRNA:
#===============================
# Inspect loaded object:
head(annotations_enhancers_fantom.gr)
head(annotations_lncRNA.gr)
annotations_regul.grl <- GRangesList(enhancers=annotations_enhancers_fantom.gr,lncRNA=annotations_lncRNA.gr)
# Count # of CpG in each annotation category for UNION