-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-consolidate_methylation_calls.R
1187 lines (930 loc) · 71.6 KB
/
10-consolidate_methylation_calls.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
#!/usr/bin/env Rscript
# 10-consolidate_methylation_calls.R
#
# Jay Moore, John Innes Centre, Norwich
#
# 28/10/2019
#
# Version 1 - 1001 methylomes data analysis, adapted from Schmitz/Becker project - methylation has previously been called for all samples. Integrate them into a single matrix
#
# Change log
#
# Data source (project_id)
# .methratio2 files from bsmap
# Summary of functions
#
# Done:
#
# Underway:
#
# To do:
#
#
#
# Results:
#
#
if(!require(optparse)){
install.packages("optparse")
library(optparse)
}
library("optparse")
option_list = list(
make_option(c("-p", "--project"), action="store", default="m1001", type='character',
help="project code for location of mapping output"),
make_option(c("-s", "--sample"), action="store", default=NA, type='character',
help="project code for location of mapping 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("Sample: ")
cat(opt$sample)
cat("\n")
cat("Context: ")
cat(opt$context)
cat("\n")
cat("Action: ")
cat(opt$action)
cat("\n")
}
# project_id, sample_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="Mp1"
#project_id="memory_line_2"
} else {
project_id=opt$project
}
sample_id=NULL
if (is.na(opt$sample)) {
# No sample defined - quit
stop("No sample specified")
#project_id="memory_line_2"
} else {
sample_id=opt$sample
}
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
# Sometimes rlang is needed for bioconductor to instal. This problem seems to have gone away now...
#if(!require(rlang)){
# install.packages("rlang")
# library(rlang)
#}
#### This part sets up libraries and contextual information and metadata about the project
# Platform-specific stuff:
# Base of path to project
# Where to find bioconductor (packages need to be pre-installed in R using singularity if running on the cluster)
on_cluster = FALSE
pathroot = ""
if (.Platform$OS.type=="windows") {
pathroot="X:/Daniel-Zilberman"
source("http://bioconductor.org/biocLite.R")
} else if (.Platform$OS.type=="unix") {
if(substr(getwd(),1,20)=="/jic/scratch/groups/") {
# assume we are running on cluster
pathroot="/jic/scratch/groups/Daniel-Zilberman"
on_cluster = TRUE
} 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")
}
}
if (!on_cluster) {
# Assume that if we are on the cluster, then we are running in a VM with all relevant packages pre-installed
#biocLite(c("GenomicRanges", "BSgenome", "BSgenome.Athaliana.TAIR.TAIR9", "MethylSeekR", "karyoploteR"))
BiocManager::install(c("GenomicRanges", "BSgenome", "BSgenome.Athaliana.TAIR.TAIR9", "MethylSeekR", "karyoploteR", "ggtree"))
}
#### 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)
}
if(!require(mixtools)){
install.packages("mixtools")
library(mixtools)
}
if(!require(rootSolve)){
install.packages("rootSolve")
library(rootSolve)
}
if(!require(ggpubr)){
install.packages("ggpubr")
library(ggpubr)
}
if(!require(sqldf)){
install.packages("sqldf")
library(sqldf)
}
library(data.table)
library(stringr)
library(ggplot2)
library(plyr)
#library(ggpubr)
library(sqldf)
# Source directory containing alignments from bs_sequel
source_dir = paste0(pathroot,"/Projects/Jay-1001_methylomes/3-alignments/")
# Working directory where outputs will be directed to
setwd(dir = paste0(pathroot,"/Projects/Jay-1001_methylomes/5-analysis/"))
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)
# Nucleosomes position data from http://plantdhs.org/Download (Jiming Jiang lab) Originally described in Wu Y.F. Zhang W.L. Jiang J.M. Genome-wide nucleosome positioning is orchestrated by genomic regions associated with DNase I hypersensitivity in rice PLoS Genet. 2014 10 e1004378
# 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 https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSM2807196
# Lyons DB, Zilberman D. DDM1 and Lsh remodelers allow methylation of DNA wrapped in nucleosomes. Elife 2017 Nov 15;6. PMID: 29140247
# GSE96994 wt.rep1.mnase_seq results
# GSM2807196_wt.rep1.nucleosomes.bed is all nucleosome calls
# GSM2807196_wt.group_1.bed is well-positioned nucleosome calls
#reference_nucleosomes = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Nucleosomes/GSM2807196_wt.rep1.nucleosomes.bed")
reference_nucleosomes = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Nucleosomes/GSM2807196_wt.group_1.bed")
# H3K9me2 normalised WT data from Dave Lyons
# For H3K9me2 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/Histone_modifications/dk9.jacobsen.normalized.bw", format="BigWig"), "/media/sf_D_DRIVE/Reference_data/Genomes/Arabidopsis/Histone_modifications/dk9.jacobsen.normalized.RDS")
reference_H3K9me2 = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Histone_modifications/dk9.jacobsen.normalized.RDS")
# H3K4me1 Plant DNase I hypersensitivity data. Downloaded from http://plantdhs.org/Download (Jiming Jiang lab) on 05/12/2017
# For H3K4me1 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/Histone_modifications/Ath_buds_H3K4me1.bw", format="BigWig"), "/media/sf_D_DRIVE/Reference_data/Genomes/Arabidopsis/Histone_modifications/Ath_buds_H3K4me1.RDS")
reference_H3K4me1 = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Histone_modifications/Ath_buds_H3K4me1.RDS")
# H3K27me3 Plant DNase I hypersensitivity data. Downloaded from http://plantdhs.org/Download (Jiming Jiang lab) on 05/12/2017
# For H3K27me3 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/Histone_modifications/Ath_leaf_H3K27me3.bw", format="BigWig"), "/media/sf_D_DRIVE/Reference_data/Genomes/Arabidopsis/Histone_modifications/Ath_leaf_H3K27me3.RDS")
reference_H3K27me3 = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Histone_modifications/Ath_leaf_H3K27me3.RDS")
# H2AW data from Jaemyung Choi
reference_H2AW = paste0(pathroot,"/Reference_data/Genomes/Arabidopsis/Histone_modifications/h2aw.w50.gff")
# Sample info from Kawakatsu et al, 2016 Table S2
# http://neomorph.salk.edu/publications/Kawakatsu_Cell.pdf
# This has useful metadata about the samples as published in the paper
sample_info = read.table(file="../0-reference/1-s2.0-S0092867416308522-mmc3.txt", sep="\t", header=TRUE)
# Sample list is a list of the samples we have sequence data for
# fastq list is the cor4respondence between fastq files we have aligned, and samples
# Global Arabidopsis accessions from Ecker study (927 of)
sample_list1 = read.table(paste0("../0-raw_data/","GSE43857.txt"), sep="\t", header=TRUE) # from https://www.ncbi.nlm.nih.gov/geo/browse/?view=samples&series=43857
fastq_list1 = read.table(paste0("../0-raw_data/","ENA_SRA065807.txt"), sep="\t", header=TRUE) # from https://www.ebi.ac.uk/ena/data/view/SRA065807
# sample_list1$Title is a string containing 3 separate data items:
# sample_info$Name (sample_info$Ecotype_id)<-Replicate_id>
# Let's split them up
sample_list1$Name = str_split_fixed(sample_list1$Title, "\\(",2)[,1]
sample_list1$Ecotype_id = as.numeric(str_split_fixed(str_split_fixed(sample_list1$Title, "\\(",2)[,2], "\\)", 2)[,1])
sample_list1$Replicate_id = str_split_fixed(str_split_fixed(sample_list1$Title, "\\(",2)[,2], "-", 2)[,2]
# sample_list1$Name is sample_info$Name
# Merge sample_list1 with sample_info by Name
# Swedish Arabidopsis accessions from separate study (284 of)
sample_list2 = read.table(paste0("../0-raw_data/","GSE54292.txt"), sep="\t", header=TRUE) # from https://www.ncbi.nlm.nih.gov/geo/browse/?view=samples&series=54292
fastq_list2 = read.table(paste0("../0-raw_data/","ENA_SRP035593.txt"), sep="\t", header=TRUE) # from https://www.ebi.ac.uk/ena/data/view/PRJNA236110
# sample_list2$Title is sample_info$Sample, and Name is not present
#sample_list2$Sample = sample_list$Title
# Merge sample_list2 with sample_info by Sample
# Put the two sets of accessions together
# This step never quite worked, due to the differences in annotation between the different labs, so did the merge manually in Excel and import this in the block of code below
#sample_list = rbind.data.frame(sample_list1, sample_list2)
fastq_list = rbind.data.frame(fastq_list1, fastq_list2)
rm(sample_list1, fastq_list1, sample_list2, fastq_list2)
# The above sample_list routine was somewhat unsatisfactory. Instead, we will read in the synthesis created manually in Excel, using the above resources as a starting point:
sample_list = read.table(paste0("../0-raw_data/","sample_info_synthesis.txt"), sep="\t", header=TRUE)
sample_list$Name = str_split_fixed(sample_list$Title, "\\(",2)[,1]
#sample_list1$Ecotype_id = as.numeric(str_split_fixed(str_split_fixed(sample_list1$Title, "\\(",2)[,2], "\\)", 2)[,1])
sample_list$Ecotype_id = as.numeric(str_split_fixed(sample_list$Sample, "_", 2)[,1])
### this merge fails - sample_info has no Title
sample_info_merged = merge(sample_info, sample_list, by=c("Sample"), all=TRUE)
### Maybe it should merge on c(Name, Ecotype_id), but not certain whether this is unique - probably not
sample_files = data.frame()
for (this_accession in sample_list$SRA.Accession) {
these_files = fastq_list[fastq_list$experiment_accession == this_accession,"fastq_ftp"]
for (this_file in these_files) {
some_files=strsplit(this_file,";")
sample_files = rbind.data.frame(sample_files, c("Accession" = as.character(sample_list[sample_list$SRA.Accession==this_accession,"Accession"]), "Title" = as.character(sample_list[sample_list$SRA.Accession==this_accession,"Title"]), "SRA_Accession" = this_accession, "FASTQ_file" = some_files[[1]][[1]]), stringsAsFactors=FALSE)
if (length(some_files[[1]]) > 1) {
sample_files = rbind.data.frame(sample_files, c("Accession" = as.character(sample_list[sample_list$SRA.Accession==this_accession,"Accession"]), "Title" = as.character(sample_list[sample_list$SRA.Accession==this_accession,"Title"]), "SRA_Accession" = this_accession, "FASTQ_file" = some_files[[1]][[2]]), stringsAsFactors=FALSE)
# it looks like a lot of the paired-end files are represented as 'SINGLE' but should be 'PAIRED' - replace where necessary
if (fastq_list[fastq_list$fastq_ftp == this_file,"library_layout"] == "SINGLE") {
fastq_list[fastq_list$fastq_ftp == this_file,"library_layout"] = "PAIRED"
}
} else {
#cat(fastq_list[fastq_list$experiment_accession == this_accession,"library_layout"])
}
}
}
colnames(sample_files) = c("Accession", "Title", "SRA_Accession", "FASTQ_file")
not_processed_samples = c()
processed_samples = c()
all_samples_meth_status = NA
no_samples = 0
for (this_sample in sample_list$SRA.Accession) {
this_file = paste0(project_id,"_",meth_context,"_",this_sample,"_all_samples_meth_status_0.005_0.05_10_25.rds")
if (file.exists(this_file)) {
if(opt$verbose) {cat(paste0("Loading ",this_sample,"\n"))}
no_samples = no_samples + 1
processed_samples=c(processed_samples, this_sample)
this_sample_meth_status = data.frame(readRDS(this_file))
if (no_samples == 1) {
all_samples_meth_status = this_sample_meth_status
} else {
all_samples_meth_status = merge(all_samples_meth_status, this_sample_meth_status, by=c("Chromosome","Locus","Strand"), all=TRUE)
}
} else {
not_processed_samples=c(not_processed_samples, this_sample)
}
}
# Dump the data table to file for a convenience cache
saveRDS(all_samples_meth_status, file=paste0(project_id,"_",meth_context,"_all_samples_meth_status_0.005_0.05_10_25.rds"))
# saved for batches 1-29 so far
#all_samples_meth_status = readRDS(file=paste0(project_id,"_",meth_context,"_all_samples_meth_status_0.005_0.05_10_25.rds"))
# Read the SNP array originating from 1001 genomes website https://1001genomes.org/data/GMI-MPI/releases/v3.1/1001genomes_snp-short-indel_only_ACGTN.vcf
# parse_1001genome_VCF.pl/.sh together should have generated a separate file of mutation sites in C and CG contexts for each sample ID, so read them in one sample at a time.
# 1001 genomes SNP files are named for the accession No. in 1001 genome project. We are assuming they correspond directly with the Ecotype_ID we pull from the substring of the sample Title string
samples_without_genomes = c()
samples_with_genomes = c()
no_ecotypes = 0
for (this_sample in processed_samples) {
# Find the ecotype corresponding to this sample
this_ecotype = sample_list[sample_list$SRA.Accession==this_sample, "Ecotype_id"]
this_file = paste0("../0-reference/",this_ecotype,"_CG_vars.txt")
if (file.exists(this_file)) {
if(opt$verbose) {cat(paste0("Loading ",this_ecotype," SNPs\n"))}
no_ecotypes = no_ecotypes + 1
samples_with_genomes=c(samples_with_genomes, this_sample)
# read in the known CG sites with SNPs for this sample. Use unique to deduplicate cases where the same site is listed more than once
this_sample_snps = unique(read.table(this_file, header=FALSE, sep="\t"))
colnames(this_sample_snps) = c("Chromosome", "Locus")
#merge snps and sample. merge the merged set with sample to get rid of any rows exclusively from
mutant_sites = merge(all_samples_meth_status[,c("Chromosome", "Locus", this_sample)], cbind(this_sample_snps, "mutation"=rep(1,nrow(this_sample_snps))), by = c("Chromosome", "Locus"), all.x=TRUE, all.y=FALSE)
all_samples_meth_status[, this_sample] = ifelse(is.na(mutant_sites$mutation), as.character(all_samples_meth_status[, this_sample]), "X")
} else {
samples_without_genomes=c(samples_without_genomes, this_sample)
}
}
# Dump the data table to file for a convenience cache
saveRDS(all_samples_meth_status, file=paste0(project_id,"_",meth_context,"_all_samples_meth_status_0.005_0.05_10_25_SNPs.rds"))
# saved for batches 1-29 so far
#all_samples_meth_status = readRDS(file=paste0(project_id,"_",meth_context,"_all_samples_meth_status_0.005_0.05_10_25_SNPs.rds"))
# alternative approach to SNPs using hdf5 file
#BiocManager::install("rhdf5")
# No need for this, it's probably already installed by another package
#library(rhdf5)
#SNP_file = "../0-reference/1001_SNP_MATRIX/imputed_snps_binary.hdf5"
#h5ls(SNP_file)
#SNP_accessions = h5read(SNP_file, name="accessions")
#SNP_positions = h5read(SNP_file, name="positions")
#SNP_snps = rhdf5::h5read(SNP_file, name="snps")
# alternative approach uses VCF file of SNP calls
#install.packages("vcfR")
#library(vcfR)
# Some samples may need to be excluded from this analysis
excluded_samples = c()
valid_samples=c()
if (project_id=="SRA035939") {
excluded_samples = c("SRR342380","SRR342390")
} else if (project_id=="PRJEB2678") {
excluded_samples = c("ERR046563","ERR046564")
}
#valid_samples=rownames(sample_info)[!(rownames(sample_info) %in% excluded_samples)]
# Start from samples_with_genomes - processed_samples for which we have SNP/indel information
valid_samples=samples_with_genomes[!(samples_with_genomes %in% excluded_samples)]
### Need to check that Col-0 is in valid list even though it has 'no genome' as no need to apply SNPs and indels
# This never gets used again so commented out:
#valid_samples_meth_status=all_samples_meth_status[,!names(all_samples_meth_status) %in% excluded_samples]
# Find out which sites are generally always methylated, and which unmethylated, across the genome
# This part sums up methylation calls across lines to give an 'average' methylation level per site irrespective of line
# Partial calls are assigned an arbitrary methylation level of 0.25
average_methylation = matrix(0, nrow=nrow(all_samples_meth_status), ncol=2)
for(sample_name in valid_samples) {
if(opt$verbose) {cat(paste0("Summing methylation levels in sample ",sample_name,"\n"))}
average_methylation[,1]=average_methylation[,1]+ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse(all_samples_meth_status[,sample_name]=="U",0,ifelse(all_samples_meth_status[,sample_name]=="M",1,ifelse(all_samples_meth_status[,sample_name]=="P",0.25,0))))
average_methylation[,2]=average_methylation[,2]+ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse(all_samples_meth_status[,sample_name]=="U",1,ifelse(all_samples_meth_status[,sample_name]=="M",1,ifelse(all_samples_meth_status[,sample_name]=="P",1,0))))
}
# Plot distribution of average methylation level of cytosines across samples
hist(average_methylation[,1]/average_methylation[,2], breaks=100)
# Count X calls across all samples
no_mutations = matrix(0, nrow=length(valid_samples), ncol=2)
for(sample_no in 1:length(valid_samples)) {
sample_name = valid_samples[sample_no]
no_mutations[sample_no,1] = sample_name
no_mutations[sample_no,2] = sum(ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse(all_samples_meth_status[,sample_name]=="X",1,0)))
}
# This version looks for M/U calls across all samples, independent of rep structure
### This produces FALSE almost everywhere due to missing data etc
sample_no=0
first_sample = TRUE
for(sample_name in valid_samples) {
#sample_no = sample_no+1
if(opt$verbose) {cat(paste0("Checking methylation calls in all-samples per-site table: ",sample_name,"\n"))}
if (first_sample) {
#all_m_or_u_regardless=ifelse(((all_samples_meth_status[,sample_no+3]=="U") | (all_samples_meth_status[,sample_no+3]=="M")),TRUE,FALSE)
all_m=ifelse(is.na(all_samples_meth_status[,sample_name]),TRUE,ifelse(all_samples_meth_status[,sample_name]=="M",TRUE,FALSE))
all_u=ifelse(is.na(all_samples_meth_status[,sample_name]),TRUE,ifelse(all_samples_meth_status[,sample_name]=="U",TRUE,FALSE))
first_sample=FALSE
} else {
#all_m_or_u_regardless=all_m_or_u_regardless & ifelse(((all_samples_meth_status[,sample_no+3]=="U") | (all_samples_meth_status[,sample_no+3]=="M")),TRUE,FALSE)
all_m=all_m & ifelse(is.na(all_samples_meth_status[,sample_name]),TRUE,ifelse(all_samples_meth_status[,sample_name]=="M",TRUE,FALSE))
all_u=all_u & ifelse(is.na(all_samples_meth_status[,sample_name]),TRUE,ifelse(all_samples_meth_status[,sample_name]=="U",TRUE,FALSE))
}
}
# Identify parental consensus calls
parental_consensus = ifelse((average_methylation[,1]/average_methylation[,2])>0.5, "M", "U")
# Identify sites where at least one offspring line differs from parental consensus, and also, count how many offspring differ at the locus
line_gen_no=0
first_line_gen = TRUE
site_varies_from_parentals = NA
num_lines_varying_from_parentals = NA
num_lines_m_to_u_from_parentals = NA
num_lines_u_to_m_from_parentals = NA
num_lines_m_or_u_excl_parentals = NA
for(sample_name in valid_samples) {
#for(line_gen in gen3_30) {
# line_gen_no = line_gen_no + 1
if(opt$verbose) {cat(paste0("Checking variation in methylation calls from parental consensus: ",sample_name,"\n"))}
if (first_line_gen) {
#all_m_or_u_3=ifelse(((all_reps_meth_status[,line_gen]=="U") | (all_reps_meth_status[,line_gen]=="M")),TRUE,FALSE)
site_varies_from_parentals=ifelse((all_samples_meth_status[,sample_name] %in% c("M","U")) & (parental_consensus %in% c("M","U")) & (all_samples_meth_status[,sample_name]!=parental_consensus),TRUE,FALSE)
num_lines_varying_from_parentals=ifelse((all_samples_meth_status[,sample_name] %in% c("M","U")) & (parental_consensus %in% c("M","U")) & (all_samples_meth_status[,sample_name]!=parental_consensus),1,0)
num_lines_m_to_u_from_parentals=ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse((all_samples_meth_status[,sample_name] == "U") & (parental_consensus == "M"),1,0))
num_lines_u_to_m_from_parentals=ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse((all_samples_meth_status[,sample_name] == "M") & (parental_consensus == "U"),1,0))
num_lines_m_or_u_excl_parentals=ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse((all_samples_meth_status[,sample_name] == "M") | (all_samples_meth_status[,sample_name] == "U"),1,0))
first_line_gen=FALSE
} else {
#all_m_or_u_3=all_m_or_u_3 & ifelse(((all_reps_meth_status[,line_gen]=="U") | (all_reps_meth_status[,line_gen]=="M")),TRUE,FALSE)
site_varies_from_parentals=site_varies_from_parentals | ifelse((all_samples_meth_status[,sample_name] %in% c("M","U")) & (parental_consensus %in% c("M","U")) & (all_samples_meth_status[,sample_name]!=parental_consensus),TRUE,FALSE)
num_lines_varying_from_parentals=num_lines_varying_from_parentals + ifelse((all_samples_meth_status[,sample_name] %in% c("M","U")) & (parental_consensus %in% c("M","U")) & (all_samples_meth_status[,sample_name]!=parental_consensus),1,0)
num_lines_m_to_u_from_parentals=num_lines_m_to_u_from_parentals + ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse((all_samples_meth_status[,sample_name] == "U") & (parental_consensus == "M"),1,0))
num_lines_u_to_m_from_parentals=num_lines_u_to_m_from_parentals + ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse((all_samples_meth_status[,sample_name] == "M") & (parental_consensus == "U"),1,0))
num_lines_m_or_u_excl_parentals=num_lines_m_or_u_excl_parentals + ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse((all_samples_meth_status[,sample_name] == "M") | (all_samples_meth_status[,sample_name] == "U"),1,0))
#first_line_gen=FALSE
}
}
### This is mostly bobbins
if (opt$verbose) {
cat(paste("No of",meth_context,"loci:",nrow(all_samples_meth_status),"\n"))
#cat(paste("No of",meth_context,"loci with all calls M or U:",nrow(all_samples_meth_status[all_m_or_u==TRUE,]),"\n"))
cat(paste("No of",meth_context,"loci with all calls M:",nrow(all_samples_meth_status[all_m==TRUE,]),"\n"))
cat(paste("No of",meth_context,"loci with all calls U:",nrow(all_samples_meth_status[all_u==TRUE,]),"\n"))
#cat(paste("No of",meth_context,"loci with strict variation in M/U calls (all parentals agree, all lines have agreement between reps):",nrow(all_samples_meth_status[((all_m_or_u==TRUE) & (all_u==FALSE) & (all_m==FALSE)),]),"\n"))
#cat(paste("No of",meth_context,"loci with strict variation in M/U calls, M at generation 3:",nrow(all_samples_meth_status[((all_m_or_u==TRUE) & (all_u==FALSE) & (all_m==FALSE) & (all_m_3==TRUE)),]),"\n"))
#cat(paste("No of",meth_context,"loci with strict variation in M/U calls, U at generation 3:",nrow(all_samples_meth_status[((all_m_or_u==TRUE) & (all_u==FALSE) & (all_m==FALSE) & (all_u_3==TRUE)),]),"\n"))
#cat(paste("No of",meth_context,"loci with strict variation in M/U calls, M at generation 3 and 30:",nrow(all_reps_meth_status[((all_m_or_u==TRUE) & (all_u==FALSE) & (all_m==FALSE) & (all_m_3_30==TRUE)),]),"\n"))
#cat(paste("No of",meth_context,"loci with strict variation in M/U calls, U at generation 3 and 30:",nrow(all_reps_meth_status[((all_m_or_u==TRUE) & (all_u==FALSE) & (all_m==FALSE) & (all_u_3_30==TRUE)),]),"\n"))
cat(paste("No of",meth_context,"loci with any variation in M/U calls (at least 2 parentals agree, at least one progeny line disagrees and has agreement between reps):",nrow(all_samples_meth_status[site_varies_from_parentals,]),"\n"))
#cat(paste("No of",meth_context,"loci with any variation in M/U calls, M at generation 3:",nrow(all_samples_meth_status[site_varies_from_parentals & (no_m_3>=2),]),"\n"))
#cat(paste("No of",meth_context,"loci with any variation in M/U calls, U at generation 3:",nrow(all_samples_meth_status[site_varies_from_parentals & (no_u_3>=2),]),"\n"))
cat(paste("No of",meth_context,"loci with a clear call in parental consensus and all offspring:",nrow(cbind.data.frame(parental_consensus, site_varies_from_parentals, num_lines_m_to_u_from_parentals, num_lines_u_to_m_from_parentals, num_lines_m_or_u_excl_parentals)[(num_lines_m_or_u_excl_parentals==4) & (parental_consensus %in% c("M","U")),]),"\n"))
# Commented out reporting of generation 31 changes for now. We should redo this analysis in a more sensitive way - check each generation 31 line for changes compared to the corresponding generation 30 line, in concert with reps, but ignoring coverage and calls in generation 3 and in all other generation 30/31 lines.
#cat(paste("No of",meth_context,"loci with strict variation in M/U calls, M at generation 3 and 30:",nrow(all_reps_meth_status[((all_m_or_u==TRUE) & (all_u==FALSE) & (all_m==FALSE) & (all_m_3_30==TRUE)),]),"\n"))
#cat(paste("No of",meth_context,"loci with strict variation in M/U calls, U at generation 3 and 30:",nrow(all_reps_meth_status[((all_m_or_u==TRUE) & (all_u==FALSE) & (all_m==FALSE) & (all_u_3_30==TRUE)),]),"\n"))
}
# Generate tables of sites with various patterns of calls
across_samples_meth_status=cbind(all_samples_meth_status[,1:3],"average_methylation"=average_methylation[,1]/average_methylation[,2])[average_methylation[,2]>0,]
#variant_calls=all_samples_meth_status[((all_m_or_u==TRUE) & (all_u==FALSE) & (all_m==FALSE)),]
variant_calls=all_samples_meth_status[site_varies_from_parentals,]
# removed these lot for space reasons and because they never get used again:
#M_to_U_calls=all_samples_meth_status[site_varies_from_parentals & (num_lines_m_to_u_from_parentals>0),]
#U_to_M_calls=all_samples_meth_status[site_varies_from_parentals & (num_lines_u_to_m_from_parentals>0),]
#all_M_calls=all_samples_meth_status[all_m==TRUE,]
#all_U_calls=all_samples_meth_status[all_u==TRUE,]
#all_clean_calls=all_samples_meth_status[(num_lines_m_or_u_excl_parentals==1001) & (parental_consensus %in% c("M","U")),]
#M_parent_calls = all_samples_meth_status[parental_consensus=="M",]
#U_parent_calls = all_samples_meth_status[parental_consensus=="U",]
# Make loci 2nt long for CG sites
if (meth_context=="CG") {
across_samples_meth_status=within(across_samples_meth_status, {Locus2=Locus+1})
variant_calls=within(variant_calls, {Locus2=Locus+1})
#M_to_U_calls=within(M_to_U_calls, {Locus2=Locus+1})
#U_to_M_calls=within(U_to_M_calls, {Locus2=Locus+1})
#all_M_calls=within(all_M_calls, {Locus2=Locus+1})
#all_U_calls=within(all_U_calls, {Locus2=Locus+1})
#all_clean_calls=within(all_clean_calls, {Locus2=Locus+1})
#M_parent_calls=within(M_parent_calls, {Locus2=Locus+1})
#U_parent_calls=within(U_parent_calls, {Locus2=Locus+1})
} else {
across_samples_meth_status=within(across_samples_meth_status, {Locus2=Locus})
variant_calls=within(variant_calls, {Locus2=Locus})
#M_to_U_calls=within(M_to_U_calls, {Locus2=Locus})
#U_to_M_calls=within(M_to_U_calls, {Locus2=Locus})
#all_M_calls=within(all_M_calls, {Locus2=Locus})
#all_U_calls=within(all_U_calls, {Locus2=Locus})
#all_clean_calls=within(all_clean_calls, {Locus2=Locus})
#M_parent_calls=within(M_parent_calls, {Locus2=Locus})
#U_parent_calls=within(U_parent_calls, {Locus2=Locus})
}
# Get the genes annotation and start carving up the CG sites by gene
## Load the annotation
annot_gff = read.delim(reference_gff, header=F, comment.char="#")
gff.exons = read.delim(reference_exons, header=F, comment.char="#")
gff.introns = read.delim(reference_introns, header=F, comment.char="#")
gff.5UTR = read.delim(reference_5UTR, header=F, comment.char="#")
gff.3UTR = read.delim(reference_3UTR, header=F, comment.char="#")
# Grab the portion relating to genes
gff.genes = annot_gff[annot_gff[,3]=="gene",]
# Grab the portion relating to transposons
gff.transposons = annot_gff[annot_gff[,3]=="transposable_element",]
rm(annot_gff)
# Convert chromosome names to uppercase to match previous objects
### All previous objects are NOT in capitals
#gff.genes$V1=toupper(gff.genes$V1)
#gff.transposons$V1=toupper(gff.transposons$V1)
#gff.exons$V1=toupper(gff.exons$V1)
#gff.introns$V1=toupper(gff.introns$V1)
#gff.5UTR$V1=toupper(gff.5UTR$V1)
#gff.3UTR$V1=toupper(gff.3UTR$V1)
# The following line sometimes fails if stringi has not been installed correctly. Not sure why. Fix is to install.packages("stringi") and to allow RStudio to restart R.
# Grab gene IDs from descriptive field
gff.genes$gene_ID=str_split_fixed(str_split_fixed(gff.genes$V9, ';',3)[,1],"=",2)[,2]
gff.transposons$gene_ID=str_split_fixed(str_split_fixed(gff.transposons$V9, ';',3)[,1],"=",2)[,2]
gff.exons$gene_ID=str_split_fixed(str_split_fixed(gff.exons$V9, ';',3)[,1],"=",2)[,2]
gff.introns$gene_ID=str_split_fixed(str_split_fixed(gff.introns$V9, ';',3)[,1],"=",2)[,2]
gff.5UTR$gene_ID=str_split_fixed(str_split_fixed(gff.5UTR$V9, ';',3)[,1],"=",2)[,2]
gff.3UTR$gene_ID=str_split_fixed(str_split_fixed(gff.3UTR$V9, ';',3)[,1],"=",2)[,2]
#Gene names are tricky - need to find the element containing "symbol="
gff.genes=within(gff.genes, {gene_name=str_split_fixed(str_split_fixed(V9, ';',3)[,2],"=",2)[,2]})
gff.transposons=within(gff.transposons, {gene_name=str_split_fixed(str_split_fixed(V9, ';',3)[,3],"=",2)[,2]})
# Fix the gene names in exon, intron and UTRs
gff.exons$exon_ID = gff.exons$gene_ID
gff.exons$gene_ID = substr(gff.exons$gene_ID,1,9)
gff.introns$intron_ID = gff.introns$gene_ID
gff.introns$gene_ID = substr(gff.introns$gene_ID,1,9)
gff.5UTR$UTR_ID = gff.5UTR$gene_ID
gff.5UTR$gene_ID = substr(gff.5UTR$gene_ID,1,9)
gff.3UTR$UTR_ID = gff.3UTR$gene_ID
gff.3UTR$gene_ID = substr(gff.3UTR$gene_ID,1,9)
# Add the exon and intron numbers
gff.exons$exon_no = as.numeric(str_split_fixed(gff.exons$exon_ID, ':',3)[,3])
gff.introns$exon_no = as.numeric(str_split_fixed(gff.introns$intron_ID, ':',3)[,3])
# Find out how many exons each gene has
#install.packages("sqldf")
#library(sqldf)
gff.genes=merge(gff.genes, sqldf('SELECT genes.gene_id, MAX(exon_no) AS no_exons FROM [gff.exons] exons, [gff.genes] genes WHERE genes.gene_ID=exons.gene_ID GROUP BY genes.gene_id'), by="gene_ID", all=TRUE)
library(GenomicRanges)
# Make a GRanges for gene space
gene_ranges=makeGRangesFromDataFrame(df = gff.genes, start.field = "V4", end.field = "V5",seqnames.field = "V1")
# Make a GRanges for transposon space
transposon_ranges=makeGRangesFromDataFrame(df = gff.transposons, start.field = "V4", end.field = "V5",seqnames.field = "V1")
# Make a GRanges for exon space
exon_ranges=makeGRangesFromDataFrame(df = gff.exons, start.field = "V4", end.field = "V5",seqnames.field = "V1")
# Make a GRanges for intron space
intron_ranges=makeGRangesFromDataFrame(df = gff.introns, start.field = "V4", end.field = "V5",seqnames.field = "V1")
# Make a GRanges for 5' UTR space
UTR5_ranges=makeGRangesFromDataFrame(df = gff.5UTR, start.field = "V4", end.field = "V5",seqnames.field = "V1")
# Make a GRanges for 3' UTR space
UTR3_ranges=makeGRangesFromDataFrame(df = gff.3UTR, start.field = "V4", end.field = "V5",seqnames.field = "V1")
# Make a GRanges for all CG sites
CG_site_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status, start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
# Make a GRanges for all_M sites
all_M_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[all_m==TRUE,], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
# Make a GRanges for all_U sites
all_U_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[all_u==TRUE,], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
# Make a GRanges for variable sites
variant_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[site_varies_from_parentals,], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
# Make a GRanges for M->U sites
m_to_u_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[site_varies_from_parentals & (num_lines_m_to_u_from_parentals>0),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
# Make a GRanges for U->M sites
u_to_m_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[site_varies_from_parentals & (num_lines_u_to_m_from_parentals>0),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
# Make a GRanges for clean call sites
### Commented out as not relevant here
#clean_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[(num_lines_m_or_u_excl_parentals==1001) & (parental_consensus %in% c("M","U")),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
# Make 'clean' versions of the site ranges, where data is available for all lines
# No need to make for all_M and all_U as they were already 'clean'
#clean_all_M_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[(all_m==TRUE) & (num_lines_m_or_u_excl_parentals==4),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
#clean_all_U_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[(all_u==TRUE) & (num_lines_m_or_u_excl_parentals==4),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
#clean_variant_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[site_varies_from_parentals & (num_lines_m_or_u_excl_parentals==4) & (parental_consensus %in% c("M","U")),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
#clean_m_to_u_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[site_varies_from_parentals & (no_m_3>=2) & (num_lines_m_or_u_excl_parentals==4) & (parental_consensus == "M"),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
#clean_u_to_m_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[site_varies_from_parentals & (no_u_3>=2) & (num_lines_m_or_u_excl_parentals==4) & (parental_consensus == "U"),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
# Make single change clean versions of the variable site ranges - sites where data is available for all lines, and where a change occurs but only in one offspring line
#clean_single_variant_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[(num_lines_varying_from_parentals == 1) & (num_lines_m_or_u_excl_parentals==4) & (parental_consensus %in% c("M","U")),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
#clean_single_m_to_u_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[(num_lines_varying_from_parentals == 1) & (no_m_3>=2) & (num_lines_m_or_u_excl_parentals==4) & (parental_consensus == "M"),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
#clean_single_u_to_m_call_ranges =makeGRangesFromDataFrame(df = all_samples_meth_status[(num_lines_varying_from_parentals == 1) & (no_u_3>=2) & (num_lines_m_or_u_excl_parentals==4) & (parental_consensus == "U"),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
### These fail due to too many NAs:
M_parent_call_ranges = makeGRangesFromDataFrame(df = all_samples_meth_status[(parental_consensus == "M"),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
U_parent_call_ranges = makeGRangesFromDataFrame(df = all_samples_meth_status[(parental_consensus == "U"),], start.field = "Locus", end.field = "Locus", seqnames.field = "Chromosome")
### Need new set of DMRs relevant to 1001 methylomes paper
DMRs.gr = reduce(c(makeGRangesFromDataFrame(df=read.table(file=Schmitz_CG_DMRs, header=FALSE, sep="\t"), start.field = "V2", end.field = "V3",seqnames.field = "V1"),makeGRangesFromDataFrame(df=read.table(file=Schmitz_non_CG_DMRs, header=FALSE, sep="\t"), start.field = "V2", end.field = "V3",seqnames.field = "V1")))
levels(DMRs.gr@seqnames@values) = toupper(as.character(levels(DMRs.gr@seqnames@values)))
DMRs.gr@seqinfo@seqnames = levels(DMRs.gr@seqnames@values)
variant_call_ranges = setdiff(variant_call_ranges, DMRs.gr, ignore.strand=TRUE)
m_to_u_call_ranges = setdiff(m_to_u_call_ranges, DMRs.gr, ignore.strand=TRUE)
u_to_m_call_ranges = setdiff(u_to_m_call_ranges, DMRs.gr, ignore.strand=TRUE)
clean_call_ranges = setdiff(clean_call_ranges, DMRs.gr, ignore.strand = TRUE)
clean_variant_call_ranges = setdiff(clean_variant_call_ranges, DMRs.gr, ignore.strand = TRUE)
clean_m_to_u_call_ranges = setdiff(clean_m_to_u_call_ranges, DMRs.gr, ignore.strand = TRUE)
clean_u_to_m_call_ranges = setdiff(clean_u_to_m_call_ranges, DMRs.gr, ignore.strand = TRUE)
clean_single_variant_call_ranges = setdiff(clean_variant_call_ranges, DMRs.gr, ignore.strand = TRUE)
clean_single_m_to_u_call_ranges = setdiff(clean_m_to_u_call_ranges, DMRs.gr, ignore.strand = TRUE)
clean_single_u_to_m_call_ranges = setdiff(clean_u_to_m_call_ranges, DMRs.gr, ignore.strand = TRUE)
M_parent_call_ranges = setdiff(M_parent_call_ranges, DMRs.gr, ignore.strand = TRUE)
U_parent_call_ranges = setdiff(U_parent_call_ranges, DMRs.gr, ignore.strand = TRUE)
# Find the overlaps
olaps_CG_sites = findOverlaps(CG_site_ranges, gene_ranges)
olaps_all_M = findOverlaps(all_M_ranges, gene_ranges)
olaps_all_U = findOverlaps(all_U_ranges, gene_ranges)
olaps_variable = findOverlaps(variant_call_ranges, gene_ranges)
olaps_m_to_u = findOverlaps(m_to_u_call_ranges, gene_ranges)
olaps_u_to_m = findOverlaps(u_to_m_call_ranges, gene_ranges)
tolaps_CG_sites = findOverlaps(CG_site_ranges, transposon_ranges)
tolaps_all_M = findOverlaps(all_M_ranges, transposon_ranges)
tolaps_all_U = findOverlaps(all_U_ranges, transposon_ranges)
tolaps_variable = findOverlaps(variant_call_ranges, transposon_ranges)
tolaps_m_to_u = findOverlaps(m_to_u_call_ranges, transposon_ranges)
tolaps_u_to_m = findOverlaps(u_to_m_call_ranges, transposon_ranges)
### exon intron and UTR olaps fail due to capitalisation or something
eolaps_CG_sites = findOverlaps(CG_site_ranges, exon_ranges)
eolaps_all_M = findOverlaps(all_M_ranges, exon_ranges)
eolaps_all_U = findOverlaps(all_U_ranges, exon_ranges)
eolaps_variable = findOverlaps(variant_call_ranges, exon_ranges)
eolaps_m_to_u = findOverlaps(m_to_u_call_ranges, exon_ranges)
eolaps_u_to_m = findOverlaps(u_to_m_call_ranges, exon_ranges)
iolaps_CG_sites = findOverlaps(CG_site_ranges, intron_ranges)
iolaps_all_M = findOverlaps(all_M_ranges, intron_ranges)
iolaps_all_U = findOverlaps(all_U_ranges, intron_ranges)
iolaps_variable = findOverlaps(variant_call_ranges, intron_ranges)
iolaps_m_to_u = findOverlaps(m_to_u_call_ranges, intron_ranges)
iolaps_u_to_m = findOverlaps(u_to_m_call_ranges, intron_ranges)
olaps5_CG_sites = findOverlaps(CG_site_ranges, UTR5_ranges)
olaps5_all_M = findOverlaps(all_M_ranges, UTR5_ranges)
olaps5_all_U = findOverlaps(all_U_ranges, UTR5_ranges)
olaps5_variable = findOverlaps(variant_call_ranges, UTR5_ranges)
olaps5_m_to_u = findOverlaps(m_to_u_call_ranges, UTR5_ranges)
olaps5_u_to_m = findOverlaps(u_to_m_call_ranges, UTR5_ranges)
olaps3_CG_sites = findOverlaps(CG_site_ranges, UTR3_ranges)
olaps3_all_M = findOverlaps(all_M_ranges, UTR3_ranges)
olaps3_all_U = findOverlaps(all_U_ranges, UTR3_ranges)
olaps3_variable = findOverlaps(variant_call_ranges, UTR3_ranges)
olaps3_m_to_u = findOverlaps(m_to_u_call_ranges, UTR3_ranges)
olaps3_u_to_m = findOverlaps(u_to_m_call_ranges, UTR3_ranges)
# Generate a count of CG sites per gene
gff.genes$CG_sites_count=table(gff.genes[subjectHits(olaps_CG_sites),]$gene_ID)[gff.genes$gene_ID]
# Replace NA values in variant_count with 0s
gff.genes$CG_sites_count=ifelse(is.na(gff.genes$CG_sites_count),0,gff.genes$CG_sites_count)
# Generate a count of all_M sites per gene
gff.genes$all_M_count=table(gff.genes[subjectHits(olaps_all_M),]$gene_ID)[gff.genes$gene_ID]
# Replace NA values in variant_count with 0s
gff.genes$all_M_count=ifelse(is.na(gff.genes$all_M_count),0,gff.genes$all_M_count)
# Generate a count of all_U sites per gene
gff.genes$all_U_count=table(gff.genes[subjectHits(olaps_all_U),]$gene_ID)[gff.genes$gene_ID]
# Replace NA values in variant_count with 0s
gff.genes$all_U_count=ifelse(is.na(gff.genes$all_U_count),0,gff.genes$all_U_count)
# Generate a count of variable sites per gene
gff.genes$variable_count=table(gff.genes[subjectHits(olaps_variable),]$gene_ID)[gff.genes$gene_ID]
# Replace NA values in variant_count with 0s
gff.genes$variable_count=ifelse(is.na(gff.genes$variable_count),0,gff.genes$variable_count)
# Generate a count of m->u sites per gene
gff.genes$m_to_u_count=table(gff.genes[subjectHits(olaps_m_to_u),]$gene_ID)[gff.genes$gene_ID]
# Replace NA values in variant_count with 0s
gff.genes$m_to_u_count=ifelse(is.na(gff.genes$m_to_u_count),0,gff.genes$m_to_u_count)
# Generate a count of u->m sites per gene
gff.genes$u_to_m_count=table(gff.genes[subjectHits(olaps_u_to_m),]$gene_ID)[gff.genes$gene_ID]
# Replace NA values in variant_count with 0s
gff.genes$u_to_m_count=ifelse(is.na(gff.genes$u_to_m_count),0,gff.genes$u_to_m_count)
# Generate a count of CG sites per transposon
gff.transposons$CG_sites_count=table(gff.transposons[subjectHits(tolaps_CG_sites),]$gene_ID)[gff.transposons$gene_ID]
# Replace NA values in variant_count with 0s
gff.transposons$CG_sites_count=ifelse(is.na(gff.transposons$CG_sites_count),0,gff.transposons$CG_sites_count)
# Generate a count of all_M sites per transposon
gff.transposons$all_M_count=table(gff.transposons[subjectHits(tolaps_all_M),]$gene_ID)[gff.transposons$gene_ID]
# Replace NA values in variant_count with 0s
gff.transposons$all_M_count=ifelse(is.na(gff.transposons$all_M_count),0,gff.transposons$all_M_count)
# Generate a count of all_U sites per transposon
gff.transposons$all_U_count=table(gff.transposons[subjectHits(tolaps_all_U),]$gene_ID)[gff.transposons$gene_ID]
# Replace NA values in variant_count with 0s
gff.transposons$all_U_count=ifelse(is.na(gff.transposons$all_U_count),0,gff.transposons$all_U_count)
# Generate a count of variable sites per transposon
gff.transposons$variable_count=table(gff.transposons[subjectHits(tolaps_variable),]$gene_ID)[gff.transposons$gene_ID]
# Replace NA values in variant_count with 0s
gff.transposons$variable_count=ifelse(is.na(gff.transposons$variable_count),0,gff.transposons$variable_count)
# Generate a count of m->u sites per transposon
gff.transposons$m_to_u_count=table(gff.transposons[subjectHits(tolaps_m_to_u),]$gene_ID)[gff.transposons$gene_ID]
# Replace NA values in variant_count with 0s
gff.transposons$m_to_u_count=ifelse(is.na(gff.transposons$m_to_u_count),0,gff.transposons$m_to_u_count)
# Generate a count of u->m sites per transposon
gff.transposons$u_to_m_count=table(gff.transposons[subjectHits(tolaps_u_to_m),]$gene_ID)[gff.transposons$gene_ID]
# Replace NA values in variant_count with 0s
gff.transposons$u_to_m_count=ifelse(is.na(gff.transposons$u_to_m_count),0,gff.transposons$u_to_m_count)
# Find out which genes are really genes, and which are 'heterochromatic genes' as per Zemach et al, 2013 Figure 6A.
### This is just putting total nos sites in the average_methylation column
gff.genes$average_methylation=table(gff.genes[subjectHits(olaps_CG_sites),]$gene_ID)[gff.genes$gene_ID]
# Find out which transposons are really transposons, and which are 'euchromatic transposons' as per my own method.
### This needs more
gff.transposons$average_methylation=table(gff.transposons[subjectHits(tolaps_CG_sites),]$gene_ID)[gff.transposons$gene_ID]
# Create a data frame with gene ID and average methylation for each overlap between a site and a gene model
olaps_average_methylation=data.frame(cbind("gene_ID"=gff.genes[subjectHits(olaps_CG_sites),"gene_ID"],"site_average_methylation"=(average_methylation[,1]/average_methylation[,2])[queryHits(olaps_CG_sites)]))
tolaps_average_methylation=data.frame(cbind("gene_ID"=gff.transposons[subjectHits(tolaps_CG_sites),"gene_ID"],"site_average_methylation"=(average_methylation[,1]/average_methylation[,2])[queryHits(tolaps_CG_sites)]))
# Convert average values to numerics
olaps_average_methylation$site_average_methylation=as.numeric(levels(olaps_average_methylation$site_average_methylation)[olaps_average_methylation$site_average_methylation])
tolaps_average_methylation$site_average_methylation=as.numeric(levels(tolaps_average_methylation$site_average_methylation)[tolaps_average_methylation$site_average_methylation])
# Find the mean of sites average methylation levels across each gene, and merge this with the genes table
#x = merge(gff.genes, aggregate(. ~ gene_ID, olaps_average_methylation[], mean), by="gene_ID", all=TRUE)
#gff.genes$average_methylation = x$site_average_methylation[match(gff.genes$gene_ID, x$gene_ID)]
gff.genes$average_methylation = merge(gff.genes, aggregate(. ~ gene_ID, olaps_average_methylation[], mean), by="gene_ID", all=TRUE)$site_average_methylation[match(gff.genes$gene_ID, merge(gff.genes, aggregate(. ~ gene_ID, olaps_average_methylation[], mean), by="gene_ID", all=TRUE)$gene_ID)]
gff.transposons$average_methylation = merge(gff.transposons, aggregate(. ~ gene_ID, tolaps_average_methylation[], mean), by="gene_ID", all=TRUE)$site_average_methylation[match(gff.transposons$gene_ID, merge(gff.transposons, aggregate(. ~ gene_ID, tolaps_average_methylation[], mean), by="gene_ID", all=TRUE)$gene_ID)]
# Find the variance of sites average methylation levels across each gene, and merge this with the genes table
gff.genes$variance_methylation = merge(gff.genes, aggregate(. ~ gene_ID, olaps_average_methylation[], var), by="gene_ID", all=TRUE)$site_average_methylation[match(gff.genes$gene_ID, merge(gff.genes, aggregate(. ~ gene_ID, olaps_average_methylation[], var), by="gene_ID", all=TRUE)$gene_ID)]
gff.transposons$variance_methylation = merge(gff.transposons, aggregate(. ~ gene_ID, tolaps_average_methylation[], var), by="gene_ID", all=TRUE)$site_average_methylation[match(gff.transposons$gene_ID, merge(gff.transposons, aggregate(. ~ gene_ID, tolaps_average_methylation[], var), by="gene_ID", all=TRUE)$gene_ID)]
# For a more hands-off way to set the threshold, fit a mixture of Gaussians to the dispersion index density, and identify the intersection of the fit models
library(mixtools)
library(rootSolve)
# Fit a mixture of 2 Gaussians to the dispersion index density
#mixmdl = normalmixEM(gff.genes[!is.na(gff.genes$variance_methylation/gff.genes$average_methylation),]$variance_methylation/gff.genes[!is.na(gff.genes$variance_methylation/gff.genes$average_methylation),]$average_methylation)
# Fit a mixture of 3 Gaussians to the dispersion index density
mixmdl = normalmixEM(gff.genes[!is.na(gff.genes$variance_methylation/gff.genes$average_methylation),]$variance_methylation/gff.genes[!is.na(gff.genes$variance_methylation/gff.genes$average_methylation),]$average_methylation, k=ifelse(meth_context=="CG",3,ifelse(meth_context=="CHG",2,2)))
# Mixture of 2 Gaussians fits better once 'P' CG sites are removed from the picture
#mixmdl = normalmixEM(gff.genes[!is.na(gff.genes$variance_methylation/gff.genes$average_methylation),]$variance_methylation/gff.genes[!is.na(gff.genes$variance_methylation/gff.genes$average_methylation),]$average_methylation, k=ifelse(meth_context=="CG",2,ifelse(meth_context=="CHG",2,2)))
# Print the characteristics of the fit curves
cat(paste0("Fit lambda values (mixture proportions):\n"))
mixmdl$lambda
# Schmitz data: 0.2853651 0.1365236 0.5781114. After converting P to U/M with mCG=0.45 cutoff: 0.2381327 0.7618673. Using Fishers p<0.05, Binomial p<0.005: 0.2413691 0.1474132 0.6112177
# Becker data: 0.08638043 0.17313572 0.74048385
# 1001 methylomes: 0.1665908 0.5831132 0.2502960
cat(paste0("Fit mu values (means):\n"))
mixmdl$mu
# Schmitz data: 0.01777965 0.10733632 0.64071733. After converting P to U/M with mCG=0.45 cutoff: 0.08997885 0.65362097. Using Fishers p<0.05, Binomial p<0.005: 0.01632469 0.10906799 0.64090168
# Becker data: 0.003837026 0.070503558 0.635220579
# 1001 methylomes: 0.001042594 0.295110614 0.016815323
cat(paste0("Fit sigma values (st.devs):\n"))
mixmdl$sigma
# Schmitz data: 0.01006439 0.05852205 0.14541845. After converting P to U/M with mCG=0.45 cutoff: 0.07725693 0.14292752. Using Fishers p<0.05, Binomial p<0.005: 0.01027012 0.05664957 0.14650136
# Becker data: 0.004954326 0.045214098 0.165311575
# 1001 methylomes: 0.0006754618 0.1620353312 0.0132676875
# Find the intersection of the two larger components in the mixture - here we cut off
mixfn <- function(x, m1, sd1, m2, sd2, p1, p2){
dnorm(x, m1, sd1) * p1 - dnorm(x, m2, sd2) * p2 }
low_model=ifelse(meth_context=="CG",2,ifelse(meth_context=="CHG",1,1))
#low_model=1
high_model=low_model+1
heterochromatic_gene_dispersion_cutoff=uniroot.all(mixfn, lower=0, upper=1, m1=mixmdl$mu[low_model], sd1=mixmdl$sigma[low_model], m2=mixmdl$mu[high_model], sd2=mixmdl$sigma[high_model], p1=mixmdl$lambda[low_model], p2=mixmdl$lambda[high_model])
heterochromatic_gene_dispersion_cutoff
# Schmitz CG data: 0.2517447, Becker CG data: 0.1896873
# Schmitz CG data after 'P' sites removed: 0.2768782, Becker:
# Schmitz data with Fishers p<0.05, Binomial p<0.005: 0.2499017
# Becker CHG data: 0.9766602
# 1001 methylomes CG data: 0.04848119
plot(mixmdl, whichplots=2)
# Plot gene methylation level vs. gene length, colour by chromosome
heterochromatic_gene_coverage_cutoff=0.6
heterochromatic_gene_coverage_minimum=0.2
gff.genes$m_class = ifelse(gff.genes$CG_sites_count<=1,"CG-poor",ifelse(gff.genes$all_M_count+gff.genes$all_U_count+gff.genes$variable_count==0,"Unknown",ifelse(gff.genes$average_methylation==0,"Unmethylated",ifelse((gff.genes$variance_methylation/gff.genes$average_methylation>heterochromatic_gene_dispersion_cutoff),"Gene-body Methylated",ifelse((gff.genes$variance_methylation/gff.genes$average_methylation<=heterochromatic_gene_dispersion_cutoff) & (gff.genes$average_methylation<heterochromatic_gene_coverage_minimum),"Unmethylated",ifelse((gff.genes$variance_methylation/gff.genes$average_methylation<=heterochromatic_gene_dispersion_cutoff) & (gff.genes$average_methylation>=heterochromatic_gene_coverage_minimum),"Heterochromatic","Really Unknown"))))))
gff.transposons$m_class = ifelse(gff.transposons$CG_sites_count<=1,"CG-poor",ifelse(gff.transposons$all_M_count+gff.transposons$all_U_count+gff.transposons$variable_count==0,"Unknown",ifelse(gff.transposons$average_methylation==0,"Unmethylated",ifelse((gff.transposons$variance_methylation/gff.transposons$average_methylation>heterochromatic_gene_dispersion_cutoff),"Gene-body Methylated",ifelse((gff.transposons$variance_methylation/gff.transposons$average_methylation<=heterochromatic_gene_dispersion_cutoff) & (gff.transposons$average_methylation<heterochromatic_gene_coverage_minimum),"Unmethylated",ifelse((gff.transposons$variance_methylation/gff.transposons$average_methylation<=heterochromatic_gene_dispersion_cutoff) & (gff.transposons$average_methylation>=heterochromatic_gene_coverage_minimum),"Heterochromatic","Really Unknown"))))))
# Make a data frame with each gene being a row and columns showing the methylation level of that gene in each sample
methylation_per_gene_per_sample = data.frame(gff.genes$gene_ID)
colnames(methylation_per_gene_per_sample)="gene_ID"
sample_no = 0
for(sample_name in valid_samples) {
sample_no = sample_no + 1
if(opt$verbose) {cat(paste0("Summing methylation levels in sample ",sample_name,"\n"))}
sample_average_methylation = matrix(0, nrow=nrow(all_samples_meth_status), ncol=2)
sample_average_methylation[,1]=ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse(all_samples_meth_status[,sample_name]=="U",0,ifelse(all_samples_meth_status[,sample_name]=="M",1,ifelse(all_samples_meth_status[,sample_name]=="P",0.25,0))))
sample_average_methylation[,2]=ifelse(is.na(all_samples_meth_status[,sample_name]),0,ifelse(all_samples_meth_status[,sample_name]=="U",1,ifelse(all_samples_meth_status[,sample_name]=="M",1,ifelse(all_samples_meth_status[,sample_name]=="P",1,0))))
sample_olaps_average_methylation=data.frame(cbind("gene_ID"=gff.genes[subjectHits(olaps_CG_sites),"gene_ID"],"site_average_methylation"=(sample_average_methylation[,1]/sample_average_methylation[,2])[queryHits(olaps_CG_sites)]))
sample_olaps_average_methylation$site_average_methylation = as.numeric(levels(sample_olaps_average_methylation$site_average_methylation)[sample_olaps_average_methylation$site_average_methylation])
#methylation_per_gene_per_sample = merge(methylation_per_gene_per_sample, sample_name=merge(gff.genes, aggregate(. ~ gene_ID, sample_olaps_average_methylation[], mean), by="gene_ID", all=TRUE)$site_average_methylation[match(gff.genes$gene_ID, merge(gff.genes, aggregate(. ~ gene_ID, sample_olaps_average_methylation[], mean), by="gene_ID", all=TRUE)$gene_ID)], by="gene_ID", all=TRUE)
methylation_per_gene_per_sample = merge(methylation_per_gene_per_sample, aggregate(. ~ gene_ID, sample_olaps_average_methylation[], mean), by="gene_ID", all=TRUE)
colnames(methylation_per_gene_per_sample)[sample_no + 1] = sample_name
}
write.table(methylation_per_gene_per_sample, file="mean_mCG_per_gene_per_sample.txt", sep="\t", quote=FALSE, rownames=FALSE, colnames=FALSE)
# methylation_per_gene_per_sample = read.delim(file="mean_mCG_per_gene_per_sample.txt")
row.names(methylation_per_gene_per_sample)=methylation_per_gene_per_sample[,1]
#library(gplots)
#heatmap.2(data.matrix(methylation_per_gene_per_sample[1:100,2:101]), col=redgreen(75))
library(pheatmap)
library("RColorBrewer")
#pheatmap(data.matrix(na.omit(methylation_per_gene_per_sample)[1:1010,2:ncol(methylation_per_gene_per_sample)]), color=brewer.pal(9,"Blues"))
pheatmap(data.matrix(methylation_per_gene_per_sample[as.logical((rowSums(is.na(methylation_per_gene_per_sample))-ncol(methylation_per_gene_per_sample)+1)),][1:500,2:ncol(methylation_per_gene_per_sample)]), color=brewer.pal(9,"Blues"))
pca = prcomp(na.omit(data.matrix(methylation_per_gene_per_sample[methylation_per_gene_per_sample$gene_ID %in% gff.genes[gff.genes$m_class=="Gene-body Methylated",]$gene_ID,][as.logical((rowSums(is.na(methylation_per_gene_per_sample[methylation_per_gene_per_sample$gene_ID %in% gff.genes[gff.genes$m_class=="Gene-body Methylated",]$gene_ID,]))-ncol(methylation_per_gene_per_sample)+1)),][,2:ncol(methylation_per_gene_per_sample)])), center=TRUE, scale=TRUE)
library(scatterplot3d)
scatterplot3d(pca$x[,1:3], pch=20, color="blue")
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", ignore.strand=TRUE))
gene_body_loci.gr=makeGRangesFromDataFrame(df=gff.genes[gff.genes$m_class=="Gene-body Methylated",c("V1","V4","V5")], start.field="V4", end.field="V5", seqnames.field="V1", ignore.strand=TRUE)
unmethylated_gene_loci.gr=makeGRangesFromDataFrame(df=gff.genes[gff.genes$m_class=="Unmethylated",c("V1","V4","V5")], start.field="V4", end.field="V5", seqnames.field="V1", ignore.strand=TRUE)
segmentation_model.gr = readRDS(file = "../../Jay-SMP_variation/5-analysis/SRA035939/SRA035939_CG_segmentation_model_draft3.rds")
# Capitalise chromosome names in segmentation_model.gr
### Commented out - we are not using capitals any more
#levels(segmentation_model.gr@seqnames)=toupper(levels(segmentation_model.gr@seqnames))
#segmentation_model.gr@seqinfo@seqnames=levels(segmentation_model.gr@seqnames)
#levels(variant_call_ranges@seqnames)=toupper(levels(variant_call_ranges@seqnames))
#variant_call_ranges@seqinfo@seqnames=levels(variant_call_ranges@seqnames)
GBM_segments.gr = segmentation_model.gr[segmentation_model.gr$segment.mStatus=="GBM"]
TEM_segments.gr = segmentation_model.gr[segmentation_model.gr$segment.mStatus=="TEM"]
UMR_segments.gr = segmentation_model.gr[segmentation_model.gr$segment.mStatus=="UMR"]
length(GBM_segments.gr)
length(TEM_segments.gr)
length(UMR_segments.gr)
# Three different versions of target_ranges:
# Identify GenomicRegions covering GBM genes which don't have a TE segment overlapping
target_ranges = setdiff(gene_body_loci.gr,gene_body_loci.gr[queryHits(findOverlaps(gene_body_loci.gr, TEM_segments.gr))])
### should we exclude gene_body_loci.gr if they only have 1 or 2 GBM sites?
# Identify GenomicRegions covering 'body' of such GBM genes (excluding 2kb 5' and 1kb 3')
#target_ranges =
# Identify GenomicRegions covering GBM range of such GBM genes (from first to last methylated base in gen 3)
#target_ranges = GBM_segments.gr[queryHits(findOverlaps(GBM_segments.gr, setdiff(gene_body_loci.gr,gene_body_loci.gr[queryHits(findOverlaps(gene_body_loci.gr, TEM_segments.gr))])))]
# Find U sites in target_ranges
target_U_sites = U_parent_call_ranges[queryHits(findOverlaps(U_parent_call_ranges, target_ranges))]
# Find M sites in target_ranges
target_M_sites = M_parent_call_ranges[queryHits(findOverlaps(M_parent_call_ranges, target_ranges))]
#z=cbind(as.data.frame(target_U_sites), dist_to_nearest_M = rep(NA,length(target_U_sites)), u_to_m_count = rep(NA,length(target_U_sites)))
zU=cbind(as.data.frame(target_U_sites), dist_to_nearest_M = rep(NA,length(target_U_sites)), sites_to_nearest_M = rep(NA,length(target_U_sites)), dist_to_nearest_U = rep(NA,length(target_U_sites)), sites_to_nearest_U = rep(NA,length(target_U_sites)), u_to_m_count = rep(NA,length(target_U_sites)))
# Find sites in target_ranges
target_U_sites = CG_site_ranges[queryHits(findOverlaps(CG_site_ranges, target_ranges))]
num_metrics = 8
methylation_summary = matrix(, nrow=length(valid_samples), ncol=num_metrics + 2)
methylation_summary[,1] = valid_samples
methylation_summary[,2] = as.character(sample_list[match(valid_samples, sample_list$SRA.Accession), "Title"])
for(sample_no in 1:length(valid_samples)) {
cat(paste0("Processing ", valid_samples[sample_no],"\n"))
this_sample = all_samples_meth_status[, valid_samples[sample_no]]
# work out whole genome methylation for sample
methylation_summary[sample_no, 3] = sum(this_sample %in% c("M"))/sum(this_sample %in% c("M","U"))
# work out whole genome coverage for sample
methylation_summary[sample_no, 4] = sum(this_sample %in% c("M","U"))/sum(this_sample %in% c("M","U","P","I"))
# work out proportion of missing data due to mutations
methylation_summary[sample_no, 9] = sum(this_sample %in% c("X"))/sum(this_sample %in% c("M","U","P","I","X"))
# proportion of sites with identified mutations, ignoring NA (zero coverage?)