-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_methyl.Rmd
More file actions
179 lines (122 loc) · 3.89 KB
/
Copy pathdiff_methyl.Rmd
File metadata and controls
179 lines (122 loc) · 3.89 KB
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
---
title: "R Notebook"
output: html_notebook
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir = "/cluster/tufts/kuperwasser01/mseraj01/methylseq-pipeline/output/methyldackel")
```
```{r Library}
# install.packages("devtools")
# devtools::install_github("al2na/methylKit") # if not installed
library(methylKit, quietly = T)
library(tidyverse, quietly = T)
library(GenomicFeatures)
library(AnnotationHub)
library(genomation)
library(ensembldb)
library(txdbmaker)
library(BiocManager)
library(TxDb.Hsapiens.UCSC.hg38.knownGene)
```
```{r CpG}
# list your methylKit files (adjust paths)
files_CpG <- list.files(pattern = "CpG", full.names = TRUE)
files <- gsub("./", "", basename(files_CpG))
# sample IDs and treatment vector: 0 = control, 1 = treated
sample.ids <- gsub(".markdup.sorted_CpG.methylKit", "", basename(files_CpG))
treatment <- ifelse(grepl("DMSO", sample.ids), 0, 1)
myobj.list <- lapply(seq_along(files_CpG), function(i) {
methRead(
location = files_CpG[i],
sample.id = sample.ids[i],
assembly = "hg38",
treatment = treatment[i],
header = TRUE,
)
})
myobj <- new("methylRawList", myobj.list)
```
```{r Coverage}
# Remove low/high coverage outliers
filtered <- filterByCoverage(myobj, lo.count=10, lo.perc=NULL, hi.count=NULL, hi.perc=99.9)
# Optionally normalize coverage across samples
normed <- normalizeCoverage(filtered)
normed@treatment <- treatment
```
```{r Meth Prep}
#meth <- methylKit::unite(normed,destrand = TRUE,)
#saveRDS(meth, "meth.rds")
meth <- readRDS("/cluster/tufts/kuperwasser01/mseraj01/methylseq-pipeline/output/methyldackel/meth.rds")
```
```{r Differentially Methylated Prep}
main_chr <- c(as.character(1:22), "X", "Y", "MT")
#myDiff <- calculateDiffMeth(meth, test = "F")
#saveRDS(myDiff, "myDiff.rds")
myDiff <- readRDS("../methyldackel/myDiff.rds")
myDiff <- myDiff[myDiff$chr %in% main_chr, ]
# Get all differentially methylated CpGs (q < 0.01)
diff_all <- getMethylDiff(myDiff, difference = 25, qvalue = 0.01)
# Hypermethylated in treated
diff_hyper <- getMethylDiff(myDiff, difference = 25, qvalue = 0.01, type = "hyper")
# Hypomethylated in treated
diff_hypo <- getMethylDiff(myDiff, difference = 25, qvalue = 0.01, type = "hypo")
```
```{r Differentially Methylated Analysis}
#diff meth per chromosome
diffMethPerChr(myDiff,plot=TRUE,qvalue.cutoff=0.01, meth.cutoff=25)
```
```{r Correlation and Clustering}
#Correlation
getCorrelation(meth,plot=TRUE)
# Clustering dendrogram (1)
d <- dist(t(percMethylation(meth)))
hc <- hclust(d, method = "ward.D")
plot(hc, labels = getSampleID(meth))
#Clustering dendrogram (2)
clusterSamples(
meth,
dist = "correlation",
method = "ward.D",
plot = TRUE
)
```
```{r PCA - meth}
PCASamples(meth, adj.lim = c(0.3, 0.1))
```
```{r}
# Load prebuilt TxDb
txdb <- TxDb.Hsapiens.UCSC.hg38.knownGene
# Create gene annotation object
gene.obj <- genomation::readTranscriptFeatures(txdb)
# Inspect
gene.obj
```
```{r}
meth_matrix <- percMethylation(meth)
row_var <- apply(meth_matrix, 1, var)
meth_top <- meth[row_var > quantile(row_var, 0.9), ]
PCASamples(meth_top)
```
```{r}
# methylation percentages
meth_data <- percMethylation(meth) # rows = CpGs, cols = samples
# your treatment vector (0 = control, 1 = treated)
treatment <- c(1, 1, 1, 1, 0) # adjust to your dataset
# mean methylation per group
meth_ctrl <- rowMeans(meth_data[, treatment == 0, drop=FALSE])
meth_treat <- rowMeans(meth_data[, treatment == 1, drop=FALSE])
# methylation difference
meth_diff <- meth_treat - meth_ctrl
# keep only variable CpGs for PCA
vars <- apply(meth_data, 1, var)
meth_var <- meth[vars > 0.01, ]
# optional: subset by DMCs (difference > 25%)
meth_top <- meth_var[abs(meth_diff[vars > 0.01]) > 25, ]
# subsample for plotting
set.seed(123)
meth_sub <- meth_top[sample(1:nrow(meth_top), min(50000, nrow(meth_top))), ]
# PCA plot
PCASamples(meth_sub)
```