-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify_genes_methylation_status.R
1953 lines (1571 loc) · 109 KB
/
classify_genes_methylation_status.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
# non-CG_methylation.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="m1001"
#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")
# Load the reference genome annotation, and anotate each gene and transposon based on the CG methylome
library(GenomicRanges)
#gff.genes=readRDS(paste0(reference_dir,"SRA035939_CG_gff.genes.rds"))
#gff.transposons=readRDS(paste0(reference_dir,"SRA035939_CG_gff.transposons.rds"))
# 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
#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)
# Find how many variable sites each gene has
#gene_info = gff.genes
#varloc_genes = NULL
# 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")
# Load the BSGenome version of the reference
library(BSgenome)
# List the available genome assemblies
#available.genomes()
# TAIR9 and TAIR10 correspond to the same genome assembly so there is no need for a BSgenome pkg for TAIR10 :-)
# https://stat.ethz.ch/pipermail/bioconductor/2010-December/036938.html
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)
# MethylSeekR expects an input file with 4 columns: Chrom, Locus, T and M. T is total reads, M is methylated reads.
# Create an input file for MethylSeekR based on the merged methylomes of all valid samples
# Sum the M (Cov_C) and T (Cov_C+Cov_T) counts for all valid samples
# Create empty tables to accumulate each of the 'across samples' columns data
#across_samples_TM=matrix(0, nrow=nrow(all_samples_meth_status), ncol=2)
# We are only dealing with one sample at a time so:
#valid_samples = sample_id
#for(sample_name in valid_samples) {
# if(opt$verbose) {cat(paste0("Adding methylation read counts to across-samples per-site table: ",sample_name,"\n"))}
# Merge each of the sample counts into the relevant accumulation table, accounting for possible NA values
# across_samples_TM[,1]=ifelse(is.na(coverage_data[coverage_data$Sample==sample_name,]$Cov_C+coverage_data[coverage_data$Sample==sample_name,]$Cov_T),across_samples_TM[,1],across_samples_TM[,1]+coverage_data[coverage_data$Sample==sample_name,]$Cov_C+coverage_data[coverage_data$Sample==sample_name,]$Cov_T)
# across_samples_TM[,2]=ifelse(is.na(coverage_data[coverage_data$Sample==sample_name,]$Cov_C),across_samples_TM[,2],across_samples_TM[,2]+coverage_data[coverage_data$Sample==sample_name,]$Cov_C)
#} # end for each sample
### Read in the SNPs data
#snps.gr <- readSNPTable(FileName=snpFname, seqLengths=sLengths)
# We will set sample_C and sample_T to 0 for all C loci where there is an annotated mutation
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)
}
doing_non_cg = TRUE
m.sel=0.09
not_processed_samples = c()
processed_samples = c()
all_samples_meth_status = NA
no_samples = 0
# make empty structures for the various things we are accumulating
gene_gbm_overlap = NULL
gene_tem_overlap = NULL
gene_umr_overlap = NULL
gene_gbm_mCG_sites = NULL
gene_tem_mCG_sites = NULL
for (this_sample in sample_list$SRA.Accession) {
cat(paste0(this_sample,"\n"))
if (doing_non_cg) {
# read in CHG methylome
meth_CHG.gr <- readMethylome(FileName=paste0(this_sample,"/",project_id,"_CHG_",this_sample,"_TM_read_counts_across_samples.tsv"), seqLengths=sLengths)
# read in CHH methylome
meth_CHH.gr <- readMethylome(FileName=paste0(this_sample,"/",project_id,"_CHH_",this_sample,"_TM_read_counts_across_samples.tsv"), seqLengths=sLengths)
# Set aside genes with no CHG sites
set_aside_no_CHG.gr = setdiff(gene_ranges, gene_ranges[unique(subjectHits(findOverlaps(meth_CHG.gr, gene_ranges)))])
#cat(paste0("set_aside_no_CHG.gr contains ",length(set_aside_no_CHG.gr)," segments which contain no CHG sites.\n"))
# 335 in first sample
# Set aside genes with no CHH sites
set_aside_no_CHH.gr = setdiff(gene_ranges, gene_ranges[unique(subjectHits(findOverlaps(meth_CHH.gr, gene_ranges)))])
#cat(paste0("set_aside_no_CHH.gr contains ",length(set_aside_no_CHH.gr)," segments which contain no CHH sites.\n"))
# 267 in first sample
CHG_genes = unique(subjectHits(findOverlaps(meth_CHG.gr, gene_ranges)))
gene_ranges_CHG = gene_ranges[CHG_genes]
#cat(paste0("gene_ranges_CHG contains ",length(gene_ranges_CHG)," segments after trimming segments with no CHG sites.\n"))
CHH_genes = unique(subjectHits(findOverlaps(meth_CHH.gr, gene_ranges)))
gene_ranges_CHH = gene_ranges[CHH_genes]
#cat(paste0("gene_ranges_CHH contains ",length(gene_ranges_CHH)," segments after trimming segments with no CHH sites.\n"))
}
# read in segmentation
segmentation_model = read.table(file = paste0(this_sample,"/",project_id,"_","CG","_",this_sample,"_segmentation_model_draft3.tsv"), sep="\t", header=TRUE)
segmentation_model$Chromosome = paste0("Chr", segmentation_model$Chromosome)
# Capitalise chromosome names in segmentation_model.gr
#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 = makeGRangesFromDataFrame(df = segmentation_model[substr(segmentation_model$Type,1,3)=="GBM",], start.field = "Start", end.field = "End",seqnames.field = "Chromosome")
TEM_segments.gr = makeGRangesFromDataFrame(df = segmentation_model[substr(segmentation_model$Type,1,3)=="TEM",], start.field = "Start", end.field = "End",seqnames.field = "Chromosome")
UMR_segments.gr = makeGRangesFromDataFrame(df = segmentation_model[substr(segmentation_model$Type,1,3)=="UMR",], start.field = "Start", end.field = "End",seqnames.field = "Chromosome")
length(GBM_segments.gr)
length(TEM_segments.gr)
length(UMR_segments.gr)
# Identify GenomicRegions covering genes
if (doing_non_cg) {
# overlap CHG methylome with gene loci and assign methylation % to each gene
meth_CHG = meth_by_segment(meth_CHG.gr, segment_model=gene_ranges_CHG, meth_context="CHG", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR"))$pmeth
meth_CHG = cbind(gff.genes[CHG_genes,"gene_ID"], meth_CHG)
colnames(meth_CHG) = c("gene_ID",this_sample)
# overlap CHH methylome with gene loci and assign methylation % to each gene
meth_CHH = meth_by_segment(meth_CHH.gr, segment_model=gene_ranges_CHH, meth_context="CHH", myGenomeSeq=Athaliana, seqLengths=sLengths, mMeth.classification=c(m.sel), mMeth.classes=c("UMR","MR"))$pmeth
meth_CHH = cbind(gff.genes[CHH_genes,"gene_ID"], meth_CHH)
colnames(meth_CHH) = c("gene_ID",this_sample)
}
# overlap gbM segments with genes and assign overlap statistic to each gene
gbm_genes = gff.genes[unique(subjectHits(findOverlaps(GBM_segments.gr, gene_ranges))),"gene_ID"]
gbm_genes = cbind(gbm_genes, rep(1, length(gbm_genes)))
colnames(gbm_genes) = c("gene_ID", this_sample)
# overlap UMR segments with genes and assign overlap statistic to each gene
umr_genes = gff.genes[unique(subjectHits(findOverlaps(UMR_segments.gr, gene_ranges))),"gene_ID"]
umr_genes = cbind(umr_genes, rep(1, length(umr_genes)))
colnames(umr_genes) = c("gene_ID", this_sample)
# overlap TEM segments with genes and assign overlap statistic to each gene
tem_genes = gff.genes[unique(subjectHits(findOverlaps(TEM_segments.gr, gene_ranges))),"gene_ID"]
tem_genes = cbind(tem_genes, rep(1, length(tem_genes)))
colnames(tem_genes) = c("gene_ID", this_sample)
# overlap TEM segments with genes and assign overlap statistic to each gene - length of overlapping segment
# Overlap genes with all_samples_meth_status and count No. of mCGs per gene, No. of sites per gene, prop of mCGs per gene
# overlap TEM segments and gbM segments with CG sites and count No. mCGs in each segment in each gene
# identify mCG sites overlapping gbM segments and TEM segments
this_meth = all_samples_meth_status[all_samples_meth_status[,colnames(all_samples_meth_status)==this_sample]=="M",c("Chromosome", "Locus")]
this_meth=cbind(this_meth, this_meth$Locus+1)
colnames(this_meth)=c("chromosome","start","end")
this_meth.gr = makeGRangesFromDataFrame(df = this_meth[!is.na(this_meth$start),])
GBM_mCG_sites = this_meth.gr[queryHits(findOverlaps(this_meth.gr, GBM_segments.gr))]
TEM_mCG_sites = this_meth.gr[queryHits(findOverlaps(this_meth.gr, TEM_segments.gr))]
this_gene_GBM_mCG_sites = table(gff.genes[subjectHits(findOverlaps(GBM_mCG_sites, gene_ranges)),"gene_ID"])
this_gene_TEM_mCG_sites = table(gff.genes[subjectHits(findOverlaps(TEM_mCG_sites, gene_ranges)),"gene_ID"])
this_gene_GBM_mCG_sites = as.data.frame(this_gene_GBM_mCG_sites)
colnames(this_gene_GBM_mCG_sites) = c("gene_ID", this_sample)
this_gene_TEM_mCG_sites = as.data.frame(this_gene_TEM_mCG_sites)
colnames(this_gene_TEM_mCG_sites) = c("gene_ID", this_sample)
# add each of the statistics vectors to the relevant all samples matrix
if (no_samples == 0) {
if (doing_non_cg) {
gene_meth_CHG = as.data.frame(meth_CHG)
gene_meth_CHH = as.data.frame(meth_CHH)
}
gene_gbm_overlap = as.data.frame(gbm_genes)
gene_tem_overlap = as.data.frame(tem_genes)
gene_umr_overlap = as.data.frame(umr_genes)
gene_gbm_mCG_sites = this_gene_GBM_mCG_sites
gene_tem_mCG_sites = this_gene_TEM_mCG_sites
} else {
if (doing_non_cg) {
gene_meth_CHG = merge(gene_meth_CHG, meth_CHG, by="gene_ID", all=TRUE)
gene_meth_CHH = merge(gene_meth_CHH, meth_CHH, by="gene_ID", all=TRUE)
}
gene_gbm_overlap = merge(gene_gbm_overlap, gbm_genes, by="gene_ID", all=TRUE)
gene_tem_overlap = merge(gene_tem_overlap, tem_genes, by="gene_ID", all=TRUE)
gene_umr_overlap = merge(gene_umr_overlap, umr_genes, by="gene_ID", all=TRUE)
gene_gbm_mCG_sites = merge(gene_gbm_mCG_sites, this_gene_GBM_mCG_sites, by="gene_ID", all=TRUE)
gene_tem_mCG_sites = merge(gene_tem_mCG_sites, this_gene_TEM_mCG_sites, by="gene_ID", all=TRUE)
}
# These lines not needed for R 4.0 and higher
#if (doing_non_cg) {
#gene_meth_CHG[,no_samples+2] = as.numeric(levels(gene_meth_CHG[,no_samples+2])[gene_meth_CHG[,no_samples+2]])
#gene_meth_CHH[,no_samples+2] = as.numeric(levels(gene_meth_CHH[,no_samples+2])[gene_meth_CHH[,no_samples+2]])
#}
#gene_gbm_overlap[,no_samples+2] = as.numeric(levels(gene_gbm_overlap[,no_samples+2])[gene_gbm_overlap[,no_samples+2]])
#gene_tem_overlap[,no_samples+2] = as.numeric(levels(gene_tem_overlap[,no_samples+2])[gene_tem_overlap[,no_samples+2]])
#gene_umr_overlap[,no_samples+2] = as.numeric(levels(gene_umr_overlap[,no_samples+2])[gene_umr_overlap[,no_samples+2]])
#gene_gbm_mCG_sites[,no_samples+2] = as.numeric(levels(gene_gbm_mCG_sites[,no_samples+2])[gene_gbm_mCG_sites[,no_samples+2]])
#gene_tem_mCG_sites[,no_samples+2] = as.numeric(levels(gene_tem_mCG_sites[,no_samples+2])[gene_tem_mCG_sites[,no_samples+2]])
# These lines are the version for R 4.0 and higher
if (doing_non_cg) {
gene_meth_CHG[,no_samples+2] = as.numeric(gene_meth_CHG[,no_samples+2])
gene_meth_CHH[,no_samples+2] = as.numeric(gene_meth_CHH[,no_samples+2])
}
gene_gbm_overlap[,no_samples+2] = as.numeric(gene_gbm_overlap[,no_samples+2])
gene_tem_overlap[,no_samples+2] = as.numeric(gene_tem_overlap[,no_samples+2])
gene_umr_overlap[,no_samples+2] = as.numeric(gene_umr_overlap[,no_samples+2])
#gene_gbm_mCG_sites[,no_samples+2] = as.numeric(levels(gene_gbm_mCG_sites[,no_samples+2])[gene_gbm_mCG_sites[,no_samples+2]])
#gene_tem_mCG_sites[,no_samples+2] = as.numeric(levels(gene_tem_mCG_sites[,no_samples+2])[gene_tem_mCG_sites[,no_samples+2]])
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)
#}
}
# Replace NAs with 0
gene_meth_CHG[is.na(gene_meth_CHG)] = 0
gene_meth_CHH[is.na(gene_meth_CHH)] = 0
gene_gbm_overlap[is.na(gene_gbm_overlap)] = 0
gene_tem_overlap[is.na(gene_tem_overlap)] = 0
gene_umr_overlap[is.na(gene_umr_overlap)] = 0
gene_gbm_mCG_sites[is.na(gene_gbm_mCG_sites)] = 0
gene_tem_mCG_sites[is.na(gene_tem_mCG_sites)] = 0
write.table(gene_meth_CHG, file=paste0(project_id, "_gene_meth_CHG.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
write.table(gene_meth_CHH, file=paste0(project_id, "_gene_meth_CHH.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
write.table(gene_gbm_overlap, file=paste0(project_id, "_gene_gbm_overlap.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
write.table(gene_tem_overlap, file=paste0(project_id, "_gene_tem_overlap.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
write.table(gene_umr_overlap, file=paste0(project_id, "_gene_umr_overlap.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
write.table(gene_gbm_mCG_sites, file=paste0(project_id, "_gene_gbm_mCG_sites.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
write.table(gene_tem_mCG_sites, file=paste0(project_id, "_gene_tem_mCG_sites.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
gene_gbm_mCG_sites$gene_ID = as.character(gene_gbm_mCG_sites$gene_ID)
gene_tem_mCG_sites$gene_ID = as.character(gene_tem_mCG_sites$gene_ID)
# 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"))
# Restore the data from the convenience dumps
#gene_meth_CHG = read.table(file=paste0(project_id, "_gene_meth_CHG.txt"), sep="\t", header=TRUE)
#gene_meth_CHH = read.table(file=paste0(project_id, "_gene_meth_CHH.txt"), sep="\t", header=TRUE)
#gene_gbm_overlap = read.table(file=paste0(project_id, "_gene_gbm_overlap.txt"), sep="\t", header=TRUE)
#gene_tem_overlap = read.table(file=paste0(project_id, "_gene_tem_overlap.txt"), sep="\t", header=TRUE)
#gene_umr_overlap = read.table(file=paste0(project_id, "_gene_umr_overlap.txt"), sep="\t", header=TRUE)
#gene_gbm_mCG_sites = read.table(file=paste0(project_id, "_gene_gbm_mCG_sites.txt"), sep="\t", header=TRUE)
#gene_tem_mCG_sites = read.table(file=paste0(project_id, "_gene_tem_mCG_sites.txt"), sep="\t", header=TRUE)
#all_samples_meth_status = readRDS(file=paste0("../5-analysis_2020-01-17/",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")
#cat(nrow(this_sample_snps[(this_sample_snps$Chromosome=="Chr5") & (this_sample_snps$Locus>=20048325) & (this_sample_snps$Locus<=20049775),]))
#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"))
# work out how many genes are both gbM and TEM
gene_both_overlap = NULL
gene_both_overlap_filtered = NULL
gene_both_overlap_resolved = NULL
gene_both_overlap_resolved_gbm = NULL
gene_both_overlap_resolved_tem = NULL
gene_gbm_calls = NULL
gene_tem_calls = NULL
no_samples = 0
for (this_sample in sample_list$SRA.Accession) {
cat(paste0(this_sample,"\n"))
gene_stats = merge(cbind("gene_ID"=gene_gbm_overlap[,"gene_ID"], "gene_gbm_overlap"=gene_gbm_overlap[,colnames(gene_gbm_overlap)==this_sample]), cbind("gene_ID"=gene_tem_overlap[,"gene_ID"], "gene_tem_overlap"=gene_tem_overlap[,colnames(gene_tem_overlap)==this_sample]), by="gene_ID", all=TRUE)
gene_stats = merge(gene_stats, cbind("gene_ID"=gene_gbm_mCG_sites[,"gene_ID"], "gene_gbm_mCG_sites"=gene_gbm_mCG_sites[,colnames(gene_gbm_mCG_sites)==this_sample]), by="gene_ID", all=TRUE)
gene_stats = merge(gene_stats, cbind("gene_ID"=gene_tem_mCG_sites[,"gene_ID"], "gene_tem_mCG_sites"=gene_tem_mCG_sites[,colnames(gene_tem_mCG_sites)==this_sample]), by="gene_ID", all=TRUE)
gene_stats[,2] = as.numeric(gene_stats[,2])
gene_stats[,3] = as.numeric(gene_stats[,3])
gene_stats[,4] = as.numeric(gene_stats[,4])
gene_stats[,5] = as.numeric(gene_stats[,5])
gene_stats$both_overlap = gene_stats$gene_gbm_overlap * gene_stats$gene_tem_overlap
gene_stats$filtered = gene_stats$both_overlap*ifelse(gene_stats$gene_gbm_mCG_sites>2,1,0)*ifelse(gene_stats$gene_tem_mCG_sites>2,1,0)
gbm_tem_ratio = 4
gene_stats$resolved = gene_stats$both_overlap*(ifelse(gene_stats$gene_gbm_mCG_sites>gene_stats$gene_tem_mCG_sites*gbm_tem_ratio,1,0) + ifelse(gene_stats$gene_tem_mCG_sites>gene_stats$gene_gbm_mCG_sites*gbm_tem_ratio,1,0))*(ifelse((gene_stats$gene_gbm_mCG_sites>2) | (gene_stats$gene_tem_mCG_sites>2),1,0))*(ifelse(!((gene_stats$gene_gbm_mCG_sites>2) & (gene_stats$gene_tem_mCG_sites>2)),1,0))
gene_stats$resolved_gbm = gene_stats$both_overlap*(ifelse(gene_stats$gene_gbm_mCG_sites>gene_stats$gene_tem_mCG_sites*gbm_tem_ratio,1,0))*(ifelse((gene_stats$gene_gbm_mCG_sites>2) | (gene_stats$gene_tem_mCG_sites>2),1,0))*(ifelse(!((gene_stats$gene_gbm_mCG_sites>2) & (gene_stats$gene_tem_mCG_sites>2)),1,0))
gene_stats$resolved_tem = gene_stats$both_overlap*(ifelse(gene_stats$gene_tem_mCG_sites>gene_stats$gene_gbm_mCG_sites*gbm_tem_ratio,1,0))*(ifelse((gene_stats$gene_gbm_mCG_sites>2) | (gene_stats$gene_tem_mCG_sites>2),1,0))*(ifelse(!((gene_stats$gene_gbm_mCG_sites>2) & (gene_stats$gene_tem_mCG_sites>2)),1,0))
# replace NAs with 0s, so that they don't proliferate when we start comparing columns
gene_stats[is.na(gene_stats)] = 0
# collect the calls that are unambiguous, or meet the filtering criteria
gene_stats$gbm_calls = gene_stats$gene_gbm_overlap * ((1-gene_stats$both_overlap) + (gene_stats$resolved_gbm))
gene_stats$tem_calls = gene_stats$gene_tem_overlap * ((1-gene_stats$both_overlap) + (gene_stats$resolved_tem))
if (no_samples == 0) {
gene_both_overlap = as.data.frame(gene_stats[,c(1,6)])
gene_both_overlap_filtered = as.data.frame(gene_stats[,c(1,7)])
gene_both_overlap_resolved = as.data.frame(gene_stats[,c(1,8)])
gene_both_overlap_resolved_gbm = as.data.frame(gene_stats[,c(1,9)])
gene_both_overlap_resolved_tem = as.data.frame(gene_stats[,c(1,10)])
gene_gbm_calls = as.data.frame(gene_stats[,c(1,11)])
gene_tem_calls = as.data.frame(gene_stats[,c(1,12)])
} else {
gene_both_overlap = merge(gene_both_overlap, gene_stats[,c(1,6)], by="gene_ID", all=TRUE)
gene_both_overlap_filtered = merge(gene_both_overlap_filtered, gene_stats[,c(1,7)], by="gene_ID", all=TRUE)
gene_both_overlap_resolved = merge(gene_both_overlap_resolved, gene_stats[,c(1,8)], by="gene_ID", all=TRUE)
gene_both_overlap_resolved_gbm = merge(gene_both_overlap_resolved_gbm, gene_stats[,c(1,9)], by="gene_ID", all=TRUE)
gene_both_overlap_resolved_tem = merge(gene_both_overlap_resolved_tem, gene_stats[,c(1,10)], by="gene_ID", all=TRUE)
gene_gbm_calls = merge(gene_gbm_calls, gene_stats[,c(1,11)], by="gene_ID", all=TRUE)
gene_tem_calls = merge(gene_tem_calls, gene_stats[,c(1,12)], by="gene_ID", all=TRUE)
}
colnames(gene_both_overlap)[no_samples+2] = this_sample
colnames(gene_both_overlap_filtered)[no_samples+2] = this_sample
colnames(gene_both_overlap_resolved)[no_samples+2] = this_sample
colnames(gene_both_overlap_resolved_gbm)[no_samples+2] = this_sample
colnames(gene_both_overlap_resolved_tem)[no_samples+2] = this_sample
colnames(gene_gbm_calls)[no_samples+2] = this_sample
colnames(gene_tem_calls)[no_samples+2] = this_sample
# These lines not needed for R 4.0 and higher
#if (doing_non_cg) {
#gene_both_overlap[,no_samples+2] = as.numeric(levels(gene_both_overlap[,no_samples+2])[gene_both_overlap[,no_samples+2]])
# These lines are the version for R 4.0 and higher
gene_both_overlap[,no_samples+2] = as.numeric(gene_both_overlap[,no_samples+2])
gene_both_overlap_filtered[,no_samples+2] = as.numeric(gene_both_overlap_filtered[,no_samples+2])
gene_both_overlap_resolved[,no_samples+2] = as.numeric(gene_both_overlap_resolved[,no_samples+2])
gene_both_overlap_resolved_gbm[,no_samples+2] = as.numeric(gene_both_overlap_resolved_gbm[,no_samples+2])
gene_both_overlap_resolved_tem[,no_samples+2] = as.numeric(gene_both_overlap_resolved_tem[,no_samples+2])
gene_gbm_calls[,no_samples+2] = as.numeric(gene_gbm_calls[,no_samples+2])
gene_tem_calls[,no_samples+2] = as.numeric(gene_tem_calls[,no_samples+2])
no_samples = no_samples + 1
}
# plot distributions of numbers of gbM genes, TEM genes and both per accession
gbm_gene_counts = colSums(gene_gbm_overlap[,2:1212], na.rm=TRUE)
tem_gene_counts = colSums(gene_tem_overlap[,2:1212], na.rm=TRUE)
both_counts = colSums(gene_both_overlap[2:1212], na.rm=TRUE)
both_filtered_counts = colSums(gene_both_overlap_filtered[2:1212], na.rm=TRUE)
both_resolved_counts = colSums(gene_both_overlap_resolved[2:1212], na.rm=TRUE)
both_resolved_gbm_counts = colSums(gene_both_overlap_resolved_gbm[2:1212], na.rm=TRUE)
both_resolved_tem_counts = colSums(gene_both_overlap_resolved_tem[2:1212], na.rm=TRUE)
pdf(file=paste0(project_id, "_gene_gbm_tem_stats.pdf"))
print(ggplot(as.data.frame(gbm_gene_counts)) + geom_histogram(aes(x=gbm_gene_counts), binwidth = 100))
print(ggplot(as.data.frame(tem_gene_counts)) + geom_histogram(aes(x=tem_gene_counts), binwidth = 100))
print(ggplot(as.data.frame(cbind(gbm_gene_counts, tem_gene_counts))) + geom_point(aes(x=gbm_gene_counts, y=tem_gene_counts)))
print(ggplot(as.data.frame(both_counts)) + geom_histogram(aes(x=both_counts), binwidth = 10))
print(ggplot(as.data.frame(cbind(gbm_gene_counts, both_counts))) + geom_point(aes(x=gbm_gene_counts, y=both_counts)))
print(ggplot(as.data.frame(cbind(tem_gene_counts, both_counts))) + geom_point(aes(x=tem_gene_counts, y=both_counts)))
print(ggplot(as.data.frame(both_filtered_counts)) + geom_histogram(aes(x=both_filtered_counts), binwidth = 10))
print(ggplot(as.data.frame(both_resolved_counts)) + geom_histogram(aes(x=both_resolved_counts), binwidth = 10))
print(ggplot(as.data.frame(cbind(both_counts, both_filtered_counts))) + geom_point(aes(x=both_counts, y=both_filtered_counts)))
print(ggplot(as.data.frame(cbind(both_counts, both_resolved_counts))) + geom_point(aes(x=both_counts, y=both_resolved_counts)))
print(ggplot(as.data.frame(cbind(both_resolved_gbm_counts, both_resolved_tem_counts))) + geom_point(aes(x=both_resolved_gbm_counts, y=both_resolved_tem_counts)))
dev.off()
cor(cbind(gbm_gene_counts, both_counts), use="complete.obs")
#gbm_gene_counts both_counts
#gbm_gene_counts 1.0000000 0.2897491
#both_counts 0.2897491 1.0000000
cor(cbind(tem_gene_counts, both_counts), use="complete.obs")
#tem_gene_counts both_counts
#tem_gene_counts 1.0000000 0.7091566
#both_counts 0.7091566 1.0000000
cor(cbind(tem_gene_counts, gbm_gene_counts), use="complete.obs")
#tem_gene_counts gbm_gene_counts
#tem_gene_counts 1.0000000 -0.1927565
#gbm_gene_counts -0.1927565 1.0000000
cor(cbind(both_counts, both_filtered_counts), use="complete.obs")
#both_counts both_filtered_counts
#both_counts 1.000000 0.451117
#both_filtered_counts 0.451117 1.000000
cor(cbind(both_counts, both_resolved_counts), use="complete.obs")
#both_counts both_resolved_counts
#both_counts 1.0000000 0.9018878
#both_resolved_counts 0.9018878 1.0000000
# before we export this for use in GWAS analysis, let's check out whether some of these calls are shite due to low coverage, and if so, replace them with NA
# Also, we don't currently have an active annotation of unmethylated genes - we just assume it's the ones which are not annotated as methylated. Once we introduce the concept of n data, we will need a positive state of unmethylated
# First check that the dimensions of the things we've already made are equal
sum(colnames(gene_gbm_calls)==colnames(gene_tem_calls))
#[1] 1212
ncol(gene_gbm_calls)
#[1] 1212
sum(colnames(gene_gbm_calls)!=colnames(gene_tem_calls))
#[1] 0
sum(gene_gbm_calls$gene_ID==gene_tem_calls$gene_ID)
#[1] 32925
nrow(gene_gbm_calls)
#[1] 32925
sum(gene_gbm_calls$gene_ID==gene_both_overlap_filtered$gene_ID)
#[1] 32925
sum(colnames(gene_gbm_calls)==colnames(gene_both_overlap_filtered))
#[1] 1212
# We make a single matrix of all genes in all samples. We assign all to U, then we add in the G, T and Bs from above.
accession_gene_calls = as.data.frame(matrix("U", nrow=nrow(gene_gbm_calls), ncol=ncol(gene_gbm_calls)))
colnames(accession_gene_calls) = colnames(gene_gbm_calls)
rownames(accession_gene_calls) = rownames(gene_gbm_calls)
accession_gene_calls$gene_ID = gene_gbm_calls$gene_ID
accession_gene_calls[gene_gbm_calls==1] = "G"
accession_gene_calls[gene_tem_calls==1] = "T"
accession_gene_calls[gene_both_overlap_filtered==1] = "B"
#accession_gene_calls still has ~400 missing genes cf. gff.genes. These are (mostly) the C and M genes. Add them in, to get gene loci, then get rid of them again:
accession_gene_calls = merge(accession_gene_calls, gff.genes, by="gene_ID", all=TRUE)
accession_gene_calls = accession_gene_calls[!(substr(accession_gene_calls$gene_ID,3,3) %in% c("C","M")),]
relevant_gene_ranges = makeGRangesFromDataFrame(df = accession_gene_calls, start.field = "V4", end.field = "V5",seqnames.field = "V1")
levels(relevant_gene_ranges@seqnames@values) = substr(as.character(levels(relevant_gene_ranges@seqnames@values)),4,4)
relevant_gene_ranges@seqinfo@seqnames = levels(relevant_gene_ranges@seqnames@values)
# load in all the alternative, trimmed segmentation models, discard any segments with less than 3 sites, and for each gene in each sample, if it is gbm or both, check that it has a gbM segment, else set it to NA
model_dir = "segmentation_models_v2"
CG_site_ranges = makeGRangesFromDataFrame(all_samples_meth_status, seqnames.field="Chromosome", start.field="Locus", end.field = "Locus")
levels(CG_site_ranges@seqnames@values) = substr(as.character(levels(CG_site_ranges@seqnames@values)),4,4)
CG_site_ranges@seqinfo@seqnames = levels(CG_site_ranges@seqnames@values)
min_segment_sites=3
ignore_list=c()
accession_gene_calls_backup = accession_gene_calls
accession_gene_calls = accession_gene_calls_backup
#CG_site_gbM_counts = rep(0,length(CG_site_ranges))
#no_models = 0
for (this_model_file in list.files(path=model_dir)) {
this_sample = substr(this_model_file, 1, str_length(this_model_file)-16)
if (!(this_sample %in% ignore_list)) {
#no_models = no_models + 1
cat(paste0(this_sample,"\n"))
this_model = read.table(paste0(model_dir, "/", this_model_file), sep="\t", header=TRUE)
this_model.gr = makeGRangesFromDataFrame(df=this_model, seqnames.field="Chromosome", start.field="Start", end.field="End")
this_model_trimmed = this_model.gr[as.vector(table(queryHits(findOverlaps(this_model.gr, CG_site_ranges)))>=min_segment_sites)]
# These are the gbM segments, trimmed to exclude empty segments, with more than 3 CG sites
# Now find any genes marked as gbm or both which do not overlap with one of these segments
gene_model_overlaps = findOverlaps(relevant_gene_ranges, this_model_trimmed)
sample_calls = accession_gene_calls[,this_sample]
# mark all the ones which were G as X (being updated)
sample_calls[accession_gene_calls[,this_sample]=="G"] = "X"
# for the ones which overlap a gene and were originally G, set them back to G
sample_calls=cbind(sample_calls, rep(NA, length(sample_calls)))
sample_calls[unique(queryHits(gene_model_overlaps)),2] = "G"
sample_calls[-unique(queryHits(gene_model_overlaps)),2] = "X"
accession_gene_calls[,this_sample] = ifelse(sample_calls[,1]=="X", ifelse(sample_calls[,2]=="G", "G", "I"), ifelse(sample_calls[,2]=="G", ifelse(sample_calls[,1]=="B","B","I"), ifelse(sample_calls[,1]=="B","I",sample_calls[,1])))
#model_site_olaps = findOverlaps(this_model_trimmed, CG_site_ranges)
#CG_site_gbM_counts[subjectHits(model_site_olaps)] = CG_site_gbM_counts[subjectHits(model_site_olaps)] + 1
#if (no_models == 1) {
# overlap_model = this_model_trimmed
#} else {
# overlap_model = union(overlap_model, this_model_trimmed, ignore.strand=TRUE)
#}
}
}
# What do the Col-0 calls look like now?
View(accession_gene_calls[,c("gene_ID","SRX446038", "SRX446039", "SRX446040", "SRX248644")])
# check the U genes now. For each gene in each accession, where it is marked as U, check that it has at least 3 sites in all_samples_meth_status set to U, else set it to NA
accession_gene_calls_backup2 = accession_gene_calls
accession_gene_calls = accession_gene_calls_backup2
min_U_sites = 3
#CG_site_gbM_counts = rep(0,length(CG_site_ranges))
#no_models = 0
for (this_model_file in list.files(path=model_dir)) {
this_sample = substr(this_model_file, 1, str_length(this_model_file)-16)
if (!(this_sample %in% ignore_list)) {
#no_models = no_models + 1
cat(paste0(this_sample,"\n"))
relevant_gene_site_olaps = findOverlaps(relevant_gene_ranges, CG_site_ranges[!is.na(all_samples_meth_status[,this_sample]) & (all_samples_meth_status[,this_sample]=="U")])
sample_gene_U_counts = table(queryHits(relevant_gene_site_olaps))
sample_calls = accession_gene_calls[,this_sample]
# mark all the ones which were U as X (being updated)
sample_calls[accession_gene_calls[,this_sample]=="U"] = "X"
# for the ones which overlap sufficient U sites, and were originally U, set them back to U
sample_calls=cbind(sample_calls, rep(NA, length(sample_calls)))
sample_calls[as.numeric(names(sample_gene_U_counts)),2] = sample_gene_U_counts
sample_calls[-as.numeric(names(sample_gene_U_counts)),2] = 0
accession_gene_calls[,this_sample] = ifelse(sample_calls[,1]=="X", ifelse(sample_calls[,2]>=min_U_sites, "U", "I"), sample_calls[,1])
#if (no_models == 1) {
# overlap_model = this_model_trimmed
#} else {
# overlap_model = union(overlap_model, this_model_trimmed, ignore.strand=TRUE)
#}
}
}
# Export the matrices to use in GWAS analysis
# These are the original matrices, where wee assume complete data, no genes overflowed with empty gbM segments etc.
write.table(gene_gbm_calls, file=paste0(project_id,"_gene_gbm_calls.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
write.table(gene_tem_calls, file=paste0(project_id,"_gene_tem_calls.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
write.table(gene_both_overlap_filtered, file=paste0(project_id,"_gene_both_calls.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
#gene_gbm_calls = read.table(file=paste0(project_id, "_gene_gbm_calls.txt"), sep="\t", header=TRUE)
#gene_tem_calls = read.table(file=paste0(project_id, "_gene_tem_calls.txt"), sep="\t", header=TRUE)
#gene_both_overlap_filtered = read.table(file=paste0(project_id, "_gene_both_calls.txt"), sep="\t", header=TRUE)
# This is the new approach where we filter out any gbM genes without 3 M sites, or U genes without 3 U sites
write.table(accession_gene_calls[,1:ncol(gene_gbm_calls)], file=paste0(project_id,"_gene_gbm_tem_umr_calls_0.txt"), sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
# Now we try some different variations where we vary the gbM and U site count thresholds, and also, whether we treat very short gbM segments as missing data or U
# We consider the above method variant 0
# Variant 1 is as per variant 0 but with threshold of 2 rather than 3 for no sites per gbM segment
# load in all the alternative, trimmed segmentation models, discard any segments with less than 3 sites, and for each gene in each sample, if it is gbm or both, check that it has a gbM segment, else set it to NA
model_dir = "segmentation_models_v2"
CG_site_ranges = makeGRangesFromDataFrame(all_samples_meth_status, seqnames.field="Chromosome", start.field="Locus", end.field = "Locus")
levels(CG_site_ranges@seqnames@values) = substr(as.character(levels(CG_site_ranges@seqnames@values)),4,4)