def test_pfba(self): fba_solution = fba(self.model) fba_flux_sum = sum((abs(val) for val in list(fba_solution.fluxes.values()))) pfba_solution = pfba(self.model) pfba_flux_sum = sum((abs(val) for val in list(pfba_solution.fluxes.values()))) # looks like GLPK finds a parsimonious solution without the flux minimization objective self.assertTrue((pfba_flux_sum - fba_flux_sum) < 1e-6, msg="FBA sum is suppose to be lower than PFBA (was %f)" % (pfba_flux_sum - fba_flux_sum))
def test_pfba_iJO(self): fba_solution = fba(iJO_MODEL) fba_flux_sum = sum((abs(val) for val in list(fba_solution.fluxes.values()))) pfba_solution = pfba(iJO_MODEL) pfba_flux_sum = sum((abs(val) for val in list(pfba_solution.fluxes.values()))) print(pfba_flux_sum) self.assertTrue((pfba_flux_sum - fba_flux_sum) < 1e-6, msg="FBA sum is suppose to be lower than PFBA (was %f)" % (pfba_flux_sum - fba_flux_sum))
def test_fba_with_reaction_filter(self, core_model): original_objective = core_model.objective solution = fba( core_model, reactions=['EX_o2_LPAREN_e_RPAREN_', 'EX_glc_LPAREN_e_RPAREN_']) assert abs(solution.objective_value - 0.873921) < 0.000001 assert len(solution.fluxes) == 2 assert core_model.objective is original_objective
def test_pfba_ijo1366(self, ijo1366): original_objective = ijo1366.objective fba_solution = fba(ijo1366) fba_flux_sum = sum((abs(val) for val in fba_solution.fluxes.values)) pfba_solution = pfba(ijo1366) pfba_flux_sum = sum((abs(val) for val in pfba_solution.fluxes.values)) assert (pfba_flux_sum - fba_flux_sum) < 1e-6, \ "FBA sum is suppose to be lower than PFBA (was %f)" % (pfba_flux_sum - fba_flux_sum) assert ijo1366.objective.expression == original_objective.expression
def test_pfba_iJO(self): original_objective = self.model.objective fba_solution = fba(iJO_MODEL) fba_flux_sum = sum((abs(val) for val in fba_solution.fluxes.values())) pfba_solution = pfba(iJO_MODEL) pfba_flux_sum = sum((abs(val) for val in pfba_solution.fluxes.values())) self.assertTrue((pfba_flux_sum - fba_flux_sum) < 1e-6, msg="FBA sum is suppose to be lower than PFBA (was %f)" % (pfba_flux_sum - fba_flux_sum)) self.assertIs(self.model.objective, original_objective)
def test_pfba(self): original_objective = self.model.objective fba_solution = fba(self.model) fba_flux_sum = sum((abs(val) for val in list(fba_solution.fluxes.values()))) pfba_solution = pfba(self.model) pfba_flux_sum = sum((abs(val) for val in list(pfba_solution.fluxes.values()))) # looks like GLPK finds a parsimonious solution without the flux minimization objective self.assertTrue((pfba_flux_sum - fba_flux_sum) < 1e-6, msg="FBA sum is suppose to be lower than PFBA (was %f)" % (pfba_flux_sum - fba_flux_sum)) self.assertIs(self.model.objective, original_objective)
def test_pfba(self, core_model): original_objective = core_model.objective fba_solution = fba(core_model) fba_flux_sum = sum((abs(val) for val in list(fba_solution.fluxes.values))) pfba_solution = pfba(core_model) pfba_flux_sum = sum((abs(val) for val in list(pfba_solution.fluxes.values))) # looks like GLPK finds a parsimonious solution without the flux minimization objective assert (pfba_flux_sum - fba_flux_sum) < 1e-6, \ "FBA sum is suppose to be lower than PFBA (was %f)" % (pfba_flux_sum - fba_flux_sum) assert core_model.objective.expression == original_objective.expression
def test_pfba_iJO(self): fba_solution = fba(iJO_MODEL) fba_flux_sum = sum( (abs(val) for val in list(fba_solution.fluxes.values()))) pfba_solution = pfba(iJO_MODEL) pfba_flux_sum = sum( (abs(val) for val in list(pfba_solution.fluxes.values()))) print(pfba_flux_sum) self.assertTrue( (pfba_flux_sum - fba_flux_sum) < 1e-6, msg="FBA sum is suppose to be lower than PFBA (was %f)" % (pfba_flux_sum - fba_flux_sum))
def test_fba(self): solution = fba(self.model) original_objective = self.model.objective self.assertAlmostEqual(solution.objective_value, 0.873921, delta=0.000001) self.assertEqual(len(solution.fluxes), len(self.model.reactions)) self.assertIs(self.model.objective, original_objective)
def test_fba(self, core_model): solution = fba(core_model) original_objective = core_model.objective assert abs(solution.objective_value - 0.873921) < 0.000001 assert len(solution.fluxes) == len(core_model.reactions) assert core_model.objective.expression == original_objective.expression
def test_fba_with_reaction_filter(self): original_objective = self.model.objective solution = fba(self.model, reactions=['EX_o2_LPAREN_e_RPAREN_', 'EX_glc_LPAREN_e_RPAREN_']) self.assertAlmostEqual(solution.objective_value, 0.873921, delta=0.000001) self.assertEqual(len(solution.fluxes), 2) self.assertIs(self.model.objective, original_objective)
def test_fba(self): solution = fba(self.model) self.assertAlmostEqual(solution.objective_value, 0.873921, delta=0.000001)
def display_on_map(self, index=0, map_name=None, palette="YlGnBu"): with TimeMachine() as tm: for ko in self.data_frame.loc[index, "reactions"]: self._model.reactions.get_by_id(ko).knock_out(tm) fluxes = fba(self._model) fluxes.display_on_map(map_name=map_name, palette=palette)
def display_on_map(self, index=0, map_name=None, palette="YlGnBu"): with self._model: for ko in self.data_frame.loc[index, "reactions"]: self._model.reactions.get_by_id(ko).knock_out() fluxes = fba(self._model) fluxes.display_on_map(map_name=map_name, palette=palette)
def run(self, target=None, max_enforced_flux=0.9, number_of_results=10, exclude=(), simulation_method=fba, simulation_kwargs=None): """ Performs a Flux Scanning based on Enforced Objective Flux (FSEOF) analysis. Parameters ---------- target: str, Reaction, Metabolite The target for optimization. max_enforced_flux : float, optional The maximal flux of secondary_objective that will be enforced, relative to the theoretical maximum ( defaults to 0.9). number_of_results : int, optional The number of enforced flux levels (defaults to 10). exclude : Iterable of reactions or reaction ids that will not be included in the output. Returns ------- FseofResult An object containing the identified reactions and the used parameters. References ---------- .. [1] H. S. Choi, S. Y. Lee, T. Y. Kim, and H. M. Woo, 'In silico identification of gene amplification targets for improvement of lycopene production.,' Appl Environ Microbiol, vol. 76, no. 10, pp. 3097–3105, May 2010. """ model = self.model target = get_reaction_for(model, target) simulation_kwargs = simulation_kwargs if simulation_kwargs is not None else {} simulation_kwargs['objective'] = self.primary_objective if 'reference' not in simulation_kwargs: reference = simulation_kwargs['reference'] = pfba( model, **simulation_kwargs) else: reference = simulation_kwargs['reference'] ndecimals = config.ndecimals # Exclude list exclude = list(exclude) + model.exchanges exclude_ids = [target.id] for reaction in exclude: if isinstance(reaction, Reaction): exclude_ids.append(reaction.id) else: exclude_ids.append(reaction) with TimeMachine() as tm: tm(do=int, undo=partial(setattr, model, "objective", model.objective)) tm(do=int, undo=partial(setattr, target, "lower_bound", target.lower_bound)) tm(do=int, undo=partial(setattr, target, "upper_bound", target.upper_bound)) # Find initial flux of enforced reaction initial_fluxes = reference.fluxes initial_flux = round(initial_fluxes[target.id], ndecimals) # Find theoretical maximum of enforced reaction max_theoretical_flux = round( fba(model, objective=target.id, reactions=[target.id]).fluxes[target.id], ndecimals) max_flux = max_theoretical_flux * max_enforced_flux # Calculate enforcement levels levels = [ initial_flux + (i + 1) * (max_flux - initial_flux) / number_of_results for i in range(number_of_results) ] # FSEOF results results = {reaction.id: [] for reaction in model.reactions} for level in levels: target.lower_bound = level target.upper_bound = level solution = simulation_method(model, **simulation_kwargs) for reaction_id, flux in solution.fluxes.iteritems(): results[reaction_id].append(round(flux, ndecimals)) # Test each reaction fseof_reactions = [] for reaction_id, fluxes in results.items(): if reaction_id not in exclude_ids \ and max(abs(max(fluxes)), abs(min(fluxes))) > abs(reference[reaction_id]) \ and min(fluxes) * max(fluxes) >= 0: fseof_reactions.append(model.reactions.get_by_id(reaction_id)) results = {rea.id: results[rea.id] for rea in fseof_reactions} run_args = dict(max_enforced_flux=max_enforced_flux, number_of_results=number_of_results, solution_method=simulation_method, simulation_kwargs=simulation_kwargs, exclude=exclude) return FSEOFResult(fseof_reactions, target, model, self.primary_objective, levels, results, run_args, reference)
def test_fba_with_reaction_filter(self, core_model): original_objective = core_model.objective solution = fba(core_model, reactions=['EX_o2_LPAREN_e_RPAREN_', 'EX_glc_LPAREN_e_RPAREN_']) assert abs(solution.objective_value - 0.873921) < 0.000001 assert len(solution.fluxes) == 2 assert core_model.objective.expression == original_objective.expression
def run(self, target=None, max_enforced_flux=0.9, number_of_results=10, exclude=(), simulation_method=fba, simulation_kwargs=None): """ Performs a Flux Scanning based on Enforced Objective Flux (FSEOF) analysis. Parameters ---------- target: str, Reaction, Metabolite The target for optimization. max_enforced_flux : float, optional The maximal flux of secondary_objective that will be enforced, relative to the theoretical maximum ( defaults to 0.9). number_of_results : int, optional The number of enforced flux levels (defaults to 10). exclude : Iterable of reactions or reaction ids that will not be included in the output. Returns ------- FseofResult An object containing the identified reactions and the used parameters. References ---------- .. [1] H. S. Choi, S. Y. Lee, T. Y. Kim, and H. M. Woo, 'In silico identification of gene amplification targets for improvement of lycopene production.,' Appl Environ Microbiol, vol. 76, no. 10, pp. 3097–3105, May 2010. """ model = self.model target = get_reaction_for(model, target) simulation_kwargs = simulation_kwargs if simulation_kwargs is not None else {} simulation_kwargs['objective'] = self.primary_objective if 'reference' not in simulation_kwargs: reference = simulation_kwargs['reference'] = pfba(model, **simulation_kwargs) else: reference = simulation_kwargs['reference'] ndecimals = config.ndecimals # Exclude list exclude = list(exclude) + model.boundary exclude_ids = [target.id] for reaction in exclude: if isinstance(reaction, Reaction): exclude_ids.append(reaction.id) else: exclude_ids.append(reaction) with TimeMachine() as tm: tm(do=int, undo=partial(setattr, model, "objective", model.objective)) tm(do=int, undo=partial(setattr, target, "lower_bound", target.lower_bound)) tm(do=int, undo=partial(setattr, target, "upper_bound", target.upper_bound)) # Find initial flux of enforced reaction initial_fluxes = reference.fluxes initial_flux = round(initial_fluxes[target.id], ndecimals) # Find theoretical maximum of enforced reaction max_theoretical_flux = round(fba(model, objective=target.id, reactions=[target.id]).fluxes[target.id], ndecimals) max_flux = max_theoretical_flux * max_enforced_flux # Calculate enforcement levels levels = [initial_flux + (i + 1) * (max_flux - initial_flux) / number_of_results for i in range(number_of_results)] # FSEOF results results = {reaction.id: [] for reaction in model.reactions} for level in levels: target.lower_bound = level target.upper_bound = level solution = simulation_method(model, **simulation_kwargs) for reaction_id, flux in solution.fluxes.iteritems(): results[reaction_id].append(round(flux, ndecimals)) # Test each reaction fseof_reactions = [] for reaction_id, fluxes in results.items(): if reaction_id not in exclude_ids \ and max(abs(max(fluxes)), abs(min(fluxes))) > abs(reference[reaction_id]) \ and min(fluxes) * max(fluxes) >= 0: fseof_reactions.append(model.reactions.get_by_id(reaction_id)) results = {rea.id: results[rea.id] for rea in fseof_reactions} run_args = dict(max_enforced_flux=max_enforced_flux, number_of_results=number_of_results, solution_method=simulation_method, simulation_kwargs=simulation_kwargs, exclude=exclude) return FSEOFResult(fseof_reactions, target, model, self.primary_objective, levels, results, run_args, reference)