Repository navigation
feat(annotator): read VRS Alleles back from annotated VCFs - #696
Open
NopeCharon wants to merge 1 commit into
Open
NopeCharon wants to merge 1 commit into
NopeCharon wants to merge 1 commit into
Conversation
The VCF annotator writes enough INFO fields (with `--vrs-attributes`) to
reconstruct the VRS Alleles of each record, but VRS-Python had no code to
do so.
Add `VcfReader`, whose `read()` method iterates over an annotated VCF and
yields each pysam record together with its VRS Alleles:
* The list of Alleles lines up with the `VRS_Allele_IDs` values: one per
REF and ALT allele, or one per ALT allele for VCFs annotated with
`--skip-ref` (INFO Number=A). Alleles without a VRS ID (`*` ALTs,
untranslatable ALTs, records with a `VRS_Error`) are `None`.
* Each Allele is built from the record's CHROM (resolved to a refget
accession through the data proxy) and its VRS_Starts, VRS_Ends,
VRS_States, VRS_Lengths and VRS_RepeatSubunitLengths values, as a
LiteralSequenceExpression, ReferenceLengthExpression or LengthExpression
state. Missing ("." or empty) values are mapped back the way the
annotator wrote them: an empty sequence for LSEs and zero-length RLEs,
no sequence for RLEs longer than the annotator's sequence limit.
* Allele and location identifiers are computed the same way as by the
translator, and the Allele ID must match the annotated one.
* `VcfReaderError` is raised for VCFs without VRS attribute annotations
and for records whose annotations are incomplete, invalid or don't
reproduce the annotated ID.
Tests round-trip the annotator's output (with and without REF alleles)
against its NDJSON dump using the local test SeqRepo, and cover star
alleles, failed records, each state type and malformed annotations. The
VCF annotator docs describe the new reader.
Closes ga4gh#595
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #595.
Problem
vrs-annotate vcf --vrs-attributeswrites INFO fields that are meant to be enough to reconstruct each record's VRS Alleles, but VRS-Python doesn't have code that reads them back. Users can't get Allele objects out of an annotated VCF, and nothing checks that the annotations actually reproduce the annotated IDs.Changes
Add
VcfReadertoga4gh.vrs.extras.annotator.vcf.VcfReader(data_proxy).read(path, assembly="GRCh38")is a generator that yields(pysam record, list of VRS Alleles)for each record:VRS_Allele_IDsvalue: one per REF and ALT (record.alleles), or one per ALT (record.alts) for VCFs annotated with--skip-ref(Number=A). Entries areNonefor alleles without a VRS ID:*ALTs, ALTs that couldn't be translated, and every allele of a record with aVRS_Error.VRS_Starts,VRS_Ends,VRS_States,VRS_LengthsandVRS_RepeatSubunitLengths, plus the record's CHROM. The data proxy resolves<assembly>:<CHROM>to a refget accession, the same way_from_gnomaddoes. The state is aLiteralSequenceExpression(no length), aReferenceLengthExpression(length and repeat subunit length) or aLengthExpression(length only)..) are mapped back the way the annotator writes them. Re theNonevs""question in the issue: an empty sequence is written as., so it is read back as""for LSEs and zero-length RLEs. RLE sequences longer thanRLE_SEQ_LIMITaren't written to the VCF, so those states come back withoutsequence. That doesn't change the identifier.VcfReaderErroris raised in these cases:--vrs-attributes);docs/extras/vcf_annotator.mdgets a "Reading VRS Alleles from an annotated VCF" section with a usage example.Tests
New
tests/extras/test_vcf_reader.py. It uses the local test SeqRepo (all test VCF records are on chr19), so no new cassettes are needed:test_read_vcf_round_trip: annotatestest_vcf_input.vcfwith and without REF alleles, reads the output back and checks that the Alleles exactly match the annotator's NDJSON dump.test_read_vcf/test_read_vcf_altsonly: the committed expected-output VCFs cover RLE REF alleles, an LSE ALT, a zero-length RLE deletion, a*ALT, an untranslatable ALT and aVRS_Errorrecord.test_read_vcf_not_annotated: rejects an unannotated VCF and one annotated without VRS attributes.test_read_vcf_invalid_annotations: rejects edited annotations (ID mismatch, missing end, invalid sequence, wrong number of values).test_get_state: covers each state type, includingLengthExpressionand an RLE whose sequence was left out.