chain

chain(converters: Sequence[Converter], *, case_sensitive: bool = True) Converter[source]

Chain several converters.

Parameters:
  • converters – A list or tuple of converters

  • case_sensitive – If false, will not allow case-sensitive duplicates

Returns:

A converter that looks up one at a time in the other converters.

Raises:

ValueError – If there are no converters

Chain is the perfect tool if you want to override parts of an existing extended prefix map. For example, if you want to use most of the Bioregistry, but you would like to specify a custom URI prefix (e.g., using Identifiers.org), you can do the following:

>>> import curies
>>> bioregistry_converter = curies.get_bioregistry_converter()
>>> overrides = curies.load_prefix_map({"pubmed": "https://identifiers.org/pubmed:"})
>>> converter = curies.chain([overrides, bioregistry_converter])
>>> converter.bimap["pubmed"]
'https://identifiers.org/pubmed:'

Similarly, this also works if you want to override a prefix. Keep in mind for this to work with a simple prefix map, you need to make sure the URI prefix matches in each converter, otherwise you will get duplicates:

>>> overrides = curies.load_prefix_map({"PMID": "https://www.ncbi.nlm.nih.gov/pubmed/"})
>>> converter = chain([overrides, bioregistry_converter])
>>> converter.bimap["PMID"]
'https://www.ncbi.nlm.nih.gov/pubmed/'

A safer way is to specify your override using an extended prefix map, which can tie together prefix synonyms and URI prefix synonyms:

>>> import curies
>>> from curies import Converter, chain, get_bioregistry_converter
>>> overrides = curies.load_extended_prefix_map(
...     [
...         {
...             "prefix": "PMID",
...             "prefix_synonyms": ["pubmed", "PubMed"],
...             "uri_prefix": "https://www.ncbi.nlm.nih.gov/pubmed/",
...             "uri_prefix_synonyms": [
...                 "https://identifiers.org/pubmed:",
...                 "http://bio2rdf.org/pubmed:",
...             ],
...         },
...     ]
... )
>>> converter = curies.chain([overrides, bioregistry_converter])
>>> converter.bimap["PMID"]
'https://www.ncbi.nlm.nih.gov/pubmed/'

Chain prioritizes based on the order given. Therefore, if two prefix maps having the same prefix but different URI prefixes are given, the first is retained

>>> c1 = curies.load_prefix_map({"GO": "http://purl.obolibrary.org/obo/GO_"})
>>> c2 = curies.load_prefix_map({"GO": "https://identifiers.org/go:"})
>>> c3 = curies.chain([c1, c2])
>>> c3.prefix_map["GO"]
'http://purl.obolibrary.org/obo/GO_'