def create_folded_model(S, model):
    r_id2new_r_id = {}
    initial_r_ids = {r_id for r_id in S.r_id2i.keys() if r_id not in S.gr_id2r_id2c}
    r_ids_to_remove = [r.getId() for r in model.getListOfReactions() if r.getId() not in S.r_ids]

    def map2new_id(r_id, new_id):
        if r_id not in S.gr_id2r_id2c:
            r_id2new_r_id[r_id] = new_id
        else:
            for sub_r_id in S.gr_id2r_id2c[r_id].keys():
                map2new_id(sub_r_id, new_id)

    for mr_id in S.r_ids - initial_r_ids:
        cl_id2c = S.gr_id2r_id2c[mr_id]
        name = 'lumped reaction %s' % mr_id
        id_ = mr_id
        r_id2st, p_id2st = S.st_matrix.get_inputs_outputs(mr_id)
        new_r_id = create_reaction(model, r_id2st, p_id2st, reversible=True, id_=id_, name=name).getId()
        for r_id in cl_id2c:
            map2new_id(r_id, new_r_id)

    for r_id in r_ids_to_remove:
        model.removeReaction(r_id)

    for r in model.getListOfReactions():
        for i in range(0, r.getNumModifiers()):
            r.removeModifier(0)

    remove_unused_species(model)
    return r_id2new_r_id
Exemplo n.º 2
0
def simple_merge_models(S, model_id2c_id2group, model_id2dfs, out_sbml):
    doc = libsbml.SBMLDocument(2, 4)
    model = doc.createModel()

    model.setId('merged_model')

    model_id2id2id = defaultdict(dict)
    common_ids = set()
    c_group2id = {}

    new_m_id2i, new_r_id2i, new_efm_id2i, new_boundary_m_ids = {}, {}, {}, []

    for model_id, [_, _, df] in model_id2dfs.items():
        for index, row in df.iterrows():
            c_id, name = row['Id'], row['Name']
            if model_id in model_id2c_id2group and c_id in model_id2c_id2group[model_id]:
                group = model_id2c_id2group[model_id][c_id]
                if group in c_group2id:
                    new_id = c_group2id[group]
                else:
                    new_id = create_compartment(model, name=name, id_='merged_%s_%s' % (model_id, c_id)).getId()
                    c_group2id[group] = new_id
                    common_ids.add(new_id)
            else:
                new_id = create_compartment(model, name=name, id_='%s_%s' % (model_id, c_id)).getId()
            model_id2id2id[model_id][c_id] = new_id

    id2id = {}

    m_id_group_ids = set(S.m_id2gr_id.values())
    for (model_id, m_id), i in ((it, i) for (it, i) in S.m_id2i.items() if it not in m_id_group_ids):
        c_id = model_id2dfs[model_id][0].at[m_id, 'Compartment']
        c_id = model_id2id2id[model_id][c_id]
        name = model_id2dfs[model_id][0].at[m_id, 'Name']
        is_boundary = (model_id, m_id) in S.boundary_m_ids
        new_id = create_species(model, compartment_id=c_id, name=name, bound=is_boundary,
                                id_='%s_%s' % (model_id, m_id)).getId()
        model_id2id2id[model_id][m_id] = new_id
        id2id[(model_id, m_id)] = new_id
        new_m_id2i[new_id] = i
        if is_boundary:
            new_boundary_m_ids.append(new_id)

    for it in m_id_group_ids:
        model_id, m_ids = next(iter(it))
        m_id = next(iter(m_ids))
        is_boundary = it in S.boundary_m_ids
        new_id = \
            create_species(model,
                           compartment_id=model_id2id2id[model_id][model_id2dfs[model_id][0].at[m_id, 'Compartment']],
                           name=model_id2dfs[model_id][0].at[m_id, 'Name'], bound=is_boundary,
                           id_='merged_%s_%s' % (model_id, m_id)).getId()
        for model_id, m_ids in it:
            model_id2id2id[model_id].update({m_id: new_id for m_id in m_ids})
        id2id[it] = new_id
        new_m_id2i[new_id] = S.m_id2i[it]
        if is_boundary:
            new_boundary_m_ids.append(new_id)
        common_ids.add(new_id)

    for ((model_id, r_id), i) in ((it, i) for (it, i) in S.r_id2i.items() if it not in S.gr_id2r_id2c.keys()):
        r_id2st, p_id2st = S.st_matrix.get_inputs_outputs((model_id, r_id))
        new_id = create_reaction(model, {id2id[m_id]: st for (m_id, st) in r_id2st.items()},
                                 {id2id[m_id]: st for (m_id, st) in p_id2st.items()},
                                 model_id2dfs[model_id][1].at[r_id, 'Name'], reversible=True,
                                 id_='%s_%s' % (model_id, r_id)).getId()
        model_id2id2id[model_id][r_id] = new_id
        new_r_id2i[new_id] = i

    for gr, it2c in S.gr_id2r_id2c.items():
        model_id, r_id = next(iter(it2c.keys()))
        r_id2st, p_id2st = S.st_matrix.get_inputs_outputs(gr)
        new_id = \
            create_reaction(model, {id2id[m_id]: st for (m_id, st) in r_id2st.items()},
                            {id2id[m_id]: st for (m_id, st) in p_id2st.items()},
                            model_id2dfs[model_id][1].at[r_id, 'Name'], reversible=True,
                            id_='merged_%s_%s' % (model_id, r_id)).getId()
        for model_id, r_id in it2c.keys():
            model_id2id2id[model_id][r_id] = new_id
        new_r_id2i[new_id] = S.r_id2i[gr]
        common_ids.add(new_id)

    for ((model_id, efm_id), i) in ((it, i) for (it, i) in S.efm_id2i.items() if it not in S.gr_id2efm_ids.keys()):
        new_id = '%s_%s' % (model_id, efm_id)
        new_efm_id2i[new_id] = i
        model_id2id2id[model_id][efm_id] = new_id

    for gr, efm_ids in S.gr_id2efm_ids.items():
        model_id, efm_id = next(efm_ids)
        new_id = 'merged_%s_%s' % (model_id, efm_id)
        new_efm_id2i[new_id] = S.r_id2i[gr]
        for model_id, efm_id in efm_ids:
            model_id2id2id[model_id][efm_id] = new_id

    libsbml.writeSBMLToFile(doc, out_sbml)

    return model_id2id2id, common_ids, System(m_id2i=new_m_id2i, r_id2i=new_r_id2i, efm_id2i=new_efm_id2i, N=S.N, V=S.V,
                                              boundary_m_ids=new_boundary_m_ids)
