-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVcfMetaLines.py
343 lines (266 loc) · 11.3 KB
/
VcfMetaLines.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
import re
import sys
class InfoLine(object):
""" represents an ##INFO line """
""" ID=ID,Number=number,Type=type,Description=description """
def __init__(self, id='', number='', type='', description=''):
self.id=id
self.number=number
self.type=type
self.description=description
def setId(self,id):
self.id=id
def getId(self):
return self.id
def setNumber(self, number):
self.number=number
def getNumber(self):
return self.number
def setType(self,type):
self.type=type
def getType(self):
return self.type
def setDescription(self,description):
self.description=description
def getDescription(self):
return self.description
def toString(self):
""" return string representation of INFO line object """
infostring=",".join(["ID="+self.id, "Number="+self.number, "Type="+self.type, "Description="+"\""+ self.description + "\""] )
hashstring="##INFO=<"
outstring=hashstring+infostring+">"
return outstring
class FormatLine(object):
""" represents a ##FORMAT line'"""
""" ##FORMAT=<ID=ID,Number=number,Type=type,Description=description> """
""" need to figure out how python inheritance works ..."""
def __init__(self, id='', number='', type='', description=''):
self.id=id
self.number=number
self.type=type
self.description=description
def setId(self,id):
self.id=id
def getId(self):
return self.id
def setNumber(self, number):
self.number=number
def getNumber(self):
return self.number
def setType(self,type):
self.type=type
def getType(self):
return self.type
def setDescription(self,description):
self.description=description
def getDescription(self):
return self.description
def toString(self):
""" return string representation of FORMAT line object """
formatstring=",".join(["ID="+self.id, "Number="+self.number, "Type="+self.type, "Description="+"\""+ self.description + "\""])
hashstring="##FORMAT=<"
outstring=hashstring+formatstring+">"
return outstring
class FilterLine(object):
""" reprsents a ##FILTER line """
""" ##FILTER=<ID=ID,Description=description> """
def __init__(self, id='', description=''):
self.id=id
self.description=description
def setId(self,id):
self.id=id
def getId(self):
return self.id
def setDescription(self,description):
self.description=description
def getDescription(self):
return self.description
def toString(self):
""" return a string representaiton of FILTER line object """
filterstring=",".join ( ["ID="+self.id, "Description="+ "\""+self.description + "\""] )
hashstring="##FILTER=<"
outstring=hashstring+filterstring+">"
return outstring
class MetaLines(object):
def __init__(self):
""" class to hold meta line info from VCF files """
self.metaFormatDict={}
""" dict for ##FORMAT lines """
self.metaFilterDict={}
""" dictionary for ##FILTER lines """
self.metaInfoDict={}
""" dictory for ##INFO lines """
self.fileFormat=''
def getMetaInfoNames(self):
""" return the INFO IDs in the metaInfoDict """
return self.metaInfoDict.keys()
def addMetaInfo(self, id, type, number, description):
""" add InfoLIne object to metaInfoDict dictionary """
if id in self.metaInfoDict.keys():
sys.stderr.write(id + " ##INFO id is already being used!\n")
exit(1)
return
else:
newinfobject=InfoLine()
newinfobject.setId(id)
newinfobject.setType(type)
newinfobject.setNumber(str(number))
newinfobject.setDescription(description)
self.metaInfoDict[ newinfobject.getId() ] = newinfobject
return
def addMetaFilter(self, id, description):
""" add FilterLine object to metaFilterDict dictionary """
if id in self.metaFilterDict.keys():
sys.stderr.write(id + "##FILTER id already being used!\n")
exit(1)
return
else:
newfilterobject=FilterLine(id, description)
self.metaFilterDict [ newfilterobject.getId() ] = newfilterobject
def addMetaFormat(self, formatObj):
""" add a ##FORMAT header """
if formatObj.getId() in self.metaFormatDict.keys():
sys.stderr.write(id + "##FORMAT id already being used!\n")
return
else:
self.metaFormatDict [ formatObj.getId() ] = formatObj
def getMetaFilterNames(self):
""" return the FILTER IDs in the metaFilterDict """
return self.metaFilterDict.keys()
def getMetaFormatNames(self):
""" return the FORMAT IDs in metaFormatDict """
return self.metaFormatDict.keys()
def getMetaInfoDescription(self):
""" return a list of tuples with (id,description) of INFO metalines """
description_strings=[]
for k in self.metaInfoDict.keys():
description_strings.append( (k, self.metaInfoDict[k].getDescription() ) )
return description_strings
def getMetaFormatDescription(self):
""" return a list of tuples with (id,description) of FORMAT metalines """
description_strings=[]
for k in self.metaFormatDict.keys():
description_strings.append( (k, self.metaFormatDict[k].getDescription() ) )
return description_strings
def getMetaFilterDescription(self):
""" return a list of tuples with (id,description) of FORMAT metalines """
description_strings=[]
for k in self.metaFilterDict.keys():
description_strings.append( (k, self.metaFilterDict[k].getDescription() ) )
return description_strings
def setFileFormat(self, formatline):
""" formatline is ##fileformat=VCFv4.1 """
self.fileFormat=formatline
def getFileFormat(self):
return self.fileFormat
def parseMetaInfo(self, line):
""" parse an ##INFO line field """
""" ##INFO<ID=ID,Number=number,Type=type,Description=description> """
line.strip()
infobject=InfoLine()
pattern= '<(.*)>'
pattern_descrip='Description=\"(.*)\"'
infostring =re.search(pattern, line).groups()[0]
if infostring == None:
sys.stderr.write('error in parsing meta ##INFO line!\n')
sys.exit(1)
else:
descriptionstring =re.search(pattern_descrip, infostring).groups()[0]
infofields=infostring.split(',')
for elem in infofields:
if 'Description' in elem:
infobject.setDescription(descriptionstring)
break
else:
(key,val)=elem.split('=')
if key=='ID': infobject.setId(val)
elif key=='Number': infobject.setNumber(val)
elif key=='Type': infobject.setType(val)
else: pass
self.metaInfoDict[ infobject.getId() ] = infobject
def parseMetaFilter(self, line):
""" parse ##FILTER field """
""" ##FILTER=<ID=ID,Description=description> """
line.strip()
filterobject=FilterLine()
pattern='<(.*)>'
filterstring=re.search(pattern, line).groups()[0]
if filterstring == None:
sys.stderr.write("error in parsing ##FORMAT line!\n")
sys.exit(1)
else:
filterfields=filterstring.split(',')
for elem in filterfields:
(key, val)=elem.split('=',1)
if key=='ID': filterobject.setId(val)
elif key=='Description': filterobject.setDescription(val)
else: pass
self.metaFilterDict [ filterobject.getId() ] = filterobject
def parseMetaFormat(self,line):
""" parse ##FORMAT """
""" ##FORMAT=<ID=ID,Number=number,Type=type,Description=description> """
formatobject = FormatLine()
pattern= '<(.*)>'
pattern_descrip='Description=\"(.*)\"'
formatstring =re.search(pattern, line).groups()[0]
if formatstring == None:
sys.stderr.write('error in parsing meta ##INFO line!\n')
exit(1)
else:
descriptionstring =re.search(pattern_descrip, formatstring).groups()[0]
formatfields=formatstring.split(',')
for elem in formatfields:
if 'Description' in elem:
formatobject.setDescription (descriptionstring)
break
else:
(key,val)=elem.split('=')
if key=='ID': formatobject.setId(val)
elif key=='Number': formatobject.setNumber(val)
elif key=='Type': formatobject.setType(val)
else: pass
self.metaFormatDict[ formatobject.getId() ] = formatobject
def yieldPrintMetaInfoLines(self):
""" yield string representation of meta ##INFO line objects """
for k in self.metaInfoDict.keys():
yield self.metaInfoDict[k].toString()
def yieldPrintMetaFormatLines(self):
""" yield string representation of meta ##FORMAT line objects """
for k in self.metaFormatDict.keys():
yield self.metaFormatDict[k].toString()
def yieldPrintMetaFilterLines(self):
""" yield string representation of meta ##FILTER line objects """
for k in self.metaFilterDict.keys():
yield self.metaFilterDict[k].toString()
######################################################################################
class HeaderLines(object):
""" """
def __init__(self):
""" class to represent VCF headerline
#CHROM POS ID REF ALT QUAL FILTER INFO are the fixed,standard,mandatory header columns """
self.headercolumns=['#CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO' ]
self.hasFormatColumn=0
self.samplelist=[]
def add_format_column(self, headerline):
""" FORMAT column is optional since not all VCFs contain genotypes; check if FORMAT is in the header line, then add it to headercolumns list """
if 'FORMAT' in headerline:
self.headercolumns.append('FORMAT')
self.hasFormatColumn=1
def append_samplelist(self, headerline):
""" check to see if vcf has sample columns and if so append th list samplelist """
fields=headerline.split('\t')
if len(fields) > 9:
self.samplelist=fields[9::]
def getSampleList(self):
return self.samplelist
def setSampleList(self,newlist):
""" set a new samplelist """
self.samplelist=newlist
def toString(self):
headerstring= "\t".join(self.headercolumns)
if len ( self.samplelist ) >=1:
samplestring="\t".join(self.samplelist)
outstring=headerstring+"\t"+samplestring
else:
outstring=headerstring
return outstring