# Path: indra/databases/identifiers.py
# def ensure_chebi_prefix(chebi_id):
#     """Return a valid CHEBI ID that has the appropriate CHEBI: prefix."""
#     return ensure_prefix('CHEBI', chebi_id)
# 
# def ensure_chembl_prefix(chembl_id):
#     """Return a valid CHEMBL ID that has the appropriate CHEMBL prefix."""
#     return ensure_prefix('CHEMBL', chembl_id, with_colon=False)
# 
# Path: indra/ontology/standardize.py
# def standardize_name_db_refs(db_refs, ontology=None, ns_order=None):
#     """Return a standardized name and 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
#     -------
#     str or None
#         The standard name based on the db refs, None if not available.
#     dict
#         The db_refs dict with standardized entries.
#     """
#     db_refs = standardize_db_refs(db_refs, ontology=ontology,
#                                   ns_order=ns_order)
#     name = get_standard_name(db_refs, ontology=ontology, ns_order=ns_order)
#     return name, db_refs
# 
# def get_standard_agent(name, db_refs, ontology=None, ns_order=None, **kwargs):
#     """Get a standard agent based on the name, db_refs, and a any other kwargs.
# 
#     name : str
#         The name of the agent that may not be standardized.
#     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.
#     kwargs :
#         Keyword arguments to pass to :func:`Agent.__init__`.
# 
#     Returns
#     -------
#     Agent
#         A standard agent
#     """
#     standard_name, db_refs = standardize_name_db_refs(db_refs,
#                                                       ontology=ontology,
#                                                       ns_order=ns_order)
#     if standard_name:
#         name = standard_name
#     assert_valid_db_refs(db_refs)
#     return Agent(name, db_refs=db_refs, **kwargs)


import logging
from xml.etree import ElementTree
from indra.statements import *
from indra.databases.identifiers import ensure_chebi_prefix, \
    ensure_chembl_prefix
from indra.statements.validate import assert_valid_db_refs
from indra.ontology.standardize import standardize_name_db_refs, \
    get_standard_agent
        name_tag = db_find(drug_element, 'db:name')
        name = name_tag.text

        db_refs = {}

        # Extract the DrugBank ID
        drugbank_id_tags = db_findall(drug_element, 'db:drugbank-id')
        # We do a sort here because sometimes there's more than one
        # DrugBank ID and we choose the "smaller" one here
        drugbank_id = sorted([di.text for di in drugbank_id_tags
                              if di.text.startswith('DB')])[0]
        db_refs['DRUGBANK'] = drugbank_id

        # Extract CAS ID
        cas_tag = db_find(drug_element, 'db:cas-number')
        if cas_tag is not None and cas_tag.text is not None:
            db_refs['CAS'] = cas_tag.text

        # Extract other xrefs
        for xref_tag in db_findall(drug_element, 'db:external-identifiers/'
                                   'db:external-identifier'):
            resource = db_find(xref_tag, 'db:resource').text
            identifier = db_find(xref_tag, 'db:identifier').text
            if resource == 'ChEMBL':
                db_refs['CHEMBL'] = ensure_chembl_prefix(identifier)
            elif resource == 'PubChem Compound':
                db_refs['PUBCHEM'] = identifier
            elif resource == 'ChEBI':
                db_refs['CHEBI'] = ensure_chebi_prefix(identifier)
        assert_valid_db_refs(db_refs)

# Path: indra/databases/identifiers.py
# def ensure_chebi_prefix(chebi_id):
#     """Return a valid CHEBI ID that has the appropriate CHEBI: prefix."""
#     return ensure_prefix('CHEBI', chebi_id)
# 
# def ensure_chembl_prefix(chembl_id):
#     """Return a valid CHEMBL ID that has the appropriate CHEMBL prefix."""
#     return ensure_prefix('CHEMBL', chembl_id, with_colon=False)
# 
# Path: indra/ontology/standardize.py
# def standardize_name_db_refs(db_refs, ontology=None, ns_order=None):
#     """Return a standardized name and 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
#     -------
#     str or None
#         The standard name based on the db refs, None if not available.
#     dict
#         The db_refs dict with standardized entries.
#     """
#     db_refs = standardize_db_refs(db_refs, ontology=ontology,
#                                   ns_order=ns_order)
#     name = get_standard_name(db_refs, ontology=ontology, ns_order=ns_order)
#     return name, db_refs
# 
# def get_standard_agent(name, db_refs, ontology=None, ns_order=None, **kwargs):
#     """Get a standard agent based on the name, db_refs, and a any other kwargs.
# 
#     name : str
#         The name of the agent that may not be standardized.
#     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.
#     kwargs :
#         Keyword arguments to pass to :func:`Agent.__init__`.
# 
#     Returns
#     -------
#     Agent
#         A standard agent
#     """
#     standard_name, db_refs = standardize_name_db_refs(db_refs,
#                                                       ontology=ontology,
#                                                       ns_order=ns_order)
#     if standard_name:
#         name = standard_name
#     assert_valid_db_refs(db_refs)
#     return Agent(name, db_refs=db_refs, **kwargs)


# Path: indra/sources/drugbank/processor.py
import logging
from xml.etree import ElementTree
from indra.statements import *
from indra.databases.identifiers import ensure_chebi_prefix, \
    ensure_chembl_prefix
from indra.statements.validate import assert_valid_db_refs
from indra.ontology.standardize import standardize_name_db_refs, \
    get_standard_agent

        name_tag = db_find(drug_element, 'db:name')
        name = name_tag.text

        db_refs = {}

        # Extract the DrugBank ID
        drugbank_id_tags = db_findall(drug_element, 'db:drugbank-id')
        # We do a sort here because sometimes there's more than one
        # DrugBank ID and we choose the "smaller" one here
        drugbank_id = sorted([di.text for di in drugbank_id_tags
                              if di.text.startswith('DB')])[0]
        db_refs['DRUGBANK'] = drugbank_id

        # Extract CAS ID
        cas_tag = db_find(drug_element, 'db:cas-number')
        if cas_tag is not None and cas_tag.text is not None:
            db_refs['CAS'] = cas_tag.text

        # Extract other xrefs
        for xref_tag in db_findall(drug_element, 'db:external-identifiers/'
                                   'db:external-identifier'):
            resource = db_find(xref_tag, 'db:resource').text
            identifier = db_find(xref_tag, 'db:identifier').text
            if resource == 'ChEMBL':
                db_refs['CHEMBL'] = ensure_chembl_prefix(identifier)
            elif resource == 'PubChem Compound':
                db_refs['PUBCHEM'] = identifier
            elif resource == 'ChEBI':
                db_refs['CHEBI'] = ensure_chebi_prefix(identifier)
        assert_valid_db_refs(db_refs)

        return get_standard_agent(name, db_refs)