Web Converters

CIR

class MSMetaEnhancer.libs.converters.web.CIR.CIR(session)

Bases: WebConverter

Chemical Identifier Resolver allows one to convert a given structure identifier into another representation or structure identifier.

Available online at: https://cactus.nci.nih.gov/chemical/structure

More info about the available conversions: https://cactus.nci.nih.gov/chemical/structure_documentation

async casno_to_smiles(cas_number)

Convert CAS number to SMILES using CIR web service

Parameters:

cas_number – given CAS number

Returns:

obtained SMILES

async inchi_to_smiles(inchi)

Convert InChi to SMILES using CIR web service

Parameters:

inchi – given InChi

Returns:

obtained SMILES

async inchikey_to_casno(inchikey)

Convert InChiKey to CAS number using CIR web service

Parameters:

inchikey – given InChiKey

Returns:

obtained CAS number

async inchikey_to_formula(inchikey)

Convert InChiKey to chemical formula using CIR web service

Parameters:

inchikey – given InChiKey

Returns:

obtained chemical formula

async inchikey_to_inchi(inchikey)

Convert InChiKey to InCHi using CIR web service

Parameters:

inchikey – given InChiKey

Returns:

obtained InCHi

async inchikey_to_smiles(inchikey)

Convert InChiKey to SMILES using CIR web service

Parameters:

inchikey – given InChiKey

Returns:

obtained SMILES

static retrieve_first(response)

CIR often returns multiple hits separated by a newline. This method takes the first hit only.

Parameters:

response – given response from CIR

Returns:

only first hit

async smiles_to_inchikey(smiles)

Convert SMILES to InChiKey using CIR web service

Parameters:

smiles – given SMILES

Returns:

obtained InChiKey

CTS

class MSMetaEnhancer.libs.converters.web.CTS.CTS(session)

Bases: WebConverter

Chemical Translation WebConverter performs batch conversions of the most common compound identifiers.

Available online at: http://cts.fiehnlab.ucdavis.edu

More info about the available conversions: http://cts.fiehnlab.ucdavis.edu/services

async casno_to_inchi(cas_number)

Convert CAS number to InChi using CTS web service

Parameters:

cas_number – given CAS number

Returns:

obtained InChi

async casno_to_inchikey(cas_number)

Convert CAS number to InChiKey using CTS web service

The method returns first found hit.

Parameters:

cas_number – given CAS number

Returns:

obtained InChiKey

async compound_name_to_inchikey(name)

Convert Chemical name to InChiKey using CTS service

Parameters:

name – given Chemical name

Returns:

obtained InChiKey

async from_inchikey(inchikey)

Convert InChiKey to all possible attributes using CTS compound service

Parameters:

inchikey – given InChiKey value

Returns:

all found data

async hmdbid_to_inchi(hmdbid)

Convert HMDB ID number to InChi using CTS web service

Parameters:

hmdbid – given HMDB ID

Returns:

obtained InChi

parse_attributes(response)

Parse all available attributes obtained from InChiKey.

Parameters:

response – CTS compound response to given InChiKey

Returns:

all parsed data

parse_single_response(response, attribute)

Parse InChiKey attribute obtained from given key.

Parameters:
  • response – CTS conversion response to given key

  • attribute – expected attribute name in the response

Returns:

parsed InChiKey

IDSM

class MSMetaEnhancer.libs.converters.web.IDSM.IDSM(session)

Bases: WebConverter

IDSM provides unique source of fast similarity and structural search functionality in databases such as ChEMBL, ChEBI or PubChem. Currently, PubChem fragment is supported.

IDSM service: https://idsm.elixir-czech.cz/

async call_service(query)

General method to call IDSM service.

Uses semaphore to control maximal number of simultaneous requests being processed. Limited to 10 as required by IDSM service.

Parameters:

query – given SPARQL query

Returns:

obtained attributes

async from_inchi(arg)
async from_name(arg)
async iupac_name_to_inchi(arg)
parse_attributes(response)

Parse all available attributes (specified in self.attributes) from given response.

Method does not return anything, instead stores data in local cache.

Parameters:

response – given JSON

Returns:

all parsed data

PubChem

class MSMetaEnhancer.libs.converters.web.PubChem.PubChem(session)

Bases: WebConverter

PubChem is the world’s largest collection of freely accessible chemical information.

PubChem service: https://pubchem.ncbi.nlm.nih.gov/

adjust_throttling(throttling_header)

Adjust current requests rate based on Dynamic Request Throttling provided by PubChem.

More info: https://pubchemdocs.ncbi.nlm.nih.gov/dynamic-request-throttling

Parameters:

throttling_header – header containing current service load info

async call_service(args, method, data)

General method to call PubChem service.

Uses a throttler to control maximal number of simultaneous requests being processed per second. Limited to 5 requests per second, can be dynamically lowered at times of excessive load.

More info: https://pubchemdocs.ncbi.nlm.nih.gov/programmatic-access$_requestvolumelimitations

Parameters:
  • args – additional url suffix

  • method – POST of GET

  • data – source data for POST request

Returns:

