Registry / http-networking / mygene

mygene

JSON →
library3.2.2pypypi✓ verified 23d ago

mygene is an easy-to-use Python wrapper to access MyGene.Info services, which provide simple-to-use REST web services to query/retrieve gene annotation data. It is currently at version 3.2.2 and is actively maintained, with releases tied to updates in the underlying MyGene.info API and its `biothings_client` dependency.

pip install mygene
INSTALL
IMPORT
SIG · MYGENE
M
mygene
http-networkingpythonv3.2.2
Install
2.2s avg
Import
397ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.2.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.414s · 22.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.2s · import 0.380s · 23MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MyGeneInfo
from mygene import MyGeneInfo
from biothings_client import get_client; mg = get_client('gene')
While 'mygene' internally uses 'biothings_client', the primary public interface for 'mygene' users remains 'mygene.MyGeneInfo()' for consistency and direct library usage.

Initialize the MyGeneInfo client and perform basic queries for gene annotation data, demonstrating retrieval of single gene information and searching by symbol with field filtering.

import mygene mg = mygene.MyGeneInfo() # Get information for a single gene (Entrez ID for CDK2) gene_info = mg.getgene(1017) print(f"Gene Symbol: {gene_info.get('symbol')}, Name: {gene_info.get('name')}") # Query for genes by symbol, returning only specific fields query_results = mg.query('CDK2', fields='symbol,name,taxid', species='human', size=2) for hit in query_results.get('hits', []): print(f"Query Hit: {hit.get('symbol')} ({hit.get('taxid')}) - {hit.get('name')}")
Debug
Known issues
breakingThe `mygene` package became a thin wrapper around `biothings_client` since v3.1.0. While the `mygene.MyGeneInfo()` interface is maintained, the underlying MyGene.info v3 API introduced changes to data structures for fields like `refseq`, `accession`, `ensembl`, and `exons`. Also, the default behavior for 'dotfield' notation changed, requiring explicit `dotfield=1` for the old behavior.
fix
Review the MyGene.info v3 API migration guide if your application relies on specific data structures or 'dotfield' behavior for annotation fields. Update your code to handle the new JSON structure or explicitly request 'dotfield=1' if needed.
affects: >=3.0.0
deprecatedThe `findgenes()` method was deprecated in version 2.0.0. It is kept as an alias for `querymany()` for backward compatibility, but `querymany()` should be used instead for new code.
fix
Replace calls to `mg.findgenes(...)` with `mg.querymany(...)`. The arguments are generally compatible.
affects: >=2.0.0
gotchaWhen querying with Ensembl gene IDs that include a version postfix (e.g., 'ENSG00000000003.14'), the `getgene()` method might not return results. The postfix should be removed.
fix
Strip the version postfix (e.g., '.14') from Ensembl gene IDs before passing them to `mg.getgene()`.
affects: All versions
gotchaIn MyGene.info API v2 (and by extension older `mygene` client versions), the 'filter' parameter was used for specifying returned fields. This parameter was replaced by 'fields' in MyGene.info API v2, though 'filter' was kept for back-compatibility in the client. Users should use 'fields' for clarity and future compatibility.
fix
Always use the `fields` parameter instead of `filter` when calling `query()` or `querymany()` to specify desired return fields.
affects: <3.0.0 (and if using older API endpoints)
gotchaWhile the `mygene` client defaults to the latest MyGene.info API (v3), older API versions (e.g., v2) can still be accessed by explicitly setting `mg.url`. However, data from older API versions are no longer updated.
fix
Ensure you are using the default API endpoint unless you have a specific reason to access an older, unmaintained version. Do not rely on data from `mg.url='http://mygene.info/v2'` for up-to-date information.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'mygene'
The 'mygene' Python package is not installed in the current environment or is not accessible to the Python interpreter being used.
fix
Install the 'mygene' package using pip: `pip install mygene`
AttributeError: module 'mygene' has no attribute 'querymany'
Developers often forget to instantiate the MyGeneInfo class after importing the `mygene` module, attempting to call methods directly on the module instead of an object.
fix
Instantiate the MyGeneInfo client before calling its methods: 
```python
import mygene
mg = mygene.MyGeneInfo()
result = mg.querymany(['gene_symbol'], scopes='symbol')
```
Error: pandas module must be installed for as_dataframe option.
This error occurs when the `as_dataframe=True` option is used in a `mygene` query method, but the pandas library is either not installed or an incompatible version is present.
fix
Install or upgrade pandas to a compatible version: `pip install pandas` or `pip install --upgrade pandas`
500 Server Error: N/A for url: http://mygene.info/v3/query/
A 500 Server Error indicates an issue on the MyGene.info API server itself, or a transient network problem, rather than an error in the `mygene` Python client.
fix
This is typically a server-side issue. It's recommended to wait and retry the request later, or check the MyGene.info status page for service announcements. If it persists, report it to the MyGene.info team (help@mygene.info).
1 input query terms found no hit:
The MyGene.info API did not find a match for one or more of the provided query terms, often due to incorrect gene identifiers, typos, or the gene not being in the database for the specified species or scope.
fix
Verify the correctness of your input gene identifiers, ensure the `scopes` and `species` parameters are appropriate for your query, and consider using `returnall=True` to inspect which specific terms yielded no hits or duplicates.
Upgrade
Version history
3.2.2latest on PyPI · released Apr 5, 2021
Audit
Dependencies
biothings_clientrequiredCore dependency; mygene is a thin wrapper over biothings_client since v3.1.0.
pandasoptionalRequired for returning results as pandas DataFrames.
Agent activity
7 hits · last 30 days
node
6
Resources