def create_layout(n2lo, layout_model, layout_plugin, ub_sps, model): h_min, (x_shift, y_shift), (w, h) = get_layout_characteristics(n2lo) scale_factor = MARGIN * 1.0 / (h_min if h_min else 1) (w, h) = scale((w, h), scale_factor) (x_shift, y_shift) = shift(scale((x_shift, y_shift), scale_factor), MARGIN, MARGIN) layout = layout_plugin.createLayout() layout.setId(generate_unique_id(layout_model, "l_")) l_id = layout.getId() layout.setDimensions(create_dimensions(w + 2 * MARGIN, h + 2 * MARGIN)) for comp in model.getListOfCompartments(): c_id = comp.getId() c_name = comp.getName() if c_id in n2lo: (x, y), (w, h) = n2lo[c_id] (x, y), (w, h) = shift(scale((x, y), scale_factor), x_shift, y_shift), scale((w, h), scale_factor) comp_glyph = layout.createCompartmentGlyph() comp_glyph.setId("cg_%s_%s" % (l_id, c_id)) comp_glyph.setCompartmentId(c_id) comp_glyph.setBoundingBox(create_bounding_box(x, y, w, h)) add_label(c_name, layout, comp_glyph, c_id, w, h, x, y) for species in model.getListOfSpecies(): s_id = species.getId() s_name = species.getName() if s_id in n2lo: if isinstance(n2lo[s_id], dict): elements = n2lo[s_id].items() else: elements = [('', n2lo[s_id])] for r_ids, [(x, y), (w, h)] in elements: if not r_ids or next((it for it in (model.getReaction(r_id) for r_id in r_ids) if it), False): (x, y), (w, h) = shift(scale((x, y), scale_factor), x_shift, y_shift), scale((w, h), scale_factor) s_glyph = layout.createSpeciesGlyph() s_glyph.setSpeciesId(s_id) s_glyph_suffix = "%s_%s" % (s_id, '_'.join(r_ids)) if r_ids else s_id s_glyph.setId("sg_%s_%s" % (l_id, s_glyph_suffix)) s_glyph.setBoundingBox(create_bounding_box(x, y, w, h)) add_label(s_name, layout, s_glyph, s_id, w, h, x, y) for reaction in model.getListOfReactions(): r_id = reaction.getId() r_name = reaction.getName() if r_id in n2lo: (x, y), (w, h) = n2lo[r_id] (x, y), (w, h) = shift(scale((x, y), scale_factor), x_shift, y_shift), scale((w, h), scale_factor) r_glyph = layout.createReactionGlyph() r_glyph.setReactionId(r_id) r_glyph.setId("rg_%s_%s" % (l_id, r_id)) r_glyph.setBoundingBox(create_bounding_box(x, y, w, h)) add_label(r_name, layout, r_glyph, r_id, w, h, x, y) link_reaction_to_species(reaction.getListOfReactants(), r_glyph, l_id, r_id, n2lo, lambda s_id: libsbml.SPECIES_ROLE_SIDESUBSTRATE if s_id in ub_sps else libsbml.SPECIES_ROLE_SUBSTRATE) link_reaction_to_species(reaction.getListOfProducts(), r_glyph, l_id, r_id, n2lo, lambda s_id: libsbml.SPECIES_ROLE_SIDEPRODUCT if s_id in ub_sps else libsbml.SPECIES_ROLE_PRODUCT)
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
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