def create_dummy_reactions(model, model_fba, unit_flux=None): """ Creates the dummy reactions. This also creates the corresponding flux parameters and flux assignments. :param model: :param model_fba: :param unit_flux: :return: """ ex_rids = utils.find_exchange_reactions(model_fba) objects = [] for ex_rid, sid in ex_rids.items(): pid_flux = FLUX_PARAMETER_PREFIX + sid rid_flux = DUMMY_REACTION_PREFIX + sid objects.append( # flux parameter: fluxe from fba (rate of reaction) fac.Parameter(sid=pid_flux, value=1.0, constant=True, unit=unit_flux, sboTerm=FLUX_PARAMETER_SBO), ) # dummy reaction (pseudoreaction) fac.create_reaction(model, rid=rid_flux, reversible=False, products={DUMMY_SPECIES_ID: 1}, sboTerm=DUMMY_REACTION_SBO, formula='0 {}'.format(unit_flux)) # flux assignment rule objects.append( fac.AssignmentRule(pid_flux, value=rid_flux, sboTerm=FLUX_ASSIGNMENTRULE_SBO), ) fac.create_objects(model, objects)
def create_dfba_species(model, model_fba, compartment_id, hasOnlySubstanceUnits=False, unit_amount=None, create_port=True, exclude_sids=[]): """ Add DFBA species and compartments from fba model to model. Creates the dynamic species and respetive compartments with the necessary ports. This is used in the bounds submodel, update submodel and the and top model. :param model: :param model_fba: :return: """ objects = [] port_sids = [] ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid in ex_rids: r = model_fba.getReaction(ex_rid) sid = r.getReactant(0).getSpecies() if sid in exclude_sids: continue s = model_fba.getSpecies(sid) # exchange species to create objects.append( fac.Species(sid=sid, name=s.getName(), initialConcentration=1.0, substanceUnit=unit_amount, hasOnlySubstanceUnits=hasOnlySubstanceUnits, compartment=compartment_id) ) # port of exchange species port_sids.append(sid) fac.create_objects(model, objects) if create_port: comp.create_ports(model, idRefs=port_sids)
def create_dynamic_bounds(model_bounds, model_fba, unit_flux=None): """ Creates the dynamic bounds for the model. It is necessary to create copies of the fixed bounds from the fba model which are subsequently used in the dynamic bounds calculation. :return: """ fba_suffix = "_fba" objects = [] ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): r = model_fba.getReaction(ex_rid) r_fbc = r.getPlugin(SBML_FBC_NAME) # get compartment from species s = model_bounds.getSpecies(sid) cid = s.getCompartment() # upper bound parameter (export from FBA, i.e increase concentration) ub_id = r_fbc.getUpperFluxBound() fba_ub_id = ub_id + fba_suffix ub_value = model_fba.getParameter(ub_id).getValue() ub_formula = "{}".format(fba_ub_id) objects.extend([ Parameter(sid=fba_ub_id, value=ub_value, unit=unit_flux, constant=True, sboTerm=FLUX_BOUND_SBO), AssignmentRule(sid=ub_id, value=ub_formula, name="fba export bound ({})".format(sid)), ]) # lower bound parameter (import to FBA, i.e decrease concentration) lb_id = r_fbc.getLowerFluxBound() fba_lb_id = lb_id + fba_suffix lb_value = model_fba.getParameter(lb_id).getValue() # concentration/amount formula for import depending on available species if s.getHasOnlySubstanceUnits(): lb_formula = "max({}, -{}/dt)".format(fba_lb_id, sid) else: lb_formula = "max({}, -{}*{}/dt)".format(fba_lb_id, cid, sid) objects.extend([ # default bounds from fba Parameter(sid=fba_lb_id, value=lb_value, unit=unit_flux, constant=False, sboTerm=FLUX_BOUND_SBO), # uptake bounds (lower bound) AssignmentRule(sid=lb_id, value=lb_formula, name="dfba import bound ({})".format(sid)), ]) factory.create_objects(model_bounds, objects)
def bounds_model(sbml_file, directory, doc_fba=None): """" Submodel for dynamically calculating the flux bounds. The dynamically changing flux bounds are the input to the FBA model. """ doc = builder.template_doc_bounds(settings.MODEL_ID) model = doc.getModel() bounds_notes = notes.format(""" <h2>BOUNDS submodel</h2> <p>Submodel for dynamically calculating the flux bounds. The dynamically changing flux bounds are the input to the FBA model.</p> """) utils.set_model_info(model, notes=bounds_notes, creators=creators, units=units, main_units=main_units) # dt compartment_id = "blood" builder.create_dfba_dt(model, time_unit=UNIT_TIME, create_port=True) # compartment builder.create_dfba_compartment(model, compartment_id=compartment_id, unit_volume=UNIT_VOLUME, create_port=True) # dynamic species model_fba = doc_fba.getModel() builder.create_dfba_species(model, model_fba, compartment_id=compartment_id, unit_amount=UNIT_AMOUNT, create_port=True) # bounds builder.create_exchange_bounds(model, model_fba=model_fba, unit_flux=UNIT_FLUX, create_ports=True) # bounds fba_prefix = "fba" model_fba = doc_fba.getModel() objects = [] ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): r = model_fba.getReaction(ex_rid) # lower & upper bound parameters r_fbc = r.getPlugin(builder.SBML_FBC_NAME) lb_id = r_fbc.getLowerFluxBound() fba_lb_id = fba_prefix + lb_id lb_value = model_fba.getParameter(lb_id).getValue() objects.extend([ # default bounds from fba mc.Parameter(sid=fba_lb_id, value=lb_value, unit=UNIT_FLUX, constant=False), # uptake bounds (lower bound) mc.AssignmentRule(sid=lb_id, value="max({}, -{}*{}/dt)".format(fba_lb_id, compartment_id, sid)), ]) mc.create_objects(model, objects) sbmlio.write_sbml(doc, filepath=pjoin(directory, sbml_file), validate=True)
def create_top_replacedBy(model, model_fba): """ Creates the replacedBy Elements in the top model. :param model: :param model_fba: :return: """ ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): comp.replaced_by(model, DUMMY_REACTION_PREFIX + sid, ref_type=comp.SBASE_REF_TYPE_PORT, submodel='fba', replaced_by="{}_port".format(EXCHANGE_REACTION_PREFIX + sid))
def create_top_replacements(model, model_fba, compartment_id): """ Create all the replacements in the top model. :param model: :param model_fba: :param compartment_id: :return: """ # compartment comp.replace_elements(model, compartment_id, ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={ 'update': ['{}_port'.format(compartment_id)], 'bounds': ['{}_port'.format(compartment_id)]}) # dt comp.replace_elements(model, 'dt', ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={'bounds': ['dt_port']}) # species dependent replacements ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): # flux parameters comp.replace_elements(model, FLUX_PARAMETER_PREFIX + sid, ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={'update': ['{}_port'.format(FLUX_PARAMETER_PREFIX + sid)]}) # dynamic species comp.replace_elements(model, sid, ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={ 'bounds': ['{}_port'.format(sid)], 'update': ['{}_port'.format(sid)]}) # bounds of exchange reactions for replace_id in [ UPPER_BOUND_PREFIX + EXCHANGE_REACTION_PREFIX + sid, LOWER_BOUND_PREFIX + EXCHANGE_REACTION_PREFIX + sid]: comp.replace_elements(model, replace_id, ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={ 'bounds': ['{}_port'.format(replace_id)], 'fba': ['{}_port'.format(replace_id)]}) # replace units for unit in model.getListOfUnitDefinitions(): uid = unit.getId() comp.replace_element_in_submodels(model, uid, ref_type=comp.SBASE_REF_TYPE_UNIT, submodels=['bounds', 'fba', 'update'])
def create_top_replacedBy(model, model_fba): """ Creates the replacedBy Elements in the top model. :param model: :param model_fba: :return: """ ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): comp.replaced_by( model, DUMMY_REACTION_PREFIX + sid, ref_type=comp.SBASE_REF_TYPE_PORT, submodel='fba', replaced_by="{}_port".format(EXCHANGE_REACTION_PREFIX + sid))
def create_update_reactions(model, model_fba, formula="-{}", unit_flux=None, modifiers=None): """ Creates all update reactions with the given formula. :param model: :param model_fba: :param formula: :param unit_flux: :param modifiers: :return: """ if modifiers is None: modifiers = [] ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): create_update_parameter(model=model, sid=sid, unit_flux=unit_flux) create_update_reaction(model=model, sid=sid, modifiers=modifiers, formula=formula)
def create_dynamic_bounds(model_bounds, model_fba, unit_flux=None): """ Creates the dynamic bounds for the model. It is necessary to create copies of the fixed bounds from the fba model which are subsequently used in the dynamic bounds calculation. :return: """ fba_suffix = "_fba" objects = [] ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): r = model_fba.getReaction(ex_rid) r_fbc = r.getPlugin(SBML_FBC_NAME) # get compartment from species s = model_bounds.getSpecies(sid) cid = s.getCompartment() # upper bound parameter (export from FBA, i.e increase concentration) ub_id = r_fbc.getUpperFluxBound() fba_ub_id = ub_id + fba_suffix ub_value = model_fba.getParameter(ub_id).getValue() ub_formula = "{}".format(fba_ub_id) objects.extend([ fac.Parameter(sid=fba_ub_id, value=ub_value, unit=unit_flux, constant=True, sboTerm=FLUX_BOUND_SBO), fac.AssignmentRule(sid=ub_id, value=ub_formula, name="fba export bound ({})".format(sid)), ]) # lower bound parameter (import to FBA, i.e decrease concentration) lb_id = r_fbc.getLowerFluxBound() fba_lb_id = lb_id + fba_suffix lb_value = model_fba.getParameter(lb_id).getValue() # concentration/amount formula for import depending on available species if s.getHasOnlySubstanceUnits(): lb_formula = "max({}, -{}/dt)".format(fba_lb_id, sid) else: lb_formula = "max({}, -{}*{}/dt)".format(fba_lb_id, cid, sid) objects.extend([ # default bounds from fba fac.Parameter(sid=fba_lb_id, value=lb_value, unit=unit_flux, constant=False, sboTerm=FLUX_BOUND_SBO), # uptake bounds (lower bound) fac.AssignmentRule(sid=lb_id, value=lb_formula, name="dfba import bound ({})".format(sid)), ]) fac.create_objects(model_bounds, objects)
def create_exchange_bounds(model_bounds, model_fba, unit_flux=None, create_ports=True): """ Creates the exchange reaction flux bounds in the bounds model. :param model_bounds: the bounds model submodel :param model_fba: the fba submodel :param unit_flux: unit of fluxes :param create_ports: should ports be created. :return: """ ex_rids = utils.find_exchange_reactions(model_fba) objects = [] port_sids = [] for ex_rid, sid in ex_rids.items(): r = model_fba.getReaction(ex_rid) # lower & upper bound parameters r_fbc = r.getPlugin(SBML_FBC_NAME) lb_id = r_fbc.getLowerFluxBound() lb_value = model_fba.getParameter(lb_id).getValue() ub_id = r_fbc.getUpperFluxBound() ub_value = model_fba.getParameter(ub_id).getValue() # This creates the dynamical flux bound variables which are initialized with the # constant fba bounds objects.extend([ # for assignments Parameter(sid=lb_id, value=lb_value, unit=unit_flux, constant=False, sboTerm=FLUX_BOUND_SBO), Parameter(sid=ub_id, value=ub_value, unit=unit_flux, constant=False, sboTerm=FLUX_BOUND_SBO), ]) port_sids.extend([lb_id, ub_id]) # create bounds factory.create_objects(model_bounds, objects) # create ports if create_ports: comp.create_ports(model_bounds, idRefs=port_sids)
def create_dfba_species(model, model_fba, compartment_id, hasOnlySubstanceUnits=False, unit_amount=None, create_port=True, exclude_sids=[]): """ Add DFBA species and compartments from fba model to model. Creates the dynamic species and respetive compartments with the necessary ports. This is used in the bounds submodel, update submodel and the and top model. :param model: :param model_fba: :return: """ objects = [] ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid in ex_rids: r = model_fba.getReaction(ex_rid) sid = r.getReactant(0).getSpecies() if sid in exclude_sids: continue # exchange species s = model_fba.getSpecies(sid) objects.append( Species(sid=sid, name=s.getName(), initialConcentration=1.0, substanceUnit=unit_amount, hasOnlySubstanceUnits=hasOnlySubstanceUnits, compartment=compartment_id, port=create_port)) factory.create_objects(model, objects)
def create_dummy_reactions(model, model_fba, unit_flux=None): """ Creates the dummy reactions. This also creates the corresponding flux parameters and flux assignments. :param model: :param model_fba: :param unit_flux: :return: """ ex_rids = utils.find_exchange_reactions(model_fba) objects = [] for ex_rid, sid in ex_rids.items(): pid_flux = FLUX_PARAMETER_PREFIX + sid rid_flux = DUMMY_REACTION_PREFIX + sid objects = [ # flux parameter: flux from fba (rate of reaction) Parameter(sid=pid_flux, value=1.0, constant=True, unit=unit_flux, sboTerm=FLUX_PARAMETER_SBO), # dummy reaction (pseudoreaction) Reaction(sid=rid_flux, reversible=False, equation=" -> {}".format(DUMMY_SPECIES_ID), sboTerm=DUMMY_REACTION_SBO, formula=('0 {}'.format(unit_flux), unit_flux)), # flux assignment rule AssignmentRule(pid_flux, value=rid_flux, sboTerm=FLUX_ASSIGNMENTRULE_SBO), ] factory.create_objects(model, objects)
def create_exchange_bounds(model_bounds, model_fba, unit_flux=None, create_ports=True): """ Creates the exchange reaction flux bounds in the bounds model. :param model_bounds: the bounds model submodel :param model_fba: the fba submodel :param unit_flux: unit of fluxes :param create_ports: should ports be created. :return: """ ex_rids = utils.find_exchange_reactions(model_fba) objects = [] port_sids = [] for ex_rid, sid in ex_rids.items(): r = model_fba.getReaction(ex_rid) # lower & upper bound parameters r_fbc = r.getPlugin(SBML_FBC_NAME) lb_id = r_fbc.getLowerFluxBound() lb_value = model_fba.getParameter(lb_id).getValue() ub_id = r_fbc.getUpperFluxBound() ub_value = model_fba.getParameter(ub_id).getValue() # This creates the dynamical flux bound variables which are initialized with the # constant fba bounds objects.extend([ # for assignments fac.Parameter(sid=lb_id, value=lb_value, unit=unit_flux, constant=False, sboTerm=FLUX_BOUND_SBO), fac.Parameter(sid=ub_id, value=ub_value, unit=unit_flux, constant=False, sboTerm=FLUX_BOUND_SBO), ]) port_sids.extend([lb_id, ub_id]) # create bounds fac.create_objects(model_bounds, objects) # create ports if create_ports: comp.create_ports(model_bounds, idRefs=port_sids)
def bounds_model(sbml_file, directory, doc_fba=None, annotations=None): """" Submodel for dynamically calculating the flux bounds. The dynamically changing flux bounds are the input to the FBA model. """ # TODO: the bounds model should be created based on the FBA model (i.e. use the exchange reactions # to create the bounds info. bounds_notes = notes.format(""" <h2>BOUNDS submodel</h2> <p>Submodel for dynamically calculating the flux bounds. The dynamically changing flux bounds are the input to the FBA model.</p> """) doc = builder.template_doc_bounds(settings.MODEL_ID) model = doc.getModel() utils.set_model_info(model, notes=bounds_notes, creators=creators, units=units, main_units=main_units) # dt compartment_id = "bioreactor" builder.create_dfba_dt(model, time_unit=UNIT_TIME, create_port=True) # compartment builder.create_dfba_compartment(model, compartment_id=compartment_id, unit_volume=UNIT_VOLUME, create_port=True) # dynamic species model_fba = doc_fba.getModel() builder.create_dfba_species(model, model_fba, compartment_id=compartment_id, unit_amount=UNIT_AMOUNT, create_port=True) # bounds builder.create_exchange_bounds(model, model_fba=model_fba, unit_flux=UNIT_FLUX, create_ports=True) # bounds fba_infix = "fba_" model_fba = doc_fba.getModel() objects = [] ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): r = model_fba.getReaction(ex_rid) # lower & upper bound parameters r_fbc = r.getPlugin(builder.SBML_FBC_NAME) lb_id = r_fbc.getLowerFluxBound() fba_lb_id = builder.LOWER_BOUND_PREFIX + fba_infix + ex_rid lb_value = model_fba.getParameter(lb_id).getValue() objects.extend([ # default bounds from fba Parameter(sid=fba_lb_id, value=lb_value, unit=UNIT_FLUX, constant=False), ]) factory.create_objects(model, objects) objects = [ # kinetic lower bounds Parameter(sid="lb_kin_EX_Glcxt", value=builder.LOWER_BOUND_DEFAULT, unit=UNIT_FLUX, constant=False, sboTerm="SBO:0000612"), Parameter(sid="lb_kin_EX_O2", value=builder.LOWER_BOUND_DEFAULT, unit=UNIT_FLUX, constant=False, sboTerm="SBO:0000612"), # parameters for kinetic bounds Parameter(sid='Vmax_EX_O2', value=15, unit=UNIT_FLUX, constant=True), Parameter(sid='Vmax_EX_Glcxt', value=10, unit=UNIT_FLUX, constant=True), Parameter(sid='Km_EX_Glcxt', value=0.015, unit=UNIT_CONCENTRATION, name="Km_vGlcxt", constant=True), # kinetic bounds (unintuitive direction due to the identical concentrations in bioreactor and model) AssignmentRule(sid="lb_kin_EX_Glcxt", value="-Vmax_EX_Glcxt * Glcxt/(Km_EX_Glcxt + Glcxt)"), AssignmentRule(sid="lb_kin_EX_O2", value="-Vmax_EX_O2"), # exchange reaction bounds # uptake bounds (lower bound) # TODO: FIXME the X hack # the bounds for the fba model have to be in mmol/h/gdw AssignmentRule( sid="lb_EX_Ac", value="max(lb_fba_EX_Ac, -Ac/X/1 l_per_mmol*bioreactor/dt)"), AssignmentRule( sid="lb_EX_X", value="max(lb_fba_EX_X, -X/X/1 l_per_mmol*bioreactor/dt)"), AssignmentRule( sid="lb_EX_Glcxt", value="max(lb_kin_EX_Glcxt, -Glcxt/X/1 l_per_mmol*bioreactor/dt)"), AssignmentRule( sid="lb_EX_O2", value="max(lb_kin_EX_O2, -O2/X/1 l_per_mmol*bioreactor/dt)"), ] factory.create_objects(model, objects) if annotations: annotator.annotate_sbml_doc(doc, annotations) sbmlio.write_sbml(doc, filepath=pjoin(directory, sbml_file), validate=True)
def update_exchange_reactions(model, flux_unit): """ Updates existing exchange reaction in FBA model. Sets all the necessary information and checks that correct. This is mainly used to prepare the exchange reactions of metabolites. :param flux_unit: :param model: :return: """ # mapping of bounds to reactions bounds_dict = dict() ex_rids = utils.find_exchange_reactions(model) for ex_rid in ex_rids: r = model.getReaction(ex_rid) # make reversible if not r.getReversible(): r.setReversible(True) logging.info("Exchange reaction set reversible: {}".format(r.getId())) # fix ids for exchange reactions sref = r.getReactant(0) sid = sref.getSpecies() rid = r.getId() if rid != EXCHANGE_REACTION_PREFIX + sid: r.setId(EXCHANGE_REACTION_PREFIX + sid) logging.warning("Exchange reaction fixd id: {} -> {}".format(rid, EXCHANGE_REACTION_PREFIX + sid)) # new lookup necessary, due to possible changed ids ex_rids = utils.find_exchange_reactions(model) for ex_rid in ex_rids: r = model.getReaction(ex_rid) fbc_r = r.getPlugin(SBML_FBC_NAME) # store bounds in dictionary for value lookup for f_bound in ["getLowerFluxBound", "getUpperFluxBound"]: bound_id = getattr(fbc_r, f_bound).__call__() bound = model.getParameter(bound_id) bounds_dict[bound_id] = bound.getValue() # create unique bounds for exchange reactions for ex_rid in ex_rids: r = model.getReaction(ex_rid) fbc_r = r.getPlugin(SBML_FBC_NAME) lb_value = model.getParameter(fbc_r.getLowerFluxBound()).getValue() ub_value = model.getParameter(fbc_r.getUpperFluxBound()).getValue() lb_id = LOWER_BOUND_PREFIX + ex_rid ub_id = UPPER_BOUND_PREFIX + ex_rid parameters = [ fac.Parameter(sid=lb_id, value=lb_value, unit=flux_unit, constant=True, sboTerm=FLUX_BOUND_SBO), fac.Parameter(sid=ub_id, value=ub_value, unit=flux_unit, constant=True, sboTerm=FLUX_BOUND_SBO), ] fac.create_objects(model, parameters) # set bounds fbc.set_flux_bounds(r, lb=lb_id, ub=ub_id) # create ports for bounds and reaction comp.create_ports(model, portType=comp.PORT_TYPE_PORT, idRefs=[ex_rid, lb_id, ub_id]) # check all the exchange reactions for ex_rid in ex_rids: check_exchange_reaction(model, ex_rid)
def update_exchange_reactions(model, flux_unit): """ Updates existing exchange reaction in FBA model. Sets all the necessary information and checks that correct. This is mainly used to prepare the exchange reactions of metabolites. :param flux_unit: :param model: :return: """ # mapping of bounds to reactions bounds_dict = dict() ex_rids = utils.find_exchange_reactions(model) for ex_rid in ex_rids: r = model.getReaction(ex_rid) # make reversible if not r.getReversible(): r.setReversible(True) logging.info("Exchange reaction set reversible: {}".format( r.getId())) # fix ids for exchange reactions sref = r.getReactant(0) sid = sref.getSpecies() rid = r.getId() if rid != EXCHANGE_REACTION_PREFIX + sid: r.setId(EXCHANGE_REACTION_PREFIX + sid) logging.warning("Exchange reaction fixd id: {} -> {}".format( rid, EXCHANGE_REACTION_PREFIX + sid)) # new lookup necessary, due to possible changed ids ex_rids = utils.find_exchange_reactions(model) for ex_rid in ex_rids: r = model.getReaction(ex_rid) fbc_r = r.getPlugin(SBML_FBC_NAME) # store bounds in dictionary for value lookup for f_bound in ["getLowerFluxBound", "getUpperFluxBound"]: bound_id = getattr(fbc_r, f_bound).__call__() bound = model.getParameter(bound_id) bounds_dict[bound_id] = bound.getValue() # create unique bounds for exchange reactions for ex_rid in ex_rids: r = model.getReaction(ex_rid) fbc_r = r.getPlugin(SBML_FBC_NAME) lb_value = model.getParameter(fbc_r.getLowerFluxBound()).getValue() ub_value = model.getParameter(fbc_r.getUpperFluxBound()).getValue() lb_id = LOWER_BOUND_PREFIX + ex_rid ub_id = UPPER_BOUND_PREFIX + ex_rid parameters = [ Parameter(sid=lb_id, value=lb_value, unit=flux_unit, constant=True, sboTerm=FLUX_BOUND_SBO), Parameter(sid=ub_id, value=ub_value, unit=flux_unit, constant=True, sboTerm=FLUX_BOUND_SBO), ] factory.create_objects(model, parameters) # set bounds fbc.set_flux_bounds(r, lb=lb_id, ub=ub_id) # create ports for bounds and reaction comp.create_ports(model, portType=comp.PORT_TYPE_PORT, idRefs=[ex_rid, lb_id, ub_id]) # check all the exchange reactions for ex_rid in ex_rids: check_exchange_reaction(model, ex_rid)
def create_top_replacements(model, model_fba, compartment_id): """ Create all the replacements in the top model. :param model: :param model_fba: :param compartment_id: :return: """ # compartment comp.replace_elements(model, compartment_id, ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={ 'update': ['{}_port'.format(compartment_id)], 'bounds': ['{}_port'.format(compartment_id)] }) # dt comp.replace_elements(model, 'dt', ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={'bounds': ['dt_port']}) # species dependent replacements ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): # flux parameters comp.replace_elements( model, FLUX_PARAMETER_PREFIX + sid, ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={ 'update': ['{}_port'.format(FLUX_PARAMETER_PREFIX + sid)] }) # dynamic species comp.replace_elements(model, sid, ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={ 'bounds': ['{}_port'.format(sid)], 'update': ['{}_port'.format(sid)] }) # bounds of exchange reactions for replace_id in [ UPPER_BOUND_PREFIX + EXCHANGE_REACTION_PREFIX + sid, LOWER_BOUND_PREFIX + EXCHANGE_REACTION_PREFIX + sid ]: comp.replace_elements(model, replace_id, ref_type=comp.SBASE_REF_TYPE_PORT, replaced_elements={ 'bounds': ['{}_port'.format(replace_id)], 'fba': ['{}_port'.format(replace_id)] }) # replace units for unit in model.getListOfUnitDefinitions(): uid = unit.getId() comp.replace_element_in_submodels( model, uid, ref_type=comp.SBASE_REF_TYPE_UNIT, submodels=['bounds', 'fba', 'update'])
def bounds_model(sbml_file, directory, doc_fba=None, annotations=None): """" Submodel for dynamically calculating the flux bounds. The dynamically changing flux bounds are the input to the FBA model. """ # TODO: the bounds model should be created based on the FBA model (i.e. use the exchange reactions # to create the bounds info. bounds_notes = notes.format(""" <h2>BOUNDS submodel</h2> <p>Submodel for dynamically calculating the flux bounds. The dynamically changing flux bounds are the input to the FBA model.</p> """) doc = builder.template_doc_bounds(settings.MODEL_ID) model = doc.getModel() utils.set_model_info(model, notes=bounds_notes, creators=creators, units=units, main_units=main_units) # dt compartment_id = "bioreactor" builder.create_dfba_dt(model, time_unit=UNIT_TIME, create_port=True) # compartment builder.create_dfba_compartment(model, compartment_id=compartment_id, unit_volume=UNIT_VOLUME, create_port=True) # dynamic species model_fba = doc_fba.getModel() builder.create_dfba_species(model, model_fba, compartment_id=compartment_id, unit_amount=UNIT_AMOUNT, create_port=True) # bounds builder.create_exchange_bounds(model, model_fba=model_fba, unit_flux=UNIT_FLUX, create_ports=True) # bounds fba_infix = "fba_" model_fba = doc_fba.getModel() objects = [] ex_rids = utils.find_exchange_reactions(model_fba) for ex_rid, sid in ex_rids.items(): r = model_fba.getReaction(ex_rid) # lower & upper bound parameters r_fbc = r.getPlugin(builder.SBML_FBC_NAME) lb_id = r_fbc.getLowerFluxBound() fba_lb_id = builder.LOWER_BOUND_PREFIX + fba_infix + ex_rid lb_value = model_fba.getParameter(lb_id).getValue() objects.extend([ # default bounds from fba mc.Parameter(sid=fba_lb_id, value=lb_value, unit=UNIT_FLUX, constant=False), ]) mc.create_objects(model, objects) objects = [ # kinetic lower bounds mc.Parameter(sid="lb_kin_EX_Glcxt", value=builder.LOWER_BOUND_DEFAULT, unit=UNIT_FLUX, constant=False, sboTerm="SBO:0000612"), mc.Parameter(sid="lb_kin_EX_O2", value=builder.LOWER_BOUND_DEFAULT, unit=UNIT_FLUX, constant=False, sboTerm="SBO:0000612"), # parameters for kinetic bounds mc.Parameter(sid='Vmax_EX_O2', value=15, unit=UNIT_FLUX, constant=True), mc.Parameter(sid='Vmax_EX_Glcxt', value=10, unit=UNIT_FLUX, constant=True), mc.Parameter(sid='Km_EX_Glcxt', value=0.015, unit=UNIT_CONCENTRATION, name="Km_vGlcxt", constant=True), # kinetic bounds (unintuitive direction due to the identical concentrations in bioreactor and model) mc.AssignmentRule(sid="lb_kin_EX_Glcxt", value="-Vmax_EX_Glcxt * Glcxt/(Km_EX_Glcxt + Glcxt)"), mc.AssignmentRule(sid="lb_kin_EX_O2", value="-Vmax_EX_O2"), # exchange reaction bounds # uptake bounds (lower bound) # TODO: FIXME the X hack # the bounds for the fba model have to be in mmol/h/gdw mc.AssignmentRule(sid="lb_EX_Ac", value="max(lb_fba_EX_Ac, -Ac/X/1 l_per_mmol*bioreactor/dt)"), mc.AssignmentRule(sid="lb_EX_X", value="max(lb_fba_EX_X, -X/X/1 l_per_mmol*bioreactor/dt)"), mc.AssignmentRule(sid="lb_EX_Glcxt", value="max(lb_kin_EX_Glcxt, -Glcxt/X/1 l_per_mmol*bioreactor/dt)"), mc.AssignmentRule(sid="lb_EX_O2", value="max(lb_kin_EX_O2, -O2/X/1 l_per_mmol*bioreactor/dt)"), ] mc.create_objects(model, objects) if annotations: annotation.annotate_sbml_doc(doc, annotations) sbmlio.write_sbml(doc, filepath=pjoin(directory, sbml_file), validate=True)