obtained attributes

async from_inchi(inchi)

Convert InChi to to all possible attributes using PubChem service More info: https://pubchemdocs.ncbi.nlm.nih.gov/pug-rest

Parameters:

inchi – given InChi

Returns:

all found data

async from_inchikey(inchikey)

Convert InChiKey to all possible attributes using PubChem service More info: https://pubchemdocs.ncbi.nlm.nih.gov/pug-rest

Parameters:

inchikey – given InChiKey

Returns:

all found data

async from_name(name)

Convert Chemical name to all possible attributes using PubChem service More info: https://pubchemdocs.ncbi.nlm.nih.gov/pug-rest

Parameters:

name – given Chemical name

Returns:

all found data

async from_pubchemid(pubchemid)

Obtain chemical identifiers based on given PubChem ID using PubChem service More info: https://pubchemdocs.ncbi.nlm.nih.gov/pug-rest

Parameters:

pubchemid – given Chemical name

Returns:

all found data

parse_attributes(response)

Parse all available attributes (specified in self.attributes) from given response.

Method does not return anything, instead stores data in local cache.

Parameters:

response – given JSON

Returns:

all parsed data

async process_request(response, url, method)

Redefined parent method with additional adjustment of throttling.

Parameters:
  • response – given async response

  • url – service URL

  • method – GET/POST

Returns:

processed response

async pubchemid_to_hmdbid(pubchemid)

Obtain HMDB ID identifier based on given PubChem ID using PubChem service More info: https://pubchemdocs.ncbi.nlm.nih.gov/pug-rest

Parameters:

pubchemid – given Chemical name

Returns:

all found data

BridgeDb

class MSMetaEnhancer.libs.converters.web.BridgeDb.BridgeDb(session)

Bases: WebConverter

BridgeDb is a framework to map identifiers between various biological databases. These mappings are provided for genes, proteins, genetic variants, metabolites, and metabolic reactions

More info about the available conversions: https://bridgedb.github.io/

async call_service(args)
async from_chebiid(chebiid)

Convert ChEBI ID to all possible IDs using BridgeDb web service

Parameters:

chebiid – given ChEBI ID number

Returns:

obtained IDs

async from_chemspiderid(chemspiderid)

Convert ChemSpider ID to all possible IDs using BridgeDb web service

Parameters:

chemspiderid – given ChemSpider ID number

Returns:

obtained IDs

async from_hmdbid(hmdbid)

Convert HMDB ID to all possible IDs using BridgeDb web service

Parameters:

hmdbid – given HMDB ID number

Returns:

obtained IDs

async from_keggid(keggid)

Convert KEGG ID to all possible IDs using BridgeDb web service

Parameters:

keggid – given KEGG ID number

Returns:

obtained IDs

async from_pubchemid(pubchemid)

Convert PubChem ID to all possible IDs using BridgeDb web service

Parameters:

pubchemid – given PubChem ID number

Returns:

obtained IDs

async from_wikidataid(wikidataid)

Convert WikiData ID to all possible IDs using BridgeDb web service

Parameters:

wikidataid – given WikiData ID number

Returns:

obtained IDs

parse_attributes(response)

Parse all available attributes obtained using BridgeDb.

Parameters:

response – BridgeDb response to given ID

Returns:

all parsed data

General Web Converter

class MSMetaEnhancer.libs.converters.web.WebConverter.WebConverter(session: ClientSession)

Bases: Converter

General class for web conversions.

FAILURE_THRESHOLD: int = 10

Number of consecutive failures before circuit breaker is opened.

async convert(source: str, target: str, data: str | int | float)

Convert data from source attribute to target attribute.

Args:

source (str): Source attribute name. target (str): Target attribute name. data (Union[str, int, float]): Data to use for the conversion

Raises:

TargetAttributeNotRetrieved: Exception raised if the target attribute is not retrieved from the converter.

Returns:

_type_: Data retrieved from the service.

async loop_request(url: str, method: str, data: Any, headers: dict) str

Execute request in a circuit breaker loop. If the request fails multiple times in a row, the circuit breaker is opened and ServiceNotAvailable exception is raised.

Parameters:
  • url – converter URL

  • method – GET/POST

  • data – given arguments for POST request

  • headers – optional headers for the request

Returns:

obtained response

async make_request(url, method, data, headers)

Enter a circuit breaker loop and execute request with type depending on specified method.

Parameters:
  • url – converter URL

  • method – GET/POST

  • data – given arguments for POST request

  • headers – optional headers for the request

Returns:

obtained response

async process_request(response: ClientResponse, url: str, method: str) str

Method to wrap response handling (same for POST and GET requests).

Parameters:
  • response – given async response

  • url – converter URL

  • method – GET/POST

Returns:

processed response

query_the_service(service: str, args: str, method: str = 'GET', data=None, headers=None) str

Make get request to given converter with arguments. Raises ConnectionError if converter is not available.

Parameters:
  • service – requested converter to be queried

  • args – additional query arguments

  • method – GET (default) or POST

  • data – data for POST request

  • headers – optional headers for the request

Returns:

obtained response