Exemplo n.º 1
0
def ubiquitize_model(in_sbml,
                     chebi,
                     groups_sbml,
                     ub_s_ids=None,
                     ub_chebi_ids=None):
    """
    Infers and marks ubiquitous species in the model.
    :param in_sbml: str, path to the input SBML file
    :param chebi: mod_sbml.onto.obo_ontology.Ontology ChEBI ontology
    :param groups_sbml: str, path to the output SBML file (with groups extension)
    :param ub_s_ids: optional, ids of ubiquitous species (will be inferred if set to None)
    :param ub_chebi_ids: optional, ids of ubiquitous ChEBI terms (will be inferred if set to None)
    :return: tuple (s_id2chebi_id, ub_s_ids): dict {species_id: ChEBI_term_id},  collection of ubiquitous species_ids.
    """
    input_doc = libsbml.SBMLReader().readSBML(in_sbml)
    input_model = input_doc.getModel()
    annotate_metabolites(input_model, chebi)

    logging.info("mapping species to ChEBI")
    s_id2chebi_id = get_species_id2chebi_id(input_model)
    _, ub_s_ids = get_ub_elements(input_model, chebi, s_id2chebi_id,
                                  ub_chebi_ids, ub_s_ids)

    save_as_comp_generalized_sbml(input_model, None, groups_sbml, {}, {},
                                  ub_s_ids, chebi)
    return s_id2chebi_id, ub_s_ids
Exemplo n.º 2
0
def update_model_element_ids(m_id, model, go2c_id, go, chebi):
    id2id = {}
    if need_boundary_compartment(model):
        separate_boundary_metabolites(model)
    annotate_metabolites(model, chebi)
    annotate_compartments(model, go)

    for c in model.getListOfCompartments():
        c_id = c.getId()
        go_id = get_go_id(c)
        if not go_id:
            go_id = c.getName()
        if go_id:
            go_id = go_id.lower()
            if go_id == CYTOSOL and (CYTOSOL not in go2c_id) and (CYTOPLASM
                                                                  in go2c_id):
                go_id = CYTOPLASM
            elif go_id == CYTOPLASM and (CYTOSOL
                                         in go2c_id) and (CYTOPLASM
                                                          not in go2c_id):
                go_id = CYTOSOL
            if go_id not in go2c_id:
                go2c_id[go_id] = "c_%s__%s" % (m_id, c_id)
            new_c_id = go2c_id[go_id]
        else:
            new_c_id = "c_%s__%s" % (m_id, c_id)
        c.setId(new_c_id)
        id2id[c_id] = new_c_id

    for c in model.getListOfCompartments():
        if c.getOutside():
            c.setOutside(id2id[c.getOutside()])

    for s in model.getListOfSpecies():
        if s.getCompartment():
            s.setCompartment(id2id[s.getCompartment()])
        old_id = s.getId()
        new_id = "s_%s__%s" % (m_id, old_id)
        s.setId(new_id)
        id2id[old_id] = new_id
        if s.getSpeciesType():
            s.unsetSpeciesType()

    for r in model.getListOfReactions():
        old_id = r.getId()
        new_id = "r_%s__%s" % (m_id, old_id)
        r.setId(new_id)
        id2id[old_id] = new_id
        if r.getCompartment():
            r.setCompartment(id2id[r.getCompartment()])
        for s_ref in r.getListOfReactants():
            s_ref.setSpecies(id2id[s_ref.getSpecies()])
        for s_ref in r.getListOfProducts():
            s_ref.setSpecies(id2id[s_ref.getSpecies()])
        for s_ref in r.getListOfModifiers():
            s_ref.setSpecies(id2id[s_ref.getSpecies()])
Exemplo n.º 3
0
def add_boundary_metabolites(in_sbml, out_sbml):
    """
    Creates a boundary compartment with the id 'Boundary',
    and transforms each input/output reaction '*_e <-> ' into '*_e <-> *_b'.
    :param in_sbml: path to the SBML file with the original model
    :param out_sbml: path where to store the resulting SBML file
    """
    input_doc = libsbml.SBMLReader().readSBML(in_sbml)
    model = input_doc.getModel()
    chebi = parse_simple(get_chebi())
    annotate_metabolites(model, chebi)
    create_boundary_species_in_boundary_reactions(model)
    libsbml.SBMLWriter().writeSBMLToFile(input_doc, out_sbml)
