Skip to main content

variantgrid_api

PyPi version Python versions

Python API client for VariantGrid Open source Variant database and analysis platform

See changelog

Install

python3 -m pip install variantgrid_api

Example

from variantgrid_api.api_client import VariantGridAPI
from variantgrid_api.data_models import EnrichmentKit

api = VariantGridAPI(server="https://variantgrid.com", api_token="YOUR_API_TOKEN")
enrichment_kit = EnrichmentKit(name="idt_haem", version=1)
result = api.create_enrichment_kit(enrichment_kit)

Annotate a VCF and download it back

Upload a VCF, have VariantGrid import + annotate any novel variants, then download the cohort-level annotated export (all samples, single-sample VCFs included). Annotation can take a while, so the quickest way in is the vg_api command line tool: the first call uploads, and running the same command again downloads the result once it's ready.

$ export VARIANTGRID_API_TOKEN=YOUR_API_TOKEN
$ vg_api annotate_vcf input.vcf.gz -o results/
Uploaded input.vcf.gz (id=13256).
Annotating input.vcf.gz - run the same command again later to download.

$ vg_api annotate_vcf input.vcf.gz -o results/        # once it's done
Annotated vcf written to results/input.vcf_annotated_v254_GRCh38.vcf.gz

From Python it's upload_file() / poll_upload_status() / download_annotated(), or the blocking annotate_vcf() one-liner. See Annotate a VCF on the wiki for batches, the submit-now/download-later pattern, and all the options.

Patients, specimens and extractions

VariantGrid can record which patient, specimen and extraction a lab's sequencing came from, and specimen-level measures such as TMB, MSI and GIS. This needs a server at or after SACGF/variantgrid#1716.

from variantgrid_api.data_models import Patient, Specimen, Extraction, ExternalReference, NucleicAcid

api.create_patient(Patient(patient_code="C0000001"))
api.create_specimen(Specimen(patient="C0000001", reference_id="2600000001"))
api.create_extraction(Extraction(specimen="2600000001", reference_id="2600000001C",
                                 nucleic_acid_source=NucleicAcid.DNA))
api.upload_file("sample.vcf.gz", path=None,
                metadata={"extraction": "2600000001C", "genome_build": "GRCh37"})

A bare string names a record by its local reference. Use ExternalReference(code=..., external_type=...) to name it by a LIMS identifier instead. See examples/example_tso500.py for a full run.

Talking to more than one VariantGrid version

Servers of different ages accept different calls. The client asks the server which features it has (GET api/v1/capabilities, fetched once on first use), and each call that needs a newer server checks that list first: the patient / specimen / extraction calls, specimen measures, link_sequencing_sample_extraction, the annotate flow (poll_upload_status, wait_for_annotation, download_annotated, annotate_vcf), and upload_file with metadata or file_type. A server without the capabilities endpoint counts as legacy and reports no features.

By default an unsupported call raises UnsupportedFeatureError. To run the same code against old and new servers, skip those calls instead: they log a warning and return None. upload_file is the exception: on a server without upload metadata it drops the metadata and still uploads the file.

from variantgrid_api.api_client import VariantGridAPI, UnsupportedFeaturePolicy

api = VariantGridAPI(server, api_token, unsupported_feature_policy=UnsupportedFeaturePolicy.SKIP)
api.create_patient(patient)        # skipped on a server without patients

# Uploads only when the server has an importer for this file type
api.upload_file("sample_CombinedVariantOutput.tsv", path=None,
                file_type="dragen_tso500_combined_variant_output")

Where the fallback isn't simply "do nothing", branch on api.supports("feature") or api.accepts_upload("file_type"). api.capabilities.version is useful for logging which server you reached. For tests, MockVariantGridAPI(capabilities=ServerCapabilities.LEGACY) behaves like an old server.

Testing

# Install required testing packages
python3 -m pip install -e ".[test]"
python3 -m pytest --cov=variantgrid_api

Metadata

Release files for variantgrid-api 1.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for variantgrid-api 1.8.0
File Size Uploaded
variantgrid_api-1.8.0.tar.gz 38.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for variantgrid-api 1.8.0
File Interpreter ABI Platform
variantgrid_api-1.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 62.8 kB

Release files / variantgrid_api-1.8.0.tar.gz

Download URL variantgrid_api-1.8.0.tar.gz
Size 38.0 kB
Tags Source
SHA-256 checksum
How to use checksums
70e776f84c711de4c3929c3d501b3cdae7cfaaa1019e0e0592d6127368513fb4
BLAKE2b-256 checksum
How to use checksums
74149beb11727a87cf43a85fdf20f9e07d8a902d84ed37b28dfea6393d4095c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / variantgrid_api-1.8.0-py3-none-any.whl

Download URL variantgrid_api-1.8.0-py3-none-any.whl
Size 24.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
47651ad4555601a878b6b17ee703040db0f9a18210ad6e7f18db3cbacb1b9db7
BLAKE2b-256 checksum
How to use checksums
3b4c9f8f910064a8e3834145d2fc1e29290c4a111b103a1c8a82faa3fc5b7817
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release history Release notifications | RSS feed

1.10.0

2 release files

1.9.0

2 release files

This release

1.8.0 This release

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.0.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1

2 release files

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page