-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
vcf-annotator.py
executable file
·398 lines (341 loc) · 15.3 KB
/
vcf-annotator.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
387
388
389
390
391
392
393
394
395
396
397
398
#! /usr/bin/env python3
"""
vcf-annotator.py [-h] [--output STRING] [--version] VCF_FILE GENBANK_FILE
Annotate variants from a VCF file using the reference genome's GenBank file.
positional arguments:
VCF_FILE VCF file of Variants
GENBANK_FILE GenBank file of the reference genome.
optional arguments:
-h, --help show this help message and exit
--output STRING File to write VCF output to (Default STDOUT).
--version show program's version number and exit
Example Usage:
./vcf-annotator.py example-data/example.vcf example-data/example.gb
"""
import collections
from Bio import SeqIO
from Bio.Seq import Seq
import vcf
VERSION = 0.7
class Annotator(object):
"""Annotate a given VCF file according to the reference GenBank."""
def __init__(self, gb_file=False, vcf_file=False):
"""Initialize variables."""
self.__annotated_features = ["CDS", "tRNA", "rRNA", "ncRNA",
"misc_feature"]
self.__gb = GenBank(gb_file)
self.__vcf = VCFTools(vcf_file)
self.add_annotation_info()
def add_annotation_info(self):
"""Add custom VCF info fields."""
self.__vcf.add_information_fields([
['RefCodon', None, 'String', 'Reference codon'],
['AltCodon', None, 'String', 'Alternate codon'],
['RefAminoAcid', None, 'String', 'Reference amino acid'],
['AltAminoAcid', None, 'String', 'Alternate amino acid'],
['CodonPosition', '1', 'Integer', 'Codon position in the gene'],
['SNPCodonPosition', '1', 'Integer', 'SNP position in the codon'],
['AminoAcidChange', None, 'String', 'Amino acid change'],
['IsSynonymous', '1', 'Integer',
'0:nonsynonymous, 1:synonymous, 9:N/A or Unknown'],
['IsTransition', '1', 'Integer',
'0:transversion, 1:transition, 9:N/A or Unknown'],
['IsGenic', '1', 'Integer', '0:intergenic, 1:genic'],
['IsPseudo', '1', 'Integer', '0:not pseudo, 1:pseudo gene'],
['LocusTag', None, 'String', 'Locus tag associated with gene'],
['Gene', None, 'String', 'Name of gene'],
['Note', None, 'String', 'Note associated with gene'],
['Inference', None, 'String', 'Inference of feature.'],
['Product', None, 'String', 'Description of gene'],
['ProteinID', None, 'String', 'Protein ID of gene'],
['Comments', None, 'String', 'Example: Negative strand: T->C'],
['VariantType', None, 'String', 'Indel, SNP, Ambiguous_SNP'],
['FeatureType', None, 'String', 'The feature type of variant.'],
])
def annotate_vcf_records(self):
"""Annotate each record in the VCF acording to the input GenBank."""
for record in self.__vcf.records:
self.__gb.accession = record.CHROM
self.__gb.version = record.CHROM
self.__gb.index = record.POS
# Set defaults
record.INFO['RefCodon'] = '.'
record.INFO['AltCodon'] = '.'
record.INFO['RefAminoAcid'] = '.'
record.INFO['AltAminoAcid'] = '.'
record.INFO['CodonPosition'] = '.'
record.INFO['SNPCodonPosition'] = '.'
record.INFO['AminoAcidChange'] = '.'
record.INFO['IsSynonymous'] = 9
record.INFO['IsTransition'] = 9
record.INFO['Comments'] = '.'
record.INFO['IsGenic'] = '0'
record.INFO['IsPseudo'] = '0'
record.INFO['LocusTag'] = '.'
record.INFO['Gene'] = '.'
record.INFO['Note'] = '.'
record.INFO['Inference'] = '.'
record.INFO['Product'] = '.'
record.INFO['ProteinID'] = '.'
record.INFO['FeatureType'] = 'inter_genic'
# Get annotation info
if self.__gb.feature_exists:
record.INFO['FeatureType'] = self.__gb.feature.type
if self.__gb.feature.type in self.__annotated_features:
feature = self.__gb.feature
if feature.type == "CDS":
record.INFO['IsGenic'] = '1'
qualifiers = {
'Note': 'note', 'LocusTag': 'locus_tag',
'Gene': 'gene', 'Product': 'product',
'ProteinID': 'protein_id',
'Inference': 'inference'
}
if feature.type == "tRNA":
qualifiers['Note'] = 'anticodon'
for k, v in qualifiers.items():
if v in feature.qualifiers:
# Spell out semi-colons, commas and spaces
record.INFO[k] = feature.qualifiers[v][0].replace(
';', '[semi-colon]'
).replace(
',', '[comma]'
).replace(
' ', '[space]'
)
if v == 'anticodon':
record.INFO[k] = 'anticodon{0}'.format(
record.INFO[k]
)
if 'pseudo' in feature.qualifiers:
record.INFO['IsPseudo'] = '1'
# Determine variant type
if record.is_indel:
if record.is_deletion:
record.INFO['VariantType'] = 'Deletion'
else:
record.INFO['VariantType'] = 'Insertion'
else:
if len(record.ALT) > 1:
record.ALT = self.__gb.determine_iupac_base(record.ALT)
record.INFO['VariantType'] = 'Ambiguous_SNP'
else:
if record.is_transition:
record.INFO['IsTransition'] = 1
else:
record.INFO['IsTransition'] = 0
record.INFO['VariantType'] = 'SNP'
if int(record.INFO['IsGenic']):
alt_base = str(record.ALT[0])
# Determine codon information
codon = self.__gb.codon_by_position(record.POS)
record.INFO['RefCodon'] = ''.join(list(codon[0]))
record.INFO['SNPCodonPosition'] = codon[1]
record.INFO['CodonPosition'] = codon[2]
# Adjust for ambiguous base and negative strand.
if feature.strand == -1:
alt_base = str(
Seq(alt_base).complement()
)
record.INFO['Comments'] = 'Negative:{0}->{1}'.format(
Seq(record.REF).complement(),
alt_base
)
# Determine alternates
record.INFO['AltCodon'] = list(record.INFO['RefCodon'])
record.INFO['AltCodon'][
record.INFO['SNPCodonPosition']
] = alt_base
record.INFO['AltCodon'] = ''.join(record.INFO['AltCodon'])
record.INFO['RefAminoAcid'] = Seq(
record.INFO['RefCodon']
).translate()
record.INFO['AltAminoAcid'] = Seq(
record.INFO['AltCodon']
).translate()
record.INFO['AminoAcidChange'] = '{0}{1}{2}'.format(
str(record.INFO['RefAminoAcid']),
record.INFO['CodonPosition'],
str(record.INFO['AltAminoAcid'])
)
if record.INFO['VariantType'] != 'Ambiguous_SNP':
ref = str(record.INFO['RefAminoAcid'])
alt = str(record.INFO['AltAminoAcid'])
if ref == alt:
record.INFO['IsSynonymous'] = 1
else:
record.INFO['IsSynonymous'] = 0
def write_vcf(self, output='/dev/stdout'):
"""Write the VCF to the specified output."""
self.__vcf.write_vcf(output)
class GenBank(object):
"""A class for parsing GenBank files."""
def __init__(self, gb=False):
"""Inititalize variables."""
self.genbank_file = gb
self.records = {}
self.record_index = {}
self.record_ids = {}
self.__gb = None
self._index = None
self._accession = None
self.__position_index = None
self.feature = None
self.features = ["CDS", "rRNA", "tRNA", "ncRNA", "repeat_region",
"misc_feature"]
self.gene_codons = {}
self.parse_genbank()
@property
def accession(self):
"""Accession for records."""
return self._index
@accession.setter
def accession(self, value):
if value not in self.records:
value = self.record_ids[value]
self._accession = value
self.__gb = self.records[value]
self.__position_index = self.record_index[value]
@property
def index(self):
"""Postion index for features."""
return self._index
@index.setter
def index(self, value):
self._index = self.__position_index[value - 1]
self.__set_feature()
def parse_genbank(self):
with open(self.genbank_file, 'r') as gb_fh:
for record in SeqIO.parse(gb_fh, 'genbank'):
self.records[record.name] = record
self.gene_codons[record.name] = {}
self.record_index[record.name] = [None] * len(record.seq)
self.record_ids[record.id] = record.name
for i in range(len(record.features)):
if record.features[i].type in self.features:
start = int(record.features[i].location.start)
end = int(record.features[i].location.end)
self.record_index[record.name][start:end] = [i] * (end - start)
def __set_feature(self):
if self._index is None:
self.feature_exists = False
self.feature = None
else:
self.feature_exists = True
self.feature = self.records[self._accession].features[self._index]
def codon_by_position(self, pos):
"""Retreive the codon given a postion of a CDS feature."""
if self._index not in self.gene_codons[self._accession]:
self.split_into_codons()
gene_position = self.position_in_gene(pos)
codon_position = gene_position // 3
return [self.gene_codons[self._accession][self._index][codon_position],
gene_position % 3,
codon_position + 1]
def split_into_codons(self):
"""Split the complete CDS feature in to a list of codons."""
start = self.feature.location.start
end = self.feature.location.end
seq = ''.join(list(self.__gb.seq[start:end]))
if self.feature.strand == -1:
seq = Seq(seq).reverse_complement()
self.gene_codons[self._accession][self._index] = [
seq[i:i + 3] for i in range(0, len(seq), 3)
]
def position_in_gene(self, pos):
"""Return a codon postion in a gene."""
if self.feature.strand == 1:
return pos - self.feature.location.start - 1
else:
return self.feature.location.end - pos
def base_by_pos(self, pos):
"""Print the base by position."""
print(self.__gb.seq[pos - 1])
def determine_iupac_base(self, bases):
"""
Determine the IUPAC symbol for a list of nucleotides.
Source: https://en.wikipedia.org/wiki/Nucleic_acid_notation
List elements are in this order: [A,C,G,T]
"""
if len(bases) > 1:
iupac_notation = {
'W': [True, False, False, True],
'S': [False, True, True, False],
'M': [True, True, False, False],
'K': [False, False, True, True],
'R': [True, False, True, False],
'Y': [False, True, False, True],
'B': [False, True, True, True],
'D': [True, False, True, True],
'H': [True, True, False, True],
'V': [True, True, True, False],
'N': [False, False, False, False]
}
base_condition = [base in bases for base in ['A', 'C', 'G', 'T']]
for symbol, iupac_condition in iupac_notation.items():
if iupac_condition == base_condition:
return symbol
def is_transition(self, ref_base, alt_base):
"""
Identify SNP as being a transition or not.
1: Transition, 0:Transversion
"""
substitution = ref_base + alt_base
transition = ['AG', 'GA', 'CT', 'TC']
if substitution in transition:
return 1
return 0
class VCFTools(object):
"""A class for parsing VCF formatted files."""
def __init__(self, vcf_file):
"""Initialize variables."""
self.reader = vcf.Reader(open(vcf_file, 'r'))
self.records = [record for record in self.reader]
def add_information_fields(self, info_list):
"""Add a given list of information fields to the VCF."""
for i in info_list:
id, num, type, desc = i
self.__add_information_field(id, num, type, desc)
def __add_information_field(self, id, num, type, desc):
"""Add a given information field to the VCF."""
_Info = collections.namedtuple('Info', ['id', 'num', 'type', 'desc'])
if id:
self.reader.infos[id] = _Info(id, num, type, desc)
def write_vcf(self, output='/dev/stdout'):
"""Write the VCF to a given output file."""
vcf_writer = vcf.Writer(open(output, 'w'), self.reader)
for record in self.records:
vcf_writer.write_record(record)
if __name__ == '__main__':
import argparse as ap
import os
import sys
parser = ap.ArgumentParser(
prog='vcf-annotator.py',
conflict_handler='resolve',
description=("Annotate variants from a VCF file using the reference "
"genome's GenBank file.")
)
parser.add_argument('vcf', metavar="VCF_FILE", type=str,
help='VCF file of variants')
parser.add_argument('gb', metavar="GENBANK_FILE", type=str,
help='GenBank file of the reference genome.')
parser.add_argument('--output', metavar="STRING", type=str,
default='/dev/stdout',
help='File to write VCF output to (Default STDOUT).')
parser.add_argument('--version', action='version',
version='%(prog)s {0}'.format(VERSION))
if len(sys.argv) == 1:
parser.print_help()
sys.exit(0)
args = parser.parse_args()
# Verify input exists
if not os.path.isfile(args.gb):
print('Unable to locate GenBank file: {0}'.format(args.gb))
sys.exit(1)
elif not os.path.isfile(args.vcf):
print('Unable to locate VCF file: {0}'.format(args.vcf))
sys.exit(1)
annotator = Annotator(gb_file=args.gb, vcf_file=args.vcf)
annotator.annotate_vcf_records()
annotator.write_vcf(args.output)