-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVcfFile.py
More file actions
224 lines (159 loc) · 7.05 KB
/
Copy pathVcfFile.py
File metadata and controls
224 lines (159 loc) · 7.05 KB
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
from VcfMetaLines import MetaLines
from VcfMetaLines import HeaderLines
from VcfRecord import VcfRecord
from VcfGenotype import VcfGenotype
import string
import sys
class VcfFile(object):
'a class representing a VCF file'
def __init__(self, filename):
self.metaline = MetaLines()
self.headerline = HeaderLines()
self.filename=filename
def parseMetaAndHeaderLines(self, fh):
""" parse meta lines that begin with ## and header line that begins with # """
for line in fh:
if '##fileformat' in line.strip():
self.metaline.setFileFormat(line.strip())
elif '##INFO' in line.strip():
self.metaline.parseMetaInfo(line)
elif '##FILTER' in line.strip():
self.metaline.parseMetaFilter(line)
elif '##FORMAT' in line.strip():
self.metaline.parseMetaFormat(line)
elif '#CHROM' in line.strip():
self.headerline.add_format_column(line.strip() )
self.headerline.append_samplelist( line.strip() )
break
else:
pass
def parseMetaLines(self, fh):
""" parse the meta lines that begin with ## in a VCF file """
for line in fh:
#print "foo "+ line.strip()
if '##fileformat' in line.strip():
self.metaline.setFileFormat(line)
elif '##INFO' in line.strip():
self.metaline.parseMetaInfo(line)
elif '##FILTER' in line.strip():
self.metaline.parseMetaFilter(line)
elif '##FORMAT' in line.strip():
self.metaline.parseMetaFormat(line)
elif '#CHROM' in line.strip():
self.headerline.add_format_column(line.strip() )
self.headerline.append_samplelist( line.strip() )
self.printHeaderLine()
break
elif '##reference' in line.strip():
break
else:
pass
def hasFormatHeader(self,headerline):
""" check if header line has FORMAT column """
self.headerline.add_format_column(headerline)
def parseHeaderLine(self,fh):
for line in fh:
#print line.strip()
if '#CHROM' not in line:
break
self.headerline.add_format_column(line.strip() )
self.headerline.append_samplelist( line.strip() )
break
def printHeaderLine(self):
print self.headerline.toString()
def getSampleList(self):
return self.headerline.getSampleList()
def setSampleList(self, newlist):
self.headerline.setSampleList(newlist)
def getMetaInfoNames(self):
""" return the IDs of the INFO metalines"""
return self.metaline.getMetaInfoNames()
def getMetaInfoDescription(self):
return self.metaline.getMetaInfoDescription()
def addMetaInfoHeader(self, id, type, number, description):
""" add an ##INFO header to a vcf file """
self.metaline.addMetaInfo(id, type,number,description)
def addMetaFilterHeader(self, id, description):
""" add ##FILTER header to a vcf file """
self.metaline.addMetaFilter(id, description)
def getMetaFilterNames(self):
""" return the IDs of the FILTER metalines """
return self.metaline.getMetaFilterNames()
def getMetaFilterDescription(self):
""" return a list of tuples with (id, description) of FILTER metalines """
return self.metaline.getMetaFilterDescription()
def getMetaFormatNames(self):
""" return the Ids of the FORMAT metalines"""
return self.metaline.getMetaFormatNames()
def addMetaFormatHeader(self, formatObj):
""" add a ##FORMAT header """
self.metaline.addMetaFormat(formatObj)
def getMetaFormatDescription(self):
""" return a list of tuples (id,descripton) for FORMAt metalines """
return self.metaline.getMetaFormatDescription()
def yieldMetaInfoLines(self):
""" yield string representation of ##INFO metalines """
return self.metaline.yieldPrintMetaInfoLines()
def yieldMetaFormatLines(self):
""" yield string reprsentation ##FORMAT metaline object """
return self.metaline.yieldPrintMetaFormatLines()
def yieldMetaFilterLines(self):
""" yield string represenation of ##FILTER metaline object """
return self.metaline.yieldPrintMetaFilterLines()
def printMetaInfoLines(self):
"""print the meta ##INFO lines for a VCF """
for str in self.yieldMetaInfoLines():
print str
def printMetaFormatLines(self):
for str in self.yieldMetaFormatLines():
print str
def printMetaFilterLines(self):
for str in self.yieldMetaFilterLines():
print str
def printMetaLines(self):
self.printMetaInfoLines()
self.printMetaFormatLines()
self.printMetaFilterLines()
""" collect all the lines that start with ## and #CHROM and rturn a string """
def returnHeader(self):
headerlines=[]
fileformat=self.metaline.getFileFormat()
headerlines.append(fileformat)
for str in self.yieldMetaInfoLines():
headerlines.append(str)
for str in self.metaline.yieldPrintMetaFormatLines():
headerlines.append(str)
for str in self.yieldMetaFilterLines():
headerlines.append(str)
headerlines.append( self.headerline.toString() )
outstr="\n".join(headerlines)
return outstr
def printMetaAndHeaderLines(self):
print self.returnHeader()
def yieldVcfRecord(self,fh):
""" yield VcfRecord object from reading a dataline in a VCF file """
for line in fh:
#print line.strip()
if '#' in line: continue
fields=line.strip().split('\t')
(chrom,pos,id,ref,alt,qual,filter,info)=fields[0:8]
yield VcfRecord(chrom,pos,id,ref,alt,qual,filter,info)
def yieldVcfDataLine(self,fh):
""" yield dataline from filehandle fh reprsenting a vcf file """
for line in fh:
if '#' in line:continue
yield line.strip()
def yieldVcfRecordwithGenotypes(self,fh):
""" yield a VcfRecord with its genotypes list populated """
for line in fh:
fields=line.strip().split('\t')
(chrom,pos,id,ref,alt,qual,filter,info)=fields[0:8]
vrec=VcfRecord(chrom,pos,id,ref,alt,qual,filter,info)
formatstring=fields[8]
(genotypestrings)=fields[9::]
for gstring in genotypestrings:
vrec.addGenotype( VcfGenotype(formatstring,gstring))
yield vrec
def printDataLineWithGenotypes(self,vcfh):
for vrec in self.yieldVcfRecordwithGenotypes(vcfh):
print vrec.toStringwithGenotypes()