-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhapmap2impute.py
executable file
·264 lines (222 loc) · 8.49 KB
/
hapmap2impute.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
#! /usr/bin/env python
# Author: Tim Flutre
# License: GPL-3
# Aim: convert a genotype file from the HapMap format into the IMPUTE format
import sys
import os
import getopt
import gzip
import time
import datetime
import math
def help():
msg = "`%s' converts a genotype file from the HapMap into the IMPUTE format.\n" % os.path.basename(sys.argv[0])
msg += "\n"
msg += "Usage: %s [OPTIONS] ...\n" % os.path.basename(sys.argv[0])
msg += "\n"
msg += "Options:\n"
msg += " -h, --help\tdisplay the help and exit\n"
msg += " -v, --verbose\tverbosity level (default=1)\n"
msg += " -i, --input\tinput pattern with the whole path to the HapMap file (gzipped, with a header line)\n"
msg += " -o, --output\toutput file in the IMPUTE format (gzipped, with a header line)\n"
msg += " -c, --chr\tchromosome numbers to convert (eg. '1-17-3', all autosomes by default)\n"
msg += " -b, --bed\tcoordinate of SNPs in a BED file (eg. output of liftOver, gzipped)\n"
msg += " -s, --snp\tfile with a list of SNP identifiers to ignore (eg. if they have conflicting coordinates)\n"
msg += " -n, --na\tfile with list of individuals to keep (one NA identifier per line)\n"
msg += "\n"
msg += "Example:\n"
msg += " %s -i ~/HMr28/genotypes_CHR_CEU_r28_nr.b36_fwd.txt.gz -o genotypes_allchrs_CEU.impute.gz" % os.path.basename(sys.argv[0])
print msg; sys.stdout.flush()
def setParamsFromCmdLine():
inPattern = ""
outFile = ""
lChrs = range(1,23)
snpCoordFile = ""
snpIgnoreFile = ""
indsToKeepFile = ""
verbose = 1
try:
opts, args = getopt.getopt(sys.argv[1:], "hv:i:o:c:b:s:n:",
["help", "verbose=", "input=",
"output=", "chr=", "bed=", "snp=", "na="])
except getopt.GetoptError, err:
sys.stderr.write("%s\n" % str(err))
help()
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
help()
sys.exit(0)
elif o in ("-v", "--verbose"):
verbose = int(a)
elif o in ("-i", "--input"):
inPattern = a
elif o in ("-o", "--output"):
outFile = a
elif o in ("-c", "--chr"):
lChrs = map(int, a.split("-"))
elif o in ("-b", "--bed"):
snpCoordFile = a
elif o in ("-s", "--snp"):
snpIgnoreFile = a
elif o in ("-n", "--na"):
indsToKeepFile = a
if inPattern == "":
msg = "ERROR: missing input pattern (-i)"
sys.stderr.write("%s\n\n" % msg)
help()
sys.exit(1)
if outFile == "":
msg = "ERROR: missing output file (-o)"
sys.stderr.write("%s\n\n" % msg)
help()
sys.exit(1)
if snpIgnoreFile != "" and not os.path.exists(snpIgnoreFile):
msg = "ERROR: file '%s' doesn't exist" % snpIgnoreFile
sys.stderr.write("%s\n\n" % msg)
help()
sys.exit(1)
if snpCoordFile != "" and not os.path.exists(snpCoordFile):
msg = "ERROR: file '%s' doesn't exist" % snpCoordFile
sys.stderr.write("%s\n\n" % msg)
help()
sys.exit(1)
if indsToKeepFile != "" and not os.path.exists(indsToKeepFile):
msg = "ERROR: file '%s' doesn't exist" % indsToKeepFile
sys.stderr.write("%s\n\n" % msg)
help()
sys.exit(1)
return inPattern, outFile, lChrs, snpCoordFile, snpIgnoreFile, indsToKeepFile, verbose
def loadFileWithListOfSnpsToIgnore(snpIgnoreFile, verbose):
lSnpsToIgnore = []
if snpIgnoreFile != "":
if verbose > 0:
print "load SNPs to ignore..."
sys.stdout.flush()
snpIgnoreH = open(snpIgnoreFile)
while True:
line = snpIgnoreH.readline()
if line == "": break
if line[:-1] not in lSnpsToIgnore:
lSnpsToIgnore.append(line[:-1])
snpIgnoreH.close()
if verbose > 0:
print "nb of SNPs to ignore: %i" % len(lSnpsToIgnore)
sys.stdout.flush()
return lSnpsToIgnore
def getNewSnpCoordinates(snpCoordFile, lSnpsToIgnore, verbose):
dSnpId2NewCoord = {}
if snpCoordFile != "":
if verbose > 0:
print "load new SNP coordinates..."
sys.stdout.flush()
snpCoordH = gzip.open(snpCoordFile)
while True:
line = snpCoordH.readline()
if line == "": break
tok = line[:-1].split()
if tok[3] in lSnpsToIgnore:
continue
if dSnpId2NewCoord.has_key(tok[3]):
msg = "ERROR: SNP '%s' is redundant" % tok[3]
sys.stderr.write("%s\n" % msg)
sys.exit(1)
dSnpId2NewCoord[tok[3]] = tok[2]
snpCoordH.close()
if verbose > 0:
print "nb of SNPs with new coords: %i" % len(dSnpId2NewCoord.keys())
sys.stdout.flush()
return dSnpId2NewCoord
def loadFileWithListOfIndsToKeep(indsToKeepFile, verbose):
lIndsToKeep = []
if indsToKeepFile != "":
if verbose > 0:
print "load individuals to keep..."
sys.stdout.flush()
indsToKeepH = open(indsToKeepFile)
while True:
line = indsToKeepH.readline()
if line == "": break
if line[:-1] not in lIndsToKeep:
lIndsToKeep.append(line[:-1])
indsToKeepH.close()
if verbose > 0:
print "nb of individuals to keep: %i" % len(lIndsToKeep)
sys.stdout.flush()
return lIndsToKeep
def convertHapMapToImpute(inPattern, chrNb, lSnpsToIgnore, dSnpId2NewCoord,
lIndsToKeep, outH, isFirstFile, verbose):
hmFile = inPattern.replace("CHR", "chr%i" % chrNb)
if verbose > 0:
print "convert file %s..." % hmFile
sys.stdout.flush()
hmH = gzip.open(hmFile)
# handle the header (write one only for the first file)
lColIdxToKeep = []
line = hmH.readline()
lToks = line.rstrip().split()
for i in range(11, len(lToks)):
indName = lToks[i]
if indName in lIndsToKeep:
lColIdxToKeep.append(i)
if isFirstFile:
txt = "chr id coord a1 a2"
for i in range(11,len(lToks)):
if i in lColIdxToKeep:
txt += " %s" % lToks[i]
outH.write("%s\n" % txt)
# handle the other lines
while True:
line = hmH.readline()
if line == "": break
lToks = line[:-1].split()
snpId = lToks[0]
if snpId in lSnpsToIgnore:
continue
txt = "chr%i" % chrNb
txt += " %s" % snpId
if dSnpId2NewCoord.has_key(snpId):
txt += " %s" % dSnpId2NewCoord[snpId]
else:
txt += " %s" % lToks[3]
a1, a2 = lToks[1].split("/")
txt += " %s" % a1
txt += " %s" % a2
for i in range(11,len(lToks)):
if i not in lColIdxToKeep:
continue
if lToks[i] == a1+a1:
txt += " 1 0 0"
elif a1 in lToks[i] and a2 in lToks[i]:
txt += " 0 1 0"
elif lToks[i] == a2+a2:
txt += " 0 0 1"
else:
txt += " 0 0 0"
outH.write("%s\n" % txt)
hmH.close()
def main():
inPattern, outFile, lChrs, snpCoordFile, snpIgnoreFile, indsToKeepFile, verbose = setParamsFromCmdLine()
if verbose > 0:
msg = "START %s" % time.strftime("%Y-%m-%d %H:%M:%S")
startTime = time.time()
print msg; sys.stdout.flush()
lSnpsToIgnore = loadFileWithListOfSnpsToIgnore(snpIgnoreFile, verbose)
dSnpId2NewCoord = getNewSnpCoordinates(snpCoordFile, lSnpsToIgnore, verbose)
lIndsToKeep = loadFileWithListOfIndsToKeep(indsToKeepFile, verbose)
outH = gzip.open(outFile, "w")
isFirstFile = True
for chrNb in lChrs:
convertHapMapToImpute(inPattern, chrNb, lSnpsToIgnore, dSnpId2NewCoord,
lIndsToKeep, outH, isFirstFile, verbose)
isFirstFile = False
outH.close()
if verbose > 0:
msg = "END %s" % time.strftime("%Y-%m-%d %H:%M:%S")
endTime = time.time()
runLength = datetime.timedelta(seconds=
math.floor(endTime - startTime))
msg += " (%s)" % str(runLength)
print msg; sys.stdout.flush()
if __name__ == "__main__":
main()