BioThings Client
biothings_client is an easy-to-use Python wrapper to access any BioThings API-based backend service. It provides unified access to various biological data APIs such as MyGene.Info, MyVariant.Info, MyChem.Info, MyDisease.Info, MyGeneset.Info, and MyTaxon.Info. The library is actively maintained, with the current stable version being 0.5.0, and typically sees updates and bug fixes on a regular cadence.
Warnings
- breaking Support for Python 3.6 was dropped in version 0.5.0. The last version to support Python 3.6 is 0.4.1. Python <=3.5 (including 2.7) was dropped as of version 0.4.0, with 0.3.1 being the last supporting version.
- breaking Version 0.4.0 replaced the underlying HTTP client from `requests` to `httpx` and the caching library from `requests_cache` to `hishel`. Code directly interacting with these internal components or expecting their behavior might break.
- breaking Version 0.4.0 introduced an `ImportError: cannot import name 'alwayslist' from 'biothings_client'` for some downstream libraries. This indicates a change in the public API or internal structure.
- gotcha The caching feature (`biothings_client[caching]`) explicitly requires Python >=3.8. It will not be available or function correctly on Python 3.7.
- gotcha Older, specific BioThings API clients (e.g., `mygene.py`, `myvariant.py`) are now thin wrappers around `biothings_client` and are expected to be deprecated in the future.
- gotcha When requesting data as pandas DataFrames (using `as_dataframe=True`), especially for APIs returning very large or complex objects (e.g., `MyChem.info`), deeply nested data structures might lead to hard-to-use DataFrames or even cause hangs.
Install
-
pip install biothings_client -
pip install biothings_client[dataframe] -
pip install biothings_client[caching]
Imports
- get_client
from biothings_client import get_client
Quickstart
from biothings_client import get_client
# Get a client for the MyGene.info API
mg_client = get_client("gene")
# Fetch gene annotation for a specific Entrez gene ID
gene_id = "1017" # Example: CDK2 gene
result = mg_client.getgene(gene_id, fields="symbol,name,entrezgene")
print(f"Gene ID {gene_id}: {result.get('symbol')} - {result.get('name')}")
# Query multiple genes by symbol
gene_symbols = ["CDK2", "BRCA1", "TP53"]
results = mg_client.querymany(gene_symbols, scopes="symbol", fields="name,entrezgene,ensembl.gene")
for res in results:
print(f"Symbol: {res.get('query')}, Name: {res.get('name')}, Entrez: {res.get('entrezgene')}")
# Get a client for the MyVariant.info API
mv_client = get_client("variant")
# Fetch variant annotation
variant_id = "chr1:g.140453134T>C"
variant_result = mv_client.getvariant(variant_id)
# print(f"Variant {variant_id}: {variant_result}") # Uncomment to see full result
print(f"Variant {variant_id}: {variant_result.get('_id')} - {variant_result.get('dbsnp', {}).get('rsid')}")