Exemplo n.º 3
0
def save_as_comp_generalized_sbml(input_model, out_sbml, groups_sbml, r_id2clu, clu2s_ids, ub_sps, onto):
    logging.info("serializing generalization")
    s_id_increment, r_id_increment = 0, 0

    if groups_sbml:
        doc = convert_to_lev3_v1(input_model)
        groups_model = doc.getModel()
        groups_plugin = groups_model.getPlugin("groups")
        if groups_plugin:
            logging.info("  saving ubiquitous species annotations")
            s_group = groups_plugin.createGroup()
            s_group.setId("g_ubiquitous_sps")
            s_group.setKind(libsbml.GROUP_KIND_COLLECTION)
            s_group.setSBOTerm(SBO_CHEMICAL_MACROMOLECULE)
            s_group.setName("ubiquitous species")
            for s_id in ub_sps:
                member = s_group.createMember()
                member.setIdRef(s_id)
            add_annotation(s_group, libsbml.BQB_IS_DESCRIBED_BY, GROUP_TYPE_UBIQUITOUS)
    if out_sbml:
        # generalized model
        generalized_doc = libsbml.SBMLDocument(input_model.getSBMLNamespaces())
        generalized_model = generalized_doc.createModel()
        copy_elements(input_model, generalized_model)

    r_id2g_eq, s_id2gr_id = {}, {}
    if not clu2s_ids:
        logging.info("  nothing to serialize")
    else:
        clu2r_ids = invert_map(r_id2clu)
        logging.info("  creating species groups")
        for ((c_id, t), s_ids) in clu2s_ids.items():
            comp = input_model.getCompartment(c_id)
            if len(s_ids) > 1:
                t = onto.get_term(t)
                t_name, t_id = (t.get_name(), t.get_id()) if t \
                    else (' or '.join(input_model.getSpecies(s_id).getName() for s_id in s_ids), None)
                if not t_id:
                    t = t_name

                if out_sbml:
                    new_species = create_species(model=generalized_model, compartment_id=comp.getId(), type_id=None,
                                                 name="{0} ({1}) [{2}]".format(t_name, len(s_ids), comp.getName()))
                    add_annotation(new_species, libsbml.BQB_IS, t_id, CHEBI_PREFIX)
                    new_s_id = new_species.getId()
                else:
                    s_id_increment += 1
                    new_s_id = generate_unique_id(input_model, "s_g_", s_id_increment)
                for s_id in s_ids:
                    s_id2gr_id[s_id] = new_s_id, t

                if groups_sbml and groups_plugin:
                    # save as a group
                    s_group = groups_plugin.createGroup()
                    s_group.setId(new_s_id)
                    s_group.setKind(libsbml.GROUP_KIND_CLASSIFICATION)
                    s_group.setSBOTerm(SBO_CHEMICAL_MACROMOLECULE)
                    g_name = "{0} [{1}]".format(t_name, comp.getName())
                    s_group.setName(g_name)
                    # logging.info("%s: %d" % (g_name, len(s_ids)))
                    if t_id:
                        add_annotation(s_group, libsbml.BQB_IS, t_id, CHEBI_PREFIX)
                    for s_id in s_ids:
                        member = s_group.createMember()
                        member.setIdRef(s_id)
                    add_annotation(s_group, libsbml.BQB_IS_DESCRIBED_BY, GROUP_TYPE_EQUIV)

        generalize_species = lambda species_id: s_id2gr_id[species_id][0] if (species_id in s_id2gr_id) else species_id
        s_id_to_generalize = set(s_id2gr_id.keys())
        logging.info("  creating reaction groups")
        for clu, r_ids in clu2r_ids.items():
            representative = input_model.getReaction(list(r_ids)[0])
            r_name = "generalized %s" % representative.getName()
            if out_sbml:
                reactants = dict(get_reactants(representative, stoichiometry=True))
                products = dict(get_products(representative, stoichiometry=True))
                if (len(r_ids) == 1) and \
                        not ((set(reactants.keys()) | set(products.keys())) & s_id_to_generalize):
                    generalized_model.addReaction(representative)
                    continue
                r_id2st = {generalize_species(it): st for (it, st) in reactants.items()}
                p_id2st = {generalize_species(it): st for (it, st) in products.items()}
                reversible = next((False for r_id in r_ids if not input_model.getReaction(r_id).getReversible()), True)
                new_r_id = create_reaction(generalized_model, r_id2st, p_id2st, name=r_name, reversible=reversible,
                                           id_=representative.getId() if len(r_ids) == 1 else None).getId()
            elif len(r_ids) > 1:
                r_id_increment += 1
                new_r_id = generate_unique_id(input_model, "r_g_", r_id_increment)
            if len(r_ids) > 1:
                for r_id in r_ids:
                    r_id2g_eq[r_id] = new_r_id, r_name
                if groups_sbml and groups_plugin:
                    # save as a group
                    r_group = groups_plugin.createGroup()
                    r_group.setId(new_r_id)
                    r_group.setKind(libsbml.GROUP_KIND_COLLECTION)
                    r_group.setSBOTerm(SBO_BIOCHEMICAL_REACTION)
                    r_group.setName(r_name)
                    for r_id in r_ids:
                        member = r_group.createMember()
                        member.setIdRef(r_id)
                    add_annotation(r_group, libsbml.BQB_IS_DESCRIBED_BY, GROUP_TYPE_EQUIV)
    if out_sbml:
        remove_unused_elements(generalized_model)
        save_as_sbml(generalized_model, out_sbml)
    if groups_sbml and groups_model:
        save_as_sbml(groups_model, groups_sbml)

    logging.info("serialized to " + groups_sbml)
    return r_id2g_eq, s_id2gr_id
