def process_strain_designs(self, strain_designs, model=None, pathway=None, aerobic=None, **kwargs): model = model.copy() assert isinstance(pathway, StrainDesign) assert isinstance(pathway, PathwayResult) final_strain_designs = [] fitness = [] yields = [] biomass = [] target_flux = [] pyield = product_yield(pathway.product, model.carbon_source) bpcy = biomass_product_coupled_min_yield(model.biomass, pathway.product, model.carbon_source) for strain_design in strain_designs: assert isinstance(strain_design, StrainDesign) _fitness, _yield, _target_flux, _biomass = self.evaluate_design( model, strain_design, pathway, aerobic, bpcy, pyield) fitness.append(_fitness) yields.append(_yield) final_strain_designs.append(strain_design) biomass.append(_biomass) target_flux.append(_target_flux) return final_strain_designs, fitness, yields, target_flux, biomass
def test_biomass_product_coupled_min_yield(self): biomass = "Biomass_Ecoli_core_N_LPAREN_w_FSLASH_GAM_RPAREN__Nmet2" product = "EX_ac_LPAREN_e_RPAREN_" substrate = "EX_glc_LPAREN_e_RPAREN_" solution = self._MockupSolution() solution.set_primal(biomass, 0.263136) solution.set_primal(product, 16.000731) solution.set_primal(substrate, -10) of = biomass_product_coupled_min_yield( biomass, product, substrate) self.assertEqual(of.name, "bpcy = (%s * min(%s)) / %s" % (biomass, product, substrate)) fitness = of(TEST_MODEL, solution, [['ATPS4r', 'CO2t', 'GLUDy', 'PPS', 'PYK'], ['ATPS4r', 'CO2t', 'GLUDy', 'PPS', 'PYK']]) self.assertAlmostEqual(0.414851, fitness, places=5)
def test_biomass_product_coupled_min_yield(self): biomass = "Biomass_Ecoli_core_N_LPAREN_w_FSLASH_GAM_RPAREN__Nmet2" product = "EX_ac_LPAREN_e_RPAREN_" substrate = "EX_glc_LPAREN_e_RPAREN_" solution = self._MockupSolution() solution.set_primal(biomass, 0.263136) solution.set_primal(product, 16.000731) solution.set_primal(substrate, -10) of = biomass_product_coupled_min_yield( biomass, product, substrate) self.assertEqual(of.name, "bpcy = (%s * min(%s)) / %s" % (biomass, product, substrate)) reactions = [TEST_MODEL.reactions.get_by_id(r) for r in ['ATPS4r', 'CO2t', 'GLUDy', 'PPS', 'PYK']] fitness = of(TEST_MODEL, solution, reactions) self.assertAlmostEqual(0.414851, fitness, places=5)
def process_strain_designs(self, strain_designs, model=None, pathway=None, aerobic=None, **kwargs): model = model.copy() assert isinstance(pathway, StrainDesign) assert isinstance(pathway, PathwayResult) final_strain_designs = [] fitness = [] yields = [] biomass = [] target_flux = [] pyield = product_yield(pathway.product, model.carbon_source) bpcy = biomass_product_coupled_min_yield(model.biomass, pathway.product, model.carbon_source) for strain_design in strain_designs: assert isinstance(strain_design, StrainDesign) _fitness, _yield, _target_flux, _biomass = self.evaluate_design(model, strain_design, pathway, aerobic, bpcy, pyield) fitness.append(_fitness) yields.append(_yield) final_strain_designs.append(strain_design) biomass.append(_biomass) target_flux.append(_target_flux) return final_strain_designs, fitness, yields, target_flux, biomass
def run(self, target=None, biomass=None, substrate=None, max_knockouts=5, variable_size=True, simulation_method=fba, growth_coupled=False, max_evaluations=20000, population_size=200, max_results=50, use_nullspace_simplification=True, seed=None, **kwargs): """ Parameters ---------- target : str, Metabolite or Reaction The design target biomass : str, Metabolite or Reaction The biomass definition in the model substrate : str, Metabolite or Reaction The main carbon source max_knockouts : int Max number of knockouts allowed variable_size : bool If true, all candidates have the same size. Otherwise the candidate size can be from 1 to max_knockouts. simulation_method : function Any method from cameo.flux_analysis.simulation or equivalent growth_coupled : bool If true will use the minimum flux rate to compute the fitness max_evaluations : int Number of evaluations before stop population_size : int Number of individuals in each generation max_results : int Max number of different designs to return if found. kwargs : dict Arguments for the simulation method. seed : int A seed for random. use_nullspace_simplification : Boolean (default True) Use a basis for the nullspace to find groups of reactions whose fluxes are multiples of each other and dead end reactions. From each of these groups only 1 reaction will be included as a possible knockout. Returns ------- OptGeneResult """ target = get_reaction_for(self._model, target) biomass = get_reaction_for(self._model, biomass) substrate = get_reaction_for(self._model, substrate) if growth_coupled: objective_function = biomass_product_coupled_min_yield( biomass, target, substrate) else: objective_function = biomass_product_coupled_yield( biomass, target, substrate) if self.manipulation_type == "genes": optimization_algorithm = GeneKnockoutOptimization( model=self._model, heuristic_method=self._algorithm, essential_genes=self._essential_genes, plot=self.plot, objective_function=objective_function, use_nullspace_simplification=use_nullspace_simplification) elif self.manipulation_type == "reactions": optimization_algorithm = ReactionKnockoutOptimization( model=self._model, heuristic_method=self._algorithm, essential_reactions=self._essential_reactions, plot=self.plot, objective_function=objective_function, use_nullspace_simplification=use_nullspace_simplification) else: raise ValueError("Invalid manipulation type %s" % self.manipulation_type) optimization_algorithm.simulation_kwargs = kwargs optimization_algorithm.simulation_method = simulation_method optimization_algorithm.archiver = ProductionStrainArchive() result = optimization_algorithm.run(max_evaluations=max_evaluations, pop_size=population_size, max_size=max_knockouts, variable_size=variable_size, maximize=True, max_archive_size=max_results, seed=seed, **kwargs) kwargs.update(optimization_algorithm.simulation_kwargs) return OptGeneResult(self._model, result, objective_function, simulation_method, self.manipulation_type, biomass, target, substrate, kwargs)
def run(self, target=None, biomass=None, substrate=None, max_swaps=5, variable_size=True, simulation_method=fba, growth_coupled=False, max_evaluations=20000, population_size=200, time_machine=None, max_results=50, seed=None, **kwargs): """ Parameters ---------- target : str, Metabolite or Reaction The design target. biomass : str, Metabolite or Reaction The biomass definition in the model. substrate : str, Metabolite or Reaction The main carbon source. max_swaps : int Max number of swaps allowed. variable_size : bool If true, all candidates have the same size. Otherwise the candidate size can be from 1 to max_knockouts. simulation_method : function Any method from cameo.flux_analysis.simulation or equivalent. growth_coupled : bool If true will use the minimum flux rate to compute the fitness. max_evaluations : int Number of evaluations before stop. population_size : int Number of individuals in each generation. time_machine : TimeMachine See TimeMachine. max_results : int Max number of different designs to return if found. kwargs : dict Arguments for the simulation method. seed : int A seed for random. Returns ------- HeuristicOptSwapResult """ target = get_reaction_for(self._model, target) biomass = get_reaction_for(self._model, biomass) substrate = get_reaction_for(self._model, substrate) if growth_coupled: objective_function = biomass_product_coupled_min_yield( biomass, target, substrate) else: objective_function = biomass_product_coupled_yield( biomass, target, substrate) optimization_algorithm = CofactorSwapOptimization( model=self._model, cofactor_id_swaps=self._cofactor_id_swaps, skip_reactions=self._skip_reactions, objective_function=objective_function) optimization_algorithm.simulation_kwargs = kwargs optimization_algorithm.simulation_method = simulation_method optimization_algorithm.archiver = ProductionStrainArchive() result = optimization_algorithm.run(max_evaluations=max_evaluations, pop_size=population_size, max_size=max_swaps, variable_size=variable_size, maximize=True, max_archive_size=max_results, seed=seed, **kwargs) kwargs.update(optimization_algorithm.simulation_kwargs) return HeuristicOptSwapResult(self._model, result, self._swap_pairs, objective_function, simulation_method, biomass, target, substrate, kwargs)
def evaluate_opt_gene(designs, pathway, model, method): if designs is None: return [] logger.info(f"Evaluating {len(designs)} OptGene designs.") pyield = product_yield(pathway.product, model.carbon_source) bpcy = biomass_product_coupled_min_yield(model.biomass, pathway.product, model.carbon_source) results = [] with model: pathway.apply(model) for design_result in designs: with model: design_result.apply(model) try: model.objective = model.biomass solution = model.optimize() p_yield = pyield(model, solution, pathway.product) bpc_yield = bpcy(model, solution, pathway.product) target_flux = solution[pathway.product.id] biomass = solution[model.biomass] except (OptimizationError, ZeroDivisionError): p_yield = None bpc_yield = None target_flux = None biomass = None else: if isnan(p_yield): p_yield = None if isnan(bpc_yield): bpc_yield = None if isnan(target_flux): target_flux = None if isnan(biomass): biomass = None knockouts = { g for g in design_result.targets if isinstance(g, cameo.core.target.GeneKnockoutTarget) } gene_targets = {} for target in knockouts: gene_id = target.id gene = model.genes.get_by_id(gene_id) gene_targets[gene_id] = [] for reaction_target in gene.reactions: rxn_id = reaction_target.id rxn = model.reactions.get_by_id(rxn_id) gene_targets[gene_id].append({ "name": gene.name, "reaction_id": rxn_id, "reaction_name": rxn.name, "subsystem": rxn.subsystem, "gpr": rxn.gene_reaction_rule, "definition_of_stoichiometry": rxn.build_reaction_string(True), }) results.append({ "id": str(uuid4()), "knockouts": list(knockouts), "heterologous_reactions": pathway.reactions, "synthetic_reactions": find_synthetic_reactions(pathway), "fitness": bpc_yield, "yield": p_yield, "product": target_flux, "biomass": biomass, "method": method, "targets": gene_targets, }) return results
def run(self, target=None, biomass=None, substrate=None, max_knockouts=5, variable_size=True, simulation_method=fba, growth_coupled=False, max_evaluations=20000, population_size=200, max_results=50, use_nullspace_simplification=True, seed=None, **kwargs): """ Parameters ---------- target : str, Metabolite or Reaction The design target biomass : str, Metabolite or Reaction The biomass definition in the model substrate : str, Metabolite or Reaction The main carbon source max_knockouts : int Max number of knockouts allowed variable_size : bool If true, all candidates have the same size. Otherwise the candidate size can be from 1 to max_knockouts. simulation_method : function Any method from cameo.flux_analysis.simulation or equivalent growth_coupled : bool If true will use the minimum flux rate to compute the fitness max_evaluations : int Number of evaluations before stop population_size : int Number of individuals in each generation max_results : int Max number of different designs to return if found. kwargs : dict Arguments for the simulation method. seed : int A seed for random. use_nullspace_simplification : Boolean (default True) Use a basis for the nullspace to find groups of reactions whose fluxes are multiples of each other and dead end reactions. From each of these groups only 1 reaction will be included as a possible knockout. Returns ------- OptGeneResult """ target = get_reaction_for(self._model, target) biomass = get_reaction_for(self._model, biomass) substrate = get_reaction_for(self._model, substrate) if growth_coupled: objective_function = biomass_product_coupled_min_yield(biomass, target, substrate) else: objective_function = biomass_product_coupled_yield(biomass, target, substrate) if self.manipulation_type == "genes": optimization_algorithm = GeneKnockoutOptimization( model=self._model, heuristic_method=self._algorithm, essential_genes=self._essential_genes, plot=self.plot, objective_function=objective_function, use_nullspace_simplification=use_nullspace_simplification) elif self.manipulation_type == "reactions": optimization_algorithm = ReactionKnockoutOptimization( model=self._model, heuristic_method=self._algorithm, essential_reactions=self._essential_reactions, plot=self.plot, objective_function=objective_function, use_nullspace_simplification=use_nullspace_simplification) else: raise ValueError("Invalid manipulation type %s" % self.manipulation_type) optimization_algorithm.simulation_kwargs = kwargs optimization_algorithm.simulation_method = simulation_method optimization_algorithm.archiver = ProductionStrainArchive() result = optimization_algorithm.run(max_evaluations=max_evaluations, pop_size=population_size, max_size=max_knockouts, variable_size=variable_size, maximize=True, max_archive_size=max_results, seed=seed, **kwargs) kwargs.update(optimization_algorithm.simulation_kwargs) return OptGeneResult(self._model, result, objective_function, simulation_method, self.manipulation_type, biomass, target, substrate, kwargs)
def run(self, target=None, biomass=None, substrate=None, max_swaps=5, variable_size=True, simulation_method=fba, growth_coupled=False, max_evaluations=20000, population_size=200, time_machine=None, max_results=50, seed=None, **kwargs): """ Parameters ---------- target : str, Metabolite or Reaction The design target. biomass : str, Metabolite or Reaction The biomass definition in the model. substrate : str, Metabolite or Reaction The main carbon source. max_swaps : int Max number of swaps allowed. variable_size : bool If true, all candidates have the same size. Otherwise the candidate size can be from 1 to max_knockouts. simulation_method : function Any method from cameo.flux_analysis.simulation or equivalent. growth_coupled : bool If true will use the minimum flux rate to compute the fitness. max_evaluations : int Number of evaluations before stop. population_size : int Number of individuals in each generation. time_machine : TimeMachine See TimeMachine. max_results : int Max number of different designs to return if found. kwargs : dict Arguments for the simulation method. seed : int A seed for random. Returns ------- HeuristicOptSwapResult """ target = get_reaction_for(self._model, target) biomass = get_reaction_for(self._model, biomass) substrate = get_reaction_for(self._model, substrate) if growth_coupled: objective_function = biomass_product_coupled_min_yield(biomass, target, substrate) else: objective_function = biomass_product_coupled_yield(biomass, target, substrate) optimization_algorithm = CofactorSwapOptimization(model=self._model, cofactor_id_swaps=self._cofactor_id_swaps, skip_reactions=self._skip_reactions, objective_function=objective_function) optimization_algorithm.simulation_kwargs = kwargs optimization_algorithm.simulation_method = simulation_method optimization_algorithm.archiver = ProductionStrainArchive() result = optimization_algorithm.run(max_evaluations=max_evaluations, pop_size=population_size, max_size=max_swaps, variable_size=variable_size, maximize=True, max_archive_size=max_results, seed=seed, **kwargs) kwargs.update(optimization_algorithm.simulation_kwargs) return HeuristicOptSwapResult(self._model, result, self._swap_pairs, objective_function, simulation_method, biomass, target, substrate, kwargs)