def run_perturbation_MOMA(self, boundaries, objectives={}): print("running MOMA...") print(boundaries) print(objectives) moma_model = self.model.copy() """ if self.pFBA: print("running pFBA") old_sol = cobra.flux_analysis.parsimonious.optimize_minimal_flux(moma_model) else: print("running FBA") old_sol = moma_model.optimize() """ reaction_position = self.reaction_position # make perturbations for (R, boundary_type, boundary_value) in boundaries: i = reaction_position[R] if boundary_type == 'l': moma_model.reactions[i].lower_bound = boundary_value elif boundary_type == 'u': moma_model.reactions[i].upper_bound = boundary_value if objectives: #model.objective = model.reactions[reaction_position[objective["objective_id"]]] # TODO moma_model.objective = { moma_model.reactions[self.reaction_position[r_id]]: int(objectives[r_id]) for r_id in objectives } #print(model.objective) self.perturbed_sol = moma.moma(self.model, moma_model) self.perturbed_fluxes = self.perturbed_sol.x_dict self.perturbed_fluxes = { x: self.perturbed_fluxes[x] for x in self.perturbed_fluxes if x in self.fluxes }
def assess_medium_component_essentiality(cobra_model, the_components=None, the_medium=None, medium_compartment='e', solver='glpk', the_condition=None, method='fba'): """Determines which components in an in silico medium are essential for growth in the context of the remaining components. cobra_model: A Model object. the_components: None or a list of external boundary reactions that will be sequentially disabled. the_medium: Is None, a string, or a dictionary. If a string then the initialize_growth_medium function expects that the_model has an attribute dictionary called media_compositions, which is a dictionary of dictionaries for various medium compositions. Where a medium composition is a dictionary of external boundary reaction ids for the medium components and the external boundary fluxes for each medium component. medium_compartment: the compartment in which the boundary reactions supplying the medium components exist NOTE: that these fluxes must be negative because the convention is backwards means something is feed into the system. solver: 'glpk', 'gurobi', or 'cplex' returns: essentiality_dict: A dictionary providing the maximum growth rate accessible when the respective component is removed from the medium. """ if method.lower() == 'moma': wt_model = cobra_model.copy() cobra_model = cobra_model.copy() if isinstance(the_medium, str): try: the_medium = cobra_model.media_compositions[the_medium] except: raise Exception( the_medium + " is not in cobra_model.media_compositions") if the_medium is not None: initialize_growth_medium(cobra_model, the_medium, medium_compartment) if the_components is None: the_components = the_medium.keys() if not the_components: raise Exception("You need to specify the_components or the_medium") essentiality_dict = {} for the_component in the_components: the_reaction = cobra_model.reactions.get_by_id(the_component) original_lower_bound = float(the_reaction.lower_bound) the_reaction.lower_bound = 0. if method.lower() == 'fba': cobra_model.optimize(solver=solver) objective_value = cobra_model.solution.f elif method.lower() == 'moma': objective_value = moma(wt_model, cobra_model, solver=solver)[ 'objective_value'] essentiality_dict[the_component] = objective_value the_reaction.lower_bound = original_lower_bound return(essentiality_dict)
def assess_medium_component_essentiality( cobra_model, the_components=None, the_medium=None, solver="glpk", the_problem="return", the_condition=None, method="fba", ): """Deterimes which components in an in silico medium are essential for growth in the context of the remaining components. cobra_model: A Model object. the_components: None or a list of exchange reactions that will be sequentially disabled. the_medium: Is None, a string, or a dictionary. If a string then the initialize_growth_medium function expects that the_model has an attribute dictionary called media_compositions, which is a dictionary of dictionaries for various medium compositions. Where a medium composition is a dictionary of exchange reaction ids for the medium components and the exchange fluxes for each medium component; note that these fluxes must be negative because they are being exchanged into the system. solver: 'glpk', 'gurobi', or 'cplex' the_problem: Is None, 'return', or an LP model object for the solver. the_condition: None or a String that provides a description of the medium simulation returns: essentiality_dict: A dictionary providing the minimum lower bounds for each component of the growth medium. """ warn("assess_medium_component_essentiality needs to be updated to " + "deal with new style reactions") from cobra.core.ArrayBasedModel import ArrayBasedModel if method.lower() == "moma": wt_model = ArrayBasedModel(cobra_model.copy()) if isinstance(cobra_model, tuple): if len(cobra_model) == 3: the_condition = cobra_model[2] the_components = cobra_model[1] cobra_model = cobra_model[0] cobra_model = ArrayBasedModel(cobra_model.copy()) if not the_components: if the_medium: if hasattr(the_medium, "keys") or ( hasattr(cobra_model, "media_compositions") and the_medium in cobra_model.media_compositions ): initialize_growth_medium(cobra_model, the_medium) the_components = cobra_model.media_compositions[the_medium] else: raise Exception("%s is not a dict and not in the model's media list" % the_medium) else: raise Exception("You need to specify the_components or the_medium") essentiality_dict = {} for the_component in the_components: component_index = cobra_model.reactions.index(the_component) tmp_lb = float(cobra_model.lower_bounds[component_index]) cobra_model.reactions[component_index].lower_bound = cobra_model.lower_bounds[component_index] = 0 if method.lower() == "fba": cobra_model.optimize(solver=solver, the_problem=the_problem) objective_value = cobra_model.solution.f elif method.lower() == "moma": objective_value = moma(wt_model, cobra_model, solver=solver)["objective_value"] essentiality_dict[the_component] = objective_value cobra_model.reactions[component_index].lower_bound = cobra_model.lower_bounds[component_index] = tmp_lb if the_condition: essentiality_dict["the_condition"] = the_condition return essentiality_dict
def MOMA2mutant(self, mutant_model, objective_sense='maximize', solver=None, tolerance_optimality=1e-8, tolerance_feasibility=1e-8,minimize_norm=False, the_problem='return', lp_method=0,combined_model=None, norm_type='euclidean'): moma.moma(self, mutant_model=mutant_model, objective_sense=objective_sense, solver=solver, tolerance_optimality=tolerance_optimality, tolerance_feasibility=tolerance_feasibility,minimize_norm=minimize_norm, the_problem=the_problem, lp_method=lp_method,combined_model=combined_model, norm_type=norm_type)