-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfindDMRs.r
402 lines (373 loc) · 12.6 KB
/
findDMRs.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
#!/usr/bin/Rscript
# John M. Gaspar ([email protected])
# Dec. 2016
# Finding differentially methylated regions from a table
# of counts produced by bisulfite sequencing data.
# Underlying statistics are performed using the R package 'DSS'
# (https://bioconductor.org/packages/release/bioc/html/DSS.html).
version <- '0.3'
copyright <- 'Copyright (C) 2016 John M. Gaspar ([email protected])'
printVersion <- function() {
cat('findDMRs.r from DMRfinder, version', version, '\n')
cat(copyright, '\n')
q()
}
usage <- function() {
cat('Usage: Rscript findDMRs.r [options] -i <input> -o <output> \\
<groupList1> <groupList2> [...]
-i <input> File listing genomic regions and methylation counts
-o <output> Output file listing methylation results
<groupList> Comma-separated list of sample names (at least two
such lists must be provided)
Options:
-n <str> Comma-separated list of group names
-k <str> Column names of <input> to copy to <output> (comma-
separated; def. "chr, start, end, CpG")
-s <str> Column names of DSS output to include in <output>
(comma-separated; def. "mu, diff, pval")
-c <int> Min. number of CpGs in a region (def. 3)
-d <float> Min. methylation difference between sample groups
([0-1]; def. 0.10)
-p <float> Max. p-value ([0-1]; def. 0.05)
-q <float> Max. q-value ([0-1]; def. 1)
-up Report only regions hypermethylated in later group
-down Report only regions hypomethylated in later group
-t <int> Report regions with at least <int> comparisons
that are significant (def. 1)
')
q()
}
# error function for missing sample name
missingSample <- function(sample, infile, names) {
cat('Error! Missing information for sample "', sample,
'" from input file ', infile, '.\n',
' Valid sample names are:', sep='')
nIdx <- xIdx <- NULL
for (n in names) {
spl <- strsplit(n, '-')[[1]]
if (length(spl) < 2) { next }
if (spl[length(spl)] == 'N') {
if (spl[-length(spl)] %in% xIdx) {
cat(' ', spl[-length(spl)], sep='')
} else {
nIdx <- c(nIdx, spl[-length(spl)])
}
} else if (spl[length(spl)] == 'X') {
if (spl[-length(spl)] %in% nIdx) {
cat(' ', spl[-length(spl)], sep='')
} else {
xIdx <- c(xIdx, spl[-length(spl)])
}
}
}
cat('\n')
q()
}
# default args/parameters
infile <- outfile <- groups <- NULL
names <- list() # list of sample names
minCpG <- 3 # min. number of CpGs
minDiff <- 0.10 # min. methylation difference
maxPval <- 0.05 # max. p-value
maxQval <- 1 # max. q-value (fdr)
up <- down <- F # report NA/hyper-/hypo- methylated results
tCount <- 1 # min. number of significant comparisons
verbose <- F # verbose option
keep <- c('chr', 'start', 'end', 'CpG') # columns of input to keep
dss <- c('chr', 'pos', 'mu', 'diff', 'pval') # columns of DSS output to keep
# get CL args
args <- commandArgs(trailingOnly=T)
i <- 1
while (i <= length(args)) {
if (substr(args[i], 1, 1) == '-') {
if (args[i] == '-h' || args[i] == '--help') {
usage()
} else if (args[i] == '--version') {
printVersion()
} else if (args[i] == '-v') {
verbose <- T
} else if (args[i] == '-up') {
up <- T
} else if (args[i] == '-down') {
down <- T
} else if (i < length(args)) {
if (args[i] == '-i') {
infile <- args[i + 1]
} else if (args[i] == '-o') {
outfile <- args[i + 1]
} else if (args[i] == '-n') {
temp <- strsplit(args[i + 1], '[ ,]')[[1]]
groups <- temp[nchar(temp) > 0]
} else if (args[i] == '-k') {
temp <- strsplit(args[i + 1], '[ ,]')[[1]]
keep <- c(keep, temp[nchar(temp) > 0])
} else if (args[i] == '-s') {
temp <- strsplit(args[i + 1], '[ ,]')[[1]]
dss <- c(dss, temp[nchar(temp) > 0])
} else if (args[i] == '-c') {
minCpG <- as.integer(args[i + 1])
} else if (args[i] == '-d') {
minDiff <- as.double(args[i + 1])
} else if (args[i] == '-p') {
maxPval <- as.double(args[i + 1])
} else if (args[i] == '-q') {
maxQval <- as.double(args[i + 1])
dss <- c(dss, 'fdr')
} else if (args[i] == '-t') {
tCount <- as.integer(args[i + 1])
} else {
cat('Error! Unknown parameter:', args[i], '\n')
usage()
}
i <- i + 1
} else {
cat('Error! Unknown parameter with no arg:', args[i], '\n')
usage()
}
} else {
temp <- strsplit(args[i], '[ ,]')[[1]]
names <- c(names, list(temp[nchar(temp) > 0]))
}
i <- i + 1
}
# check for parameter errors
if (is.null(infile) || is.null(outfile)) {
cat('Error! Must specify input and output files\n')
usage()
}
if (length(names) < 2) {
cat('Error! Must specify at least two groups of samples\n')
usage()
}
# check DSS installation
if (verbose) {
cat('Loading DSS package\n')
}
if (!suppressMessages(suppressWarnings(require(DSS)))) {
stop('Required package "DSS" not installed.\n',
' For installation information, please see:\n',
' https://bioconductor.org/packages/release/bioc/html/DSS.html\n')
}
# check for I/O errors, repeated columns
input <- tryCatch( file(infile, 'r'), warning=NULL,
error=function(e) stop('Cannot read input file ',
infile, '\n', call.=F) )
output <- tryCatch( file(outfile, 'w'), warning=NULL,
error=function(e) stop('Cannot write to output file ',
outfile, '\n', call.=F) )
if (any(duplicated(unlist(names)))) {
stop('Sample(s) repeated in different groups: ',
paste(unique(unlist(names)[duplicated(unlist(names))]),
collapse=', '), '\n')
}
keep <- unique(keep)
dss <- unique(dss)
# group samples into a named list
samples <- list()
for (i in 1:length(names)) {
if (! is.null(groups) && i <= length(groups)) {
group <- groups[i]
} else {
group <- paste(names[i][[1]], collapse='_')
}
if (group %in% names(samples)) {
stop('Duplicated group name: ', group)
}
samples[[ group ]] <- names[i][[1]]
}
# load data, check for errors
if (verbose) {
cat('Loading methylation data from', infile, '\n')
}
data <- read.csv(input, sep='\t', header=T, check.names=F)
if (any( ! keep %in% colnames(data))) {
stop('Missing column(s) in input file ', infile, ': ',
paste(keep[! keep %in% colnames(data)], collapse=', '), '\n')
}
if (colnames(data)[1] != 'chr') {
stop('Improperly formatted input file ', infile, ':\n',
' Must have "chr" as first column\n')
}
# determine columns for samples
idx <- list()
idx[[ 'N' ]] <- list()
idx[[ 'X' ]] <- list()
for (i in names(samples)) {
idx[[ 'N' ]][[ i ]] <- rep(NA, length(samples[[ i ]]))
idx[[ 'X' ]][[ i ]] <- rep(NA, length(samples[[ i ]]))
}
for (i in names(samples)) {
for (j in 1:length(samples[[ i ]])) {
for (k in 1:ncol(data)) {
spl <- strsplit(colnames(data)[k], '-')[[1]]
if (length(spl) < 2) { next }
if (spl[-length(spl)] == samples[[ i ]][ j ]) {
if (spl[length(spl)] == 'N') {
idx[[ 'N' ]][[ i ]][ j ] <- k
} else if (spl[length(spl)] == 'X') {
idx[[ 'X' ]][[ i ]][ j ] <- k
}
}
}
if ( is.na(idx[[ 'N' ]][[ i ]][ j ])
|| is.na(idx[[ 'X' ]][[ i ]][ j ]) ) {
missingSample(samples[[ i ]][ j ], infile, colnames(data))
}
}
}
# for each sample, create data frames to meet DSS requirements
frames <- list()
for (i in names(samples)) {
for (j in 1:length(samples[[ i ]])) {
tab <- data.frame('chr'=data$chr, 'pos'=data$start,
'N'=data[, idx[[ 'N' ]][[ i ]][ j ] ],
'X'=data[, idx[[ 'X' ]][[ i ]][ j ] ])
frames[[ samples[[ i ]][ j ] ]] <- tab
}
}
# perform DML pairwise tests using DSS
bsdata <- makeBSseqData(frames, names(frames))
res <- data[, keep] # results table
mat <- matrix(nrow=nrow(res), ncol=length(samples)*(length(samples)-1)/2)
# matrix of booleans: does region meet threshold(s) for each comparison
comps <- c() # group comparison strings
for (i in 1:(length(samples)-1)) {
for (j in (i+1):length(samples)) {
# perform DML test
comp <- paste(names(samples)[i], names(samples)[j], sep='->')
comps <- c(comps, comp)
if (verbose) {
cat('Comparing group "', names(samples)[i],
'" to group "', names(samples)[j], '"\n ', sep='')
}
if (length(samples[[ i ]]) < 2 || length(samples[[ j ]]) < 2) {
# without replicates, must set equal.disp=T
if (verbose) {
dml <- DMLtest(bsdata, group1=samples[[ i ]], group2=samples[[ j ]],
equal.disp=T)
} else {
sink('/dev/null')
dml <- DMLtest(bsdata, group1=samples[[ i ]], group2=samples[[ j ]],
equal.disp=T)
sink()
}
} else {
if (verbose) {
dml <- DMLtest(bsdata, group1=samples[[ i ]], group2=samples[[ j ]])
} else {
sink('/dev/null')
dml <- DMLtest(bsdata, group1=samples[[ i ]], group2=samples[[ j ]])
sink()
}
}
# make sure necessary columns are present, remove extraneous
col <- colnames(dml)
if (any( ! dss %in% col & ! paste(dss, '1', sep='') %in% col)) {
stop('Missing column(s) from DSS result: ',
paste(dss[! dss %in% col & ! paste(dss, '1', sep='') %in% col],
collapse=', '), '\n')
}
dml[, ! col %in% dss & ! substr(col, 1, nchar(col)-1) %in% dss] <- NULL
# add results to res table
start <- ncol(res) + 1
res <- suppressWarnings( merge(res, dml,
by.x=c('chr', 'start'), by.y=c('chr', 'pos'), all.x=T) )
# determine if rows meet threshold(s)
if (maxQval < 1) {
mat[, length(comps)] <- ! (is.na(res[, 'diff'])
| abs(res[, 'diff']) < minDiff
| (up & res[, 'diff'] > 0) | (down & res[, 'diff'] < 0)
| is.na(res[, 'pval']) | res[, 'pval'] > maxPval
| is.na(res[, 'fdr']) | res[, 'fdr'] > maxQval)
} else {
mat[, length(comps)] <- ! (is.na(res[, 'diff'])
| abs(res[, 'diff']) < minDiff
| (up & res[, 'diff'] > 0) | (down & res[, 'diff'] < 0)
| is.na(res[, 'pval']) | res[, 'pval'] > maxPval)
}
# add groups to column names
for (k in start:ncol(res)) {
col <- colnames(res)[k]
if (substr(col, nchar(col), nchar(col)) == '1') {
colnames(res)[k] <- paste(names(samples)[i],
substr(col, 1, nchar(col)-1), sep=':')
} else if (substr(col, nchar(col), nchar(col)) == '2') {
colnames(res)[k] <- paste(names(samples)[j],
substr(col, 1, nchar(col)-1), sep=':')
} else {
colnames(res)[k] <- paste(comp, col, sep=':')
}
}
}
}
# filter regions based on CpG sites and mat matrix
if (verbose) {
cat('Producing output file', outfile, '\n')
}
res <- res[res[, 'CpG'] >= minCpG & rowSums(mat) >= tCount, ]
# for repeated columns, average the values
repCols <- c()
sampleCols <- c()
groupCols <- c()
for (i in 1:ncol(res)) {
if (i %in% repCols) { next }
# find duplicated columns
repNow <- c(i)
if (i < ncol(res)) {
for (j in (i + 1):ncol(res)) {
if (colnames(res)[i] == colnames(res)[j]) {
repNow <- c(repNow, j)
}
}
}
# average duplicates
if (length(repNow) > 1) {
res[, i] <- rowMeans(res[, repNow], na.rm=T)
repCols <- c(repCols, repNow)
sampleCols <- c(sampleCols, colnames(res)[i])
} else if (! colnames(res)[i] %in% keep) {
groupCols <- c(groupCols, colnames(res)[i])
}
}
res <- res[, c(keep, sampleCols, groupCols)]
# limit results to 7 digits; reverse sign on diffs
options(scipen=999)
for (col in c(sampleCols, groupCols)) {
spl <- strsplit(col, ':')[[1]]
if (spl[length(spl)] == 'diff') {
res[, col] <- -round(res[, col], digits=7)
} else {
res[, col] <- round(res[, col], digits=7)
}
}
# sort chromosome names by number/letter
level <- levels(res$chr)
intChr <- strChr <- intLev <- strLev <- c()
for (i in 1:length(level)) {
if (substr(level[i], 1, 3) == 'chr') {
sub <- substr(level[i], 4, nchar(level[i]))
if (!is.na(suppressWarnings(as.integer(sub)))) {
intChr <- c(intChr, as.numeric(sub))
} else {
strChr <- c(strChr, sub)
}
} else {
sub <- level[i]
if (!is.na(suppressWarnings(as.integer(sub)))) {
intLev <- c(intLev, as.numeric(sub))
} else {
strLev <- c(strLev, sub)
}
}
}
# put numeric chroms first, then strings
chrOrder <- c(paste('chr', levels(factor(intChr)), sep=''),
levels(factor(intLev)),
paste('chr', levels(factor(strChr)), sep=''),
levels(factor(strLev)))
# write output results
write.table(res[order(match(res$chr, chrOrder), res$start), ],
output, sep='\t', quote=F, row.names=F)
if (verbose) {
cat('Regions reported:', nrow(res), '\n')
}