Migrating BioMart code¶
This guide is for code that queries Ensembl BioMart, directly or through pybiomart or scanpy.queries. BioMart is frozen at release 116 and is not moving to the new Ensembl platform. ens-tables supports the queries and releases described below; annotation after release 116 needs the new platform’s tables.
1. Keep existing BioMart queries¶
pybiomart¶
For the supported query and navigation methods below, change the import. The tested queries preserve pybiomart’s columns and dtypes; check the differences below before migrating other uses.
from ens_tables.pybiomart import Dataset, Server # instead of: from pybiomart import Dataset, Server
dataset = Dataset(name="hsapiens_gene_ensembl", host="http://www.ensembl.org")
genes = dataset.query(attributes=["ensembl_gene_id", "external_gene_name"], filters={"chromosome_name": ["MT"]})
mart = Server(host="http://www.ensembl.org")["ENSEMBL_MART_ENSEMBL"]
mouse = mart["mmusculus_gene_ensembl"]
The host only selects the Ensembl release:
Host |
Release |
|---|---|
|
116 |
|
116 … 105 |
|
80, 77, 75 |
any other host |
pass |
An explicit release=N overrides the host mapping and selects the standard Ensembl dump for that release; it does not select another provider or the current GRCh37 service. The default host, including host=None, is Ensembl, whereas original pybiomart defaults to www.biomart.org.
scanpy.queries¶
from ens_tables import scanpy as queries # instead of: scanpy.queries
mito = queries.mitochondrial_genes("hsapiens")
annotations = queries.biomart_annotations(
"hsapiens", ["ensembl_gene_id", "start_position", "end_position", "chromosome_name"]
).set_index("ensembl_gene_id")
Raw martservice XML¶
A single-dataset XML query with supported attributes and filters becomes one query() call:
<Query virtualSchemaName="default" formatter="TSV" header="0" uniqueRows="1">
<Dataset name="hsapiens_gene_ensembl" interface="default">
<Filter name="chromosome_name" value="21"/>
<Attribute name="ensembl_gene_id"/>
<Attribute name="hgnc_symbol"/>
</Dataset>
</Query>
import ens_tables as et
et.query("hsapiens_gene_ensembl", ["ensembl_gene_id", "hgnc_symbol"], {"chromosome_name": "21"}, release=116)
uniqueRows="1" is the default; pass unique=False for uniqueRows="0". A boolean filter written as excluded="1" becomes False, and excluded="0" becomes True.
query() takes Python arguments, not XML, and is not a drop-in replacement for GSEApy’s Biomart class. Multi-dataset queries and other BioMart formatters are not supported.
Differences from a BioMart server¶
BioMart and pybiomart |
ens-tables |
|
|---|---|---|
Option filters such as |
pybiomart rejects them as unknown |
supported |
Empty filter values ( |
ignored by some filters, rejected by others |
an error; leave the filter out instead |
|
supported |
an error; for one interval, use |
Sequences and other server-computed attributes |
supported |
marked unavailable in |
Marts other than |
available |
an error |
Attributes from several attribute pages in one query |
an error |
the same error |
|
control the remote connection and requests cache |
accepted and ignored; the local table cache is always used |
Other HTTP methods and connection properties |
available on pybiomart objects |
not reproduced |
Errors |
|
|
Row order |
server order |
not guaranteed; sort if order matters |
Metadata lists |
pybiomart reads its filter and attribute descriptions |
include additional option filters and resolved attribute pointers from the dump configuration |
Single-column results |
pybiomart drops missing values through TSV parsing |
the adapters do the same; native |
GRCh37 |
|
|
Release 89 |
archive |
not supported; its dumps are compressed twice |
Nine release-116 comparisons with pybiomart 0.2.0 and Scanpy 1.13.0 on human and mouse match on columns, dtypes and values after sorting rows.
2. Move to the new Ensembl platform¶
Choose tables by assembly accession and annotation date, and homologies also by homology date:
import ens_tables as et
human = {"assembly": "GCA_000001405.29", "annotation": "2026_04"}
genes = et.genes(**human)
For human and mouse 2026_04, the tested shared gene and transcript field values match release 116 when empty and missing gene descriptions are treated alike. Human HGNC mappings and human and mouse Entrez mappings also match after projection to unique gene and external ID pairs, excluding missing BioMart mappings. These comparisons use the default include_alt=True. The new functions return separate tables with their own rows and dtypes, so a migrated query can need a projection, deduplication or join.
BioMart attribute |
New platform |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The table maps fields only; GO, UniProt and homology relations can differ between the platforms. xrefs() keeps records at gene, transcript and protein level. For a gene-level mapping, select the gene and external ID columns and drop duplicates, as in the common tasks.
Homologies come from a different method: the new platform publishes homolog_rbbh and homolog_bbh pairs from each species’ own file instead of Compara’s one-to-one and one-to-many types. Human-to-mouse and mouse-to-human pairs are different sets, so read the file of the species you start from.
Recipes¶
# Mitochondrial genes, as scanpy.queries.mitochondrial_genes("hsapiens")
mito = genes.loc[genes["chromosome_name"] == "MT", ["external_gene_name"]]
# HGNC symbols as query(..., ["ensembl_gene_id", "hgnc_symbol"]) gives them, without the genes that have none
hgnc = et.xrefs(**human, db="HGNC Symbol")[["ensembl_gene_id", "xref_label"]]
hgnc = hgnc.drop_duplicates().rename(columns={"xref_label": "hgnc_symbol"})
# Mouse homologs of human genes, one row per pair
mouse = et.homologies(**human, homology="2026_07_13", ref_species="mus_musculus")
pairs = mouse[["ensembl_gene_id", "ref_gene_stable_id", "homology_type"]]
Every db value is a full db_name; a wrong name raises an error that lists the available ones.
3. Offline use and CI¶
The cache lives in
~/.cache/ens-tables($XDG_CACHE_HOMEis respected);ENS_TABLES_CACHEmoves it. Run the intended queries once online to fill all required tables and metadata, then restore that cache for offline jobs. Include the ens-tables version in the CI cache key: an update can require a new conversion-cache format.Queries and table functions work offline once their inputs are cached.
annotations()always reads the published directory listing online; record the selected assembly and dates in the script instead of discovering them during an offline run.ENS_TABLES_FTPandENS_TABLES_ORGANISMSpoint to mirrors ofhttps://ftp.ensembl.org/pubandhttps://ftp.ebi.ac.uk/pub/ensemblorganisms, includingfile://directories.Several processes can fill the same cache at once; each writes to its own temporary files.
logging.basicConfig(level=logging.INFO)shows downloads, conversions and cache hits.