def _data_frame(solution): notice("%s: %f" % self.solutions.axes[0][solution - 1][0]) notice("%s: %f" % self.solutions.axes[0][solution - 1][1]) df = self.solutions.iloc[solution - 1] df = df[abs(df.normalized_gaps) >= non_zero_flux_threshold] df = df.sort_values('normalized_gaps') display(df)
def _data_frame(solution): df = self.nth_panel(solution - 1) notice("biomass: {0:g}".format(df['biomass'].iat[0])) notice("production: {0:g}".format(df['production'].iat[0])) df = df.loc[abs(df['normalized_gaps']) >= non_zero_flux_threshold] df.sort_values('normalized_gaps', inplace=True) display(df)
def plot(self, grid=None, width=None, height=None, title=None, axis_font_size=None): if len(self.variable_ids) > 1: notice("Multi-dimensional plotting is not supported") return plotting.plot_production_envelope(self._phase_plane, objective=self.objective, key=self.variable_ids[0], grid=grid, width=width, height=height, title=title, axis_font_size=axis_font_size)
def __call__(self, product='L-glutamate', hosts=hosts, database=None, view=config.default_view): """The works. The following workflow will be followed to determine suitable metabolic engineering strategies for a desired product: - Determine production pathways for desired product and host organisms. Try a list of default hosts if no hosts are specified. - Determine maximum theoretical yields and production envelopes for all routes. - Determine if production routes can be coupled to growth. - Determine over-expression, down-regulation, and KO targets. Parameters ---------- product : str or Metabolite The desired product. hosts : list or Model or Host A list of hosts (e.g. cameo.api.hosts), models, mixture thereof, or a single model or host. Returns ------- Designs """ if database is None: database = universal.metanetx_universal_model_bigg_rhea notice("Starting searching for compound %s" % product) product = self.__translate_product_to_universal_reactions_model_metabolite(product, database) pathways = self.predict_pathways(product, hosts=hosts, database=database) optimization_reports = self.optimize_strains(pathways, view) return optimization_reports
def predict_pathways(self, product, hosts=None, database=None): # TODO: make this work with a single host or model """Predict production routes for a desired product and host spectrum. Parameters ---------- product : str or Metabolite The desired product. hosts : list or Model or Host A list of hosts (e.g. cameo.api.hosts), models, mixture thereof, or a single model or host. Returns ------- dict ... """ pathways = dict() product = self.__translate_product_to_universal_reactions_model_metabolite(product, database) for host in hosts: logging.debug('Processing host {}'.format(host.name)) if isinstance(host, Model): host = Host(name='UNKNOWN_HOST', models=[host]) for model in list(host.models): identifier = searching() logging.debug('Processing model {} for host {}'.format(model.id, host.name)) notice('Predicting pathways for product %s in %s (using model %s).' % (product.name, host, model.id)) logging.debug('Predicting pathways for model {}'.format(model.id)) pathway_predictor = pathway_prediction.PathwayPredictor(model, universal_model=database, compartment_regexp=re.compile(".*_c$")) # TODO adjust these numbers to something reasonable predicted_pathways = pathway_predictor.run(product, max_predictions=4, timeout=3 * 60, silent=True) stop_loader(identifier) pathways[(host, model)] = predicted_pathways self.__display_pathways_information(predicted_pathways, host, model) return pathways
def plot(self, index=None, variables=None, grid=None, width=None, height=None, title=None, palette=None, **kwargs): if len(self.variable_ids) > 1: notice("Multi-dimensional plotting is not supported") return if index is not None: self._plot_flux_variability_analysis(index, variables=variables, width=width, grid=grid, palette=palette) else: self._plot_production_envelope(title=title, grid=grid, width=width, height=height)
def plot(self, grid=None, width=None, height=None, title=None): if len(self.variable_ids) > 1: notice("Multi-dimensional plotting is not supported") return title = "DifferentialFVA Result" if title is None else title x = [elem[0][1] for elem in list(self.solutions.items)] y = [elem[1][1] for elem in list(self.solutions.items)] colors = ["red" for _ in x] plotting.plot_production_envelope(self._phase_plane, objective=self.objective, key=self.variable_ids[0], grid=grid, width=width, height=height, title=title, points=[x, y], points_colors=colors)
def __translate_product_to_universal_reactions_model_metabolite(self, product, database): if isinstance(product, Metabolite): return product elif isinstance(product, str): search_result = products.search(product) notice("Found %d compounds that match query '%s'" % (len(search_result), product)) self.__display_product_search_result(search_result) notice("Choosing best match (%s) ... please interrupt if this is not the desired compound." % search_result.name[0]) self.__display_compound(search_result.InChI[0]) return database.metabolites.get_by_id(search_result.index[0])
def translate_product_to_universal_reactions_model_metabolite(self, product, database): if isinstance(product, Metabolite): return product elif isinstance(product, six.text_type) or isinstance(product, six.string_types): search_result = products.search(product) search_result = search_result.loc[[i for i in search_result.index if i in database.metabolites]] if len(search_result) == 0: raise KeyError("No compound matches found for query %s" % product) notice("Found %d compounds that match query '%s'" % (len(search_result), product)) self.__display_product_search_result(search_result) notice("Choosing best match (%s) ... please interrupt if this is not the desired compound." % search_result.name.values[0]) self.__display_compound(search_result.InChI.values[0]) return database.metabolites.get_by_id(search_result.index[0])
def __translate_product_to_universal_reactions_model_metabolite( self, product, database): if isinstance(product, Metabolite): return product elif isinstance(product, str): search_result = products.search(product) notice("Found %d compounds that match query '%s'" % (len(search_result), product)) self.__display_product_search_result(search_result) notice( "Choosing best match (%s) ... please interrupt if this is not the desired compound." % search_result.name[0]) self.__display_compound(search_result.InChI[0]) return database.metabolites.get_by_id(search_result.index[0])
def predict_pathways(self, product, hosts, database, aerobic=True): """Predict production routes for a desired product and host spectrum. Parameters ---------- product : str or Metabolite The desired product. hosts : list A list of hosts (e.g. cameo.api.hosts), models, mixture thereof, or a single model or host. database: Model A model to use as database. See also: cameo.models.universal aerobic: bool If True, it will set `model.reactions.EX_o2_e.lower_bound` to 0. Returns ------- dict ([Host, Model] -> PredictedPathways) """ pathways = dict() product = self.__translate_product_to_universal_reactions_model_metabolite( product, database) for host in hosts: logging.debug('Processing host {}'.format(host.name)) if isinstance(host, Model): host = Host(name='UNKNOWN_HOST', models=[host]) for model in list(host.models): with model: if not aerobic and "EX_o2_e" in model.reactions: model.reactions.EX_o2_e.lower_bound = 0 identifier = searching() logging.debug('Processing model {} for host {}'.format( model.id, host.name)) notice( 'Predicting pathways for product %s in %s (using model %s).' % (product.name, host, model.id)) logging.debug('Predicting pathways for model {}'.format( model.id)) pathway_predictor = pathway_prediction.PathwayPredictor( model, universal_model=database) predicted_pathways = pathway_predictor.run( product, max_predictions=self.options.max_pathway_predictions, timeout=self.options.pathway_prediction_timeout, silent=not self.options.verbose) stop_loader(identifier) pathways[(host, model)] = predicted_pathways self.__display_pathways_information( predicted_pathways, host, model) return pathways
def __call__(self, product='L-glutamate', hosts=HOSTS, database=None, aerobic=True, view=config.default_view): """The works. The following workflow will be followed to determine suitable metabolic engineering strategies for a desired product: - Determine production pathways for desired product and host organisms. Try a list of default hosts if no hosts are specified. - Determine maximum theoretical yields and production envelopes for all routes. - Determine if production routes can be coupled to growth. - Determine over-expression, down-regulation, and KO targets. Parameters ---------- product : str or Metabolite The desired product. hosts : list or Model or Host A list of hosts (e.g. cameo.api.hosts), models, mixture thereof, or a single model or host. aerobic: bool If false, sets `model.reaction.EX_o2_e.lower_bound = 0` Returns ------- Designs """ if database is None: database = universal.metanetx_universal_model_bigg notice("Starting searching for compound %s" % product) try: product = self.__translate_product_to_universal_reactions_model_metabolite( product, database) except Exception: raise KeyError("Product %s is not in the %s database" % (product, database.id)) pathways = self.predict_pathways(product, hosts=hosts, database=database, aerobic=aerobic) optimization_reports = self.optimize_strains(pathways, view, aerobic=aerobic) return optimization_reports
def predict_pathways( self, product, hosts=None, database=None): # TODO: make this work with a single host or model """Predict production routes for a desired product and host spectrum. Parameters ---------- product : str or Metabolite The desired product. hosts : list or Model or Host A list of hosts (e.g. cameo.api.hosts), models, mixture thereof, or a single model or host. Returns ------- dict ... """ pathways = dict() product = self.__translate_product_to_universal_reactions_model_metabolite( product, database) for host in hosts: logging.debug('Processing host {}'.format(host.name)) if isinstance(host, Model): host = Host(name='UNKNOWN_HOST', models=[host]) for model in list(host.models): identifier = searching() logging.debug('Processing model {} for host {}'.format( model.id, host.name)) notice( 'Predicting pathways for product %s in %s (using model %s).' % (product.name, host, model.id)) logging.debug('Predicting pathways for model {}'.format( model.id)) pathway_predictor = pathway_prediction.PathwayPredictor( model, universal_model=database, compartment_regexp=re.compile(".*_c$")) # TODO adjust these numbers to something reasonable predicted_pathways = pathway_predictor.run(product, max_predictions=4, timeout=3 * 60, silent=True) stop_loader(identifier) pathways[(host, model)] = predicted_pathways self.__display_pathways_information(predicted_pathways, host, model) return pathways
def plot(self, grid=None, width=None, height=None, title=None, axis_font_size=None, palette=None, points=None, points_colors=None, **kwargs): if title is None: title = "Phenotypic Phase Plane" if len(self.variable_ids) == 1: variable = self.variable_ids[0] x_axis_label = self.nice_variable_ids[0] y_axis_label = self.nice_objective_id dataframe = pandas.DataFrame(columns=["ub", "lb", "value", "strain"]) for _, row in self.iterrows(): _df = pandas.DataFrame([[row['objective_upper_bound'], row['objective_lower_bound'], row[variable], "WT"]], columns=dataframe.columns) dataframe = dataframe.append(_df) plot = plotter.production_envelope(dataframe, grid=grid, width=width, height=height, title=title, y_axis_label=y_axis_label, x_axis_label=x_axis_label, palette=palette, points=points, points_colors=points_colors) elif len(self.variable_ids) == 2: var_1 = self.variable_ids[0] var_2 = self.variable_ids[1] x_axis_label = self.nice_variable_ids[0] y_axis_label = self.nice_variable_ids[1] z_axis_label = self.nice_objective_id dataframe = pandas.DataFrame(columns=["ub", "lb", "value1", "value2", "strain"]) for _, row in self.iterrows(): _df = pandas.DataFrame([[row['objective_upper_bound'], row['objective_lower_bound'], row[var_1], row[var_2], "WT"]], columns=dataframe.columns) dataframe = dataframe.append(_df) plot = plotter.production_envelope_3d(dataframe, grid=grid, width=width, height=height, title=title, y_axis_label=y_axis_label, x_axis_label=x_axis_label, z_axis_label=z_axis_label, palette=palette, points=points, points_colors=points_colors) else: notice("Multi-dimensional plotting is not supported") return if grid is None: plotter.display(plot)
def predict_pathways(self, product, hosts, database, aerobic=True): """Predict production routes for a desired product and host spectrum. Parameters ---------- product : str or Metabolite The desired product. hosts : list A list of hosts (e.g. cameo.api.hosts), models, mixture thereof, or a single model or host. database: Model A model to use as database. See also: cameo.models.universal aerobic: bool If True, it will set `model.reactions.EX_o2_e.lower_bound` to 0. Returns ------- dict ([Host, Model] -> PredictedPathways) """ pathways = dict() product = self.translate_product_to_universal_reactions_model_metabolite(product, database) for host in hosts: logging.debug('Processing host {}'.format(host.name)) if isinstance(host, Model): host = Host(name='UNKNOWN_HOST', models=[host]) for model in list(host.models): with model: if not aerobic and "EX_o2_e" in model.reactions: model.reactions.EX_o2_e.lower_bound = 0 identifier = searching() logging.debug('Processing model {} for host {}'.format(model.id, host.name)) notice('Predicting pathways for product %s in %s (using model %s).' % (product.name, host, model.id)) logging.debug('Predicting pathways for model {}'.format(model.id)) pathway_predictor = pathway_prediction.PathwayPredictor(model, universal_model=database) predicted_pathways = pathway_predictor.run(product, max_predictions=self.options.max_pathway_predictions, timeout=self.options.pathway_prediction_timeout, silent=not self.options.verbose) stop_loader(identifier) pathways[(host, model)] = predicted_pathways self.__display_pathways_information(predicted_pathways, host, model) return pathways
def plot(self, grid=None, width=None, height=None, title=None, axis_font_size=None, palette=None, points=None, points_colors=None, estimate='flux', **kwargs): """plot phenotypic phase plane result create a plot of a phenotypic phase plane analysis Parameters ---------- grid: plotting grid the grid for plotting width: int the width of the plot height: int the height of the plot title: string the height of the plot axis_font_size: int the font sizes for the axis palette: string name of color palette to use, e.g. RdYlBlu points: iterable of points additional points to plot as x, y iterable points_colors: iterable of strings iterable with colors for the points estimate: string either flux, mass_yield (g output / g output) or c_yield (mol carbon output / mol carbon input) """ possible_estimates = { 'flux': ('objective_upper_bound', 'objective_lower_bound', 'flux', '[mmol gDW^-1 h^-1]'), 'mass_yield': ('mass_yield_upper_bound', 'mass_yield_lower_bound', 'mass yield, src={}'.format(self.source_reaction), '[g/g(src) h^-1]'), 'c_yield': ('c_yield_upper_bound', 'c_yield_lower_bound', 'carbon yield, src={}'.format(self.source_reaction), '[mmol(C)/mmol(C(src)) h^-1]') } if estimate not in possible_estimates: raise Exception('estimate must be one of %s' % ', '.join(possible_estimates.keys())) upper, lower, description, unit = possible_estimates[estimate] if title is None: title = "Phenotypic Phase Plane ({})".format(description) if len(self.variable_ids) == 1: variable = self.variable_ids[0] y_axis_label = self._axis_label(self.objective, self.nice_objective_id, unit) x_axis_label = self._axis_label(variable, self.nice_variable_ids[0], '[mmol gDW^-1 h^-1]') dataframe = pandas.DataFrame( columns=["ub", "lb", "value", "strain"]) for _, row in self.iterrows(): _df = pandas.DataFrame( [[row[upper], row[lower], row[variable], "WT"]], columns=dataframe.columns) dataframe = dataframe.append(_df) plot = plotter.production_envelope(dataframe, grid=grid, width=width, height=height, title=title, y_axis_label=y_axis_label, x_axis_label=x_axis_label, palette=palette, points=points, points_colors=points_colors) elif len(self.variable_ids) == 2: var_1 = self.variable_ids[0] var_2 = self.variable_ids[1] x_axis_label = self._axis_label(var_1, self.nice_variable_ids[0], '[mmol gDW^-1 h^-1]') y_axis_label = self._axis_label(var_2, self.nice_variable_ids[1], '[mmol gDW^-1 h^-1]') z_axis_label = self._axis_label(self.objective, self.nice_objective_id, unit) dataframe = pandas.DataFrame( columns=["ub", "lb", "value1", "value2", "strain"]) for _, row in self.iterrows(): _df = pandas.DataFrame( [[row[upper], row[lower], row[var_1], row[var_2], "WT"]], columns=dataframe.columns) dataframe = dataframe.append(_df) plot = plotter.production_envelope_3d(dataframe, grid=grid, width=width, height=height, title=title, y_axis_label=y_axis_label, x_axis_label=x_axis_label, z_axis_label=z_axis_label, palette=palette, points=points, points_colors=points_colors) else: notice("Multi-dimensional plotting is not supported") return if grid is None: plotter.display(plot)
def _data_frame(solution): notice("%s: %f" % self.solutions.axes[0][solution - 1][0]) notice("%s: %f" % self.solutions.axes[0][solution - 1][1]) display(self.solutions.iloc[solution - 1])
def display_pathway(pathway, i): notice("Pathway %i" % i) if in_ipnb(): display(pathway.data_frame) else: print(pathway.data_frame)
def predict_pathways(self, product, hosts=None, database=None, aerobic=True): """Predict production routes for a desired product and host spectrum. Parameters ---------- product : str or Metabolite The desired product. hosts : list or Model or Host A list of hosts (e.g. cameo.api.hosts), models, mixture thereof, or a single model or host. database: SolverBasedModel A model to use as database. See also: cameo.models.universal aerobic: bool If True, it will set `model.reactions.EX_o2_e.lower_bound` to 0. Returns ------- dict ([Host, Model] -> PredictedPathways) """ max_predictions = 8 timeout = 3 * 60 pathways = dict() product = self.__translate_product_to_universal_reactions_model_metabolite( product, database) for host in hosts: logging.debug('Processing host {}'.format(host.name)) if isinstance(host, Model): host = Host(name='UNKNOWN_HOST', models=[host]) for model in list(host.models): with TimeMachine() as tm: if not aerobic and "EX_o2_e" in model.reactions: model.reactions.EX_o2_e.change_bounds(lb=0, time_machin=tm) identifier = searching() logging.debug('Processing model {} for host {}'.format( model.id, host.name)) notice( 'Predicting pathways for product %s in %s (using model %s).' % (product.name, host, model.id)) logging.debug('Predicting pathways for model {}'.format( model.id)) pathway_predictor = pathway_prediction.PathwayPredictor( model, universal_model=database, compartment_regexp=re.compile(".*_c$")) if self.debug: max_predictions = 2 timeout = 60 predicted_pathways = pathway_predictor.run( product, max_predictions=max_predictions, timeout=timeout, silent=True) stop_loader(identifier) pathways[(host, model)] = predicted_pathways self.__display_pathways_information( predicted_pathways, host, model) return pathways