Exemplo n.º 4
0
def generalize_model(in_sbml, chebi, groups_sbml, out_sbml, ub_s_ids=None, ub_chebi_ids=None, ignore_biomass=True):
    """
    Generalizes a model.
    :param in_sbml: str, path to the input SBML file
    :param chebi: mod_sbml.onto.obo_ontology.Ontology ChEBI ontology
    :param groups_sbml: str, path to the output SBML file (with groups extension)
    :param out_sbml: str, path to the output SBML file (generalized)
    :param ub_s_ids: optional, ids of ubiquitous species (will be inferred if set to None)
    :param ub_chebi_ids: optional, ids of ubiquitous ChEBI terms (will be inferred if set to None)
    :param ignore_biomass: boolean, whether to ignore the biomass reaction (and its stoichiometry preserving constraint)
    :return: tuple (r_id2g_eq, s_id2gr_id, s_id2chebi_id, ub_s_ids):
    dict {reaction_id: reaction_group_id}, dict {species_id: species_group_id}, dict {species_id: ChEBI_term_id},
    collection of ubiquitous species_ids.
    """
    # input_model
    input_doc = libsbml.SBMLReader().readSBML(in_sbml)
    input_model = input_doc.getModel()
    r_ids_to_ignore = get_biomass_r_ids(input_model) if ignore_biomass else None

    remove_is_a_reactions(input_model)
    annotate_metabolites(input_model, chebi)
    # TODO: fix comp separation
    # separate_boundary_metabolites(input_model)
    remove_unused_elements(input_model)

    logging.info("mapping species to ChEBI")
    s_id2chebi_id = get_species_id2chebi_id(input_model)
    ub_chebi_ids, ub_s_ids = get_ub_elements(input_model, chebi, s_id2chebi_id, ub_chebi_ids, ub_s_ids)

    terms = (t for t in (chebi.get_term(t_id) for t_id in s_id2chebi_id.values()) if t)
    old_onto_len = len(chebi)
    filter_ontology(chebi, terms, relationships=EQUIVALENT_RELATIONSHIPS, min_deepness=3)
    logging.info('Filtered the ontology from %d terms to %d' % (old_onto_len, len(chebi)))

    threshold = min(max(3, int(0.1 * input_model.getNumReactions())), UBIQUITOUS_THRESHOLD)
    s_id2clu, ub_s_ids = generalize_species(input_model, s_id2chebi_id, ub_s_ids, chebi, ub_chebi_ids, threshold,
                                            r_ids_to_ignore=r_ids_to_ignore)
    logging.info("generalized species")
    r_id2clu = generalize_reactions(input_model, s_id2clu, s_id2chebi_id, ub_chebi_ids,
                                    r_ids_to_ignore=r_ids_to_ignore)
    logging.info("generalized reactions")

    clu2s_ids = {(c_id, term): s_ids for ((c_id, (term, )), s_ids) in invert_map(s_id2clu).items()}
    r_id2g_eq, s_id2gr_id = save_as_comp_generalized_sbml(input_model, out_sbml, groups_sbml, r_id2clu, clu2s_ids,
                                                          ub_s_ids, chebi)
    return r_id2g_eq, s_id2gr_id, s_id2chebi_id, ub_s_ids
