/
vcf_annotation.py
419 lines (374 loc) · 17.1 KB
/
vcf_annotation.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""Module containing tools for annotating VCFs with VRS
Example of how to run from root of vrs-python directory:
python3 -m src.ga4gh.vrs.extras.vcf_annotation --vcf_in input.vcf.gz \
--vcf_out output.vcf.gz --vrs_pickle_out vrs_objects.pkl
"""
import logging
import pickle
from enum import Enum
from typing import Dict, List, Optional
from timeit import default_timer as timer
import click
import pysam
from biocommons.seqrepo import SeqRepo
from pydantic import ValidationError
from ga4gh.core import VrsObjectIdentifierIs, use_ga4gh_compute_identifier_when
from ga4gh.vrs.dataproxy import SeqRepoDataProxy, SeqRepoRESTDataProxy, DataProxyValidationError
from ga4gh.vrs.extras.translator import AlleleTranslator
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
class VCFAnnotatorException(Exception):
"""Custom exceptions for VCF Annotator tool"""
class SeqRepoProxyType(str, Enum):
"""Define constraints for SeqRepo Data Proxy types"""
LOCAL = "local"
REST = "rest"
@click.command()
@click.option(
"--vcf_in",
required=True,
type=str,
help="The path for the input VCF file to annotate"
)
@click.option(
"--vcf_out",
required=False,
type=str,
help=("The path for the output VCF file. If not provided, must provide "
"--vrs_pickle_out.")
)
@click.option(
"--vrs_pickle_out",
required=False,
type=str,
help=("The path for the output VCF pickle file. If not provided, must provide "
"--vcf_out")
)
@click.option(
"--vrs_attributes",
is_flag=True,
default=False,
help="Will include VRS_Start, VRS_End, VRS_State fields in the INFO field.",
show_default=True
)
@click.option(
"--seqrepo_dp_type",
required=False,
default=SeqRepoProxyType.LOCAL,
type=click.Choice([v.value for v in SeqRepoProxyType.__members__.values()],
case_sensitive=True),
help="The type of the SeqRepo Data Proxy to use",
show_default=True,
show_choices=True
)
@click.option(
"--seqrepo_root_dir",
required=False,
default="/usr/local/share/seqrepo/latest",
help="The root directory for local SeqRepo instance",
show_default=True
)
@click.option(
"--seqrepo_base_url",
required=False,
default="http://localhost:5000/seqrepo",
help="The base url for SeqRepo REST API",
show_default=True
)
@click.option(
"--assembly",
required=False,
default="GRCh38",
show_default=True,
help="The assembly that the `vcf_in` data uses.",
type=str
)
@click.option(
"--skip_ref",
is_flag=True,
default=False,
show_default=True,
help="Skip VRS computation for REF alleles."
)
@click.option(
"--require_validation",
is_flag=True,
default=False,
show_default=True,
help="Require validation checks to pass in order to return a VRS object"
)
def annotate_click( # pylint: disable=too-many-arguments
vcf_in: str, vcf_out: Optional[str], vrs_pickle_out: Optional[str],
vrs_attributes: bool, seqrepo_dp_type: SeqRepoProxyType, seqrepo_root_dir: str,
seqrepo_base_url: str, assembly: str, skip_ref: bool, require_validation: bool
) -> None:
"""Annotate VCF file via click
Example arguments:
--vcf_in input.vcf.gz --vcf_out output.vcf.gz --vrs_pickle_out vrs_objects.pkl
"""
annotator = VCFAnnotator(seqrepo_dp_type, seqrepo_base_url, seqrepo_root_dir)
start = timer()
msg = f"Annotating {vcf_in} with the VCF Annotator..."
_logger.info(msg)
click.echo(msg)
annotator.annotate(
vcf_in, vcf_out=vcf_out, vrs_pickle_out=vrs_pickle_out,
vrs_attributes=vrs_attributes, assembly=assembly,
compute_for_ref=(not skip_ref), require_validation=require_validation
)
end = timer()
msg = f"VCF Annotator finished in {(end - start):.5f} seconds"
_logger.info(msg)
click.echo(msg)
class VCFAnnotator: # pylint: disable=too-few-public-methods
"""Provides utility for annotating VCF's with VRS Allele IDs.
VCF's are read using pysam and stored as pysam objects.
Alleles are translated into VRS Allele IDs using VRS-Python Translator.
"""
# Field names for VCF
VRS_ALLELE_IDS_FIELD = "VRS_Allele_IDs"
VRS_STARTS_FIELD = "VRS_Starts"
VRS_ENDS_FIELD = "VRS_Ends"
VRS_STATES_FIELD = "VRS_States"
VRS_ERROR_FIELD = "VRS_Error"
# VCF character escape map
VCF_ESCAPE_MAP = [
("%", "%25"),
(";", "%3B"),
(",", "%2C"),
("\r", "%0D"),
("\n", "%0A"),
("\t", "%09"),
]
def __init__(self, seqrepo_dp_type: SeqRepoProxyType = SeqRepoProxyType.LOCAL,
seqrepo_base_url: str = "http://localhost:5000/seqrepo",
seqrepo_root_dir: str = "/usr/local/share/seqrepo/latest") -> None:
"""Initialize the VCFAnnotator class
:param SeqRepoProxyType seqrepo_dp_type: The type of SeqRepo Data Proxy to use
:param str seqrepo_base_url: The base url for SeqRepo REST API
:param str seqrepo_root_dir: The root directory for the local SeqRepo instance
"""
if seqrepo_dp_type == SeqRepoProxyType.LOCAL:
self.dp = SeqRepoDataProxy(SeqRepo(seqrepo_root_dir))
else:
self.dp = SeqRepoRESTDataProxy(seqrepo_base_url)
self.tlr = AlleleTranslator(self.dp)
@use_ga4gh_compute_identifier_when(VrsObjectIdentifierIs.MISSING)
def annotate( # pylint: disable=too-many-arguments,too-many-locals
self, vcf_in: str, vcf_out: Optional[str] = None,
vrs_pickle_out: Optional[str] = None, vrs_attributes: bool = False,
assembly: str = "GRCh38", compute_for_ref: bool = True,
require_validation: bool = True
) -> None:
"""Annotates an input VCF file with VRS Allele IDs & creates a pickle file
containing the vrs object information.
:param str vcf_in: The path for the input VCF file to annotate
:param Optional[str] vcf_out: The path for the output VCF file
:param Optional[str] vrs_pickle_out: The path for the output VCF pickle file
:param bool vrs_attributes: If `True` will include VRS_Start, VRS_End,
VRS_State fields in the INFO field. If `False` will not include these fields.
Only used if `vcf_out` is provided.
:param str assembly: The assembly used in `vcf_in` data
:param compute_for_ref: If true, compute VRS IDs for the reference allele
:param bool require_validation: If `True` then validation checks must pass in
order to return a VRS object. If `False` then VRS object will be returned
even if validation checks fail.
"""
if not any((vcf_out, vrs_pickle_out)):
raise VCFAnnotatorException(
"Must provide one of: `vcf_out` or `vrs_pickle_out`")
info_field_num = "R" if compute_for_ref else "A"
info_field_desc = "REF and ALT" if compute_for_ref else "ALT"
vrs_data = {}
vcf_in = pysam.VariantFile(filename=vcf_in) # pylint: disable=no-member
vcf_in.header.info.add(
self.VRS_ALLELE_IDS_FIELD, info_field_num, "String",
("The computed identifiers for the GA4GH VRS Alleles corresponding to the "
f"GT indexes of the {info_field_desc} alleles")
)
vcf_in.header.info.add(
self.VRS_ERROR_FIELD, ".", "String",
("If an error occurred computing a VRS Identifier, the error message")
)
if vrs_attributes:
vcf_in.header.info.add(
self.VRS_STARTS_FIELD, info_field_num, "String",
("Interresidue coordinates used as the location starts for the GA4GH "
f"VRS Alleles corresponding to the GT indexes of the {info_field_desc} alleles")
)
vcf_in.header.info.add(
self.VRS_ENDS_FIELD, info_field_num, "String",
("Interresidue coordinates used as the location ends for the GA4GH VRS "
f"Alleles corresponding to the GT indexes of the {info_field_desc} alleles")
)
vcf_in.header.info.add(
self.VRS_STATES_FIELD, info_field_num, "String",
("The literal sequence states used for the GA4GH VRS Alleles "
f"corresponding to the GT indexes of the {info_field_desc} alleles")
)
if vcf_out:
vcf_out = pysam.VariantFile(vcf_out, "w", header=vcf_in.header) # pylint: disable=no-member
output_vcf = bool(vcf_out)
output_pickle = bool(vrs_pickle_out)
for record in vcf_in:
additional_info_fields = [self.VRS_ALLELE_IDS_FIELD]
if vrs_attributes:
additional_info_fields += [self.VRS_STARTS_FIELD, self.VRS_ENDS_FIELD, self.VRS_STATES_FIELD]
try:
vrs_field_data = self._get_vrs_data(
record, vrs_data, assembly, additional_info_fields,
vrs_attributes=vrs_attributes, output_pickle=output_pickle,
output_vcf=output_vcf, compute_for_ref=compute_for_ref,
require_validation=require_validation
)
except Exception as ex:
_logger.exception("VRS error on %s-%s", record.chrom, record.pos)
err_msg = f"{ex}" or f"{type(ex)}"
for search_repl in VCFAnnotator.VCF_ESCAPE_MAP:
err_msg = err_msg.replace(search_repl[0], search_repl[1])
additional_info_fields = [self.VRS_ERROR_FIELD]
vrs_field_data = {self.VRS_ERROR_FIELD: [err_msg]}
_logger.debug("VCF record %s-%s generated vrs_field_data %s", record.chrom, record.pos, vrs_field_data)
if output_vcf:
for k in additional_info_fields:
record.info[k] = [value or "." for value in vrs_field_data[k]]
vcf_out.write(record)
vcf_in.close()
if output_vcf:
vcf_out.close()
if vrs_pickle_out:
with open(vrs_pickle_out, "wb") as wf:
pickle.dump(vrs_data, wf)
def _get_vrs_object( # pylint: disable=too-many-arguments,too-many-locals
self, vcf_coords: str, vrs_data: Dict, vrs_field_data: Dict, assembly: str,
vrs_data_key: Optional[str] = None, output_pickle: bool = True,
output_vcf: bool = False, vrs_attributes: bool = False,
require_validation: bool = True
) -> None:
"""Get VRS Object given `vcf_coords`. `vrs_data` and `vrs_field_data` will
be mutated.
:param str vcf_coords: Allele to get VRS object for. Format is chr-pos-ref-alt
:param Dict vrs_data: Dictionary containing the VRS object information for the
VCF
:param Dict vrs_field_data: If `output_vcf`, will keys will be VRS Fields and
values will be list of VRS data. Else, will be an empty dictionary
:param str assembly: The assembly used in `vcf_coords`
:param Optional[str] vrs_data_key: The key to update in `vrs_data`. If not
provided, will use `vcf_coords` as the key.
:param bool output_pickle: `True` if VRS pickle file will be output.
`False` otherwise.
:param bool output_vcf: `True` if annotated VCF file will be output.
`False` otherwise.
:param bool vrs_attributes: If `True` will include VRS_Start, VRS_End,
VRS_State fields in the INFO field. If `False` will not include these fields.
Only used if `output_vcf` set to `True`.
:param bool require_validation: If `True` then validation checks must pass in
order to return a VRS object. If `False` then VRS object will be returned
even if validation checks fail. Defaults to `True`.
"""
try:
vrs_obj = self.tlr._from_gnomad(
vcf_coords,
assembly_name=assembly,
require_validation=require_validation
)
except (ValidationError, DataProxyValidationError) as e:
vrs_obj = None
_logger.error("ValidationError when translating %s from gnomad: %s", vcf_coords, str(e))
raise
except KeyError as e:
vrs_obj = None
_logger.error("KeyError when translating %s from gnomad: %s", vcf_coords, str(e))
raise
except AssertionError as e:
vrs_obj = None
_logger.error("AssertionError when translating %s from gnomad: %s", vcf_coords, str(e))
raise
except Exception as e: # pylint: disable=broad-except
vrs_obj = None
_logger.error("Unhandled Exception when translating %s from gnomad: %s", vcf_coords, str(e))
raise
else:
if not vrs_obj:
_logger.debug("None was returned when translating %s from gnomad", vcf_coords)
if output_pickle and vrs_obj:
key = vrs_data_key if vrs_data_key else vcf_coords
vrs_data[key] = str(vrs_obj.model_dump(exclude_none=True))
if output_vcf:
allele_id = vrs_obj.id if vrs_obj else ""
vrs_field_data[self.VRS_ALLELE_IDS_FIELD].append(allele_id)
if vrs_attributes:
if vrs_obj:
start = str(vrs_obj.location.start)
end = str(vrs_obj.location.end)
if vrs_obj.state.sequence:
alt = str(vrs_obj.state.sequence.root)
else:
alt = ""
else:
start = ""
end = ""
alt = ""
vrs_field_data[self.VRS_STARTS_FIELD].append(start)
vrs_field_data[self.VRS_ENDS_FIELD].append(end)
vrs_field_data[self.VRS_STATES_FIELD].append(alt)
def _get_vrs_data( # pylint: disable=too-many-arguments,too-many-locals
self, record: pysam.VariantRecord, vrs_data: Dict, assembly: str, # pylint: disable=no-member
additional_info_fields: List[str], vrs_attributes: bool = False,
output_pickle: bool = True, output_vcf: bool = True,
compute_for_ref: bool = True, require_validation: bool = True
) -> Dict:
"""Get VRS data for record's reference and alt alleles.
:param pysam.VariantRecord record: A row in the VCF file
:param Dict vrs_data: Dictionary containing the VRS object information for the
VCF. Will be mutated if `output_pickle = True`
:param str assembly: The assembly used in `record`
:param List[str] additional_info_fields: Additional VRS fields to add in INFO
field
:param bool vrs_attributes: If `True` will include VRS_Start, VRS_End,
VRS_State fields in the INFO field. If `False` will not include these fields.
Only used if `output_vcf` set to `True`.
:param bool output_pickle: `True` if VRS pickle file will be output.
`False` otherwise.
:param bool output_vcf: `True` if annotated VCF file will be output.
`False` otherwise.
:return: If `output_vcf = True`, a dictionary containing VRS Fields and list
of associated values. If `output_vcf = False`, an empty dictionary will be
returned.
:param compute_for_ref: If true, compute VRS IDs for the reference allele
:param bool require_validation: If `True` then validation checks must pass in
order to return a VRS object. A `DataProxyValidationError` will be raised if
validation checks fail. If `False` then VRS object will be returned even if
validation checks fail. Defaults to `True`.
"""
vrs_field_data = {field: [] for field in additional_info_fields} if output_vcf else {}
# Get VRS data for reference allele
gnomad_loc = f"{record.chrom}-{record.pos}"
if compute_for_ref:
reference_allele = f"{gnomad_loc}-{record.ref}-{record.ref}"
self._get_vrs_object(
reference_allele, vrs_data, vrs_field_data, assembly,
output_pickle=output_pickle, output_vcf=output_vcf,
vrs_attributes=vrs_attributes, require_validation=require_validation
)
# Get VRS data for alts
alts = record.alts or []
alleles = [f"{gnomad_loc}-{record.ref}-{a}" for a in [*alts]]
data = f"{record.chrom}\t{record.pos}\t{record.ref}\t{record.alts}"
for allele in alleles:
if "*" in allele:
_logger.debug("Star allele found: %s", allele)
if output_vcf:
for field in additional_info_fields:
vrs_field_data[field].append("")
else:
self._get_vrs_object(
allele, vrs_data, vrs_field_data, assembly, vrs_data_key=data,
output_pickle=output_pickle, output_vcf=output_vcf,
vrs_attributes=vrs_attributes, require_validation=require_validation
)
return vrs_field_data
if __name__ == "__main__":
# python3 -m src.ga4gh.vrs.extras.vcf_annotation --vcf_in input.vcf.gz \
# --vcf_out output.vcf.gz --vrs_pickle_out vrs_objects.pkl
annotate_click() # pylint: disable=no-value-for-parameter