Skip to content

webservices.ols

Lookup of ontology terms in the Ontology Lookup Service (OLS).

OLS resolves an ontology term to its label, description, synonyms and cross references. RDFAnnotationData uses it to fill in what an annotation actually refers to.

from pymetadata.webservices.ols import ONTOLOGIES, OLSQuery

query = OLSQuery(ontologies=ONTOLOGIES)
info = query.query_ols(ontology="chebi", term="CHEBI:33699")
print(query.process_response(info)["label"])

ONTOLOGIES lists the ontologies used in most projects together with the IRI pattern needed to build the term IRI OLS expects.

See https://www.ebi.ac.uk/ols4.

OLSOntology dataclass

OLSOntology(name, iri_pattern=None)

An ontology available in OLS.

Attributes:

Name Type Description
name str

lowercase ontology id, e.g., chebi

iri_pattern str | None

pattern of the term IRI with the placeholder {$Id}, defaults to the OBO purl of the ontology

OLSQuery

OLSQuery(ontologies, cache_path=None, cache=None)

Queries against the Ontology Lookup Service.

Responses are cached on disk for CACHE_DURATION_ONTOLOGY hours, see pymetadata.CACHE_USE. If OLS cannot be reached, cached content is used however old it is.

Attributes:

Name Type Description
ontologies dict[str, OLSOntology]

the queryable ontologies by name

cache_path

directory of the cached responses

cache

whether responses are cached

Initialize the query.

Parameters:

Name Type Description Default
ontologies list[OLSOntology]

ontologies which can be queried, e.g., ONTOLOGIES

required
cache_path Path | None

directory for cached responses, defaults to pymetadata.CACHE_PATH

None
cache bool | None

cache responses, defaults to pymetadata.CACHE_USE

None

get_iri

get_iri(ontology, term)

Build the term IRI which OLS expects.

Parameters:

Name Type Description Default
ontology str

ontology id, e.g., chebi

required
term str

term of the ontology, e.g., CHEBI:33699

required

Returns:

Type Description
str

The IRI of the term, or an empty string for an unknown ontology.

query_ols

query_ols(ontology, term)

Query OLS for a single term.

Parameters:

Name Type Description Default
ontology str | None

ontology id, e.g., chebi

required
term str | None

term of the ontology, e.g., CHEBI:33699

required

Returns:

Type Description
dict

The OLS response, with errors and warnings describing problems

dict

with the query.

process_response

process_response(term)

Reduce an OLS response to the information used for annotations.

Parameters:

Name Type Description Default
term dict

OLS response from query_ols

required

Returns:

Type Description
dict[str, Any]

Dictionary with label, description, synonyms and xrefs.