# Path: indra/databases/chebi_client.py
# def _add_prefix(chid):
# def get_pubchem_id(chebi_id):
# def get_chebi_id_from_pubchem(pubchem_id):
# def get_chembl_id(chebi_id):
# def get_chebi_id_from_chembl(chembl_id):
# def get_chebi_id_from_cas(cas_id):
# def get_chebi_name_from_id(chebi_id, offline=True):
# def get_chebi_id_from_name(chebi_name):
# def get_chebi_entry_from_web(chebi_id):
# def _get_chebi_value_from_entry(entry, key):
# def get_chebi_name_from_id_web(chebi_id):
# def get_inchi_key(chebi_id):
# def get_primary_id(chebi_id):
# def get_specific_id(chebi_ids):
#     def isa_cmp(a, b):
# def get_chebi_id_from_hmdb(hmdb_id):
# def _read_chebi_to_pubchem():
# def _read_chebi_to_chembl():
# def _read_cas_to_chebi():
# def _read_hmdb_to_chebi():
# def _read_resource_csv(fname):
# 
# Path: indra/ontology/standardize.py
# def standardize_db_refs(db_refs, ontology=None, ns_order=None):
#     """Return a standardized db refs dict for a given db refs dict.
# 
#     Parameters
#     ----------
#     db_refs : dict
#         A dict of db refs that may not be standardized, i.e., may be
#         missing an available UP ID corresponding to an existing HGNC ID.
#     ontology : Optional[indra.ontology.IndraOntology]
#         An IndraOntology object, if not provided, the default BioOntology
#         is used.
#     ns_order : Optional[list]
#         A list of namespaces which are in order of priority with higher
#         priority namespaces appearing earlier in the list.
# 
#     Returns
#     -------
#     dict
#         The db_refs dict with standardized entries.
#     """
#     if ontology is None:
#         from indra.ontology.bio import bio_ontology
#         ontology = bio_ontology
# 
#     # We iterate over all the db_refs entries that currently exist
#     for source_db_ns, source_db_id in deepcopy(db_refs).items():
#         source_db_id = _preprocess_for_mapping(source_db_ns, source_db_id)
#         # If there is a replacement for this entry, we apply it
#         replacement = ontology.get_replacement(source_db_ns, source_db_id)
#         if replacement:
#             source_db_ns, source_db_id = replacement
#             db_refs[source_db_ns] = source_db_id
#         # For the entry we get all its xref mappings as a list
#         # of tuples and turn it into a dict keyed by namespace
#         mappings = _get_mappings_dict(
#             ontology.get_mappings(source_db_ns, source_db_id))
#         # We iterate over these mappings and check if they should
#         # be applied
#         for mapped_db_ns, mapped_db_ids in mappings.items():
#             # If the db_refs doesn't yet contain a mapping for this
#             # name space then we always add this mapping. If there
#             # is already an entry for this name space then
#             # we overwrite it if the source name space is higher
#             # priority than the name space being mapped to.
#             if mapped_db_ns not in db_refs or \
#                     prioritize(mapped_db_ns, source_db_ns,
#                                ns_order=ns_order):
#                 db_refs[mapped_db_ns] = sorted(mapped_db_ids)[0]
#     return db_refs


from indra.databases import chebi_client
from indra.ontology.standardize import standardize_db_refs


minerva_to_indra_map = {
    'UNIPROT': 'UP',
    'NCBI_PROTEIN': 'NCBIPROTEIN',
    'REFSEQ': 'REFSEQ_PROT',
    'ENTREZ': 'EGID',
    'INTERPRO': 'IP',
    'MESH_2012': 'MESH',
    'EC': 'ECCODE',
    'PUBCHEM_SUBSTANCE': 'PUBCHEM.SUBSTANCE',
    'KEGG_COMPOUND': 'KEGG.COMPOUND'
}


def fix_id_standards(db_ns, db_id):
    if db_ns == 'CHEBI':
        if not db_id.startswith('CHEBI:'):
            db_id = f'CHEBI:{db_id}'
        db_id = chebi_client.get_primary_id(db_id)
    elif db_ns == 'HGNC' and db_id.startswith('HGNC:'):
        db_id = db_id[5:]
    return db_ns, db_id


def indra_db_refs_from_minerva_refs(refs):
    db_refs = {}
    for db_ns, db_id in refs:
        db_ns = minerva_to_indra_map[db_ns] \

