def display_on_map(self, index=0, map_name=None, palette="YlGnBu"): with self._model: for ko in self.data_frame.loc[index, "reactions"]: swap_cofactors(self._model.reactions.get_by_id(ko), self._model, self._swap_pairs) fluxes = self._simulation_method(self._model, **self._simulation_kwargs) fluxes.display_on_map(map_name=map_name, palette=palette)
def process_reaction_swap_solution(model, solution, simulation_method, simulation_kwargs, biomass, target, substrate, objective_function, swap_pairs): """ Arguments --------- model: cobra.Model A constraint-based model solution: tuple - (reactions, knockouts) The output of a decoder simulation_method: function See see cameo.flux_analysis.simulation simulation_kwargs: dict Keyword arguments to run the simulation method biomass: Reaction Cellular biomass reaction target: Reaction The strain design target substrate: Reaction The main carbon source uptake rate objective_function: cameo.strain_design.heuristic.evolutionary.objective_functions.ObjectiveFunction The objective function used for evaluation. swap_pairs: The metabolites to swap Returns ------- list A list with: reactions, size, fva_min, fva_max, target flux, biomass flux, yield, fitness, [fitness, [fitness]] """ with model: reactions = [model.reactions.get_by_id(rid) for rid in solution] for reaction in reactions: swap_cofactors(reaction, model, swap_pairs) flux_dist = simulation_method(model, reactions=objective_function.reactions, objective=biomass, **simulation_kwargs) model.objective = biomass fva = flux_variability_analysis(model, fraction_of_optimum=0.99, reactions=[target]) target_yield = flux_dist[target] / abs(flux_dist[substrate]) return [ solution, fva.lower_bound(target), fva.upper_bound(target), flux_dist[target], flux_dist[biomass], target_yield, objective_function(model, flux_dist, reactions) ]
def plot(self, plotter, index=0, grid=None, width=None, height=None, title=None, palette=None, **kwargs): wt_production = phenotypic_phase_plane(self._model, objective=self._target, variables=[self._biomass]) with self._model: for ko in self.data_frame.loc[index, "reactions"]: swap_cofactors(self._model.reactions.get_by_id(ko), self._model, self._swap_pairs) mt_production = phenotypic_phase_plane(self._model, objective=self._target, variables=[self._biomass]) if title is None: title = "Production Envelope" dataframe = DataFrame(columns=["ub", "lb", "value", "strain"]) for _, row in wt_production.iterrows(): _df = DataFrame([[ row['objective_upper_bound'], row['objective_lower_bound'], row[self._biomass.id], "WT" ]], columns=dataframe.columns) dataframe = dataframe.append(_df) for _, row in mt_production.iterrows(): _df = DataFrame([[ row['objective_upper_bound'], row['objective_lower_bound'], row[self._biomass.id], "MT" ]], columns=dataframe.columns) dataframe = dataframe.append(_df) plot = plotter.production_envelope(dataframe, grid=grid, width=width, height=height, title=title, x_axis_label=self._biomass.id, y_axis_label=self._target.id, palette=palette) plotter.display(plot)
def process_reaction_swap_solution(model, solution, simulation_method, simulation_kwargs, biomass, target, substrate, objective_function, swap_pairs): """ Arguments --------- model: cobra.Model A constraint-based model solution: tuple - (reactions, knockouts) The output of a decoder simulation_method: function See see cameo.flux_analysis.simulation simulation_kwargs: dict Keyword arguments to run the simulation method biomass: Reaction Cellular biomass reaction target: Reaction The strain design target substrate: Reaction The main carbon source uptake rate objective_function: cameo.strain_design.heuristic.evolutionary.objective_functions.ObjectiveFunction The objective function used for evaluation. swap_pairs: The metabolites to swap Returns ------- list A list with: reactions, size, fva_min, fva_max, target flux, biomass flux, yield, fitness, [fitness, [fitness]] """ with model: reactions = [model.reactions.get_by_id(rid) for rid in solution] for reaction in reactions: swap_cofactors(reaction, model, swap_pairs) flux_dist = simulation_method(model, reactions=objective_function.reactions, objective=biomass, **simulation_kwargs) model.objective = biomass fva = flux_variability_analysis(model, fraction_of_optimum=0.99, reactions=[target]) target_yield = flux_dist[target] / abs(flux_dist[substrate]) return [solution, fva.lower_bound(target), fva.upper_bound(target), flux_dist[target], flux_dist[biomass], target_yield, objective_function(model, flux_dist, reactions)]
def evaluate_individual(self, individual): swap_reactions = self.decoder(individual)[0] with self.model: for reaction in swap_reactions: swap_cofactors(reaction, self.model, self.swap_pair, inplace=True) try: solution = self.simulation_method(self.model, cache=self.cache, volatile=False, raw=True, reactions=self.objective_function.reactions, **self.simulation_kwargs) fitness = self.objective_function(self.model, solution, swap_reactions) except OptimizationError as e: logger.debug(e) fitness = self.objective_function.worst_fitness() return fitness
def plot(self, index=0, grid=None, width=None, height=None, title=None, palette=None, **kwargs): wt_production = phenotypic_phase_plane(self._model, objective=self._target, variables=[self._biomass]) with self._model: for ko in self.data_frame.loc[index, "reactions"]: swap_cofactors(self._model.reactions.get_by_id(ko), self._model, self._swap_pairs) mt_production = phenotypic_phase_plane(self._model, objective=self._target, variables=[self._biomass]) if title is None: title = "Production Envelope" dataframe = DataFrame(columns=["ub", "lb", "value", "strain"]) for _, row in wt_production.iterrows(): _df = DataFrame([[row['objective_upper_bound'], row['objective_lower_bound'], row[self._biomass.id], "WT"]], columns=dataframe.columns) dataframe = dataframe.append(_df) for _, row in mt_production.iterrows(): _df = DataFrame([[row['objective_upper_bound'], row['objective_lower_bound'], row[self._biomass.id], "MT"]], columns=dataframe.columns) dataframe = dataframe.append(_df) plot = plotter.production_envelope(dataframe, grid=grid, width=width, height=height, title=title, x_axis_label=self._biomass.id, y_axis_label=self._target.id, palette=palette) plotter.display(plot)
def evaluate_individual(self, individual): swap_reactions = self.decoder(individual)[0] with self.model: for reaction in swap_reactions: swap_cofactors(reaction, self.model, self.swap_pair, inplace=True) try: solution = self.simulation_method( self.model, cache=self.cache, volatile=False, raw=True, reactions=self.objective_function.reactions, **self.simulation_kwargs) fitness = self.objective_function(self.model, solution, swap_reactions) except OptimizationError as e: logger.debug(e) fitness = self.objective_function.worst_fitness() return fitness
def _evaluate_individual(self, individual): swap_reactions = self.decoder(individual) with TimeMachine() as tm: for reaction in swap_reactions: swap_cofactors(reaction, self.model, self.swap_pair, inplace=True, time_machine=tm) try: solution = self.simulation_method( self.model, cache=self.cache, volatile=False, raw=True, reactions=self.objective_function.reactions, **self.simulation_kwargs) fitness = self.objective_function(self.model, solution, swap_reactions) except SolveError as e: logger.debug(e) fitness = self.objective_function.worst_fitness() return fitness
def apply(self, model, time_machine=None): reaction = model.reactions.get_by_id(self.id) swap_cofactors(reaction, model, self.swap_pairs, time_machine=time_machine)
def apply(self, model): reaction = model.reactions.get_by_id(self.id) swap_cofactors(reaction, model, self.swap_pairs)