Skip to main content

Filter VCF/BCF files with Python expressions.

Project description

vembrane: variant filtering using python expressions

Vembrane allows to simultaneously filter variants based on any INFO field, CHROM, POS, REF, ALT, QUAL, and the annotation field ANN. When filtering based on ANN, annotation entries are filtered first. If no annotation entry remains, the entire variant is deleted.

Filter expression

The filter expression can be any valid python expression that evaluates to bool. However, functions and symbols available have been restricted to the following:

  • any, all, min, max, sum
  • list, dict, set, tuple,
  • zip, map
  • Any function or symbol from math
  • Regular expressions via re

Available fields

The following VCF fields can be accessed in the filter expression:

Name Type Interpretation Example expression
INFO Dict[str, Any¹] INFO field -> Value INFO["DP"] > 0
ANN Dict[str, Any²] ANN field -> Value ANN["Gene_Name"] == "CDH2"
CHROM str Chromosome Name CHROM == "chr2"
POS int Chromosomal position 24 < POS < 42
ID str Variant ID ID == "rs11725853"
REF str Reference allele REF == "A"
ALT List[str] Alternative alleles "C" in ALT or ALT[0] == "G"
QUAL float Quality QUAL >= 60
FILTER
FORMAT Dict[str, Dict[str, Any¹]] Sample -> (Format -> Value) FORMAT[SAMPLES[0]]["DP"][0] > 0
SAMPLES List[str] [Sample] "Tumor" in SAMPLES

¹ depends on type specified in VCF header

² for the usual snpeff and vep annotations, custom types have been specified; any unknown ANN field will simply be of type str. If something lacks a custom parser/type, please consider filing an issue in the issue tracker.

Examples

  • Only keep annotations and variants where gene equals "CDH2" and its impact is "HIGH":
    vembrane variants.bcf 'ANN["Gene_Name"] == "CDH2" and ANN["Annotation_Impact"] == "HIGH"'
    
  • Only keep variants with quality at least 30:
    vembrane variants.vcf 'QUAL >= 30'
    
  • Only keep annotations and variants where feature (transcript) is ENST00000307301:
    vembrane variants.bcf 'ANN["Feature"] == "ENST00000307301"'
    
  • Only keep annotations and variants where protein position is less than 10:
    vembrane variants.bcf 'ANN["Protein"].start < 10'
    
  • Only keep variants where mapping quality is exactly 60:
    vembrane variants.bcf 'INFO["MQ"] == 60'
    
  • Only keep annotations and variants where consequence contains the word "stream" (matching "upstream" and "downstream"):
    vembrane variants.vcf 're.search("stream", ANN["Consequence"])'
    
  • Only keep annotations and variants where CLIN_SIG contains "pathogenic", "likely_pathogenic" or "drug_response":
    vembrane variants.vcf 'any(entry in ANN["CLIN_SIG"] for entry in ("pathogenic", "likely_pathogenic", "drug_response"))'
    

Custom ANN types

vembrane parses the following annotation fields to a custom type:

  • (snpeff) cDNA.pos / cDNA.length, CDS.pos / CDS.length and AA.pos / AA.length are re-exposed as cDNA, CDS and AA respectively with properties start, end and length, e.g. can be accessed like this: ANN["cDNA"].start
  • (vep) cDNA_position, CDS_position and Protein_position are re-exposed as cDNA, CDS and Protein respectively with properties start, end and length, e.g. can be accessed like this: ANN["cDNA"].start
  • CLIN_SIG is split at '&' into a list of entries

Any unknown annotation field will be left as is.

Missing values in annotations

If a certain annotation field lacks a value, it will be replaced with the special value of NA. Comparing with this value will always result in False, e.g. ANN["cDNA"].start > 0 will always evaluate to False if there was no value in the "cDNA.pos / cDNA.length" field of (snpeff) ANN (otherwise the comparison will be carried out with the usual semantics). One way to handle optional values is by asserting that the field is not None, e.g ID and "foo" in ID.

Development

pre-commit hooks

Since we enforce code formatting with black by checking for that in CI, we can avoid "fmt" commits by ensuring formatting is done upon comitting changes:

  1. make sure pre-commit is installed on your machine / in your env (should be available in pip, conda, archlinux repos, ...)
  2. run pre-commit install. This will activate pre-commit hooks to your local .git

Now when calling git commit, your changed code will be formatted with black, checked withflake8, get trailing whitespace removed and trailing newlines added (if needed)

Authors

  • Marcel Bargull (@mbargull)
  • Jan Forster (@jafors)
  • Till Hartmann (@tedil)
  • Johannes Köster (@johanneskoester)
  • Elias Kuthe (@eqt)
  • Felix Mölder (@felixmoelder)
  • Christopher Schröder (@christopher-schroeder)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vembrane-0.2.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

vembrane-0.2.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file vembrane-0.2.1.tar.gz.

File metadata

  • Download URL: vembrane-0.2.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.8.4 Linux/5.3.0-1031-azure

File hashes

Hashes for vembrane-0.2.1.tar.gz
Algorithm Hash digest
SHA256 33ffcb6bf5adda268c4c0f573aa913c40219953f068ac4baefacf60d7893db35
MD5 f08d89250c092c72448f36c1bcf5d1bb
BLAKE2b-256 a90f487fec7f0f79c81411b17322df6cf38f1a1186ba067615516149d51c11d7

See more details on using hashes here.

File details

Details for the file vembrane-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: vembrane-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.8.4 Linux/5.3.0-1031-azure

File hashes

Hashes for vembrane-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 68439a08a4274f015c2847edfd2c0736daec9f92cacc10cebbfd9ce7a5a87e3e
MD5 553418946d8ea20fde9f16de505a2647
BLAKE2b-256 c5b26fc14d13417b9ab644023862f06fed952b3b3b24c30d64edf74ff0f6b2a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page