def setUp(self): self.model = TEST_MODEL self.single_objective_function = product_yield('product', 'substrate') self.multiobjective_function = MultiObjectiveFunction([ product_yield('product', 'substrate'), number_of_knockouts() ])
def setUp(self): self.model = TEST_MODEL self.single_objective_function = product_yield('product', 'substrate') self.multiobjective_function = [ product_yield('product', 'substrate'), number_of_knockouts() ]
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_evaluate_swap(self): TEST_MODEL.objective = TEST_MODEL.reactions.EX_etoh_LPAREN_e_RPAREN_ py = product_yield(TEST_MODEL.reactions.EX_etoh_LPAREN_e_RPAREN_, TEST_MODEL.reactions.EX_glc_LPAREN_e_RPAREN_) reactions = ['ACALD', 'ALCD2x', 'G6PDH2r', 'GAPD'] optimization = CofactorSwapOptimization(model=TEST_MODEL, objective_function=py, candidate_reactions=reactions) optimization_result = optimization.run(max_evaluations=16, max_size=2) fitness = optimization_result.data_frame.iloc[0].fitness self.assertAlmostEqual(fitness, 0.66667, places=3)
def test_swap_reaction_identification(self): expected_reactions = ['ACALD', 'AKGDH', 'ALCD2x', 'G6PDH2r', 'GAPD', 'GLUDy', 'GLUSy', 'GND', 'ICDHyr', 'LDH_D', 'MDH', 'ME1', 'ME2', 'NADH16', 'PDH'] py = product_yield(TEST_MODEL.reactions.EX_etoh_LPAREN_e_RPAREN_, TEST_MODEL.reactions.EX_glc_LPAREN_e_RPAREN_) optimization = CofactorSwapOptimization(model=TEST_MODEL, objective_function=py) self.assertEquals(expected_reactions, optimization.representation) self.assertNotIn('PGI', optimization.representation)
def test_yield(self): solution = self._MockupSolution() solution.set_primal('biomass', 0.6) solution.set_primal('product', 2) solution.set_primal('substrate', -10) of = product_yield("product", "substrate") self.assertEqual(of.name, "yield = (product / substrate)") fitness = of(None, solution, None) self.assertAlmostEqual(2.0 / 10.0, fitness) solution.set_primal('substrate', 0) fitness = of(None, solution, None) self.assertEquals(0, fitness)
def test_initializer(self): objective1 = biomass_product_coupled_yield( "Biomass_Ecoli_core_N_LPAREN_w_FSLASH_GAM_RPAREN__Nmet2", "EX_ac_LPAREN_e_RPAREN_", "EX_glc_LPAREN_e_RPAREN_") decoder = ReactionSetDecoder(["PGI", "PDH", "FUM", "FBA", "G6PDH2r", "FRD7", "PGL", "PPC"], TEST_MODEL) evaluator = KnockoutEvaluator(TEST_MODEL, decoder, objective1, fba, {}) self.assertEquals(evaluator.decoder, decoder) self.assertEquals(evaluator.objective_function, objective1) self.assertTrue(hasattr(evaluator, "__call__")) objective2 = product_yield("EX_ac_LPAREN_e_RPAREN_", "EX_glc_LPAREN_e_RPAREN_") evaluator = KnockoutEvaluator(TEST_MODEL, decoder, MultiObjectiveFunction([objective1, objective2]), fba, {}) self.assertEquals(evaluator.objective_function.objectives, [objective1, objective2])
def test_evaluate_multiobjective(self): representation = ["ATPS4r", "PYK", "GLUDy", "PPS", "CO2t", "PDH", "FUM", "FBA", "G6PDH2r", "FRD7", "PGL", "PPC"] decoder = ReactionKnockoutDecoder(representation, TEST_MODEL) objective1 = biomass_product_coupled_yield( "Biomass_Ecoli_core_N_LPAREN_w_FSLASH_GAM_RPAREN__Nmet2", "EX_ac_LPAREN_e_RPAREN_", "EX_glc_LPAREN_e_RPAREN_") objective2 = product_yield("EX_ac_LPAREN_e_RPAREN_", "EX_glc_LPAREN_e_RPAREN_") evaluator = KnockoutEvaluator(TEST_MODEL, decoder, [objective1, objective2], fba, {}) fitness = evaluator([[0, 1, 2, 3, 4]])[0] self.assertIsInstance(fitness, Pareto) self.assertAlmostEqual(fitness[0], 0.41, delta=0.02) self.assertAlmostEqual(fitness[1], 1.57, delta=0.035)
def test_initializer(self): objective1 = biomass_product_coupled_yield( "Biomass_Ecoli_core_N_LPAREN_w_FSLASH_GAM_RPAREN__Nmet2", "EX_ac_LPAREN_e_RPAREN_", "EX_glc_LPAREN_e_RPAREN_") decoder = ReactionKnockoutDecoder(["PGI", "PDH", "FUM", "FBA", "G6PDH2r", "FRD7", "PGL", "PPC"], TEST_MODEL) evaluator = KnockoutEvaluator(TEST_MODEL, decoder, objective1, fba, {}) self.assertEquals(evaluator.decoder, decoder) self.assertEquals(evaluator.objective_function, objective1) self.assertFalse(evaluator.is_mo) self.assertTrue(hasattr(evaluator, "__call__")) objective2 = product_yield("EX_ac_LPAREN_e_RPAREN_", "EX_glc_LPAREN_e_RPAREN_") evaluator = KnockoutEvaluator(TEST_MODEL, decoder, [objective1, objective2], fba, {}) self.assertEquals(evaluator.objective_function, [objective1, objective2]) self.assertTrue(evaluator.is_mo)
def cofactor_swap_optimization(pathway, model): with model: model.objective = model.biomass growth = model.slim_optimize() model.reactions.get_by_id(model.biomass).lower_bound = 0.05 * growth pathway.apply(model) model.objective = pathway.product.id pyield = product_yield(pathway.product.id, model.carbon_source) with model: # TODO (Moritz Beber): By default swaps NADH with NADPH using BiGG # notation. predictor = CofactorSwapOptimization(model=model, objective_function=pyield, plot=False) designs = predictor.run(max_size=5, diversify=True) return designs
def test_evaluate_multiobjective(self): representation = ["ATPS4r", "PYK", "GLUDy", "PPS", "CO2t", "PDH", "FUM", "FBA", "G6PDH2r", "FRD7", "PGL", "PPC"] decoder = ReactionSetDecoder(representation, TEST_MODEL) objective1 = biomass_product_coupled_yield( "Biomass_Ecoli_core_N_LPAREN_w_FSLASH_GAM_RPAREN__Nmet2", "EX_ac_LPAREN_e_RPAREN_", "EX_glc_LPAREN_e_RPAREN_") objective2 = product_yield("EX_ac_LPAREN_e_RPAREN_", "EX_glc_LPAREN_e_RPAREN_") objective = MultiObjectiveFunction([objective1, objective2]) evaluator = KnockoutEvaluator(TEST_MODEL, decoder, objective, fba, {}) fitness = evaluator([[0, 1, 2, 3, 4]])[0] self.assertIsInstance(fitness, Pareto) self.assertAlmostEqual(fitness[0], 0.41, delta=0.02) self.assertAlmostEqual(fitness[1], 1.57, delta=0.035)
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 evaluate_production( model: cobra.Model, production_id: str, carbon_source_id: str ) -> Tuple[Optional[float], Optional[float], Optional[float], Optional[float]]: """ Evaluate the production levels in the specific model conditions. Warnings -------- This function is expected to be called within a context since it modifies the model's objective. Parameters ---------- model : cobra.Model The constraint-based metabolic model of the production organism. production_id : str The identifier of the reaction representing production, for example, a demand reaction on the compound. carbon_source_id : str The identifier of the reaction representing carbon uptake, for example, a glucose exchange reaction. Returns ------- tuple float or None The theoretical maximum production rate if any. float or None The maximal product flux yield if any. float or None The maximal product carbon yield if any. float or None The maximal product yield by weight if any. """ pyield = product_yield(production_id, carbon_source_id) # Compute the number of weighted carbon atoms. carbon_uptake = model.reactions.get_by_id(carbon_source_id) production = model.reactions.get_by_id(production_id) input_components = [reaction_elements(carbon_uptake)] output_components = reaction_elements(production) # Compute the masses. try: input_weights = [reaction_weight(carbon_uptake)] output_weight = reaction_weight(production) # If the reactions are ill-defined or the metabolite weight is unknown. except (ValueError, TypeError): input_weights = [] output_weight = [] try: model.objective = production_id solution = model.optimize() production_flux = solution[production_id] except OptimizationError as error: logger.error( "Could not determine production due to a solver error. %r", error ) production_flux = None production_flux_yield = None production_carbon_yield = None production_mass_yield = None else: try: production_flux_yield = pyield(model, solution, None) except ZeroDivisionError: logger.error("Division by zero in yield calculation.") production_flux_yield = None production_carbon_yield = total_yield( [solution[carbon_source_id]], input_components, solution[production_id], output_components, ) if isnan(production_carbon_yield): production_carbon_yield = None production_mass_yield = total_yield( [solution[carbon_source_id]], input_weights, solution[production_id], output_weight, ) if isnan(production_mass_yield): production_mass_yield = None return ( production_flux, production_flux_yield, production_carbon_yield, production_mass_yield, )
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