# Path: indra/databases/chebi_client.py
# def _add_prefix(chid):
# def get_pubchem_id(chebi_id):
# def get_chebi_id_from_pubchem(pubchem_id):
# def get_chembl_id(chebi_id):
# def get_chebi_id_from_chembl(chembl_id):
# def get_chebi_id_from_cas(cas_id):
# def get_chebi_name_from_id(chebi_id, offline=True):
# def get_chebi_id_from_name(chebi_name):
# def get_chebi_entry_from_web(chebi_id):
# def _get_chebi_value_from_entry(entry, key):
# def get_chebi_name_from_id_web(chebi_id):
# def get_inchi_key(chebi_id):
# def get_primary_id(chebi_id):
# def get_specific_id(chebi_ids):
#     def isa_cmp(a, b):
# def get_chebi_id_from_hmdb(hmdb_id):
# def _read_chebi_to_pubchem():
# def _read_chebi_to_chembl():
# def _read_cas_to_chebi():
# def _read_hmdb_to_chebi():
# def _read_resource_csv(fname):
# 
# Path: indra/ontology/standardize.py
# def standardize_db_refs(db_refs, ontology=None, ns_order=None):
#     """Return a standardized db refs dict for a given db refs dict.
# 
#     Parameters
#     ----------
#     db_refs : dict
#         A dict of db refs that may not be standardized, i.e., may be
#         missing an available UP ID corresponding to an existing HGNC ID.
#     ontology : Optional[indra.ontology.IndraOntology]
#         An IndraOntology object, if not provided, the default BioOntology
#         is used.
#     ns_order : Optional[list]
#         A list of namespaces which are in order of priority with higher
#         priority namespaces appearing earlier in the list.
# 
#     Returns
#     -------
#     dict
#         The db_refs dict with standardized entries.
#     """
#     if ontology is None:
#         from indra.ontology.bio import bio_ontology
#         ontology = bio_ontology
# 
#     # We iterate over all the db_refs entries that currently exist
#     for source_db_ns, source_db_id in deepcopy(db_refs).items():
#         source_db_id = _preprocess_for_mapping(source_db_ns, source_db_id)
#         # If there is a replacement for this entry, we apply it
#         replacement = ontology.get_replacement(source_db_ns, source_db_id)
#         if replacement:
#             source_db_ns, source_db_id = replacement
#             db_refs[source_db_ns] = source_db_id
#         # For the entry we get all its xref mappings as a list
#         # of tuples and turn it into a dict keyed by namespace
#         mappings = _get_mappings_dict(
#             ontology.get_mappings(source_db_ns, source_db_id))
#         # We iterate over these mappings and check if they should
#         # be applied
#         for mapped_db_ns, mapped_db_ids in mappings.items():
#             # If the db_refs doesn't yet contain a mapping for this
#             # name space then we always add this mapping. If there
#             # is already an entry for this name space then
#             # we overwrite it if the source name space is higher
#             # priority than the name space being mapped to.
#             if mapped_db_ns not in db_refs or \
#                     prioritize(mapped_db_ns, source_db_ns,
#                                ns_order=ns_order):
#                 db_refs[mapped_db_ns] = sorted(mapped_db_ids)[0]
#     return db_refs


# Path: indra/sources/minerva/id_mapping.py
from indra.databases import chebi_client
from indra.ontology.standardize import standardize_db_refs



minerva_to_indra_map = {
    'UNIPROT': 'UP',
    'NCBI_PROTEIN': 'NCBIPROTEIN',
    'REFSEQ': 'REFSEQ_PROT',
    'ENTREZ': 'EGID',
    'INTERPRO': 'IP',
    'MESH_2012': 'MESH',
    'EC': 'ECCODE',
    'PUBCHEM_SUBSTANCE': 'PUBCHEM.SUBSTANCE',
    'KEGG_COMPOUND': 'KEGG.COMPOUND'
}


def fix_id_standards(db_ns, db_id):
    if db_ns == 'CHEBI':
        if not db_id.startswith('CHEBI:'):
            db_id = f'CHEBI:{db_id}'
        db_id = chebi_client.get_primary_id(db_id)
    elif db_ns == 'HGNC' and db_id.startswith('HGNC:'):
        db_id = db_id[5:]
    return db_ns, db_id


def indra_db_refs_from_minerva_refs(refs):
    db_refs = {}
    for db_ns, db_id in refs:
        db_ns = minerva_to_indra_map[db_ns] \

            if db_ns in minerva_to_indra_map else db_ns