PrefixCommons Python API
PrefixCommons is a Python API designed for working with ID prefixes, enabling the canonicalization, expansion, and contraction of CURIEs (Compact URIs). It is currently at version 0.1.12 and maintains an infrequent release cadence, often aligned with specific project requirements such as those from the Alliance of Genome Resources.
Common errors
-
ModuleNotFoundError: No module named 'prefixcommons.curie_util'
cause Attempting to import core utility functions directly from the top-level `prefixcommons` package instead of their specific submodule `curie_util`.fixChange your import statement from `from prefixcommons import expand_uri` (or similar) to `from prefixcommons.curie_util import expand_uri, contract_uri`. -
KeyError: 'UNKNOWN_PREFIX'
cause The CURIE prefix (e.g., 'MYAPP' in 'MYAPP:123') is not found in the currently active prefix map when attempting to expand or contract a URI.fixEnsure that the `prefix_map` supplied to `expand_uri` or `contract_uri` (or the default map being used) contains the required prefix. You can load specific contexts using `prefixcommons.curie_util.read_biocontext()` or pass a custom dictionary: `expand_uri('MYAPP:123', prefix_map={'MYAPP': 'http://example.com/myapp/'})`.
Warnings
- gotcha The default prefix map, loaded internally from `resources/prefixcommons.jsonld`, may not be exhaustive for all domains or may become stale over time. For robust applications, relying solely on this default map for critical or very specific prefixes is not recommended.
- gotcha PrefixCommons has an infrequent release cadence, often tied to specific project needs. While stable, this means that updates to its default prefix maps for very new or niche identifiers might not be as frequent as external data sources.
Install
-
pip install prefixcommons
Imports
- expand_uri
from prefixcommons import expand_uri
from prefixcommons.curie_util import expand_uri
- contract_uri
from prefixcommons import contract_uri
from prefixcommons.curie_util import contract_uri
- read_biocontext
from prefixcommons.curie_util import read_biocontext
Quickstart
from prefixcommons.curie_util import expand_uri, contract_uri, read_biocontext
# Using the default prefix map
expanded_go = expand_uri('GO:0008150')
print(f"Expanded GO: {expanded_go}")
contracted_go = contract_uri('http://purl.obolibrary.org/obo/GO_0008150')
print(f"Contracted GO: {contracted_go}\n")
# Using a custom prefix map
custom_map = {'EXAMPLE': 'http://example.org/myterms/'}
expanded_custom = expand_uri('EXAMPLE:001', prefix_map=custom_map)
print(f"Expanded custom: {expanded_custom}")
# Loading a biomedical context map
bio_context = read_biocontext()
expanded_ncbi = expand_uri('NCBIGENE:12345', prefix_map=bio_context)
print(f"Expanded NCBIGENE from bio_context: {expanded_ncbi}")