def test_flattenExternalModelDefinition(): sbml_path = data.DFBA_EMD_SBML print(sbml_path) doc = sbmlio.read_sbml(sbml_path) # test that resource could be read assert doc is not None # test that model in document assert doc.getModel() is not None print(doc) print(doc.getModel().getId()) # check that model exists doc_no_emd = flattenExternalModelDefinitions(doc, validate=True) assert doc_no_emd is not None # check that there are no external model definitions comp_doc_no_emd = doc_no_emd.getPlugin("comp") assert 0 == comp_doc_no_emd.getNumExternalModelDefinitions() # check that all model definitions are still there assert 3 == comp_doc_no_emd.getNumModelDefinitions() # check model consistency Nall, Nerr, Nwarn = validation.check_doc(doc_no_emd)
def clip_prefixes(path_in, path_out): """ Clips all prefixes in model. This takes very long. :param path_in: :param path_out: :return: """ doc_fba = sbmlio.read_sbml(path_in) model = doc_fba.getModel() # clip R_ reaction and M_ metabolite prefixes utils.clip_prefixes_in_model(model) sbmlio.write_sbml(doc_fba, filepath=path_out, validate=False)
def fba_model(sbml_file, directory): """ Create FBA submodel. """ # Read the model fba_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/iAB_RBC_283.xml') doc_fba = sbmlio.read_sbml(fba_path) # add comp doc_fba.enablePackage( "http://www.sbml.org/sbml/level3/version1/comp/version1", "comp", True) doc_fba.setPackageRequired("comp", True) # add notes model = doc_fba.getModel() fba_notes = notes.format("""DFBA FBA submodel.""") utils.set_model_info(model, notes=fba_notes, creators=None, units=units, main_units=main_units) # clip R_ reaction and M_ metabolite prefixes utils.clip_prefixes_in_model(model) # set id & framework model.setId('{}_fba'.format(settings.MODEL_ID)) model.setName('{} (FBA)'.format(settings.MODEL_ID)) model.setSBOTerm(comp.SBO_FLUX_BALANCE_FRAMEWORK) # add units and information for species in model.getListOfSpecies(): species.setInitialConcentration(0.0) species.setHasOnlySubstanceUnits(False) species.setUnits(UNIT_AMOUNT) for compartment in model.getListOfCompartments(): compartment.setUnits(UNIT_VOLUME) # update the existing exchange reactions builder.update_exchange_reactions(model, flux_unit=UNIT_FLUX) # write SBML file sbmlio.write_sbml(doc_fba, filepath=pjoin(directory, sbml_file), validate=True) return doc_fba
def fba_model(sbml_file, directory): """ Create FBA submodel. """ # Read the model fba_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/iAB_RBC_283.xml') doc_fba = sbmlio.read_sbml(fba_path) # add comp doc_fba.enablePackage("http://www.sbml.org/sbml/level3/version1/comp/version1", "comp", True) doc_fba.setPackageRequired("comp", True) # add notes model = doc_fba.getModel() fba_notes = notes.format("""DFBA FBA submodel.""") utils.set_model_info(model, notes=fba_notes, creators=None, units=units, main_units=main_units) # clip R_ reaction and M_ metabolite prefixes utils.clip_prefixes_in_model(model) # set id & framework model.setId('{}_fba'.format(settings.MODEL_ID)) model.setName('{} (FBA)'.format(settings.MODEL_ID)) model.setSBOTerm(comp.SBO_FLUX_BALANCE_FRAMEWORK) # add units and information for species in model.getListOfSpecies(): species.setInitialConcentration(0.0) species.setHasOnlySubstanceUnits(False) species.setUnits(UNIT_AMOUNT) for compartment in model.getListOfCompartments(): compartment.setUnits(UNIT_VOLUME) # update the existing exchange reactions builder.update_exchange_reactions(model, flux_unit=UNIT_FLUX) # write SBML file sbmlio.write_sbml(doc_fba, filepath=pjoin(directory, sbml_file), validate=True) return doc_fba
def fba_model(sbml_file, directory): """ Create FBA submodel. """ # Read the model fba_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/ecoli_fba.xml') doc_fba = sbmlio.read_sbml(fba_path) # add comp doc_fba.enablePackage("http://www.sbml.org/sbml/level3/version1/comp/version1", "comp", True) doc_fba.setPackageRequired("comp", True) # add notes model = doc_fba.getModel() fba_notes = notes.format("""DFBA FBA submodel.""") utils.set_model_info(model, notes=fba_notes, creators=None, units=units, main_units=main_units) # clip R_ reaction and M_ metabolite prefixes utils.clip_prefixes_in_model(model) # set id & framework model.setId('ecoli_fba') model.setName('ecoli (FBA)') model.setSBOTerm(comp.SBO_FLUX_BALANCE_FRAMEWORK) # add units and information for species in model.getListOfSpecies(): species.setInitialConcentration(0.0) species.setHasOnlySubstanceUnits(False) species.setUnits(UNIT_AMOUNT) for compartment in model.getListOfCompartments(): compartment.setUnits(UNIT_VOLUME) # The ATPM (atp maintainance reactions creates many problems in the DFBA) # mainly resulting in infeasible solutions when some metabolites run out. # ATP -> ADP is part of the biomass, so we set the lower bound to zero r_ATPM = model.getReaction('ATPM') r_ATPM_fbc = r_ATPM.getPlugin(builder.SBML_FBC_NAME) lb_id = r_ATPM_fbc.getLowerFluxBound() model.getParameter(lb_id).setValue(0.0) # 8.39 before # make unique upper and lower bounds for exchange reaction if not biomass_weighting: builder.update_exchange_reactions(model=model, flux_unit=UNIT_FLUX) else: builder.update_exchange_reactions(model=model, flux_unit=UNIT_FLUX_PER_G) # add exchange reaction for biomass (X) # we are adding the biomass component to the biomass function and create an # exchange reaction for it r_biomass = model.getReaction('BIOMASS_Ecoli_core_w_GAM') # FIXME: refactor in function # FIXME: annotate biomass species (SBO for biomass missing) mc.create_objects(model, [ mc.Parameter(sid='cf_X', value=1.0, unit="g_per_mmol", name="biomass conversion factor", constant=True), mc.Species(sid='X', initialAmount=0.001, compartment='c', name='biomass', substanceUnit='g', hasOnlySubstanceUnits=True, conversionFactor='cf_X') ]) pr_biomass = r_biomass.createProduct() pr_biomass.setSpecies('X') pr_biomass.setStoichiometry(1.0) pr_biomass.setConstant(True) # FIXME: the flux units must fit to the species units. if not biomass_weighting: builder.create_exchange_reaction(model, species_id='X', flux_unit=UNIT_FLUX, exchange_type=builder.EXCHANGE_EXPORT) else: builder.create_exchange_reaction(model, species_id='X', flux_unit=UNIT_FLUX_PER_G, exchange_type=builder.EXCHANGE_EXPORT) # write SBML file sbmlio.write_sbml(doc_fba, filepath=pjoin(directory, sbml_file), validate=True) # Set kinetic laws to zero for kinetic simulation ''' for reaction in model.getListOfReactions(): ast_node = mc.ast_node_from_formula(model=model, formula='0 {}'.format(UNIT_FLUX)) law = reaction.createKineticLaw() law.setMath(ast_node) ''' return doc_fba
prefix = plugin.getPrefix() name = plugin.getPackageName() doc.enablePackage(uri, prefix, True) # print(k, plugin) # print(uri, prefix) # print("\n") # add model definition for model md = libsbml.ModelDefinition(ref_model) comp_doc.addModelDefinition(md) # remove the emds afterwards for emd_id in emd_ids: # remove the emd from the model comp_doc.removeExternalModelDefinition(emd_id) # validate if validate: validation.check_doc(doc) return doc if __name__ == "__main__": from sbmlutils.tests import data from sbmlutils import sbmlio doc = sbmlio.read_sbml(data.DFBA_EMD_SBML) doc_no_emd = flattenExternalModelDefinitions(doc, validate=True)