Exemplo n.º 5
0
def ubiquitize_model(in_sbml, chebi, groups_sbml, ub_s_ids=None, ub_chebi_ids=None):
    """
    Infers and marks ubiquitous species in the model.
    :param in_sbml: str, path to the input SBML file
    :param chebi: mod_sbml.onto.obo_ontology.Ontology ChEBI ontology
    :param groups_sbml: str, path to the output SBML file (with groups extension)
    :param ub_s_ids: optional, ids of ubiquitous species (will be inferred if set to None)
    :param ub_chebi_ids: optional, ids of ubiquitous ChEBI terms (will be inferred if set to None)
    :return: tuple (s_id2chebi_id, ub_s_ids): dict {species_id: ChEBI_term_id},  collection of ubiquitous species_ids.
    """
    input_doc = libsbml.SBMLReader().readSBML(in_sbml)
    input_model = input_doc.getModel()
    annotate_metabolites(input_model, chebi)

    logging.info("mapping species to ChEBI")
    s_id2chebi_id = get_species_id2chebi_id(input_model)
    _, ub_s_ids = get_ub_elements(input_model, chebi, s_id2chebi_id, ub_chebi_ids, ub_s_ids)

    save_as_comp_generalized_sbml(input_model, None, groups_sbml, {}, {}, ub_s_ids, chebi)
    return s_id2chebi_id, ub_s_ids
Exemplo n.º 6
0
def generalize_model(in_sbml,
                     chebi,
                     groups_sbml,
                     out_sbml,
                     ub_s_ids=None,
                     ub_chebi_ids=None,
                     ignore_biomass=True):
    """
    Generalizes a model.
    :param in_sbml: str, path to the input SBML file
    :param chebi: mod_sbml.onto.obo_ontology.Ontology ChEBI ontology
    :param groups_sbml: str, path to the output SBML file (with groups extension)
    :param out_sbml: str, path to the output SBML file (generalized)
    :param ub_s_ids: optional, ids of ubiquitous species (will be inferred if set to None)
    :param ub_chebi_ids: optional, ids of ubiquitous ChEBI terms (will be inferred if set to None)
    :param ignore_biomass: boolean, whether to ignore the biomass reaction (and its stoichiometry preserving constraint)
    :return: tuple (r_id2g_eq, s_id2gr_id, s_id2chebi_id, ub_s_ids):
    dict {reaction_id: reaction_group_id}, dict {species_id: species_group_id}, dict {species_id: ChEBI_term_id},
    collection of ubiquitous species_ids.
    """
    # input_model
    input_doc = libsbml.SBMLReader().readSBML(in_sbml)
    input_model = input_doc.getModel()
    r_ids_to_ignore = get_biomass_r_ids(
        input_model) if ignore_biomass else None

    remove_is_a_reactions(input_model)
    annotate_metabolites(input_model, chebi)
    # TODO: fix comp separation
    # separate_boundary_metabolites(input_model)
    remove_unused_elements(input_model)

    logging.info("mapping species to ChEBI")
    s_id2chebi_id = get_species_id2chebi_id(input_model)
    ub_chebi_ids, ub_s_ids = get_ub_elements(input_model, chebi, s_id2chebi_id,
                                             ub_chebi_ids, ub_s_ids)

    terms = (t for t in (chebi.get_term(t_id)
                         for t_id in s_id2chebi_id.values()) if t)
    old_onto_len = len(chebi)
    filter_ontology(chebi,
                    terms,
                    relationships=EQUIVALENT_RELATIONSHIPS,
                    min_deepness=3)
    logging.info('Filtered the ontology from %d terms to %d' %
                 (old_onto_len, len(chebi)))

    threshold = min(max(3, int(0.1 * input_model.getNumReactions())),
                    UBIQUITOUS_THRESHOLD)
    s_id2clu, ub_s_ids = generalize_species(input_model,
                                            s_id2chebi_id,
                                            ub_s_ids,
                                            chebi,
                                            ub_chebi_ids,
                                            threshold,
                                            r_ids_to_ignore=r_ids_to_ignore)
    logging.info("generalized species")
    r_id2clu = generalize_reactions(input_model,
                                    s_id2clu,
                                    s_id2chebi_id,
                                    ub_chebi_ids,
                                    r_ids_to_ignore=r_ids_to_ignore)
    logging.info("generalized reactions")

    clu2s_ids = {(c_id, term): s_ids
                 for ((c_id, (term, )), s_ids) in invert_map(s_id2clu).items()}
    r_id2g_eq, s_id2gr_id = save_as_comp_generalized_sbml(
        input_model, out_sbml, groups_sbml, r_id2clu, clu2s_ids, ub_s_ids,
        chebi)
    return r_id2g_eq, s_id2gr_id, s_id2chebi_id, ub_s_ids