-
Notifications
You must be signed in to change notification settings - Fork 0
/
messner.py
executable file
·347 lines (288 loc) · 9.35 KB
/
messner.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
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
#!/usr/bin/env python
import sys, os, re, os.path, getopt
version = 0.2
debug = False
PEAK_THRESHOLD = 3.0
filename_pat = None
#simwalk_filename_pat = re.compile("^SCORE\-(\d\d)\_(\d+)\.ALL$")
simwalk_line_pat = re.compile("^\s*\,\s*(\-?\d+\.\d+)\s*\,\s*(\-?\d+\.\d+)\s*\,\s*\,\s*(\-?\d+\.\d+)\s*\,\s*(\-?\d+\.\d+)")
simwalk_line_pat2 = re.compile("^(\-\d+)")
#gh_filename_pat = re.compile("^gh_(\d+).out$")
#gh_line_pat = re.compile("^\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)$")
gh_line_pat = re.compile("^\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*\((\-?\d+\.\d+)\,\ (\-?\d+\.\d+)\)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)")
#al_line_pat = re.compile("^\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(rs\d+)")
al_line_pat = re.compile("^\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(\-?\d+\.\d+)\s*(rs\d+)")
mode = "simwalk"
map_format = "decode"
last_neg_to_pos = None
look_for_neg_to_pos = True
peak = False
peak_value = -1
last = 0
marker_map = {}
physical_positions = {}
phys2marker = {}
mapfile = None
def usage() :
print >> sys.stderr, "usage: ./%s -m map_file [-sgh] [-l lod threshold]" % sys.argv[0]
print >> sys.stderr, "\t-m, --map\t\tmap file location (MANDATORY)"
print >> sys.stderr, "\t-x, --mapformat:\tformat of map file [decode (default), illumina]"
print >> sys.stderr, "\t-l, --lod:\t\tset lod threshold, (default = 3)"
print >> sys.stderr, "\t-s, --simwalk:\t\tparse simwalk output files (cannot be used in combination with -g or -a)"
print >> sys.stderr, "\t-g, --genehunter:\tparse genehunter output files (cannot be used in combination with -s or -a)"
print >> sys.stderr, "\t-a, --allegro:\t\tparse allegro output files (cannot be used in combination with -g or -s)"
print >> sys.stderr, "\t-f, --force:\t\tignore warnings and carry on regardless\
\n\t\t\t\t(use of this option implies you are no longer doing science...)"
print >> sys.stderr, "\t-h, --help:\t\tsee usage info\n"
def find_phys(positions, chromosome, pos):
#print "find_phys: %f, chr: %d" % (pos, chromosome)
pos *= 1e6
poz = positions[chromosome]
index = len(poz) / 2
next_jump = index / 2
while 1 :
phys1 = poz[index]
next_jump /= 2
if next_jump < 1 :
next_jump = 1
#else :
# print next_jump
if pos < phys1 :
index -= next_jump
else :
index += next_jump
if index == len(poz) :
return poz[-1], poz[-1]
if index == 0 :
return poz[0], poz[0]
try :
phys2 = poz[index]
except Exception, e:
print "==========="
print index
print next_jump
print len(poz)
print str(e)
sys.exit(-1)
if (next_jump == 1) :
if (phys1 < phys2) and (phys1 < pos) and (phys2 > pos):
return phys1,phys2
if (phys2 < phys1) and (phys2 < pos) and (phys1 > pos):
return phys2,phys1
# parse command line args
try:
opts, args = getopt.getopt(sys.argv[1:], "hm:x:l:vsgaf", ["help","map=","mapformat=","lod=","verbose","simwalk","genehunter","allegro","force"])
except getopt.GetoptError, err:
print >> sys.stderr, str(err)
usage()
sys.exit(-1)
output = None
verbose = False
genehunter_flag = False
simwalk_flag = False
allegro_flag = False
formats = ("illumina","decode")
format = "decode"
force_flag = False
for o, a in opts:
if o == "-v":
debug = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-f","--force"):
force_flag = True
elif o in ("-m", "--map"):
mapfile = a
elif o in ("-s","--simwalk"):
simwalk_flag = True
elif o in ("-g","--genehunter"):
genehunter_flag = True
elif o in ("-a","--allegro"):
allegro_flag = True
elif o in ("-x","--mapformat"):
format = a.lower()
if format not in formats :
print >> sys.stderr, "Error: illegal map file format (see -h for details): %s" % format
sys.exit(-1)
elif o in ("-l","--lod"):
try :
PEAK_THRESHOLD = float(a)
except :
print >> sys.stderr, "Error: lod score must be a number"
sys.exit(-1)
else:
assert False, "unhandled option"
if (not (simwalk_flag ^ genehunter_flag ^ allegro_flag)) :
print >> sys.stderr, "%s: only one output file format can be specified" % sys.argv[0]
sys.exit(-1)
elif genehunter_flag :
mode = "genehunter"
elif allegro_flag :
mode = "allegro"
else :
mode = "simwalk"
if mapfile == None :
print >> sys.stderr, "Error: map file not specified"
usage()
sys.exit(-1)
try :
mf = open(mapfile)
except IOError, err:
print >> sys.stderr, str(err)
sys.exit(-1)
# parse map file
header = mf.readline()
num_columns = len(header.split())
if (((num_columns == 4 and format != "illumina")) or ((num_columns == 8 and format != "decode"))) and (force_flag != True):
print >> sys.stderr, "warning: are you sure %s is in %s format?" % (mapfile,format)
print >> sys.stderr, "\tif I'm wrong about this, then rerun with the -f or --force flag to run anyway\n"
sys.exit(-1)
for line in mf :
data = line.split()
if format == "decode":
probe_id = data[1]
phys_pos = data[3]
rs_id = data[4]
chromosome = data[0]
elif format == "illumina":
probe_id = data[0]
phys_pos = data[3]
rs_id = data[0]
chromosome = data[2]
try :
if not physical_positions.has_key(int(chromosome)) :
physical_positions[int(chromosome)] = []
physical_positions[int(chromosome)].append(int(phys_pos))
marker_map[probe_id] = (phys_pos, rs_id)
phys2marker[(int(chromosome), int(phys_pos))] = probe_id
except ValueError :
continue
mf.close()
#physical_positions.sort()
#for i in physical_positions :
# print i
#
#sys.exit(-1)
# find chromosome directories
chromosomes = []
for chrdir in os.listdir('.') :
if os.path.isdir(chrdir) and re.match("^c\d\d$", chrdir) :
chromosomes.append(int(chrdir[1:]))
# for each chromosomes directory... (in order)
for c in sorted(chromosomes) :
chrdir = "c%02d" % c
# find all the score files
if mode == "simwalk" :
filename_pat = re.compile("^SCORE\-%02d\_(\d+)\.ALL$" % c)
elif mode == "genehunter" :
filename_pat = re.compile("^gh_(\d+).out$")
elif mode == "allegro" :
filename_pat = re.compile("^linall_mpt\.(\d+)$")
filelist = filter(lambda x : filename_pat.match(x), os.listdir(chrdir))
results_indices = map(lambda x : int(filename_pat.match(x).group(1)), filelist)
if len(results_indices) == 0 :
print >> sys.stderr, "no results files in %s/ (are you sure this is output from %s?)" % (chrdir, mode)
continue
largest = max(results_indices)
gh_xsum = 0.0
if mode == "allegro":
largest = 1
for i in range(1, largest + 1) :
filename = chrdir + os.sep
if mode == "simwalk":
filename += "SCORE-%02d_%d.ALL" % (c,i)
elif mode == "genehunter":
filename += "gh_%d.out" % i
elif mode == "allegro":
filename += "param_mpt.%02d" % c
#filename += "linall_mpt.%02d" % c
if debug :
print filename
try :
f = open(filename)
except :
print >> sys.stderr, "could not open file: %s" % filename
sys.exit(-1)
lines = f.readlines()
f.close()
# find markers and LOD scores
tmp = []
gh_max_x = 0.0
for line in lines :
if mode == "simwalk":
# simwalk LOD score
m = simwalk_line_pat.match(line)
if m :
tmp.append(m.groups())
continue
# simwalk marker name
m = simwalk_line_pat2.match(line)
elif mode == "genehunter":
line.replace("-INFINITY", "-99.99")
line.replace("NaN", "-0.5")
line.replace("nan", "-0.5")
m = gh_line_pat.match(line)
elif mode == "allegro":
m = al_line_pat.match(line)
if m :
if mode == "simwalk" :
marker = m.group(1)
elif mode == "genehunter" :
tmp.append(m.groups())
marker = m.group(1)
elif mode == "allegro":
tmp.append(m.groups())
marker = m.group(5)
for t in tmp:
pos,loc = map(float, t[:2])
if mode == "genehunter" :
if gh_max_x < pos :
gh_max_x = pos
pos += gh_xsum
marker = pos
#print marker
#marker = str(float(marker) + gh_xsum)
if debug :
print marker,
print t
# is this a peak, if the same peak update the score
if (loc > PEAK_THRESHOLD) and (loc > peak_value) :
peak_value = loc
if debug and not peak :
print "peak of %f found @ %s" % (peak_value, str(marker))
peak = True
# do the LOD scores go from neg -> pos?
if look_for_neg_to_pos :
if loc >= 0 and last <= 0 :
last_neg_to_pos = marker
look_for_neg_to_pos = False
if debug :
print "neg -> pos (%f --> %f)" % (last,loc)
# else do they go from pos -> neg? (this is to record crossing the x-axis)
else :
if last >= 0 and loc <= 0 :
look_for_neg_to_pos = True
if debug :
print "pos -> neg (%f --> %f)" % (last,loc)
# if we are descending a peak, print the data
if peak :
peak = False
prefix = ""
if mode == "simwalk" and format != "illumina":
prefix = "SNP_A"
elif mode == "genehunter":
pa1,pa2 = find_phys(physical_positions, c, float(last_neg_to_pos))
pb1,pb2 = find_phys(physical_positions, c, float(marker))
last_neg_to_pos = phys2marker[c,pa1]
marker = phys2marker[c,pb2]
prefix = ""
phys_1, rs_1 = marker_map[prefix + str(last_neg_to_pos)]
phys_2, rs_2 = marker_map[prefix + str(marker)]
print "chr%d %s %s %s_%s_%f" % (c, phys_1, phys_2, rs_1, rs_2, peak_value)
#print "\t%s %s" % (str(last_neg_to_pos),str(marker))
peak_value = -1
# print out peak
last = loc
tmp = []
gh_xsum += gh_max_x