-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvcflib.py
More file actions
193 lines (162 loc) · 4.1 KB
/
vcflib.py
File metadata and controls
193 lines (162 loc) · 4.1 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
# vcflib.py - Function library for rawDNA2vcf suite
# Copyright (C) 2017 Philip Baltar (psbaltar@gmail.com)
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import re
import gzip
def isDIV(record = [], *args):
VC = getVC(record)
if VC == 'DIV':
return True
else:
return False
def isSNP(record = [], *args):
VC = getVC(record)
if VC == 'SNV':
return True
else:
return False
def getVC(record = [], *args):
INFO = record[7]
VC = re.findall("(^.*;)(VC=\w*)(;.*$)", INFO)[0][1].split('=')[1]
return VC
def getRSPOS(record = [], *args):
INFO = record[7]
RSPOS = re.findall("(^.*;)(RSPOS=\d*)(;.*$)", INFO)[0][1].split('=')[1]
return RSPOS
def getRSPOSmatches(rspos, records = [], *args):
matches = []
for record in records:
myRSPOS = getRSPOS(record)
if myRSPOS == rspos:
matches.append(record)
return matches
def getVCmatches(vc, records = [], *args):
matches = []
for record in records:
myVC = getVC(record)
if myVC == vc:
matches.append(record)
return matches
def getIDmatches(ID, records = [], *args):
matches = []
for record in records:
myID = record[2]
if myID == ID:
matches.append(record)
return matches
def getPOSmatches(pos, records = [], *args):
matches = []
for record in records:
mypos = record[1]
if mypos == pos:
matches.append(record)
return matches
def getVCFheaders(vcffilename):
headerstring = ''
if re.search("\.gz$", vcffilename):
vcffile = gzip.open(vcffilename, 'rt')
else:
vcffile = open(vcffilename, 'r')
for line in vcffile:
if re.search("^#", line):
headerstring += line
else:
break
vcffile.close()
return headerstring
def calcGT(genotype, ref, alt):
alts = alt.split(',')
alleles = [ref]+alts
# figure out what VC we have based on REF and ALT fields
if len(ref) == 1 and len(alts[0]) == 1:
vc = 'SNP'
elif len(ref) > len(alts[0]):
vc = 'DEL'
else:
vc = 'INS'
gt = []
for allele in genotype:
if allele == '-': # no call
gt.append('.')
elif vc == 'SNP' and allele in ['A','T','C','G'] and allele in alleles:
gt.append(str(alleles.index(allele)))
elif vc == 'DEL' and allele in ['D','I']:
if allele == 'D':
gt.append('1')
else:
gt.append('0')
elif vc == 'INS' and allele in ['D','I']:
if allele == 'I':
gt.append('1')
else:
gt.append('0')
else:
gt.append('E')
if re.search("^\.+$", ''.join(gt)):
GT = '.'
else:
GT = '/'.join(gt)
return GT
def calcGTsnp(genotype, ref, alt):
if genotype == '--':
return '.'
#double check that we're looking at a SNP
if (len(ref.split(',')[0]) != 1) and (len(alt.split(',')[0]) != 1):
return '.'
alts = alt.split(',')
if genotype[0] == ref:
g0 = '0'
elif genotype[0] in alts:
g0 = str(alts.index(genotype[0])+1)
else:
g0 = '.'
if genotype[1] == ref:
g1 = '0'
elif genotype[1] in alts:
g1 = str(alts.index(genotype[1])+1)
else:
g1 = '.'
if g0 != '.' and g1 != '.':
return g0+'/'+g1
else:
return '.'
def calcGTdiv(genotype, ref, alt):
if genotype == '--':
return '.'
if len(ref) < len(alt):
vartype = 'I'
else:
vartype = 'D'
if vartype == 'I':
if genotype[0] == 'D':
g0 = '0'
else:
g0 = '1'
if genotype[1] == 'D':
g1 = '0'
else:
g1 = '1'
if vartype == 'D':
if genotype[0] == 'I':
g0 = '0'
else:
g0 = '1'
if genotype[1] == 'I':
g1 = '0'
else:
g1 = '1'
return g0+'/'+g1