Exemplo n.º 4
0
def save_as_comp_generalized_sbml(input_model, out_sbml, groups_sbml, r_id2clu, clu2s_ids, ub_sps, onto):
    logging.info("serializing generalization")
    s_id_increment, r_id_increment = 0, 0

    if groups_sbml:
        doc = convert_to_lev3_v1(input_model)
        groups_model = doc.getModel()
        groups_plugin = groups_model.getPlugin("groups")
        if groups_plugin:
            logging.info("  saving ubiquitous species annotations")
            s_group = groups_plugin.createGroup()
            s_group.setId("g_ubiquitous_sps")
            s_group.setKind(libsbml.GROUP_KIND_COLLECTION)
            s_group.setSBOTerm(SBO_CHEMICAL_MACROMOLECULE)
            s_group.setName("ubiquitous species")
            for s_id in ub_sps:
                member = s_group.createMember()
                member.setIdRef(s_id)
            add_annotation(s_group, libsbml.BQB_IS_DESCRIBED_BY, GROUP_TYPE_UBIQUITOUS)
    if out_sbml:
        # generalized model
        generalized_doc = convert_to_lev3_v1(input_model)
        generalized_model = generalized_doc.getModel()
        for _ in range(0, generalized_model.getNumReactions()):
            generalized_model.removeReaction(0)

    r_id2g_eq, s_id2gr_id = {}, {}
    if not clu2s_ids:
        logging.info("  nothing to serialize")
    else:
        clu2r_ids = invert_map(r_id2clu)
        logging.info("  creating species groups")
        for ((c_id, t), s_ids) in clu2s_ids.items():
            comp = input_model.getCompartment(c_id)
            if len(s_ids) > 1:
                t = onto.get_term(t)
                t_name, t_id = (t.get_name(), t.get_id()) if t \
                    else (' or '.join(input_model.getSpecies(s_id).getName() for s_id in s_ids), None)
                if not t_id:
                    t = t_name

                if out_sbml:
                    new_species = create_species(model=generalized_model, compartment_id=comp.getId(), type_id=None,
                                                 name="{0} ({1}) [{2}]".format(t_name, len(s_ids), comp.getName()))
                    add_annotation(new_species, libsbml.BQB_IS, t_id, CHEBI_PREFIX)
                    new_s_id = new_species.getId()
                else:
                    s_id_increment += 1
                    new_s_id = generate_unique_id(input_model, "s_g_", s_id_increment)
                for s_id in s_ids:
                    s_id2gr_id[s_id] = new_s_id, t

                if groups_sbml and groups_plugin:
                    # save as a group
                    s_group = groups_plugin.createGroup()
                    s_group.setId(new_s_id)
                    s_group.setKind(libsbml.GROUP_KIND_CLASSIFICATION)
                    s_group.setSBOTerm(SBO_CHEMICAL_MACROMOLECULE)
                    g_name = "{0} [{1}]".format(t_name, comp.getName())
                    s_group.setName(g_name)
                    # logging.info("%s: %d" % (g_name, len(s_ids)))
                    if t_id:
                        add_annotation(s_group, libsbml.BQB_IS, t_id, CHEBI_PREFIX)
                    for s_id in s_ids:
                        member = s_group.createMember()
                        member.setIdRef(s_id)
                    add_annotation(s_group, libsbml.BQB_IS_DESCRIBED_BY, GROUP_TYPE_EQUIV)

        generalize_species = lambda species_id: s_id2gr_id[species_id][0] if (species_id in s_id2gr_id) else species_id
        s_id_to_generalize = set(s_id2gr_id.keys())
        logging.info("  creating reaction groups")
        for clu, r_ids in clu2r_ids.items():
            representative = input_model.getReaction(list(r_ids)[0])
            r_name = "generalized %s" % representative.getName()
            if out_sbml:
                reactants = dict(get_reactants(representative, stoichiometry=True))
                products = dict(get_products(representative, stoichiometry=True))
                if (len(r_ids) == 1) and \
                        not ((set(reactants.keys()) | set(products.keys())) & s_id_to_generalize):
                    create_reaction(generalized_model, reactants, products, name=representative.getName(),
                                    reversible=representative.getReversible(), id_=representative.getId())
                    continue
                r_id2st = {generalize_species(it): st for (it, st) in reactants.items()}
                p_id2st = {generalize_species(it): st for (it, st) in products.items()}
                reversible = next((False for r_id in r_ids if not input_model.getReaction(r_id).getReversible()), True)
                new_r_id = create_reaction(generalized_model, r_id2st, p_id2st, name=r_name, reversible=reversible,
                                           id_=representative.getId() if len(r_ids) == 1 else None).getId()
            elif len(r_ids) > 1:
                r_id_increment += 1
                new_r_id = generate_unique_id(input_model, "r_g_", r_id_increment)
            if len(r_ids) > 1:
                for r_id in r_ids:
                    r_id2g_eq[r_id] = new_r_id, r_name
                if groups_sbml and groups_plugin:
                    # save as a group
                    r_group = groups_plugin.createGroup()
                    r_group.setId(new_r_id)
                    r_group.setKind(libsbml.GROUP_KIND_COLLECTION)
                    r_group.setSBOTerm(SBO_BIOCHEMICAL_REACTION)
                    r_group.setName(r_name)
                    for r_id in r_ids:
                        member = r_group.createMember()
                        member.setIdRef(r_id)
                    add_annotation(r_group, libsbml.BQB_IS_DESCRIBED_BY, GROUP_TYPE_EQUIV)
    if out_sbml:
        remove_unused_elements(generalized_model)
        save_as_sbml(generalized_model, out_sbml)
    if groups_sbml and groups_model:
        save_as_sbml(groups_model, groups_sbml)

    logging.info("serialized to " + groups_sbml)
    return r_id2g_eq, s_id2gr_id