Examples¶
These tasks are adapted from the examples in Bioconductor’s biomaRt vignette (version 2.68), pybiomart’s README and Scanpy’s documentation. The outputs are from release 116 and the new platform’s human 2026_04 annotation. The BioMart queries return the same rows as the release-116 BioMart, though row order can differ.
BioMart queries¶
import ens_tables as et
Location of TP53 and its mouse homolog¶
biomaRt vignette, section 6.11. Homolog attributes are on a separate attribute page, so the location and the homolog come from separate queries.
et.query("hsapiens_gene_ensembl", ["ensembl_gene_id", "chromosome_name", "start_position"], {"hgnc_symbol": "TP53"})
ensembl_gene_id chromosome_name start_position
0 ENSG00000141510 17 7661779
homologs = et.query(
"hsapiens_gene_ensembl", ["ensembl_gene_id", "mmusculus_homolog_ensembl_gene"], {"hgnc_symbol": "TP53"}
)
et.query(
"mmusculus_gene_ensembl",
["refseq_mrna", "chromosome_name", "start_position"],
{"ensembl_gene_id": homologs["mmusculus_homolog_ensembl_gene"].tolist()},
)
refseq_mrna chromosome_name start_position
0 NM_011640 11 69469669
1 NM_001127233 11 69469669
2 NaN 11 69469669
GO terms for Entrez gene IDs¶
Section 6.2.
go = et.query("hsapiens_gene_ensembl", ["entrezgene_id", "go_id"], {"entrezgene_id": ["673", "837"]})
go.head()
entrezgene_id go_id
0 673 GO:0005524
1 673 GO:0007165
2 673 GO:0006468
3 673 GO:0004672
4 673 GO:0000166
Genes with a GO term¶
Section 6.6, MAP kinase activity.
et.query("hsapiens_gene_ensembl", ["entrezgene_id", "hgnc_symbol"], {"go": "GO:0004707"})
entrezgene_id hgnc_symbol
0 1022 CDK7
1 225689 MAPK15
2 6300 MAPK12
3 5596 MAPK4
4 5600 MAPK11
5 5595 MAPK3
6 6885 MAP3K7
7 1432 MAPK14
8 5597 MAPK6
9 5127 CDK16
10 5594 MAPK1
11 5891 MOK
12 2932 GSK3B
13 8621 CDK13
14 5599 MAPK8
15 5601 MAPK9
16 5603 MAPK13
17 5602 MAPK10
18 51701 NLK
19 5598 MAPK7
Genes with given GO terms on given chromosomes¶
Section 6.3. A gene must match one of the GO terms and one of the chromosomes.
et.query(
"hsapiens_gene_ensembl",
["hgnc_symbol"],
{"go": ["GO:0051330", "GO:0000080", "GO:0000114", "GO:0000082"], "chromosome_name": ["17", "20", "Y"]},
)
hgnc_symbol
0 RPS6KB1
1 CDK3
Gene symbols and locations for probe IDs¶
Section 6.1.
et.query(
"hsapiens_gene_ensembl",
["affy_hg_u133_plus_2", "hgnc_symbol", "chromosome_name", "start_position", "end_position", "band"],
{"affy_hg_u133_plus_2": ["202763_at", "209310_s_at", "207500_at"]},
)
affy_hg_u133_plus_2 hgnc_symbol chromosome_name start_position end_position band
0 207500_at CASP5 11 104991932 105023169 q22.3
1 202763_at CASP3 4 184627695 184650418 q35.1
2 209310_s_at CASP4 11 104942866 104969366 q22.3
Probes on genes in a region¶
Section 6.5. start and end select the genes that overlap the interval; genes without a probe have a missing value.
region = et.query(
"hsapiens_gene_ensembl",
["affy_hg_u133_plus_2", "ensembl_gene_id"],
{"chromosome_name": "16", "start": 1100000, "end": 1250000},
)
region.dropna()
affy_hg_u133_plus_2 ensembl_gene_id
9 215502_at ENSG00000260532
11 220339_s_at ENSG00000116176
12 210084_x_at ENSG00000197253
13 217023_x_at ENSG00000197253
14 216474_x_at ENSG00000197253
15 205683_x_at ENSG00000197253
16 207134_x_at ENSG00000197253
17 215382_x_at ENSG00000197253
18 210084_x_at ENSG00000172236
19 217023_x_at ENSG00000172236
20 216474_x_at ENSG00000172236
21 205683_x_at ENSG00000172236
22 207134_x_at ENSG00000172236
23 215382_x_at ENSG00000172236
24 205845_at ENSG00000196557
New Ensembl platform¶
Two of the tasks above, from the human 2026_04 tables. GO annotations and homology types can differ from BioMart’s; see the platform differences.
human = {"assembly": "GCA_000001405.29", "annotation": "2026_04"}
Mouse homologs of TP53¶
genes = et.genes(**human)
tp53 = genes.loc[genes["external_gene_name"] == "TP53", "ensembl_gene_id"]
mouse = et.homologies(**human, homology="2026_07_13", ref_species="mus_musculus")
mouse.loc[mouse["ensembl_gene_id"].isin(tp53), ["ensembl_gene_id", "ref_gene_stable_id", "homology_type"]]
ensembl_gene_id ref_gene_stable_id homology_type
7846 ENSG00000141510 ENSMUSG00000059552 homolog_rbbh
GO terms for Entrez gene IDs¶
entrez = et.xrefs(**human, db="NCBI gene (formerly Entrezgene)")
go = et.xrefs(**human, db="GO")
selected = entrez.loc[entrez["xref_id"].isin(["673", "837"]), ["ensembl_gene_id", "xref_id"]].drop_duplicates()
gene_go = go[["ensembl_gene_id", "xref_id"]].drop_duplicates()
selected.merge(gene_go, on="ensembl_gene_id", suffixes=("_entrez", "_go")).head()
ensembl_gene_id xref_id_entrez xref_id_go
0 ENSG00000157764 673 GO:0000166
1 ENSG00000157764 673 GO:0004672
2 ENSG00000157764 673 GO:0004674
3 ENSG00000157764 673 GO:0005524
4 ENSG00000157764 673 GO:0005829
Code written for pybiomart¶
The example from pybiomart’s README, with the import changed.
from ens_tables.pybiomart import Dataset
dataset = Dataset(name="hsapiens_gene_ensembl", host="http://www.ensembl.org")
dataset.query(attributes=["ensembl_gene_id", "external_gene_name"], filters={"chromosome_name": ["1", "2"]})
Gene stable ID Gene name
0 ENSG00000227364 NaN
1 ENSG00000286905 NaN
2 ENSG00000231482 NaN
3 ENSG00000228391 NaN
4 ENSG00000225234 TRAPPC12-AS1
... ... ...
12790 ENSG00000173372 C1QA
12791 ENSG00000164007 CLDN19
12792 ENSG00000221986 MYBPHL
12793 ENSG00000203705 TATDN3
12794 ENSG00000132688 NES
[12795 rows x 2 columns]
Code written for scanpy.queries¶
The examples from Scanpy’s documentation, with the import changed. adata is an AnnData object indexed by Ensembl gene IDs.
from ens_tables import scanpy as queries
annot = queries.biomart_annotations(
"hsapiens",
["ensembl_gene_id", "start_position", "end_position", "chromosome_name"],
).set_index("ensembl_gene_id")
adata.var[annot.columns] = annot
mito_gene_names = queries.mitochondrial_genes("hsapiens")
mito_ensembl_ids = queries.mitochondrial_genes("hsapiens", attrname="ensembl_gene_id")
mito_ensembl_ids.head()
ensembl_gene_id
0 ENSG00000210049
1 ENSG00000211459
2 ENSG00000210077
3 ENSG00000210082
4 ENSG00000209082