Esempio n. 1
0
def combine_models(model_id2sbml, model_id2S, path):
    logging.info('Going to merge models...')
    chebi = parse_simple(get_chebi())
    model_id2dfs = get_model_data(model_id2sbml)
    model_id2c_id_groups, model_id2m_id_groups, model_id2c_id2i = \
        map_metabolites_compartments(model_id2dfs, chebi=chebi)
    logging.info('Mapped metabolites and compartments.')
    ignore_m_ids = get_ignored_metabolites(model_id2dfs, get_proton_ch_ids())
    S = join(model_id2m_id_groups, model_id2S)
    ignore_m_ids |= {S.m_id2gr_id[m_id] for m_id in ignore_m_ids if m_id in S.m_id2gr_id}
    merge(S, ignore_m_ids)
    model_id2r_id_groups = get_r_id_groups(S)
    logging.info('Mapped reactions.')
    sbml = os.path.join(path, 'Merged_model.xml')
    model_id2id2id, common_ids, S = simple_merge_models(S, model_id2c_id2i, model_id2dfs, sbml)
    return sbml, S, model_id2id2id, common_ids, model_id2dfs, \
           (model_id2c_id_groups, model_id2m_id_groups, model_id2r_id_groups)
Esempio n. 2
0
def get_key_elements(model,
                     r,
                     s_id2clu,
                     s_id2term_id,
                     ubiquitous_chebi_ids,
                     ignore_ch_ids=get_proton_ch_ids()):
    """
    Gets elements that compose a reaction key: ubiquitous_reactants, ubiquitous_products,
    specific_reactant_classes, specific_product_classes
    :param model: libsbml.Model
    :param r: libsbml.Reaction reaction of interest
    :param s_id2clu: dict {metabolite_id: (compartment_id, cluster)}
    :param s_id2term_id: dict {metabolite_id: ChEBI_term_id}
    :param ubiquitous_chebi_ids: set of ubiquitous ChEBI_ids
    :param ignore_ch_ids: set of ChEBI_ids to be excluded from the result (by default protons)
    if there is anything else in the result
    :return: tuple (ubiquitous_reactants, ubiquitous_products,
    specific_reactant_classes, specific_product_classes)
    """
    def classify(s_ids):
        specific, ubiquitous, ignored_ubs = [], [], []
        for s_id in s_ids:
            c_id = model.getSpecies(s_id).getCompartment()
            if ubiquitous_chebi_ids and s_id in s_id2term_id and s_id2term_id[
                    s_id] in ubiquitous_chebi_ids:
                if s_id2term_id[s_id] in ignore_ch_ids:
                    ignored_ubs.append((s_id2term_id[s_id], c_id))
                else:
                    ubiquitous.append((s_id2term_id[s_id], c_id))
            else:
                specific.append((s_id2clu[s_id][1] if s_id in s_id2clu else ((
                    s_id2term_id[s_id] if s_id in s_id2term_id else s_id), ),
                                 c_id))
        transform = lambda collection: tuple(sorted(collection))
        return transform(specific), transform(ubiquitous), transform(
            ignored_ubs)

    specific_reactant_classes, ubiquitous_reactants, ignored_reactants = classify(
        get_reactants(r))
    specific_product_classes, ubiquitous_products, ignored_products = classify(
        get_products(r))
    if not ubiquitous_reactants and not ubiquitous_products \
            and not specific_reactant_classes and not specific_product_classes:
        ubiquitous_reactants, ubiquitous_products = ignored_reactants, ignored_products
    return ubiquitous_reactants, ubiquitous_products, specific_reactant_classes, specific_product_classes
Esempio n. 3
0
def get_key_elements(model, r, s_id2clu, s_id2term_id, ubiquitous_chebi_ids, ignore_ch_ids=get_proton_ch_ids()):
    """
    Gets elements that compose a reaction key: ubiquitous_reactants, ubiquitous_products,
    specific_reactant_classes, specific_product_classes
    :param model: libsbml.Model
    :param r: libsbml.Reaction reaction of interest
    :param s_id2clu: dict {metabolite_id: (compartment_id, cluster)}
    :param s_id2term_id: dict {metabolite_id: ChEBI_term_id}
    :param ubiquitous_chebi_ids: set of ubiquitous ChEBI_ids
    :param ignore_ch_ids: set of ChEBI_ids to be excluded from the result (by default protons)
    if there is anything else in the result
    :return: tuple (ubiquitous_reactants, ubiquitous_products,
    specific_reactant_classes, specific_product_classes)
    """

    def classify(s_ids):
        specific, ubiquitous, ignored_ubs = [], [], []
        for s_id in s_ids:
            c_id = model.getSpecies(s_id).getCompartment()
            if ubiquitous_chebi_ids and s_id in s_id2term_id and s_id2term_id[s_id] in ubiquitous_chebi_ids:
                if s_id2term_id[s_id] in ignore_ch_ids:
                    ignored_ubs.append((s_id2term_id[s_id], c_id))
                else:
                    ubiquitous.append((s_id2term_id[s_id], c_id))
            else:
                specific.append((s_id2clu[s_id][1] if s_id in s_id2clu
                                else ((s_id2term_id[s_id] if s_id in s_id2term_id else s_id), ), c_id))
        transform = lambda collection: tuple(sorted(collection))
        return transform(specific), transform(ubiquitous), transform(ignored_ubs)

    specific_reactant_classes, ubiquitous_reactants, ignored_reactants = classify(get_reactants(r))
    specific_product_classes, ubiquitous_products, ignored_products = classify(get_products(r))
    if not ubiquitous_reactants and not ubiquitous_products \
            and not specific_reactant_classes and not specific_product_classes:
        ubiquitous_reactants, ubiquitous_products = ignored_reactants, ignored_products
    return ubiquitous_reactants, ubiquitous_products, specific_reactant_classes, specific_product_classes