-
Notifications
You must be signed in to change notification settings - Fork 1
/
insertMissingRows.py
386 lines (322 loc) · 14.5 KB
/
insertMissingRows.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
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
# insertMissingRows.py
# The goal of this script is to insert rows that were omitted from merging VCF files from different strains
# and to rename the strain columns with TaxonIDs
# To run this script, you will need Biopython on your local machine (or wherever you're running this script from)
# For downloading the package, see http://biopython.org/wiki/Download
# For installation instructions, see http://biopython.org/DIST/docs/install/Installation.html
#
# INPUT FILEs: 1) Galaxy merged VCF file, 2) original VCF files to search, fullpath, 3) file with list of missing rows
# OUTPUT: 1) new merged VCF file with taxonIDs
#
from Bio import Entrez
import re
import sys, os
import getopt
import urllib2
import time
from dateutil.parser import parse
## database connection stuff -- for taxon IDs
from sqlalchemy import create_engine, and_
"""from ..models.models import DBSession, Straindbentity, DBentity
from ..data_helpers.data_helpers import get_eco_ids, get_output, SUBMISSION_VERSION
engine = create_engine(os.getenv('SQLALCHEMY_PROD_DB_URI'), pool_recycle=3600)
DBSession.configure(bind=engine)
"""
###############
## VARIABLES ##
###############
STRAINS = {
'BY4742': 'S288C',
'CEN.PK': 'CEN.PK',
'CEN.PK2': 'CEN.PK',
'D273-10B': 'D273-10B',
'FL100': 'FL100',
'JK9-3d': 'JK9-3d',
'JK9': 'JK9-3d',
'RM11-1A': 'RM11-1a',
'RM11_1A': 'RM11-1a',
'SEY6210': 'SEY6210',
'SEY': 'SEY6210',
'10560_6B': 'Sigma1278b',
'SK1': 'SK1',
'W303': 'W303',
'W303-1a': 'W303',
'W303-1A': 'W303',
'W303-K6001': 'W303',
'W303-1b': 'W303',
'W303-1B': 'W303',
'X2180': 'X2180-1A',
'Y55': 'Y55'
}
CHR_ORD = ('chrI', 'chrII', 'chrIII', 'chrIV', 'chrV', 'chrVI', 'chrVII',
'chrVIII', 'chrIX', 'chrX', 'chrXI', 'chrXII', 'chrXIII', 'chrXIV',
'chrXV', 'chrXVI', 'chrM')
STRAINSTOTAXONS = {
'BY4742': 'TAX:559292',
'CEN.PK': 'TAX:889517',
'D273': 'NTR:101',
'FL100': 'TAX:947036',
'JK9': 'NTR:104',
'RM11_1A': 'TAX:285006',
'SEY6210': 'NTR:107',
'SEY': 'NTR:107',
'10560_6B': 'TAX:658763',
'SK1': 'TAX:580239',
'SIGMA': 'TAX:658763',
'W303': 'TAX:580240',
'X2180': 'NTR:108',
'Y55': 'NTR:112'
}
# BY4742 CEN.PK D273 FL100 JK9 RM11_1A SEY6210 SK1 SIGMA W303 X2180 Y55
#
# #def getStrains(strainslist): ## convert strains to NCBI Taxon ID
def processInfile(filename):
vcfDataObj = dict()
strainsInVcf = list()
colHeaders = list()
with open(filename, 'r') as reader:
for row in reader.readlines():
## CHECK FOR comments/headers at the beginning. Add to 'header'
if re.match("^#", row):
## DO SOMETHING SPECIAL IF IT has the headers with strains ##
if re.match("^#CHROM", row):
print "HEADER ROW: " + row
colHeaders = row.split("\t")
numCols = len(colHeaders)
if numCols > 9:
for each in colHeaders[9:numCols - 1]:
# print each + "==" + each.rstrip()
strainsInVcf.append(each.rstrip())
if vcfDataObj.has_key('header'):
vcfDataObj['header'].append(row.rstrip())
else:
vcfDataObj['header'] = [row.rstrip()]
## NON-COMMENT/HEADER rows ##
## MAKE AN OBJ -- CHROMOSOME, POS, REF - '
else:
dataRow = row.split("\t")
tempObj = dict()
for h in colHeaders:
# print "."
# print str(colHeaders.index(h)) + "." + h.strip(
# ) + ": " + dataRow[colHeaders.index(h)]
tempObj[h.rstrip()] = dataRow[colHeaders.index(h)].rstrip()
# print h.strip() + " indexed at " + str(colHeaders.index(h))
# chromosome, position, ref, alt,
chr = dataRow[0]
pos = int(dataRow[1])
ref = dataRow[3]
alt = dataRow[4]
if vcfDataObj.has_key(chr):
if vcfDataObj[chr].has_key(pos):
if vcfDataObj[chr][pos].has_key(ref):
if vcfDataObj[chr][pos][ref].has_key(alt):
continue #already there
else:
vcfDataObj[chr][pos][ref][alt] = tempObj
else:
vcfDataObj[chr][pos][ref] = dict()
vcfDataObj[chr][pos][ref][alt] = tempObj
else:
vcfDataObj[chr][pos] = dict()
vcfDataObj[chr][pos][ref] = dict()
vcfDataObj[chr][pos][ref][alt] = tempObj
else:
print chr + ":" + str(pos)
# for pair in tempObj.items():
# print(pair)
# sys.exit()
vcfDataObj[chr] = dict()
vcfDataObj[chr][pos] = dict()
vcfDataObj[chr][pos][ref] = dict()
vcfDataObj[chr][pos][ref][alt] = tempObj
return (vcfDataObj, colHeaders, strainsInVcf)
def getMissingRows(missingrowsfile, dirToSearch, headers):
missingDataObj = dict()
# open file
# for each line, split and then grep #
# make an object of each row returning # #associate with the strain/file #
with open(missingrowsfile, 'r') as reader:
for row in reader.readlines():
searchTerms = row.split("\t")
while ("" in searchTerms):
searchTerms.remove("")
print "|".join(searchTerms)
command = "grep '^" + searchTerms[
0] + "\t' " + dirToSearch + "*.vcf | grep '\t" + searchTerms[
1] + "\t' | grep '\t" + searchTerms[3] + "\t'"
#print command
missingLines = os.popen(command) ## list
for each in missingLines:
# print each
oriVcf, dataRow = each.split(".vcf")
data = dataRow.split("\t") # get datarow
headcommand = "grep '#CHROM' " + oriVcf + ".vcf" # grep the column headers from the file
headrow = os.popen(headcommand)
for one in headrow:
fileheadrow = one.split("\t")
tempObj = dict()
for col in fileheadrow:
# print str(fileheadrow.index(col)) + ": " + col
# making a temp obj of the data row
tempObj[col.rstrip()] = data[fileheadrow.index(
col)].lstrip(":").rstrip()
chrom = data[0].lstrip(":")
pos = int(data[1])
ref = data[3]
alt = data[4]
strainnum = len(data) - 1
strbkd = fileheadrow[strainnum].rstrip()
# check if chromosome key
if missingDataObj.has_key(chrom):
if missingDataObj[chrom].has_key(pos): #check pos
if missingDataObj[chrom][pos].has_key(
ref): # check ref
if missingDataObj[chrom][pos][ref].has_key(
alt): #check alt
if missingDataObj[chrom][pos][ref][
alt].has_key(strbkd):
continue # skip if strain already there
else: # just add strain
missingDataObj[chrom][pos][ref][alt][
strbkd] = data[len(data) - 1].rstrip()
else: # no strain, no alt
missingDataObj[chrom][pos][ref][alt] = tempObj
else: # no strain, no alt, no ref
missingDataObj[chrom][pos][ref] = dict()
missingDataObj[chrom][pos][ref][alt] = tempObj
else:
# no strain, no alt, no ref, no pos
missingDataObj[chrom][pos] = dict()
missingDataObj[chrom][pos][ref] = dict()
missingDataObj[chrom][pos][ref][alt] = tempObj
else: # no chromosome, pos, ref, alt, strain
missingDataObj[chrom] = dict()
missingDataObj[chrom][pos] = dict()
missingDataObj[chrom][pos][ref] = dict()
missingDataObj[chrom][pos][ref][alt] = tempObj
return missingDataObj
def merge_data(vcfData, missing, strains):
mergeDataObj = vcfData
for chrom in missing.keys():
for pos in missing[chrom].keys():
for ref in missing[chrom][pos].keys():
for alt in missing[chrom][pos][ref].keys():
newRowObj = missing[chrom][pos][ref][alt]
for one in strains: # adding '.' for all strains without info
if newRowObj.has_key(one.rstrip()):
# print 'strain ' + one + " already for " + ",".join(
# [chrom, pos, ref, alt])
continue
else:
# if one == 'BY4742':
# print 'missing: ' + chrom + ',' + pos + ',' + ref + ',' + alt + ' adding . for ' + one
newRowObj[one.rstrip()] = "."
if mergeDataObj[chrom].has_key(pos):
if mergeDataObj[chrom][pos].has_key(ref):
if mergeDataObj[chrom][pos][ref].has_key(alt):
continue
else:
mergeDataObj[chrom][pos][ref][alt] = newRowObj
else:
mergeDataObj[chrom][pos][ref] = dict()
mergeDataObj[chrom][pos][ref][alt] = newRowObj
else:
mergeDataObj[chrom][pos] = dict()
mergeDataObj[chrom][pos][ref] = dict()
mergeDataObj[chrom][pos][ref][alt] = newRowObj
return (mergeDataObj)
def write_lines(fileout, wlines):
print 'printing to:' + fileout
writer = open(fileout, 'w')
# writer.write(u'')
for line in wlines:
# print line
if re.match(
"^#CHROM", line
): ##IF it is the header, replace strain names with taxon IDs
newheader = list()
headerrow = line.split("\t")
for col in headerrow:
# print col + " for header"
if STRAINSTOTAXONS.has_key(col.rstrip()):
# print "taxon:" + STRAINSTOTAXONS[col.rstrip()]
newheader.append(STRAINSTOTAXONS[col.rstrip()])
else:
newheader.append(col.rstrip())
writer.write("\t".join(newheader) + "\n")
else:
writer.write(line + '\n')
writer.close()
def main(argv):
if len(argv) > 2:
opts, args = getopt.getopt(argv, 'i:m:l:o:',
['inputfile=', 'missing=', 'list=', 'out='])
print 'getting opts' + str(argv)
# outpath = './'
for opt, arg in opts:
arg = re.sub('=', '', arg)
print opt + ":" + arg
if opt in ("-i", "--inputfile"):
inputfile = arg
if opt in ("-l", "--list"):
searchdir = arg
if opt in ("-m", "--missing"):
missingRows = arg
if opt in ("-o", "--out"):
outfile = arg
### here's where we run the functions
headerList = [
'#CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO',
'FORMAT', 'BY4742', 'CEN.PK', 'D273', 'FL100', 'JK9', 'RM11_1A',
'SEY6210', 'SK1', 'SIGMA', 'W303', 'X2180', 'Y55'
]
(data, headerList, strains) = processInfile(inputfile)
# print "strains:" + "*".join(strains)
missingdata = getMissingRows(missingRows, searchdir, headerList)
alldata = merge_data(data, missingdata, strains)
print outfile
write_lines(outfile, data['header'])
writer = open(outfile, 'a')
for each in CHR_ORD:
#
# positionsList = alldata[each].keys()
# print(positionsList)
sortedpos = sorted(alldata[each].keys())
# print(sortedpos)
#sys.exit()
for pos in sortedpos:
for ref in alldata[each][pos].keys():
for alt in alldata[each][pos][ref].keys():
rowdata = alldata[each][pos][ref][alt]
tempRow = list()
for col in headerList:
if rowdata.has_key(col.rstrip()):
#print col.rstrip() + ":" + rowdata[col.rstrip()]
if (rowdata[col.rstrip()] == 'chrM'):
tempRow.append('chrMt')
else:
tempRow.append(rowdata[col.rstrip()])
else:
tempRow.append(".")
writer.write("\t".join(tempRow) + "\n")
# if each != 'chrMt':
# writer.write("\n") #newline at end of each chromosome
writer.close()
# if (len(result) > 0):
#output_obj = get_output(result)
# file_name = 'src/data_dump/SGD' + SUBMISSION_VERSION + 'basicGeneInformation.json'
# json_file_str = os.path.join(root_path, file_name)
# with open(json_file_str, 'w+') as res_file:
# res_file.write(json.dumps(output_obj))
else:
print 'Usage: insertMissingRows.py -i <input file> -l <where to find original VCF files> -m <file with missing rows> -o <outfile path>'
sys.exit(2)
'''except getopt.GetoptError, err:
print str(err)
print 'Usage: insertMissingRows.py -i <input file> -l <list of original VCFs> -m <file with missing rows> -o <outfile path>'
sys.exit(2)
'''
if __name__ == "__main__":
main(sys.argv[1:])
# main()