-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-methylation_calls_optimise_non_CG_segmentationf_2018-03-14.R
1388 lines (1107 loc) · 118 KB
/
5-methylation_calls_optimise_non_CG_segmentationf_2018-03-14.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
# Known bugs:
# Genes with 'Unknown' methylation status (too high or low coverage) are masked out. Some of these are microRNAs, and probably should not be masked out. In fact, should we really be masking out any of these genes? (Fixed by removing 'Unknown' genes from the mask.)
# Could consider, instead, masking out high- and low-coverage zones based on a segmentation of coverage.
# We should use both CHH and CHG thresholds (or in combination) to make a determination between GBM and TEM segments. Currently, there are a number of short segments with low mCHG (and few CHG sites) and high mCHH.
# There are some cases where GBM and TEM segments adjoin, and where, really, both should be TEM - get a list and go through them.
# Where gaps occur due to short segments with either no CG, or no CHG sites, first, check if the segment overlaps any TEs and exons. If no and yes, respectively, then classify it as GBM
#### This part parses command line options or sets defaults
if(!require(optparse)){
install.packages("optparse")
library(optparse)
}
library("optparse")
option_list = list(
make_option(c("-p", "--project"), action="store", default=NA, type='character',
help="project code for location of bs_sequel output"),
make_option(c("-c", "--context"), action="store", default=NA, type='character',
help="methylation context (CG, CHG or CHH)"),
make_option(c("-a", "--action"), action="store", default="all", type='character',
help="actions to perform (load, plot, call, merge, analyse or all)"),
make_option(c("-v", "--verbose"), action="store_true", default=TRUE,
help="Should the program print extra stuff out? [default %default]"),
make_option(c("-q", "--quiet"), action="store_false", dest="verbose",
help="Make the program not be verbose.")
)
opt = parse_args(OptionParser(option_list=option_list))
if (opt$verbose) {
# show the user which options were chosen
cat("Project: ")
cat(opt$project)
cat("\n")
cat("Context: ")
cat(opt$context)
cat("\n")
cat("Action: ")
cat(opt$action)
cat("\n")
}
# project_id and meth_context together are used for finding raw data and naming output files
project_id=NULL
if (is.na(opt$project)) {
# No project defined - set a default
#project_id="test"
project_id="SRA035939" # Schmitz et al, 2011
#project_id="PRJEB2678" # Becker et al, 2011
} else {
project_id=opt$project
}
meth_context=NULL
if (is.na(opt$context)) {
# No context defined - set a default
#meth_context="CG"
#meth_context="CHG"
meth_context="CHH"
} else {
meth_context=opt$context
}
action=NULL
if (is.na(opt$action)) {
# No action defined - set a default
action="all"
#action="load"
#action="plot"
#action="call"
#action="merge"
#action="analyse"
} else {
action=opt$action
}
#### This part installs packages so will be slow the first time per platform
if(!require(data.table)){
install.packages("data.table")
library(data.table)
}
if(!require(stringr)){
install.packages("stringr")
library(stringr)
}
if(!require(ggplot2)){
install.packages("ggplot2")
library(ggplot2)
}
install.packages("ggpubr")
#### This part sets up libraries and contextual information and metadata about the project
library(data.table)
library(stringr)
library(ggplot2)
library(plyr)
library(ggpubr)
# Platform-specific stuff:
# Base of path to project
# Where to find bioconductor (unresolved on cluster at the moment)
pathroot = ""
if (.Platform$OS.type=="windows") {
pathroot="D:"
source("http://bioconductor.org/biocLite.R")
} else if (.Platform$OS.type=="unix") {
if(substr(getwd(),1,20)=="/nbi/Research-Groups") {
# assume we are running on cluster
pathroot="/nbi/Research-Groups/JIC/Daniel-Zilberman"
} else {
# assume we are running in Virtualbox with shared folder /media/sf_D_DRIVE
pathroot="/media/sf_D_DRIVE"
source("http://bioconductor.org/biocLite.R")
}
}
# Source directory containing alignments from bs_sequel
source_dir = paste0(pathroot,"/Projects/Jay-SMP_variation/3-alignments/",project_id,"/")
# Working directory where outputs will be directed to
setwd(dir = paste0(pathroot,"/Projects/Jay-SMP_variation/5-analysis/",project_id,"/"))
reference_fasta = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/TAIR10/TAIR10_Chr.all.fasta") # TAIR10 genome assembly This never gets used at the moment
reference_CG_sites = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/TAIR10/TAIR10_Chr.all.CG_sites.tsv") # We prepared this earlier using a perl script to parse the TAIR10 genome assembly
reference_gff = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Araport11/Araport11_GFF3_genes_transposons.201606.gff") # AraPort11 annotation
reference_exons = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Araport11/GFF/Araport11_GFF3_genes_transposons.201606-exon.gff") # Prepared from AraPort11 annotation by a script in bs_sequel
reference_introns = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Araport11/GFF/Araport11_GFF3_genes_transposons.201606-intron.gff") # Prepared from AraPort11 annotation by a script in bs_sequel
reference_5UTR = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Araport11/GFF/Araport11_GFF3_genes_transposons.201606-five_prime_UTR.gff") # Prepared from AraPort11 annotation by a script in bs_sequel
reference_3UTR = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Araport11/GFF/Araport11_GFF3_genes_transposons.201606-three_prime_UTR.gff") # Prepared from AraPort11 annotation by a script in bs_sequel
### SNP_loci.txt - we need to import a list of all known SNPs between lines considered for analysis, so SNP loci can inform site exclusion criteria
reference_chromatin_states = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Chromatin_states/Sequeira-Mendes_et_al_2014_tpc124578_SupplementalIDS2.txt") # genomic ranges assigning each segment of the nuclear genome to one of 9 chromatin states
reference_DHS_loci = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/DHS_scores/TAIR10_DHSs.gff") # DNase-hypersensitivity sites (indicative of open chromatin)
# For nucleosome positioning data need to first read in from BigWig file, but Windows can't do this so did it using VM, then exported resulting genomicranges object.
# This is the code to run in R in the VM:
#source("http://www.bioconductor.org/biocLite.R")
#biocLite("rtracklayer")
#library(rtracklayer)
#saveRDS(import("/media/sf_D_DRIVE/Reference_data/Genomes/Arabidopsis/Nucleosomes/Ath_leaf_NPS.bw", format="BigWig"), "/media/sf_D_DRIVE/Reference_data/Genomes/Arabidopsis/Nucleosomes/Ath_leaf_NPS.RDS")
reference_nucleosomes = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Nucleosomes/Ath_leaf_NPS.RDS") # Nucleosomes position data from http://plantdhs.org/Download (Jiming Jiang lab)
# get a list of the samples from the subdirectories of the source directory
samples = list.dirs(path = source_dir, full.names = FALSE, recursive = FALSE)
no_samples=length(samples)
# Read the sample metadata in from a text file in the source directory
sample_metadata=read.delim(paste0(source_dir,project_id,"_metadata.tsv"), header=TRUE, sep="\t")
rownames(sample_metadata)=sample_metadata$Identifier
sample_info <- readRDS(paste0(project_id,"_",meth_context,"_sample_info.rds"))
sample_info=cbind(sample_info,sample_metadata)
# Load up genomic ranges and get what we need from the annotation
biocLite("GenomicRanges")
library(GenomicRanges)
gff.genes=readRDS(paste0(project_id,"_CG_gff.genes.rds"))
gff.transposons=readRDS(paste0(project_id,"_CG_gff.transposons.rds"))
# Load the BSGenome version of the reference
biocLite("BSgenome")
library(BSgenome.Athaliana.TAIR.TAIR9)
# Find the sequence lengths for each chromosome
sLengths=seqlengths(Athaliana)
# Use the MethylSeekR package to segment the methylome
library(MethylSeekR)
# Define some loci to mask out from the genome (mitochondria, chloroplast, genes found previously to have coverage anomalies)
mask_loci=data.frame(Chromosome=character(), start_site=integer(), end_site=integer())
mask_loci=rbind(mask_loci,list(Chromosome="ChrM",start_site=1,end_site=sLengths[6]),list(Chromosome="ChrC",start_site=1,end_site=sLengths[7]))
#colnames(mask_loci)=c("Chromosome","start_site","end_site")
# Commented out the part which masks out genes annotated as mCG 'Unknown'
#unknown_gene_loci=gff.genes[gff.genes$m_class=="Unknown",c("V1","V4","V5")]
#colnames(unknown_gene_loci)=colnames(mask_loci)
#unknown_gene_loci$Chromosome=paste0(substr(unknown_gene_loci$Chromosome,1,1),tolower(substr(unknown_gene_loci$Chromosome,2,3)),substr(unknown_gene_loci$Chromosome,4,4))
#mask_loci=rbind.data.frame(mask_loci,unknown_gene_loci)
#rm(unknown_gene_loci)
mask_loci.gr=makeGRangesFromDataFrame(df=mask_loci, start.field="start_site", end.field="end_site", seqnames.field="Chromosome")
# Define genomicranges representing the whole genome
genome_loci=data.frame(Chromosome=character(), start_site=integer(), end_site=integer())
levels(genome_loci$Chromosome)=c("CHR1","CHR2","CHR3","CHR4","CHR5","CHRM","CHRC")
for (chrom_no in 1:length(sLengths)) {
genome_loci[chrom_no,]=list(Chromosome=toupper(names(sLengths[chrom_no])),start_site=1,end_site=sLengths[chrom_no])
}
genome_loci.gr=makeGRangesFromDataFrame(df=genome_loci, start.field="start_site", end.field="end_site", seqnames.field="Chromosome")
# The following function is from the package MethylSeekR
# The following changes have been made:
# nCG.classification threshold on no CG sites to differentiate between LMR and UMR segments replaced by an array of 'median methylation' cutoff levels (may want to change this to use pmeth instead)
# This was done because in Arabidopsis, there is not a clear separation in segment size between unmethylated and low-methylated segments, but there is a clear separation in methylation level, in contrast to mammals.
# In Arabidopsis, CG sites tend to be fully methylated, or unmethylated, so using the median values introduces sampling artefacts
# median.meth replaced by pmeth in scatterplot
# nCG replaced by nSites for generality across contexts
segmentUMRsLMRs <- function (m, meth.cutoff = 0.5, nCpG.cutoff = 3, PMDs = NA, pdfFilename = NULL,
num.cores = 1, myGenomeSeq, seqLengths, nCpG.smoothing = 3,
minCover = 5)
{
#nCG.classification <- 30
#mMeth.classification=c(0.2,0.4,0.75)
mMeth.classification=c(0.2)
message("identifying UMRs and LMRs")
m = m[values(m)[, 1] >= minCover]
nCGsPerChr = table(as.character(seqnames(m)))
chrs = names(nCGsPerChr)[nCGsPerChr >= nCpG.smoothing]
res <- mclapply(chrs, function(chr) {
sel <- which(as.character(seqnames(m)) == chr)
mean.meth <- runmean(Rle(values(m)[sel, 2]/values(m)[sel,1]), k = nCpG.smoothing, endrule = "constant")
indx <- mean.meth < meth.cutoff
runValue(indx)[runLength(indx) < nCpG.cutoff & runValue(indx) == TRUE] = FALSE
runValue(indx)[runLength(indx) < nCpG.cutoff & runValue(indx) == FALSE] = TRUE
tmp.ids <- rep(1:length(runLength(indx)), runLength(indx))
tmp <- split(1:length(sel), tmp.ids)
tmp <- tmp[runValue(indx) == TRUE]
if (length(tmp) > 0) {
coords <- cbind(sapply(tmp, min), sapply(tmp, max))
starts <- round((start(m)[sel[pmax(1, coords[, 1] - 1)]] + start(m)[sel[coords[, 1]]])/2)
ends <- round((start(m)[sel[coords[, 2]]] + start(m)[sel[pmin(length(sel), coords[, 2] + 1)]])/2)
hmr.gr = GRanges(seqnames = unique(seqnames(m[sel])),
strand = "*", ranges = IRanges(starts, ends),
seqlengths = seqLengths)
}
else {
hmr.gr = GRanges(, seqlengths = seqLengths)
}
hmr.gr
}, mc.cores = num.cores)
segments.gr = do.call(c, unname(res))
if (class(PMDs) == "GRanges") {
segments.gr = subsetByOverlaps(segments.gr, PMDs[values(PMDs)$type == "notPMD"])
}
nSites = vcountPattern("CG", getSeq(myGenomeSeq, resize(segments.gr, width(segments.gr), fix = "start"), as.character = FALSE))
ov <- findOverlaps(m, segments.gr)
T = tapply(values(m[queryHits(ov)])[, 1], subjectHits(ov), sum)
M = tapply(values(m[queryHits(ov)])[, 2], subjectHits(ov), sum)
nSites.segmentation = tapply(values(m[queryHits(ov)])[, 1], subjectHits(ov), length)
median.meth = tapply(as.vector(runmean(Rle(values(m[queryHits(ov)])[,2]/values(m[queryHits(ov)])[, 1]), nCpG.smoothing, endrule = "constant")), subjectHits(ov), median)
median.meth = pmax(0, median.meth)
if (!all.equal(as.numeric(names(T)), 1:length(segments.gr))) {
message("error in calculating methylation levels for PMDs")
}
type=ifelse(median.meth<mMeth.classification[1],"UMR", "LMR")
#type=ifelse(median.meth<mMeth.classification[1],0,ifelse(median.meth<mMeth.classification[2],1,ifelse(median.meth<mMeth.classification[3],2,3)))
#type = c("UMR", "LMR")[as.integer(nCG < nCG.classification) + 1]
values(segments.gr) = DataFrame(nSites.segmentation, nSites, T, M, pmeth = M/T, median.meth = median.meth, type)
jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
if (!is.null(pdfFilename)) {
pdf(pdfFilename, height = 5, width = 5)
}
smoothScatter(log2(values(segments.gr)$nSites), 100 * values(segments.gr)$pmeth,
colramp = jet.colors, xlab = "log2 number of sites in segment",
ylab = "mean methylation (%)")
#abline(v = log2(nCG.classification), lty = 5)
abline(h = 100 * mMeth.classification[1], lty = 5)
abline(h = 100 * mMeth.classification[2], lty = 5)
abline(h = 100 * mMeth.classification[3], lty = 5)
if (!is.null(pdfFilename))
dev.off()
segments.gr
}
meth_by_segment <- function (m, segment_model, meth_context, num.cores = 1, myGenomeSeq, seqLengths, nSite.smoothing = 3, mMeth.classification=c(0.7), mMeth.classes=c("LMR","FMR"))
{
# Replaced nCG with nSites for generality
# m is a genomic ranges object representing a methylome
# segment_model is an arbitrary genomic ranges object representing a set of query segments of interest
# Count CG sites in each segment
# Added fixed=FALSE so that ambiguity codes will work properly
nSites = vcountPattern(meth_context, getSeq(Athaliana, resize(segment_model, width(segment_model), fix = "start"), as.character = FALSE), fixed=FALSE)
# Overlap the methylome with the segments
ov <- findOverlaps(m, segment_model)
# Count Ts and Ms per segment
T = tapply(values(m[queryHits(ov)])[, 1], subjectHits(ov), sum)
M = tapply(values(m[queryHits(ov)])[, 2], subjectHits(ov), sum)
nSites.segmentation = tapply(values(m[queryHits(ov)])[, 1], subjectHits(ov), length)
# The next step carries out a robust smoothing of the methylation values across the segment. It takes k adjacent sites (default=3) and calculates M/T for a sliding window of that size, then takes the median value of this proportion across the segment. k=3 give 'minimal' robust smoothing eliminating isolated outliers.
# This might be problematic for us, as in cases where each site is either fully methylated or unmethylated, it will tend to generate sequences like this for a window size of 3:
# 0,0,0,1/3,1/3,1/3,1/3,2/3,2/3,2/3,1,1,1,1,1 - this will lead to clusters of median methylation at 1/3 or 2/3 except for mostly unmethylated or methylated segments.
median.meth = tapply(as.vector(runmean(Rle(values(m[queryHits(ov)])[,2]/values(m[queryHits(ov)])[, 1]), nSite.smoothing, endrule = "constant")), subjectHits(ov), median)
median.meth = pmax(0, median.meth)
if (!all.equal(as.numeric(names(T)), 1:length(segment_model))) {
message("error in calculating methylation levels for PMDs")
}
# Original code used median.meth to make a 'type' determination for the segment. We will use pmeth instead
#type=ifelse(median.meth<mMeth.classification[1],mMeth.classes[1], mMeth.classes[2])
type=ifelse(M/T<mMeth.classification[1],mMeth.classes[1], mMeth.classes[2])
#type=ifelse(median.meth<mMeth.classification[1],0,ifelse(median.meth<mMeth.classification[2],1,ifelse(median.meth<mMeth.classification[3],2,3)))
#type = c("UMR", "LMR")[as.integer(nCG < nCG.classification) + 1]
DataFrame(nSites.segmentation, nSites, T, M, pmeth = M/T, median.meth = median.meth, type)
}
# Function to return NAs for cases where segmentation model doen's overlap any sites in the given context
NA_by_segment <- function (segment_model)
{
segment_count = length(segment_model)
nSites.segmentation = rep(as.integer(NA),segment_count)
nSites = rep(as.integer(NA),segment_count)
T = array(NA,segment_count)
M = array(NA,segment_count)
pmeth = array(NA,segment_count)
median.meth = rep(as.numeric(NA),segment_count)
type = array(NA,segment_count)
DataFrame(nSites.segmentation, nSites, T, M, pmeth = M/T, median.meth = median.meth, type)
}
# Define a corresponding convenience function to save an annotated genomic ranges object
# Function adapted from MethylSeekR package
saveUMRLMRSegments <- function (segs, GRangesFilename = NULL, TableFilename = NULL)
{
# Replaced nCG with nSites for generality
if (!is.null(GRangesFilename))
saveRDS(segs, GRangesFilename)
if (!is.null(TableFilename)) {
D = data.frame(chr = as.character(seqnames(segs)), start = start(segs),
end = end(segs), type = values(segs)$type, nSites.segmentation = values(segs)$nSites.segmentation,
nSites.seq = values(segs)$nSites, mean.meth = 100 * values(segs)$pmeth,
median.meth = 100 * values(segs)$median.meth)
write.table(D, file = TableFilename, quote = FALSE, sep = "\t",
row.names = FALSE)
}
}
# Define a convenience function to convert chromosome names to mixed-case (e.g. Chr9, ChrC)
mixedCaseChr <- function(s, strict = FALSE) {
paste0(toupper(substr(s,1,1)),tolower(substr(s,2,3)),toupper(substr(s,4,4)))
}
# Set chromosome names to mixed case in genome segments object
levels(genome_loci.gr@seqnames@values) = mixedCaseChr(as.character(levels(genome_loci.gr@seqnames@values)))
genome_loci.gr@seqinfo@seqnames = levels(genome_loci.gr@seqnames@values)
levels(mask_loci.gr@seqnames@values) = mixedCaseChr(as.character(levels(mask_loci.gr@seqnames@values)))
mask_loci.gr@seqinfo@seqnames = levels(mask_loci.gr@seqnames@values)
methylated_loci.gr=reduce(makeGRangesFromDataFrame(df=rbind(gff.transposons[,c("V1","V4","V5")],gff.genes[gff.genes$m_class=="Heterochromatic",c("V1","V4","V5")]), start.field="V4", end.field="V5", seqnames.field="V1"))
gene_body_loci.gr=reduce(makeGRangesFromDataFrame(df=gff.genes[gff.genes$m_class=="Gene-body Methylated",c("V1","V4","V5")], start.field="V4", end.field="V5", seqnames.field="V1"))
unmethylated_loci.gr=reduce(makeGRangesFromDataFrame(df=gff.genes[gff.genes$m_class=="Unmethylated",c("V1","V4","V5")], start.field="V4", end.field="V5", seqnames.field="V1"))
# Concatenate the CHG and CHH methylomes, and sort them, by position and chromosome
meth_parents_CHG_CHH.gr <- sort(sortSeqlevels(c(readMethylome(FileName=paste0(project_id,"_CHG_TM_read_counts_across_parents.tsv"), seqLengths=sLengths), readMethylome(FileName=paste0(project_id,"_CHH_TM_read_counts_across_parents.tsv"), seqLengths=sLengths))))
m_n_non_CG_optimisation=data.frame(m=numeric(), n=numeric(), UMR=integer(), LMR=integer(), fp=numeric(), tp=numeric())
# Loop through values for m and n parameters
#m_CHG.seq=seq(0.002,0.1, by=0.002)
m_non_CG.seq=seq(0.05,0.5, by=0.05)
#n_CHG.seq=seq(1,20, by=1)
n_non_CG.seq=seq(50,500, by=50)
for (m_non_CG.sel in m_non_CG.seq) {
for (n_non_CG.sel in n_non_CG.seq) {
#n.sel=4
#UMRLMRsegments_CHG.gr <- segmentUMRsLMRs(m=meth_parents_CHG.gr, meth.cutoff=m_CHG.sel, nCpG.cutoff=n_CHG.sel, num.cores=1, myGenomeSeq=Athaliana, seqLengths=sLengths)
#UMRLMRsegments_CHG.gr <- segmentUMRsLMRs(m=meth_parents_CHH.gr, meth.cutoff=m_CHG.sel, nCpG.cutoff=n_CHG.sel, num.cores=1, myGenomeSeq=Athaliana, seqLengths=sLengths)
UMRLMRsegments_non_CG.gr <- segmentUMRsLMRs(m=meth_parents_CHG_CHH.gr, meth.cutoff=m_non_CG.sel, nCpG.cutoff=n_non_CG.sel, num.cores=1, myGenomeSeq=Athaliana, seqLengths=sLengths)
# Capitalise chromosome names in segmentation object
levels(UMRLMRsegments_non_CG.gr@seqnames)=c("CHR1","CHR2","CHR3","CHR4","CHR5","CHRM","CHRC")
UMRLMRsegments_non_CG.gr@seqinfo@seqnames=c("CHR1","CHR2","CHR3","CHR4","CHR5","CHRM","CHRC")
# Find overlaps between segments and positive and negative 'control' loci (annotated genes and transposons)
fp_hits=findOverlaps(methylated_loci.gr, UMRLMRsegments_non_CG.gr)
fp_overlaps <- pintersect(methylated_loci.gr[queryHits(fp_hits)], UMRLMRsegments_non_CG.gr[subjectHits(fp_hits)])
fp_overlap_prop <- sum(width(fp_overlaps)) / sum(width(methylated_loci.gr))
tp_hits=findOverlaps(unmethylated_loci.gr, UMRLMRsegments_non_CG.gr)
tp_overlaps <- pintersect(unmethylated_loci.gr[queryHits(tp_hits)], UMRLMRsegments_non_CG.gr[subjectHits(tp_hits)])
tp_overlap_prop <- sum(width(tp_overlaps)) / sum(width(unmethylated_loci.gr))
# Find overlaps between segments and variable loci
# variable_sites_UMR_olaps = findOverlaps(variant_call_ranges, UMRLMRsegments_CHG.gr[UMRLMRsegments_CHG.gr@elementMetadata$type=="UMR"])
# variable_sites_LMR_olaps = findOverlaps(variant_call_ranges, UMRLMRsegments_CHG.gr[UMRLMRsegments_CHG.gr@elementMetadata$type=="LMR"])
m_n_non_CG_optimisation=rbind(m_n_non_CG_optimisation, list(m=m_non_CG.sel, n=n_non_CG.sel, UMR=0, LMR=0, fp=fp_overlap_prop, tp=tp_overlap_prop))
# How many variable sites were captured?
# cat(paste0("m.sel=",m_CHG.sel," n.sel=",n_CHG.sel," Variable sites in UMRs: ",length(variable_sites_UMR_olaps@from)," LMRs: ",length(variable_sites_LMR_olaps@from),"\n"))
}
}
m_n_non_CG_optimisation$variable_sites_captured=m_n_non_CG_optimisation$UMR+m_n_non_CG_optimisation$LMR
# Plot ROC curves for each value of n, and estimate AUC
pdf(paste0(project_id,"_",meth_context,"_MethylSeekR_m_n_non_CG_optimisation_parents_ROC.pdf"))
print(ggplot(m_n_non_CG_optimisation, aes(x=fp, y=tp)) + geom_point() +geom_text(aes(label=paste0(m)),hjust=0, vjust=0) +geom_line() +facet_wrap(~ n))
dev.off()
# Estimate AUC from trapezium approximation
cat(paste0("Estimated AUC values from ROC curves generated by varying m parameter for each value of n in MethylSeekR:\n"))
best_n_non_CG = 0
prev_best_auc_non_CG = 0
for (n_non_CG.sel in n_non_CG.seq) {
m_n_non_CG_optimisation_auc=0
prev_tp=0
prev_fp=0
for (m_non_CG.sel in m_non_CG.seq) {
m_n_non_CG_optimisation_auc = m_n_non_CG_optimisation_auc + (1-m_n_non_CG_optimisation[(m_n_non_CG_optimisation$m==m_non_CG.sel) & (m_n_non_CG_optimisation$n==n_non_CG.sel),]$fp)*(m_n_non_CG_optimisation[(m_n_non_CG_optimisation$m==m_non_CG.sel) & (m_n_non_CG_optimisation$n==n_non_CG.sel),]$tp-prev_tp) + ((m_n_non_CG_optimisation[(m_n_non_CG_optimisation$m==m_non_CG.sel) & (m_n_non_CG_optimisation$n==n_non_CG.sel),]$tp-prev_tp)*(m_n_non_CG_optimisation[(m_n_non_CG_optimisation$m==m_non_CG.sel) & (m_n_non_CG_optimisation$n==n_non_CG.sel),]$fp-prev_fp))/2
prev_tp=m_n_non_CG_optimisation[(m_n_non_CG_optimisation$m==m_non_CG.sel) & (m_n_non_CG_optimisation$n==n_non_CG.sel),]$tp
prev_fp=m_n_non_CG_optimisation[(m_n_non_CG_optimisation$m==m_non_CG.sel) & (m_n_non_CG_optimisation$n==n_non_CG.sel),]$fp
}
if (m_n_non_CG_optimisation_auc > prev_best_auc_non_CG) {
best_n_non_CG = n_non_CG.sel
prev_best_auc_non_CG = m_n_non_CG_optimisation_auc
}
cat(paste0(" n=",n_non_CG.sel," AUC=",m_n_non_CG_optimisation_auc,"\n"))
}
cat(paste0("n=",best_n_non_CG," maximises AUC (",prev_best_auc_non_CG,").\n"))
#n=50 AUC=0.853405558876841
#n=100 AUC=0.880384843230491
#n=150 AUC=0.890148958302221
#n=200 AUC=0.896297635230469
#n=250 AUC=0.90066653773841
#n=300 AUC=0.904636365469396
#n=350 AUC=0.907922263804411
#n=400 AUC=0.910975041031063
#n=450 AUC=0.913507235932189
#n=500 AUC=0.916029402508772
#n=500 maximises AUC (0.916029402508772).
# best_n_non_CG = 500
# Inspection of ROC curves indicates that tp rate maxes out (~1.0) around m=0.15, with fp rate minimised at .25 for n=500.
# Free up some memory by removing largest object(s) and garbage collection
### This is not always effective (R may not defrag memory effectively). If that happens, and you still run out of memory when trying to segment after gc(), try:
### Quit R; rerun everything before the above segmentation m/n optimisation loop; set best_n_non_CG by hand to the value found previously; then continue here:
rm(UMRLMRsegments_non_CG.gr)
gc()
# Segment using best_n and m=0.9 to find appropriate cutoff for m.sel
UMRLMRsegments_non_CG.gr <- segmentUMRsLMRs(m=meth_parents_CHG_CHH.gr, meth.cutoff=0.9, nCpG.cutoff=best_n_non_CG, num.cores=1, myGenomeSeq=Athaliana, seqLengths=sLengths, pdfFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_non_CG_segmentation_landscape_",best_n_non_CG,"_0_9.pdf"))
saveUMRLMRSegments(segs=UMRLMRsegments_non_CG.gr, GRangesFilename=paste0("UMRsLMRs_parents_non_CG_0.9_",best_n_non_CG,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_non_CG_0.9_",best_n_non_CG,".tsv"))
# UMRLMRsegments_non_CG.gr <- readRDS(paste0("UMRsLMRs_parents_non_CG_0.9_",best_n_non_CG,".gr.rds"))
# More formally:
# Plot density distribution of segment mCH*
pdf(paste0(project_id,"_",meth_context,"_MethylSeekR_non_CG_segmentation_landscape_",best_n_non_CG,"_0_9_density.pdf"))
print(ggplot(as.data.frame(UMRLMRsegments_non_CG.gr), aes(x=pmeth)) + geom_histogram(binwidth=0.01))
dev.off()
# Visual inspection of SRA035939_CHH_MethylSeekR_non_CG_segmentation_landscape_500_0_9.pdf shows three clear populations of segments. Short methylated segments (n<2^5, m>0.15), and longer unmethylated and low-methylated segments (n>2^10, m<0.1; m>0.1).
# Fit mixture of three Gaussians to mCHG density to identify cutoff
# Initially tried fitting 3 Gaussians to whole data set, but third Gaussian was used to cover the scattering of high-pMeth sgments (m>0.3). Accordingly, fit to segments with m<0.3 only
#install.packages("MASS") # for fitting negative binomial models to data
#library(MASS)
install.packages("mixtools")
library(mixtools)
m_non_CG_mixmdl = normalmixEM(as.data.frame(UMRLMRsegments_non_CG.gr)[(!is.na(as.data.frame(UMRLMRsegments_non_CG.gr)$pmeth)) & (as.data.frame(UMRLMRsegments_non_CG.gr)$pmeth<0.3),]$pmeth, k=3)
# Had to exclude sites with NA CHG methylation or the fitting algorithm exploded
# Print the characteristics of the fit curves
cat(paste0("Fit lambda values (mixture proportions):\n"))
m_non_CG_mixmdl$lambda
# Schmitz data:
# Becker data:
cat(paste0("Fit mu values (means):\n"))
m_non_CG_mixmdl$mu
# Schmitz data:
# Becker data:
cat(paste0("Fit sigma values (st.devs):\n"))
m_non_CG_mixmdl$sigma
# Schmitz data:
# Becker data:
# Find the intersection of the two larger components in the mixture - here we cut off
#packages were installed and mixfn defined earlier
#install.packages("rootSolve")
library(rootSolve)
mixfn <- function(x, m1, sd1, m2, sd2, p1, p2){
dnorm(x, m1, sd1) * p1 - dnorm(x, m2, sd2) * p2 }
m_non_CG_low_model=1
m_non_CG_high_model=m_non_CG_low_model+1
segment_m_non_CG_cutoff=uniroot.all(mixfn, lower=0, upper=1, m1=m_non_CG_mixmdl$mu[m_non_CG_low_model], sd1=m_non_CG_mixmdl$sigma[m_non_CG_low_model], m2=m_non_CG_mixmdl$mu[m_non_CG_high_model], sd2=m_non_CG_mixmdl$sigma[m_non_CG_high_model], p1=m_non_CG_mixmdl$lambda[m_non_CG_low_model], p2=m_non_CG_mixmdl$lambda[m_non_CG_high_model])
segment_m_non_CG_cutoff
# Schmitz data: 0.05920002 but 3 Gaussians doesn't look like a great fit
# Becker data:
pdf(paste0(project_id,"_",meth_context,"_m_non_CG_segmentation_m_non_CG_cutoff_density_fitted.pdf"))
print(plot(m_non_CG_mixmdl,which=2, breaks=seq(0,1,0.001)))
dev.off()
# Segment based on optimum parameters (m~0.067)
# Clean up memory space
rm(UMRLMRsegments_non_CG.gr)
gc()
m_non_CG.sel = segment_m_non_CG_cutoff
# Run optimised segmentation
UMRLMRsegments_non_CG.gr <- segmentUMRsLMRs(m=meth_parents_CHG_CHH.gr, meth.cutoff=m_non_CG.sel, nCpG.cutoff=best_n_non_CG, num.cores=1, myGenomeSeq=Athaliana, seqLengths=sLengths)
# Save segmentation
saveUMRLMRSegments(segs=UMRLMRsegments_non_CG.gr, GRangesFilename=paste0("UMRsLMRs_parents_non_CG_",m_non_CG.sel,"_",best_n_non_CG,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_non_CG_",m_non_CG.sel,"_",best_n_non_CG,".tsv"))
# Fitting Gaussians to separate the components of the pmeth distribution was generally unsuccessful. It was able to identify the cutoff around pmeth=0.067, but this led to highly fragmented unmethylated regions, with very many very short methylated segments that were unconvincing. Manually setting the cutoff to pmeth=0.15 in the segmentation, on the basis of visual inspection of the methylation landscape, produced a much better segmentation result.
m_non_CG.sel=0.15
# Clean up memory space
rm(UMRLMRsegments_non_CG.gr)
gc()
UMRLMRsegments_non_CG.gr <- segmentUMRsLMRs(m=meth_parents_CHG_CHH.gr, meth.cutoff=m_non_CG.sel, nCpG.cutoff=best_n_non_CG, num.cores=1, myGenomeSeq=Athaliana, seqLengths=sLengths, pdfFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_non_CG_segmentation_landscape_",best_n_non_CG,"_",m_non_CG.sel,".pdf"))
pdf(paste0(project_id,"_",meth_context,"_MethylSeekR_CG_segmentation_landscape_",best_n_non_CG,"_",m_non_CG.sel,"_density.pdf"))
print(ggplot(as.data.frame(UMRLMRsegments_non_CG.gr), aes(x=pmeth)) + geom_histogram(binwidth=0.01))
dev.off()
# Save segmentation
saveUMRLMRSegments(segs=UMRLMRsegments_non_CG.gr, GRangesFilename=paste0("UMRsLMRs_parents_non_CG_",m_non_CG.sel,"_",best_n_non_CG,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_non_CG_",m_non_CG.sel,"_",best_n_non_CG,".tsv"))
#MG_segments.gr = setdiff(genome_loci.gr, mask_loci.gr)
m_non_CG_segments.gr = setdiff(setdiff(genome_loci.gr, UMRLMRsegments_non_CG.gr), mask_loci.gr)
UMR_non_CG_segments.gr = setdiff(UMRLMRsegments_non_CG.gr, mask_loci.gr)
# Switch to CG context to complete the segmentation
meth_context="CG"
# Free up some space
rm(meth_parents_CHG_CHH.gr)
gc()
# Read the CG methylome data in as a Granges object
meth_parents_CG.gr <- readMethylome(FileName=paste0(project_id,"_",meth_context,"_TM_read_counts_across_parents.tsv"), seqLengths=sLengths)
# Read the CHG methylome data in as a Granges object
#meth_parents_CHG.gr <- readMethylome(FileName=paste0(project_id,"_",meth_context,"_TM_read_counts_across_parents.tsv"), seqLengths=sLengths)
# Identify the parts of the parental mCGome which overlap with unmethylated parental mCHGome
#potential_CG_segments.gr = meth_parents_CG.gr[unique(subjectHits(findOverlaps(UMRLMRsegments_CHG.gr, meth_parents_CG.gr)))]
potential_CG_segments.gr = meth_parents_CG.gr[unique(subjectHits(findOverlaps(UMR_non_CG_segments.gr, meth_parents_CG.gr)))]
# Try m.sel=0.95 to check out whole methylome landscape
m.sel=0.95
n.sel=1 # We segment using minimum segment size of 1 so that we can extend our mCHG segments by a single CG site only, if necessary (minimise likelihood of extending a mCHG segment into an adjacent mCG but non-mCHG segment).
UMRLMRsegments_CG.gr <- segmentUMRsLMRs(m=potential_CG_segments.gr, meth.cutoff=m.sel, nCpG.cutoff=n.sel, num.cores=1, myGenomeSeq=Athaliana, seqLengths=sLengths, pdfFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_CHG_segmentation_landscape_",n.sel,"_",m.sel,".pdf"))
# Save a copy for visualisation
saveUMRLMRSegments(segs=UMRLMRsegments_CG.gr, GRangesFilename=paste0("UMRsLMRs_parents_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_",m.sel,"_",n.sel,".tsv"))
# Visual inspection of landscape shows clear cutoff at 0.4 between methylated and unmethylated segments among non-mCHG loci
m.sel=0.4
n.sel=1 # We segment using minimum segment size of 1 so that we can extend our m_non_CG segments by a single CG site only, if necessary (minimise likelihood of extending a m_non_CG segment into an adjacent mCG but non-m_non_CG segment).
UMRLMRsegments_CG.gr <- segmentUMRsLMRs(m=potential_CG_segments.gr, meth.cutoff=m.sel, nCpG.cutoff=n.sel, num.cores=1, myGenomeSeq=Athaliana, seqLengths=sLengths)
# Save a copy for visualisation
saveUMRLMRSegments(segs=UMRLMRsegments_CG.gr, GRangesFilename=paste0("UMRsLMRs_parents_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_",m.sel,"_",n.sel,".tsv"))
# Find the mCG segments, rather than the unmethylated, and filter by masked genome:
# First numbers = with 'Unknown' genes masked out, second numbers with only CHRM and CHRC masked
mCG_segments_1.gr = setdiff(setdiff(genome_loci.gr, mask_loci.gr),UMRLMRsegments_CG.gr)
length(mCG_segments_1.gr)
# 41863, 41808 mCG segments
# Adjust segments to remove any segments which don't overlap with any CG sites
mCG_segments_1.gr = mCG_segments_1.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, mCG_segments_1.gr)))]
length(mCG_segments_1.gr)
# 41844, 41805 mCG segments
# Add back the mCG values for the mCG segments, so it will save properly
values(mCG_segments_1.gr) = cbind(values(mCG_segments_1.gr),meth_by_segment(meth_parents_CG.gr, segment_model=mCG_segments_1.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
# Save a copy for visualisation
saveUMRLMRSegments(segs=mCG_segments_1.gr, GRangesFilename=paste0("UMRsLMRs_parents_mCG_masked_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_mCG_masked_",m.sel,"_",n.sel,".tsv"))
# Find the mCG segments which overlap with m_non_CG segments:
#mCHG_mCG_segments.gr=mCG_segments_1.gr[unique(subjectHits(findOverlaps(setdiff(setdiff(genome_loci.gr, mask_loci.gr),UMRLMRsegments_CHG.gr), mCG_segments_1.gr)))]
m_non_CG_mCG_segments.gr=mCG_segments_1.gr[unique(subjectHits(findOverlaps(m_non_CG_segments.gr, mCG_segments_1.gr)))]
length(m_non_CG_mCG_segments.gr)
# 2683, 2584 mCG segments have an overlap with m_non_CG segments
# Add back the mCG values for the mCG overlap segments, so it will save properly
values(m_non_CG_mCG_segments.gr) = cbind(values(m_non_CG_mCG_segments.gr),meth_by_segment(meth_parents_CG.gr, segment_model=m_non_CG_mCG_segments.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
# Save a copy for visualisation
saveUMRLMRSegments(segs=m_non_CG_mCG_segments.gr, GRangesFilename=paste0("UMRsLMRs_parents_non_CG_CG_overlap_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_non_CG_CG_overlap_",m.sel,"_",n.sel,".tsv"))
# Take the Union of the m_non_CG best fit segments (subtracted from masked genome) and those mCG 1 0.4 segments which overlap them, to extend the m_non_CG best fit segments to the next closest CG site where appropriate:
m_non_CG_segments.gr = reduce(union(setdiff(setdiff(genome_loci.gr, UMR_non_CG_segments.gr), mask_loci.gr), m_non_CG_mCG_segments.gr))
length(m_non_CG_segments.gr)
# 8492, 8423 segments
# Adjust segments to remove any segments which don't overlap with any CG sites
m_non_CG_segments.gr = m_non_CG_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, m_non_CG_segments.gr)))]
length(m_non_CG_segments.gr)
# 8286, 8196 m_non_CG_segments
# Add back the mCG values for the mCHG overlap segments, so it will save properly
values(m_non_CG_segments.gr) = cbind(values(m_non_CG_segments.gr),meth_by_segment(meth_parents_CG.gr, segment_model=m_non_CG_segments.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
# Save a copy for visualisation
saveUMRLMRSegments(segs=m_non_CG_segments.gr, GRangesFilename=paste0("UMRsLMRs_parents_non_CG_segments.gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_non_CG_.tsv"))
# Rebuild the non-m_non_CG fraction of the masked genome for subsequent segmentation in the CG context
potential_CG_segments.gr = meth_parents_CG.gr[unique(subjectHits(findOverlaps(setdiff(setdiff(genome_loci.gr, m_non_CG_segments.gr), mask_loci.gr), meth_parents_CG.gr)))]
### This is where we may have optimised the minimum segment length for segmenting the mCGome. This code moved to separate script, and the numbers set here:
# Schmitz numbers:
best_n = 9
prev_best_auc = 0.936370415538498
# Execute the preferred segmentation model, visualise and save the results
#UMRLMRsegments.gr <- segmentUMRsLMRs(m=meth.gr, meth.cutoff=0.012, nCpG.cutoff=9, num.cores=1, myGenomeSeq=Athaliana, seqLengths=sLengths, pdfFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segmentation_landscape.pdf"))
# Here are a couple of other ideas for setting cutoffs between U and M, especially for short segments:
#m.sel=0.025
# Set m.sel to 99ile of C/(C+T) for "U" sites
m.sel=quantile(cbind(coverage_data,meth_data)[(!is.na(coverage_data$Cov_C)) & (meth_data$meth_status=="U"),]$Cov_C/(cbind(coverage_data,meth_data)[(!is.na(coverage_data$Cov_C)) & (meth_data$meth_status=="U"),]$Cov_C+cbind(coverage_data,meth_data)[(!is.na(coverage_data$Cov_C)) & (meth_data$meth_status=="U"),]$Cov_T),.99)
cat(paste0("mCG proportion which captures 99% of sites 'U' across all samples is ",m.sel,".\n"))
# Schmitz: mCG proportion which captures 99% of sites 'U' across all samples is 0.0769230769230769.
# Fishers p<0.05, Binomial p<0.005: mCG proportion which captures 99% of sites 'U' across all samples is 0.0833333333333333.
# Using pmeth, rather than median.meth with .99 threshold for unmethylated segments produces some bleed into UMR from SMR particularly. .95 is a better cutoff:
m.sel=quantile(cbind(coverage_data,meth_data)[(!is.na(coverage_data$Cov_C)) & (meth_data$meth_status=="U"),]$Cov_C/(cbind(coverage_data,meth_data)[(!is.na(coverage_data$Cov_C)) & (meth_data$meth_status=="U"),]$Cov_C+cbind(coverage_data,meth_data)[(!is.na(coverage_data$Cov_C)) & (meth_data$meth_status=="U"),]$Cov_T),.95)
cat(paste0("mCG proportion which captures 95% of sites 'U' across all samples is ",m.sel,".\n"))
# Schmitz: mCG proportion which captures 95% of sites 'U' across all samples is 0.0377358490566038.
# Fishers p<0.05, Binomial p<0.005: mCG proportion which captures 95% of sites 'U' across all samples is 0.0384615384615385.
# Finally, we chose m.sel iteratively. By building a finished model of long, medium and short segments in CG context, and assessing density distribution of mCG per segment, we identified a lot of short, lowly methylated segments, that are not credible. We fit a mixture of Gaussians to this distribution and use their crossing point to set the mCG threshold here:
m.sel=0.09309094
# Segment mCG using the minimum segment which maximises AUC for ROC curve
n.sel=best_n
UMRLMRsegments.gr <- segmentUMRsLMRs(m=potential_CG_segments.gr, meth.cutoff=m.sel, nCpG.cutoff=n.sel, num.cores=1, nCpG.smoothing = 1, myGenomeSeq=Athaliana, seqLengths=sLengths, pdfFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segmentation_landscape_parents_CG_",m.sel,"_",n.sel,".pdf"))
#head(UMRLMRsegments.gr)
#plotFinalSegmentation(m=meth.gr, segs=UMRLMRsegments.gr,meth.cutoff=m.sel)
saveUMRLMRSegments(segs=UMRLMRsegments.gr, GRangesFilename=paste0("UMRsLMRs_parents_CG_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_CG_",m.sel,"_",n.sel,".tsv"))
# We observe that n.sel=9 generates reasonable segments, not breaking up the majority of GBM loci. However, it does miss short isolated regions of mCG which look like real GBM.
# As a strategy to address this we carry out a new segmentation with the same m.sel, but with n.sel=3 then again with n.sel=1
# We will use these finer segmentations to split Unmethylated segments, where necessary, but will not use resulting short unmethylated segments to further divide mCG segments.
# Segment using minimum segment length of 3
n.sel=3
UMRLMRsegments_medium.gr <- segmentUMRsLMRs(m=potential_CG_segments.gr, meth.cutoff=m.sel, nCpG.cutoff=n.sel, num.cores=1, nCpG.smoothing = 1, myGenomeSeq=Athaliana, seqLengths=sLengths, pdfFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segmentation_landscape_parents_CG_",m.sel,"_",n.sel,".pdf"))
#head(UMRLMRsegments.gr)
#plotFinalSegmentation(m=meth.gr, segs=UMRLMRsegments.gr,meth.cutoff=m.sel)
saveUMRLMRSegments(segs=UMRLMRsegments_medium.gr, GRangesFilename=paste0("UMRsLMRs_parents_CG_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_CG_",m.sel,"_",n.sel,".tsv"))
# Segment using minimum segment length of 1
n.sel=1
UMRLMRsegments_fine.gr <- segmentUMRsLMRs(m=potential_CG_segments.gr, meth.cutoff=m.sel, nCpG.cutoff=n.sel, num.cores=1, nCpG.smoothing = 1, myGenomeSeq=Athaliana, seqLengths=sLengths, pdfFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segmentation_landscape_parents_CG_",m.sel,"_",n.sel,".pdf"))
#head(UMRLMRsegments.gr)
#plotFinalSegmentation(m=meth.gr, segs=UMRLMRsegments.gr,meth.cutoff=m.sel)
saveUMRLMRSegments(segs=UMRLMRsegments_fine.gr, GRangesFilename=paste0("UMRsLMRs_parents_CG_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_segments_parents_CG_",m.sel,"_",n.sel,".tsv"))
# We have generated Unmethylated Regions with minimum lengths of 9, 3 and 1 CG sites respectively (UMR9, UMR3 and UMR1)
# Masked Genome (MG) = Genome - Mask
# Long Methylated regions (LMR) = MG - (UMR9 + mCHG)
# Medium Methylated regions (MMR) = (MG - (UMR3 + mCHG)) - LMR
# Short methylated regions (SMR) = (MG - (UMR1 + mCHG)) - (LMR + MMR)
# Unmethylated regions (UMR) = UMR9 - (SMR + MMR +mCHG)
# Methylated regions (MR) = MG - UMR
# Load parental CHG and CHH methylomes separately, ready to use for annotating segments
meth_parents_CHG.gr <- readMethylome(FileName=paste0(project_id,"_CHG_TM_read_counts_across_parents.tsv"), seqLengths=sLengths)
# Now the SLOW bit
meth_parents_CHH.gr <- readMethylome(FileName=paste0(project_id,"_CHH_TM_read_counts_across_parents.tsv"), seqLengths=sLengths)
MG_segments.gr = setdiff(genome_loci.gr, mask_loci.gr)
#LMR_segments.gr = setdiff(MG_segments.gr, UMRLMRsegments.gr)
#MMR_segments.gr = setdiff(setdiff(MG_segments.gr, UMRLMRsegments_medium.gr), LMR_segments.gr)
#SMR_segments.gr = setdiff(setdiff(MG_segments.gr, UMRLMRsegments_fine.gr), c(LMR_segments.gr, MMR_segments.gr))
#UMR_segments.gr = setdiff(UMRLMRsegments.gr, c(SMR_segments.gr, MMR_segments.gr))
#MR_segments.gr = setdiff(MG_segments.gr, UMR_segments.gr)
MG_non_CG_segments.gr = setdiff(setdiff(genome_loci.gr, m_non_CG_segments.gr), mask_loci.gr)
#LMR_segments.gr = setdiff(MG_non_CG_segments.gr, UMRLMRsegments.gr)
LMR_segments.gr = setdiff(setdiff(setdiff(genome_loci.gr, UMRLMRsegments.gr), m_non_CG_segments.gr), mask_loci.gr)
#MMR_segments.gr = setdiff(setdiff(MG_non_CG_segments.gr, UMRLMRsegments_medium.gr), LMR_segments.gr)
MMR_segments.gr = setdiff(setdiff(setdiff(setdiff(genome_loci.gr, UMRLMRsegments_medium.gr), m_non_CG_segments.gr), mask_loci.gr), LMR_segments.gr)
#SMR_segments.gr = setdiff(setdiff(MG_non_CG_segments.gr, UMRLMRsegments_fine.gr), c(LMR_segments.gr, MMR_segments.gr))
SMR_segments.gr = setdiff(setdiff(setdiff(setdiff(setdiff(genome_loci.gr, UMRLMRsegments_fine.gr), m_non_CG_segments.gr), mask_loci.gr), LMR_segments.gr), MMR_segments.gr)
#UMR_segments.gr = setdiff(UMRLMRsegments.gr, c(SMR_segments.gr, MMR_segments.gr))
#UMR_segments.gr = setdiff(setdiff(setdiff(setdiff(UMRLMRsegments.gr, SMR_segments.gr), MMR_segments.gr), mCHG_segments.gr), mask_loci.gr)
#MR_segments.gr = setdiff(MG_non_CG_segments.gr, UMR_segments.gr)
# Adjust segments to set aside any segments which don't overlap with any CG sites. We will deal with them later
# Counts here are first, using Fishers p<0.01, Binomial p<0.01; second, using Fishers p<0.05, Binomial p<0.005; third, masking mCHG segments prior to segmenting mCG; fourth, segmenting CHG and CHH together, then masking; fifth masking only CHRM ande CHRC
set_aside_no_CG.gr = c(setdiff(LMR_segments.gr, LMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, LMR_segments.gr)))]), setdiff(MMR_segments.gr, MMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, MMR_segments.gr)))]), setdiff(SMR_segments.gr, SMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, SMR_segments.gr)))]))
cat(paste0("set_aside_no_CG.gr contains ",length(set_aside_no_CG.gr)," segments which contain no CG sites.\n"))
# 45
MG_segments.gr = MG_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, MG_segments.gr)))]
cat(paste0("MG_segments.gr contains ",length(MG_segments.gr)," segments after trimming segments with no ",meth_context," sites.\n"))
# 1362, 1065, 1065, 1029, 5
LMR_segments.gr = LMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, LMR_segments.gr)))]
cat(paste0("LMR_segments.gr contains ",length(LMR_segments.gr)," segments after trimming segments with no ",meth_context," sites.\n"))
# 16500, 16909, 13659, 12401, 12419
MMR_segments.gr = MMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, MMR_segments.gr)))]
cat(paste0("MMR_segments.gr contains ",length(MMR_segments.gr)," segments after trimming segments with no ",meth_context," sites.\n"))
# 5294, 5315, 5898, 5053, 5069
SMR_segments.gr = SMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, SMR_segments.gr)))]
cat(paste0("SMR_segments.gr contains ",length(SMR_segments.gr)," segments after trimming segments with no ",meth_context," sites.\n"))
# 5635, 8385, 9949, 7955, 7648
#MR_segments.gr = MR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, MR_segments.gr)))]
#cat(paste0("MR_segments.gr contains ",length(MR_segments.gr)," segments after trimming segments with no ",meth_context," sites.\n"))
# 27429, 30609, 29506, 25106
# Annotate segments with CG methylation values
#values(MG_segments.gr) = cbind(values(MG_segments.gr),meth_by_segment(meth_parents_CG.gr, segment_model=MG_segments.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(LMR_segments.gr) = cbind(values(LMR_segments.gr),meth_by_segment(meth_parents_CG.gr, segment_model=LMR_segments.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","LMR")))
values(MMR_segments.gr) = cbind(values(MMR_segments.gr),meth_by_segment(meth_parents_CG.gr, segment_model=MMR_segments.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MMR")))
values(SMR_segments.gr) = cbind(values(SMR_segments.gr),meth_by_segment(meth_parents_CG.gr, segment_model=SMR_segments.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","SMR")))
#values(MR_segments.gr) = cbind(values(MR_segments.gr),meth_by_segment(meth_parents_CG.gr, segment_model=MR_segments.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
#replace values previously added:
values(m_non_CG_segments.gr) = cbind(meth_by_segment(meth_parents_CG.gr, segment_model=m_non_CG_segments.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m_non_CG.sel), mMeth.classes=c("UMR","TEM")))
# Adjust SMRs to remove any with too low mCG
# Visual observation of the distribution of mCG density, and good sense (we are averaging over six samples) suggests SMRs with mCG<0.25 are not credible
SMR_segments.gr = SMR_segments.gr[ as.vector((!is.na(SMR_segments.gr$pmeth)) & (SMR_segments.gr$pmeth>0.25)) ]
cat(paste0("SMR_segments.gr contains ",length(SMR_segments.gr)," segments after removing segments with mCG<0.25.\n"))
# 4619, 5255, 4570, 4562, 4564
# Build set of UMRs corresponding to new set of LMRs, MMRs, SMRs, TEMs, set_asides:
UMR_segments.gr = setdiff(setdiff(setdiff(setdiff(setdiff(setdiff(genome_loci.gr, SMR_segments.gr), MMR_segments.gr), LMR_segments.gr), m_non_CG_segments.gr), set_aside_no_CG.gr), mask_loci.gr)
cat(paste0("UMR_segments.gr contains ",length(UMR_segments.gr)," segments.\n"))
# 26495, 26255, 26178
# Remove any UMR segments containing no CG sites, after setting them aside
set_aside_no_CG.gr = c(set_aside_no_CG.gr, setdiff(UMR_segments.gr, UMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, UMR_segments.gr)))]))
UMR_segments.gr = UMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CG.gr, UMR_segments.gr)))]
cat(paste0("UMR_segments.gr contains ",length(UMR_segments.gr)," segments after trimming segments with no ",meth_context," sites.\n"))
# 27188, 30383, 29026, 26389, 26137
cat(paste0("set_aside_no_CG.gr contains ",length(set_aside_no_CG.gr)," segments which contain no CG sites.\n"))
# 86
values(UMR_segments.gr) = cbind(values(UMR_segments.gr),meth_by_segment(meth_parents_CG.gr, segment_model=UMR_segments.gr, meth_context="CG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
# Adjust segments to remove any segments which don't overlap with any CHG sites, after setting them aside
set_aside_no_CHG.gr = c(setdiff(LMR_segments.gr, LMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, LMR_segments.gr)))]), setdiff(MMR_segments.gr, MMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, MMR_segments.gr)))]), setdiff(SMR_segments.gr, SMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, SMR_segments.gr)))]))
cat(paste0("set_aside_no_CHG.gr contains ",length(set_aside_no_CHG.gr)," segments which contain no CHG sites.\n"))
# 1099
MG_segments.gr = MG_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, MG_segments.gr)))]
cat(paste0("MG_segments.gr contains ",length(MG_segments.gr)," segments after trimming segments with no CHG sites.\n"))
# 1344, 1047, 1047, 1012, 5
LMR_segments.gr = LMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, LMR_segments.gr)))]
cat(paste0("LMR_segments.gr contains ",length(LMR_segments.gr)," segments after trimming segments with no CHG sites.\n"))
# 16483, 16892, 13621, 12304, 12329
MMR_segments.gr = MMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, MMR_segments.gr)))]
cat(paste0("MMR_segments.gr contains ",length(MMR_segments.gr)," segments after trimming segments with no CHG sites.\n"))
# 5205, 5221, 5750, 4873, 4893
SMR_segments.gr = SMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, SMR_segments.gr)))]
cat(paste0("SMR_segments.gr contains ",length(SMR_segments.gr)," segments after trimming segments with no CHG sites.\n"))
# 4677, 6672, 7964, 3797, 3731
UMR_segments.gr = UMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, UMR_segments.gr)))]
cat(paste0("UMR_segments.gr contains ",length(UMR_segments.gr)," segments after trimming segments with no CHG sites.\n"))
# 27185, 30374, 29000, 26268, 26087
#MR_segments.gr = MR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, MR_segments.gr)))]
#cat(paste0("MR_segments.gr contains ",length(MR_segments.gr)," segments after trimming segments with no CHG sites.\n"))
# 26365, 28785, 27335, 23169,
m_non_CG_segments.gr = m_non_CG_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHG.gr, m_non_CG_segments.gr)))]
cat(paste0("m_non_CG_segments.gr contains ",length(m_non_CG_segments.gr)," segments after trimming segments with no CHG sites.\n"))
# , , 4437, 8193, 8117
# Annotate segments with CHG methylation values
#values(MG_segments.gr) = cbind(values(MG_segments.gr),meth_by_segment(meth_parents_CHG.gr, segment_model=MG_segments.gr, meth_context="CHG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(LMR_segments.gr) = cbind(values(LMR_segments.gr),meth_by_segment(meth_parents_CHG.gr, segment_model=LMR_segments.gr, meth_context="CHG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(MMR_segments.gr) = cbind(values(MMR_segments.gr),meth_by_segment(meth_parents_CHG.gr, segment_model=MMR_segments.gr, meth_context="CHG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(SMR_segments.gr) = cbind(values(SMR_segments.gr),meth_by_segment(meth_parents_CHG.gr, segment_model=SMR_segments.gr, meth_context="CHG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(UMR_segments.gr) = cbind(values(UMR_segments.gr),meth_by_segment(meth_parents_CHG.gr, segment_model=UMR_segments.gr, meth_context="CHG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
#values(MR_segments.gr) = cbind(values(MR_segments.gr),meth_by_segment(meth_parents_CHG.gr, segment_model=MR_segments.gr, meth_context="CHG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(m_non_CG_segments.gr) = cbind(values(m_non_CG_segments.gr),meth_by_segment(meth_parents_CHG.gr, segment_model=m_non_CG_segments.gr, meth_context="CHG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
# Adjust segments to remove any segments which don't overlap with any CHH sites, after setting them aside
set_aside_no_CHH.gr = c(setdiff(LMR_segments.gr, LMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, LMR_segments.gr)))]), setdiff(MMR_segments.gr, MMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, MMR_segments.gr)))]), setdiff(SMR_segments.gr, SMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, SMR_segments.gr)))]))
cat(paste0("set_aside_no_CHG.gr contains ",length(set_aside_no_CHG.gr)," segments which contain no CHG sites.\n"))
# 1099
MG_segments.gr = MG_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, MG_segments.gr)))]
cat(paste0("MG_segments.gr contains ",length(MG_segments.gr)," segments after trimming segments with no CHH sites.\n"))
# 1012
LMR_segments.gr = LMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, LMR_segments.gr)))]
cat(paste0("LMR_segments.gr contains ",length(LMR_segments.gr)," segments after trimming segments with no CHH sites.\n"))
# 12328
MMR_segments.gr = MMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, MMR_segments.gr)))]
cat(paste0("MMR_segments.gr contains ",length(MMR_segments.gr)," segments after trimming segments with no CHH sites.\n"))
# 4890
SMR_segments.gr = SMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, SMR_segments.gr)))]
cat(paste0("SMR_segments.gr contains ",length(SMR_segments.gr)," segments after trimming segments with no CHH sites.\n"))
# 3720
UMR_segments.gr = UMR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, UMR_segments.gr)))]
cat(paste0("UMR_segments.gr contains ",length(UMR_segments.gr)," segments after trimming segments with no CHH sites.\n"))
# 26087
#MR_segments.gr = MR_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, MR_segments.gr)))]
#cat(paste0("MR_segments.gr contains ",length(MR_segments.gr)," segments after trimming segments with no CHH sites.\n"))
# 23116
m_non_CG_segments.gr = m_non_CG_segments.gr[unique(subjectHits(findOverlaps(meth_parents_CHH.gr, m_non_CG_segments.gr)))]
cat(paste0("m_non_CG_segments.gr contains ",length(m_non_CG_segments.gr)," segments after trimming segments with no CHH sites.\n"))
# 8117
# Annotate segments with CHH methylation values
#values(MG_segments.gr) = cbind(values(MG_segments.gr),meth_by_segment(meth_parents_CHH.gr, segment_model=MG_segments.gr, meth_context="CHH", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(LMR_segments.gr) = cbind(values(LMR_segments.gr),meth_by_segment(meth_parents_CHH.gr, segment_model=LMR_segments.gr, meth_context="CHH", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(MMR_segments.gr) = cbind(values(MMR_segments.gr),meth_by_segment(meth_parents_CHH.gr, segment_model=MMR_segments.gr, meth_context="CHH", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(SMR_segments.gr) = cbind(values(SMR_segments.gr),meth_by_segment(meth_parents_CHH.gr, segment_model=SMR_segments.gr, meth_context="CHH", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(UMR_segments.gr) = cbind(values(UMR_segments.gr),meth_by_segment(meth_parents_CHH.gr, segment_model=UMR_segments.gr, meth_context="CHH", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
#values(MR_segments.gr) = cbind(values(MR_segments.gr),meth_by_segment(meth_parents_CHH.gr, segment_model=MR_segments.gr, meth_context="CHH", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
values(m_non_CG_segments.gr) = cbind(values(m_non_CG_segments.gr),meth_by_segment(meth_parents_CHH.gr, segment_model=m_non_CG_segments.gr, meth_context="CHH", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR")))
# Merge the various distinct classes of segment
segmentation_model.gr = c(LMR_segments.gr, MMR_segments.gr, SMR_segments.gr, UMR_segments.gr, m_non_CG_segments.gr)
length(segmentation_model.gr)
# 55873, 55142
# Define a convenience function to save a segmentation model. This is adapted from MethylSeekR package
saveSegmentationModel = function (segs, GRangesFilename = NULL, TableFilename = NULL)
{
if (!is.null(GRangesFilename))
saveRDS(segs, GRangesFilename)
if (!is.null(TableFilename)) {
D = data.frame(chr = as.character(seqnames(segs)), start = start(segs),
end = end(segs), type = values(segs)$type)
write.table(D, file = TableFilename, quote = FALSE, sep = "\t",
row.names = FALSE)
}
}
saveSegmentationModel(segs=LMR_segments.gr, GRangesFilename=paste0("LMRs_parents_CG_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_LMR_segments_parents_CG_",m.sel,"_",n.sel,".tsv"))
saveSegmentationModel(segs=MMR_segments.gr, GRangesFilename=paste0("MMRs_parents_CG_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_MMR_segments_parents_CG_",m.sel,"_",n.sel,".tsv"))
saveSegmentationModel(segs=SMR_segments.gr, GRangesFilename=paste0("SMRs_parents_CG_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_SMR_segments_parents_CG_",m.sel,"_",n.sel,".tsv"))
saveSegmentationModel(segs=UMR_segments.gr, GRangesFilename=paste0("UMRs_parents_CG_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_UMR_segments_parents_CG_",m.sel,"_",n.sel,".tsv"))
saveSegmentationModel(segs=MR_segments.gr, GRangesFilename=paste0("MRs_parents_CG_",m.sel,"_",n.sel,".gr.rds"), TableFilename=paste0(project_id,"_",meth_context,"_MethylSeekR_MR_segments_parents_CG_",m.sel,"_",n.sel,".tsv"))
# Plot mCHG vs mCG for each mCG class of segment
pdf(paste0(project_id,"_",meth_context,"_genome_segment_mCHG_vs_mCG_median_by_mCG_class.pdf"))
print(ggplot(data.frame(values(segmentation_model.gr)@listData), aes(x=median.meth, y=median.meth.1)) + geom_point(aes(colour=type)) + theme_minimal())
dev.off()
pdf(paste0(project_id,"_",meth_context,"_genome_segment_mCHG_vs_mCG_mean_by_mCG_class.pdf"))
print(ggplot(data.frame(values(segmentation_model.gr)@listData), aes(x=pmeth, y=pmeth.1)) + geom_point(aes(colour=type)) + theme_minimal())
dev.off()
# Plot density of segment mCHG
pdf(paste0(project_id,"_",meth_context,"_genome_segment_median_mCHG_density.pdf"))
print(ggplot(data.frame(values(segmentation_model.gr)@listData), aes(x=median.meth.1)) + geom_density(aes(colour=type)) + theme_minimal())
dev.off()
pdf(paste0(project_id,"_",meth_context,"_genome_segment_mean_mCHG_density.pdf"))
print(ggplot(data.frame(values(segmentation_model.gr)@listData), aes(x=pmeth.1)) + geom_density(aes(colour=type)) + theme_minimal())
ggplot(data.frame(values(segmentation_model.gr)@listData)[data.frame(values(segmentation_model.gr)@listData)$type=="LMR",], aes(x=pmeth.1)) + geom_density(aes(colour=type)) + theme_minimal()
dev.off()
# Show a table of counts of segments by type
#table(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$median.meth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth)),]$type)
table(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$pmeth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth)),]$type)
# Schmitz data:
#LMR MMR SMR UMR
#16546 5201 6769 30811
#16508 5205 4 6668 30643
# Using TEM segments separately derived from segmenting mCHG:
#LMR MMR MR SMR TEM UMR
#13146 5726 1 2940 4343 25645
# Using TEM segments separately derived from CHG/CHH combined:
#11813 4850 9 3724 7823 27519
# Not masking 'Unknown' genes
#12081 4843 28 3719 7565 26900
# 4,1 segments ended up with type="MR" (short segments with relatively high levels of methylation) so we set these to be "SMR":
segmentation_model.gr@elementMetadata@listData$type[(!is.na(segmentation_model.gr@elementMetadata@listData$type)) & (segmentation_model.gr@elementMetadata@listData$type=="MR")]="SMR"
# Show a table of counts of segments by type
table(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$pmeth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth)),]$type)
#Schmitz data:
#LMR MMR SMR TEM UMR
#11813 4850 3733 7823 27519
#12081 4843 3747 7565 26900
# We set mCG_cutoff_mean here to the value previously found, and reiterate mCHG cutoffs to be used later for classifying finished segments
mCG_cutoff_mean = 0.09309094 # derived by fitting mixture of Gaussians to density distribution of segment mCG of whole parental methylome segmented in CG context
m.sel = 0.09309094
segment_mCHG_cutoff = 0.1262584 # derived by fitting mixture of Gaussians to density distribution of segment mCHG of whole parental methylome segmented in CHG context
mCHG_cutoff_mean = 0.1262584
mCHG_cutoff_median = 0.1262584
segment_mCHH_cutoff = 0.1 # this is somewhat arbitrary, and is only used to classify the segments as UMR/MR in CHH. May come back to this, and give it a better cutoff later, if this is thought to be useful
mCHH_cutoff_mean = 0.1
# Plot mCG vs. segment size, with segments coloured by type
pdf(paste0(project_id,"_",meth_context,"_genome_segment_mCG_vs_length_by_type_revised.pdf"))
# Plot distribution of segment mCG and mCHG by type
#print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$median.meth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth)),], aes(x=median.meth)) + geom_density(aes(colour=type)) + theme_minimal() + facet_wrap(~ type, ncol=1, scales="free_y") + xlab("Segment Median mCG"))
print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$pmeth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth)),], aes(x=pmeth)) + geom_density(aes(colour=type)) + theme_minimal() + facet_wrap(~ type, ncol=1, scales="free_y") + xlab("Segment Mean mCG"))
#print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=ifelse(values(segmentation_model.gr)@listData$median.meth.1>mCHG_cutoff_median,"High","Low"), seg_width=segmentation_model.gr@ranges@width), aes(x=seg_width, y=median.meth)) + geom_point(aes(colour=type), size=0.1, alpha=1) + coord_trans(x = "log10") + theme_minimal() +xlab("Segment length (nt)") +ylab("Segment Median mCG") + scale_x_continuous(breaks = seq(0, 250000, by = 20000)))
print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=ifelse(values(segmentation_model.gr)@listData$pmeth.1>mCHG_cutoff_mean,"High","Low"), seg_width=segmentation_model.gr@ranges@width), aes(x=seg_width, y=pmeth)) + geom_point(aes(colour=type), size=0.1, alpha=1) + coord_trans(x = "log10") + theme_minimal() +xlab("Segment length (nt)") +ylab("Segment Mean mCG") + scale_x_continuous(breaks = seq(0, 250000, by = 20000)))
#print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$median.meth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth)),], aes(x=median.meth.1)) + geom_density(aes(colour=type)) + theme_minimal() + facet_wrap(~ type, ncol=1, scales="free_y") + xlab("Segment Median mCHG"))
print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$pmeth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth)),], aes(x=pmeth.1)) + geom_density(aes(colour=type)) + theme_minimal() + facet_wrap(~ type, ncol=1, scales="free_y") + xlab("Segment Mean mCHG"))
#print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=ifelse(values(segmentation_model.gr)@listData$median.meth.1>mCHG_cutoff_median,"High","Low"), seg_width=segmentation_model.gr@ranges@width), aes(x=median.meth, y=median.meth.1)) + geom_point(aes(colour=type), size=0.1, alpha=0.5) + theme_minimal() + xlab("Segment Median mCG") + ylab("Segment Median mCHG"))
print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=ifelse(values(segmentation_model.gr)@listData$pmeth.1>mCHG_cutoff_mean,"High","Low"), seg_width=segmentation_model.gr@ranges@width), aes(x=pmeth, y=pmeth.1)) + geom_point(aes(colour=type), size=0.1, alpha=0.5) + theme_minimal() + xlab("Segment Mean mCG") + ylab("Segment Mean mCHG"))
#print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHH=values(segmentation_model.gr)@listData$median.meth.2, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth.2)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth)),], aes(x=median.meth.2)) + geom_density(aes(colour=type)) + theme_minimal() + facet_wrap(~ type, ncol=1, scales="free_y") + xlab("Segment Median mCHH"))
print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHH=values(segmentation_model.gr)@listData$pmeth.2, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth.2)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth)),], aes(x=pmeth.1)) + geom_density(aes(colour=type)) + theme_minimal() + facet_wrap(~ type, ncol=1, scales="free_y") + xlab("Segment Mean mCHH"))
#print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHH=ifelse(values(segmentation_model.gr)@listData$median.meth.1>mCHH_cutoff_median,"High","Low"), seg_width=segmentation_model.gr@ranges@width), aes(x=median.meth, y=median.meth.2)) + geom_point(aes(colour=type), size=0.1, alpha=0.5) + theme_minimal() + xlab("Segment Median mCG") + ylab("Segment Median mCHH"))
print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHH=ifelse(values(segmentation_model.gr)@listData$pmeth.2>mCHH_cutoff_mean,"High","Low"), seg_width=segmentation_model.gr@ranges@width), aes(x=pmeth, y=pmeth.2)) + geom_point(aes(colour=type), size=0.1, alpha=0.5) + theme_minimal() + xlab("Segment Mean mCG") + ylab("Segment Mean mCHH"))
print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHH=ifelse(values(segmentation_model.gr)@listData$pmeth.2>mCHH_cutoff_mean,"High","Low"), seg_width=segmentation_model.gr@ranges@width), aes(x=pmeth.1, y=pmeth.2)) + geom_point(aes(colour=type), size=0.1, alpha=0.5) + theme_minimal() + xlab("Segment Mean mCHG") + ylab("Segment Mean mCHH"))
### Cut-offs to separate UMR, GBM, TEM:
# What threshold of mCG would capture 80% of TMR LMRs?
#mCHG_quantile = 0.2
#TMR_mCG_cutoff = quantile(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$median.meth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth)) & (values(segmentation_model.gr)@listData$median.meth.1 > mCHG_cutoff) & (values(segmentation_model.gr)@listData$type=="LMR"),]$median.meth,mCHG_quantile)
#cat(paste0(100*(1-mCHG_quantile),"% of 'transposon-like' Long Methylated Regions have mCG>",TMR_mCG_cutoff,".\n"))
# Schmitz: 80% of 'transposon-like' Long Methylated Regions have mCG>0.754069511898385.
# What threshold of mCG would capture 80% of GBM LMRs?
#mCG_quantile = 0.8
#GBM_mCG_cutoff = quantile(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$median.meth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth)) & (values(segmentation_model.gr)@listData$median.meth.1 <= mCHG_cutoff) & (values(segmentation_model.gr)@listData$median.meth > m.sel) & (values(segmentation_model.gr)@listData$type=="LMR"),]$median.meth,mCG_quantile)
#cat(paste0(100*mCG_quantile,"% of 'gene-body methylated-like' Long Methylated Regions have mCG<",GBM_mCG_cutoff,".\n"))
# Schmitz: 80% of 'gene-body methylated-like' Long Methylated Regions have mCG<0.673452805081518.
# Try to fit 3 Gaussians model to pick mCHG cutoff to separate TEMs from GBMs:
#plot(normalmixEM(data.frame(values(segmentation_model.gr)@listData)[(data.frame(values(segmentation_model.gr)@listData)$pmeth>m.sel) & (data.frame(values(segmentation_model.gr)@listData)$pmeth.1>0.01) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth.1)),]$pmeth.1,k=3), which=2, breaks=seq(0,1,0.01))
#m.sel=0.2
#print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$median.meth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth)),], aes(x=seg_width, y=median.meth)) + geom_point(aes(colour=mCHG), alpha=0.1) + coord_trans(x = "log10") + theme_minimal() + facet_wrap(~ ifelse(mCHG>mCHG_cutoff*2,1,ifelse(median.meth>m.sel,2,3)), ncol=1) +geom_density2d() +scale_colour_gradient(low="green", high="red"))
#print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$median.meth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$median.meth)),], aes(x=seg_width, y=median.meth)) + geom_point(aes(colour=type), size=0.1, alpha=0.25) + coord_trans(x = "log10") + theme_minimal() + facet_wrap(~ ifelse(mCHG>mCHG_cutoff_median,paste0("1. 'Transposon-like' segments:\n median mCHG > ",round(mCHG_cutoff_median, digits=4)),ifelse(median.meth>m.sel,paste0("2. 'Gene-body-like' segments:\n median mCG > ",round(m.sel, digits=4),"\n median mCHG <= ",round(mCHG_cutoff_median, digits=4)),paste0("3. Unmethylated segments:\n median mCG <= ",round(m.sel, digits=4),"\n median mCHG <= ",round(mCHG_cutoff_median, digits=4)))), ncol=3) + xlab("Segment length (nt)") +ylab("Segment Median mCG") + scale_x_continuous(breaks = seq(0, 250000, by = 10000)))
print(ggplot(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$pmeth.1,mCHH=values(segmentation_model.gr)@listData$pmeth.2, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth)),], aes(x=seg_width, y=pmeth)) + geom_point(aes(colour=type), size=0.1, alpha=0.25) + coord_trans(x = "log10") + theme_minimal() + facet_wrap(~ ifelse((mCHG>segment_mCHG_cutoff) | (mCHH>segment_mCHH_cutoff),paste0("1. 'Transposon-like' segments:\n mean mCHG > ",round(segment_mCHG_cutoff, digits=4),"\n or mean mCHH > ",round(segment_mCHH_cutoff, digits=4)),ifelse(pmeth>m.sel,paste0("2. 'Gene-body-like' segments:\n mean mCG > ",round(m.sel, digits=4),"\n mean mCHG <= ",round(segment_mCHG_cutoff, digits=4),"\n and mean mCHH <= ",round(segment_mCHH_cutoff, digits=4)),paste0("3. Unmethylated segments:\n mean mCG <= ",round(m.sel, digits=4),"\n mean mCHG <= ",round(segment_mCHG_cutoff, digits=4),"\n and mean mCHH <= ",round(segment_mCHH_cutoff, digits=4)))), ncol=3) + xlab("Segment length (nt)") +ylab("Segment Mean mCG") + scale_x_continuous(breaks = seq(0, 250000, by = 10000)))
dev.off()
# Crystalise segment methylation status as a column in the segmentation object
# mCHG cutoff is segment_mCHG_cutoff
# mCHH cutoff is segment_mCHH_cutoff
# mCG cutoff is m.sel
segmentation_model.gr@elementMetadata@listData$segment.mStatus = ifelse((cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$pmeth.1, seg_width=segmentation_model.gr@ranges@width)[,]$mCHG>segment_mCHG_cutoff) | (cbind(data.frame(values(segmentation_model.gr)@listData),mCHH=values(segmentation_model.gr)@listData$pmeth.2, seg_width=segmentation_model.gr@ranges@width)[,]$mCHH>segment_mCHH_cutoff),"TEM",ifelse(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$pmeth.1, seg_width=segmentation_model.gr@ranges@width)[,]$pmeth>m.sel,"GBM","UMR"))
# For Schmitz data, two of these segments have NA pmeth values
#segmentation_model.gr@elementMetadata@listData$segment.mStatus = ifelse(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$pmeth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth)),]$mCHG>mCHG_cutoff_mean,"TEM",ifelse(cbind(data.frame(values(segmentation_model.gr)@listData),mCHG=values(segmentation_model.gr)@listData$pmeth.1, seg_width=segmentation_model.gr@ranges@width)[(!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth.1)) & (!is.na(data.frame(values(segmentation_model.gr)@listData)$pmeth)),]$pmeth>m.sel,"GBM","UMR"))
# Set aside any unclassified segments
length(segmentation_model.gr)
# 55873, 55142
set_aside_no_class.gr = segmentation_model.gr[is.na(as.data.frame(segmentation_model.gr)$segment.mStatus)]
segmentation_model.gr = segmentation_model.gr[!is.na(as.data.frame(segmentation_model.gr)$segment.mStatus)]
length(segmentation_model.gr)
# 55867, 55136
cat(paste0("There are ",length(set_aside_no_class.gr), " segments with no methylation status classification.\n"))
# Schmitz data: 6
# It's not exactly clear why this is not zero - if these segments don't overlap CHG or CHH sites they should have been already set aside.
# On inspection, these segments all have mCG data (in Schmitz data set)