Batch annotate a pandas DataFrame and merge GeneBe results
When the same variant appears in multiple samples or experiments, annotate it once and attach the result to every source row. This example starts with normalized, single-allele hg38 (GRCh38) variants and keeps the original sample metadata and row count.
Send unique variants to GeneBe​
Install the dependencies with python -m pip install pandas requests. The table needs genome, chr, pos, ref and alt for every variant. If you have hg19 coordinates or HGVS, first use the liftover or HGVS conversion workflow.
Send a JSON array with chr, pos, ref and alt to POST /cloud/api-public/v1/variants?genome=hg38&omitNormalization=true. With already normalized input, omitNormalization=true preserves the allele representation used for the join. Normalize indels consistently before running the workflow if your source uses different representations for the same allele.
import pandas as pd
import requests
variants = pd.DataFrame(
[
("A", "hg38", "chr7", 117540213, "T", "A", 43),
("B", "hg38", "7", 117540213, "T", "A", 61),
("C", "hg38", "7", 140753336, "A", "T", 54),
],
columns=["sample", "genome", "chr", "pos", "ref", "alt", "depth"],
).reset_index(drop=True)
key = ["genome", "chr", "pos", "ref", "alt"]
fields = ["gene_symbol", "acmg_classification", "clinvar_classification", "gnomad_exomes_af"]
def variant_keys(frame):
keys = frame[key].copy()
keys["chr"] = keys["chr"].astype("string").str.removeprefix("chr")
keys["pos"] = keys["pos"].astype("Int64")
for allele in ("ref", "alt"):
keys[allele] = keys[allele].astype("string").str.upper()
return keys
keys = variant_keys(variants)
if keys.isna().any().any() or not keys["genome"].eq("hg38").all():
raise ValueError("Expected complete hg38 variant keys")
queries = keys[["chr", "pos", "ref", "alt"]].drop_duplicates()
url = "https://api.genebe.net/cloud/api-public/v1/variants"
records = []
with requests.Session() as session:
for start in range(0, len(queries), 50):
batch = queries.iloc[start : start + 50].to_dict("records")
response = session.post(
url,
params={"genome": "hg38", "omitNormalization": "true"},
json=batch,
timeout=30,
)
response.raise_for_status()
for variant in response.json()["variants"]:
records.append({
"genome": "hg38",
**{name: variant[name] for name in key[1:]},
**{name: variant.get(name) for name in fields},
})
annotations = pd.DataFrame.from_records(records, columns=key + fields)
annotations[key] = variant_keys(annotations)
# Join on temporary columns to preserve the original spelling, e.g. chr7.
join_names = {name: f"__key_{name}" for name in key}
source = variants.join(keys.rename(columns=join_names))
merged = source.merge(
annotations.rename(columns=join_names),
on=list(join_names.values()),
how="left",
validate="many_to_one",
indicator="annotation_match",
)
if len(merged) != len(variants):
raise ValueError("Annotation changed the number of source rows")
merged.drop(columns=list(join_names.values())).to_csv("variants.annotated.csv", index=False)
Check the joined table​
This example sends two unique variants and writes all three source rows. The annotation values are examples and can change as the databases update.
| Sample | Original chromosome | Depth | Gene | GeneBe ACMG classification | Annotation match |
|---|---|---|---|---|---|
| A | chr7 | 43 | CFTR | Uncertain_significance | both |
| B | 7 | 61 | CFTR | Uncertain_significance | both |
| C | 7 | 54 | BRAF | Pathogenic | both |
Use annotation_match == "left_only" to find source variants with no matching annotation row. An annotation can match and still have a missing field. For example, an empty gnomad_exomes_af means the response did not supply an exome frequency. Preserve that missing value when filtering. See the API response fields for the available annotations.