| name | mygene |
| description | Gene ID conversion and annotation. Invoke when you need to map between any gene identifier formats. |
MyGene
Python client for the MyGene.info API — fast gene ID mapping and annotation.
When to Use
- Data has Ensembl IDs (ENSG..., ENSMUSG...) but you need gene symbols
- Data has RefSeq IDs (NM_..., NP_...) but you need gene symbols
- Data has Entrez IDs but you need symbols (or vice versa)
- Need gene annotations (GO terms, pathway membership, genomic position)
- Before running enrichment analysis (gseapy needs gene symbols)
Quick Start
import mygene
mg = mygene.MyGeneInfo()
result = mg.getgene('ENSG00000141510', fields='symbol,name')
gene_ids = ['ENSG00000141510', 'ENSG00000012048', 'ENSG00000146648']
results = mg.querymany(gene_ids, scopes='ensembl.gene', fields='symbol,entrezgene', species='human')
Batch ID Conversion
Ensembl → Symbol (most common)
import mygene
import pandas as pd
mg = mygene.MyGeneInfo()
ensembl_ids = df.index.tolist()
results = mg.querymany(
ensembl_ids,
scopes='ensembl.gene',
fields='symbol',
species='human',
as_dataframe=True
)
id_map = results['symbol'].dropna().to_dict()
df['gene_symbol'] = df.index.map(id_map)
Symbol → Entrez
results = mg.querymany(
gene_symbols,
scopes='symbol',
fields='entrezgene',
species='human',
as_dataframe=True
)
RefSeq → Symbol
results = mg.querymany(
refseq_ids,
scopes='refseq',
fields='symbol',
species='human',
as_dataframe=True
)
Entrez → Symbol
results = mg.querymany(
entrez_ids,
scopes='entrezgene',
fields='symbol',
species='human',
as_dataframe=True
)
Key Parameters
| Parameter | Values | Purpose |
|---|
scopes | 'ensembl.gene', 'symbol', 'entrezgene', 'refseq' | Input ID type |
fields | 'symbol', 'entrezgene', 'name', 'go', 'pathway.kegg' | What to return |
species | 'human', 'mouse', 'rat', or taxonomy ID (9606, 10090) | Organism filter |
as_dataframe | True / False | Return DataFrame vs list of dicts |
Detecting ID Types
'ENSG00000141510'
'ENSMUSG00000059552'
'7157'
'TP53', 'BRCA1', 'EGFR'
'NM_000546'
'NP_000537'
Handling Common Issues
Unmapped genes
results = mg.querymany(ids, scopes='ensembl.gene', fields='symbol', species='human', as_dataframe=True)
unmapped = results[results['notfound'] == True] if 'notfound' in results.columns else pd.DataFrame()
print(f"Mapped: {len(results) - len(unmapped)}, Unmapped: {len(unmapped)}")
mapped = results[results['symbol'].notna()]
Duplicate mappings
results = results[~results.index.duplicated(keep='first')]
Mouse gene symbols
Mouse genes use different capitalization (e.g., Tp53 not TP53):
results = mg.querymany(
mouse_ensembl_ids,
scopes='ensembl.gene',
fields='symbol',
species='mouse',
as_dataframe=True
)
Common Pitfalls
-
Wrong species: Always specify species. Ensembl IDs are species-specific (ENSG = human, ENSMUSG = mouse) but specifying the species avoids ambiguous matches.
-
Version suffixes: Ensembl IDs sometimes have versions (ENSG00000141510.12). Strip them:
clean_ids = [x.split('.')[0] for x in ensembl_ids]
-
Network required: mygene queries an online API. It will fail without network access.
-
Large batches: For >1000 genes, querymany handles batching automatically. No need to split manually.
-
Mixed ID types: If your data has mixed ID formats, detect the type first:
if ids[0].startswith('ENS'):
scopes = 'ensembl.gene'
elif ids[0].isdigit():
scopes = 'entrezgene'
else:
scopes = 'symbol'
Typical Workflow: DE Results → Enrichment
import mygene
import gseapy as gp
mg = mygene.MyGeneInfo()
de_results = pd.read_csv('deseq2_results.csv', index_col=0)
clean_ids = [x.split('.')[0] for x in de_results.index]
mapped = mg.querymany(clean_ids, scopes='ensembl.gene', fields='symbol', species='human', as_dataframe=True)
id_map = mapped['symbol'].dropna().to_dict()
sig_genes = de_results[de_results['padj'] < 0.05].index
sig_symbols = [id_map[g.split('.')[0]] for g in sig_genes if g.split('.')[0] in id_map]
enr = gp.enrichr(gene_list=sig_symbols, gene_sets='GO_Biological_Process_2021', organism='human')