def run(self, max_knockouts=5, biomass=None, target=None, max_results=1, *args, **kwargs): """ Perform the OptKnock simulation Parameters ---------- target: str, Metabolite or Reaction The design target biomass: str, Metabolite or Reaction The biomass definition in the model max_knockouts: int Max number of knockouts allowed max_results: int Max number of different designs to return if found Returns ------- OptKnockResult """ # TODO: why not required arguments? if biomass is None or target is None: raise ValueError('missing biomass and/or target reaction') target = get_reaction_for(self._model, target, add=False) biomass = get_reaction_for(self._model, biomass, add=False) knockout_list = [] fluxes_list = [] production_list = [] biomass_list = [] loader_id = ui.loading() with self._model: self._model.objective = target.id self._number_of_knockouts_constraint.lb = self._number_of_knockouts_constraint.ub - max_knockouts count = 0 while count < max_results: try: solution = self._model.optimize(raise_error=True) except OptimizationError as e: logger.debug( "Problem could not be solved. Terminating and returning " + str(count) + " solutions") logger.debug(str(e)) break knockouts = tuple(reaction for y, reaction in self._y_vars.items() if round(y.primal, 3) == 0) assert len(knockouts) <= max_knockouts if self.reaction_groups: combinations = decompose_reaction_groups( self.reaction_groups, knockouts) for kos in combinations: knockout_list.append({r.id for r in kos}) fluxes_list.append(solution.fluxes) production_list.append(solution.f) biomass_list.append(solution.fluxes[biomass.id]) else: knockout_list.append({r.id for r in knockouts}) fluxes_list.append(solution.fluxes) production_list.append(solution.f) biomass_list.append(solution.fluxes[biomass.id]) # Add an integer cut y_vars_to_cut = [ y for y in self._y_vars if round(y.primal, 3) == 0 ] integer_cut = self._model.solver.interface.Constraint( Add(*y_vars_to_cut), lb=1, name="integer_cut_" + str(count)) if len(knockouts) < max_knockouts: self._number_of_knockouts_constraint.lb = self._number_of_knockouts_constraint.ub - len( knockouts) self._model.add_cons_vars(integer_cut) count += 1 ui.stop_loader(loader_id) return OptKnockResult(self._original_model, knockout_list, fluxes_list, production_list, biomass_list, target.id, biomass)
def run(self, max_knockouts=5, biomass=None, target=None, max_results=1, *args, **kwargs): """ Perform the OptKnock simulation Parameters ---------- target: str, Metabolite or Reaction The design target biomass: str, Metabolite or Reaction The biomass definition in the model max_knockouts: int Max number of knockouts allowed max_results: int Max number of different designs to return if found Returns ------- OptKnockResult """ target = self._model.reaction_for(target, add=False) biomass = self._model.reaction_for(biomass, add=False) knockout_list = [] fluxes_list = [] production_list = [] biomass_list = [] loader_id = ui.loading() with TimeMachine() as tm: self._model.objective = target.id self._number_of_knockouts_constraint.lb = self._number_of_knockouts_constraint.ub - max_knockouts count = 0 while count < max_results: try: solution = self._model.solve() except SolveError as e: logger.debug("Problem could not be solved. Terminating and returning " + str(count) + " solutions") logger.debug(str(e)) break knockouts = set(reaction.id for y, reaction in self._y_vars.items() if round(y.primal, 3) == 0) assert len(knockouts) <= max_knockouts knockout_list.append(knockouts) fluxes_list.append(solution.fluxes) production_list.append(solution.f) biomass_list.append(solution.fluxes[biomass.id]) # Add an integer cut y_vars_to_cut = [y for y in self._y_vars if round(y.primal, 3) == 0] integer_cut = self._model.solver.interface.Constraint(Add(*y_vars_to_cut), lb=1, name="integer_cut_" + str(count)) if len(knockouts) < max_knockouts: self._number_of_knockouts_constraint.lb = self._number_of_knockouts_constraint.ub - len(knockouts) tm(do=partial(self._model.solver.add, integer_cut), undo=partial(self._model.solver.remove, integer_cut)) count += 1 ui.stop_loader(loader_id) return OptKnockResult(self._original_model, knockout_list, fluxes_list, production_list, biomass_list, target.id, biomass)
def run(self, max_knockouts=5, biomass=None, target=None, max_results=1, *args, **kwargs): """ Perform the OptKnock simulation Parameters ---------- target: str, Metabolite or Reaction The design target biomass: str, Metabolite or Reaction The biomass definition in the model max_knockouts: int Max number of knockouts allowed max_results: int Max number of different designs to return if found Returns ------- OptKnockResult """ target = self._model._reaction_for(target, add=False) biomass = self._model._reaction_for(biomass, add=False) knockout_list = [] fluxes_list = [] production_list = [] biomass_list = [] loader_id = ui.loading() with TimeMachine() as tm: self._model.objective = target.id self._number_of_knockouts_constraint.lb = self._number_of_knockouts_constraint.ub - max_knockouts count = 0 while count < max_results: try: solution = self._model.solve() except SolveError as e: logger.debug("Problem could not be solved. Terminating and returning " + str(count) + " solutions") logger.debug(str(e)) break knockouts = set(reaction.id for y, reaction in self._y_vars.items() if round(y.primal, 3) == 0) assert len(knockouts) <= max_knockouts knockout_list.append(knockouts) fluxes_list.append(solution.fluxes) production_list.append(solution.f) biomass_list.append(solution.fluxes[biomass.id]) # Add an integer cut y_vars_to_cut = [y for y in self._y_vars if round(y.primal, 3) == 0] integer_cut = self._model.solver.interface.Constraint(Add(*y_vars_to_cut), lb=1, name="integer_cut_" + str(count)) if len(knockouts) < max_knockouts: self._number_of_knockouts_constraint.lb = self._number_of_knockouts_constraint.ub - len(knockouts) tm(do=partial(self._model.solver.add, integer_cut), undo=partial(self._model.solver.remove, integer_cut)) count += 1 ui.stop_loader(loader_id) return OptKnockResult(self._original_model, knockout_list, fluxes_list, production_list, biomass_list, target.id, biomass)