-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffMeth2.py
254 lines (227 loc) · 6.34 KB
/
diffMeth2.py
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
#!/usr/bin/python
# JMG 4/26/16
# Testing regions for differential methylation.
# Version 2: comparing sets of samples
import sys, math
#scip = 0 # boolean for scipy import
#try:
# from scipy import stats
#except ImportError:
# print 'Warning! The scipy module is not installed'
# print ' (see www.scipy.org/install.html).'
# print ' All p-values will be reported as "NA".'
# scip = 1
def usage():
print "Usage: python diffMeth2.py [options] -i <input> -o <output> \ \n\
<groupList> \n\
<groupList> Comma-separated list of sample names (as found in \n\
the header of <input>) \n\
<input> File listing genomic regions and methylation results \n\
(output from combineRegions2.py) \n\
Options (whether or not to report a region): \n\
-c <int> Minimum number of CpGs in a region (def. 1) \n\
-d <float> Minimum methylation difference between sample groups \n\
([0-1]; def. 0 [all results reported]) "
#-p <float> Maximum p-value ([0-1]; def. 1 [all results reported]) "
sys.exit(-1)
def openFile(fname):
'''
Open a file for reading.
'''
try:
f = open(fname, 'rU')
except IOError:
sys.stderr.write('Error! Cannot open %s for reading\n' % fname)
sys.exit(-1)
return f
def getInt(arg):
'''
Convert given argument to int.
'''
try:
val = int(arg)
except ValueError:
sys.stderr.write('Error! Cannot convert %s to int\n' % arg)
sys.exit(-1)
return val
def getFloat(arg, minVal = None, maxVal = None):
'''
Convert given argument to float. Ensure it is within a
supplied range, if applicable.
'''
try:
val = float(arg)
except ValueError:
sys.stderr.write('Error! Cannot convert %s to float\n' % arg)
sys.exit(-1)
if (minVal != None and val < minVal) or \
(maxVal != None and val > maxVal):
sys.stderr.write('Error! Value %f is outside of range [%f,%f]\n' \
% (val, minVal, maxVal))
sys.exit(-1)
return val
def getSample(csv):
'''
Return a list of samples.
'''
arr = []
for tok in csv.split(','):
arr.append(tok)
return arr
def saveIndexes(fIn, samples):
'''
Find indexes for samples in input file.
'''
idxs = [] # for indexes
res = [] # for ordered sample names
for i in range(len(samples)):
idxs.append([])
res.append([])
idxExtra = [] # for gene, distance, location
resExtra = []
# get indexes
header = fIn.readline().rstrip()
spl = header.split('\t')
for i in range(len(spl)):
for j in range(len(samples)):
if spl[i] in samples[j]:
idxs[j].append(i)
res[j].append(spl[i])
if spl[i] in ['gene', 'distance', 'location']:
idxExtra.append(i)
resExtra.append(spl[i])
for i in range(len(idxs)):
if len(idxs[i]) != len(samples[i]):
sys.stderr.write('Error! Cannot find all sample names in input file\n')
sys.exit(-1)
# construct header for output file
head = spl[:4]
for i in range(len(res)):
head += [','.join(res[i])]
head += resExtra
for i in range(len(res)-1):
for j in range(i+1, len(res)):
head += ['->'.join([str(i), str(j)])]
return idxs, idxExtra, head
def calcAvg(spl, idxs):
'''
Calculate average for a subset of values in a list.
'''
avg = 0.0
sample = 0
for idx in idxs:
if spl[idx] == 'NA':
continue
avg += getFloat(spl[idx])
sample += 1
if sample:
avg /= sample
return avg, sample
def processLine(line, cpg, idxs, idxExtra):
'''
Calculate mean difference and p-value.
'''
spl = line.split('\t')
if len(spl) < max([max(idx) for idx in idxs]):
sys.stderr.write('Error! Poorly formatted record:\n%s' % line)
if int(spl[3]) < cpg:
return [], 0 # fewer than min. CpGs
# calculate averages
avg = []
for idx in idxs:
av, sample = calcAvg(spl, idx)
if sample:
avg.append(av)
else:
avg.append('NA')
# calculate differences
diff = []
maxDiff = -1
pr = 0 # boolean for printing line
for i in range(len(avg)-1):
for j in range(i+1, len(avg)):
if avg[i] == 'NA' or avg[j] == 'NA':
diff.append('NA')
else:
dif = avg[j] - avg[i]
diff.append(dif)
if abs(dif) > maxDiff:
maxDiff = abs(dif)
pr = 1
if not pr:
return [], 0 # no diffs for these groups
# calculate p-value (Welch's t-test)
#if len(sample1) < 2 or len(sample2) < 2 or scip:
# pval = 'NA'
#elif diff == 0:
# pval = 1
#elif max(sample1) - min(sample1) == 0 and \
# max(sample2) - min(sample2) == 0:
# pval = 0
#else:
# pval = stats.ttest_ind(sample1, sample2, equal_var=False)[1]
# construct record for output file
res = spl[:4]
for av in avg:
res.append(str(av))
for idx in idxExtra:
res.append(spl[idx])
for dif in diff:
res.append(str(dif))
return res, maxDiff
def main():
'''
Main.
'''
# Default parameters
cpg = 0 # min. number of CpGs
d = 0 # min. methylation difference
#p = 1 # max. p-value
# get command-line args
args = sys.argv[1:]
if len(args) < 2: usage()
fIn = None
fOut = None
samples = []
i = 0
while i < len(args):
if args[i] == '-i':
fIn = openFile(args[i+1])
elif args[i] == '-o':
fOut = open(args[i+1], 'w')
elif args[i] == '-c':
cpg = getInt(args[i+1])
elif args[i] == '-d':
d = getFloat(args[i+1], 0, 1)
#elif args[i] == '-p':
#p = getFloat(args[i+1], 0, 1)
elif args[i] == '-h':
usage()
else:
sample = getSample(args[i])
samples.append(sample)
if args[i][0] != '-':
i += 1
else:
i += 2
# check for errors
if fIn == None or fOut == None:
sys.stderr.write('Error! Must specify input and output files\n')
usage()
if len(samples) < 2:
sys.stderr.write('Error! Must have at least two sets of samples\n')
usage()
# save indexes of samples from header
idxs, idxExtra, res = saveIndexes(fIn, samples)
fOut.write('\t'.join(res) + '\n')
# process file
for line in fIn:
res, maxDiff = processLine(line.rstrip(), cpg, \
idxs, idxExtra)
# print result
if res and maxDiff >= d:
fOut.write('\t'.join(res) + '\n')
fIn.close()
fOut.close()
if __name__ == '__main__':
main()