def _repr_html_(self): return """ <table> <tr> <td><strong>Reaction identifier</strong></td><td>{id}</td> </tr><tr> <td><strong>Name</strong></td><td>{name}</td> </tr><tr> <td><strong>Memory address</strong></td> <td>{address}</td> </tr><tr> <td><strong>Stoichiometry</strong></td> <td> <p style='text-align:right'>{stoich_id}</p> <p style='text-align:right'>{stoich_name}</p> </td> </tr><tr> <td><strong>GPR</strong></td><td>{gpr}</td> </tr><tr> <td><strong>Lower bound</strong></td><td>{lb}</td> </tr><tr> <td><strong>Upper bound</strong></td><td>{ub}</td> </tr> </table> """.format( id=format_long_string(self.id, 100), name=format_long_string(self.name, 100), address="0x0%x" % id(self), stoich_id=format_long_string(self.build_reaction_string(), 200), stoich_name=format_long_string(self.build_reaction_string(True), 200), gpr=format_long_string(self.gene_reaction_rule, 100), lb=self.lower_bound, ub=self.upper_bound, )
def _repr_html_(self): return """ <table> <tr> <td><strong>Metabolite identifier</strong></td><td>{id}</td> </tr><tr> <td><strong>Name</strong></td><td>{name}</td> </tr><tr> <td><strong>Memory address</strong></td> <td>{address}</td> </tr><tr> <td><strong>Formula</strong></td><td>{formula}</td> </tr><tr> <td><strong>Compartment</strong></td><td>{compartment}</td> </tr><tr> <td><strong>In {n_reactions} reaction(s)</strong></td><td> {reactions}</td> </tr> </table>""".format(id=self.id, name=format_long_string(self.name), formula=self.formula, address='0x0%x' % id(self), compartment=self.compartment, n_reactions=len(self.reactions), reactions=format_long_string( ', '.join(r.id for r in self.reactions), 200))
def _repr_html_(self): return """ <table> <tr> <td><strong>Metabolite identifier</strong></td><td>{id}</td> </tr><tr> <td><strong>Name</strong></td><td>{name}</td> </tr><tr> <td><strong>Memory address</strong></td> <td>{address}</td> </tr><tr> <td><strong>Formula</strong></td><td>{formula}</td> </tr><tr> <td><strong>Compartment</strong></td><td>{compartment}</td> </tr><tr> <td><strong>In {n_reactions} reaction(s)</strong></td><td> {reactions}</td> </tr> </table>""".format(id=self.id, name=format_long_string(self.name), formula=self.formula, address='0x0%x' % id(self), compartment=self.compartment, n_reactions=len(self.reactions), reactions=format_long_string( ', '.join(r.id for r in self.reactions), 200))
def _repr_html_(self): return """ <table> <tr> <td><strong>Reaction identifier</strong></td><td>{id}</td> </tr><tr> <td><strong>Name</strong></td><td>{name}</td> </tr><tr> <td><strong>Memory address</strong></td> <td>{address}</td> </tr><tr> <td><strong>Stoichiometry</strong></td> <td> <p style='text-align:right'>{stoich_id}</p> <p style='text-align:right'>{stoich_name}</p> </td> </tr><tr> <td><strong>GPR</strong></td><td>{gpr}</td> </tr><tr> <td><strong>Lower bound</strong></td><td>{lb}</td> </tr><tr> <td><strong>Upper bound</strong></td><td>{ub}</td> </tr> </table> """.format(id=format_long_string(self.id, 100), name=format_long_string(self.name, 100), address='0x0%x' % id(self), stoich_id=format_long_string( self.build_reaction_string(), 200), stoich_name=format_long_string( self.build_reaction_string(True), 200), gpr=format_long_string(self.gene_reaction_rule, 100), lb=self.lower_bound, ub=self.upper_bound)
def _repr_html_(self): return """ <table> <tr> <td><strong>Name</strong></td> <td>{name}</td> </tr><tr> <td><strong>Memory address</strong></td> <td>{address}</td> </tr><tr> <td><strong>Number of metabolites</strong></td> <td>{num_metabolites}</td> </tr><tr> <td><strong>Number of reactions</strong></td> <td>{num_reactions}</td> </tr><tr> <td><strong>Number of groups</strong></td> <td>{num_groups}</td> </tr><tr> <td><strong>Objective expression</strong></td> <td>{objective}</td> </tr><tr> <td><strong>Compartments</strong></td> <td>{compartments}</td> </tr> </table>""".format( name=self.id, address="0x0%x" % id(self), num_metabolites=len(self.metabolites), num_reactions=len(self.reactions), num_groups=len(self.groups), objective=format_long_string(str(self.objective.expression), 100), compartments=", ".join(v if v else k for k, v in iteritems(self.compartments)), )
def _repr_html_(self): return """ <table> <tr> <td><strong>Name</strong></td> <td>{name}</td> </tr><tr> <td><strong>Memory address</strong></td> <td>{address}</td> </tr><tr> <td><strong>Number of metabolites</strong></td> <td>{num_metabolites}</td> </tr><tr> <td><strong>Number of reactions</strong></td> <td>{num_reactions}</td> </tr><tr> <td><strong>Objective expression</strong></td> <td>{objective}</td> </tr><tr> <td><strong>Compartments</strong></td> <td>{compartments}</td> </tr> </table>""".format( name=self.id, address='0x0%x' % id(self), num_metabolites=len(self.metabolites), num_reactions=len(self.reactions), objective=format_long_string(str(self.objective.expression), 100), compartments=", ".join( v if v else k for k, v in iteritems(self.compartments) ))
def _repr_html_(self): return """ <table> <tr> <td><strong>Gene identifier</strong></td><td>{id}</td> </tr><tr> <td><strong>Name</strong></td><td>{name}</td> </tr><tr> <td><strong>Memory address</strong></td> <td>{address}</td> </tr><tr> <td><strong>Functional</strong></td><td>{functional}</td> </tr><tr> <td><strong>In {n_reactions} reaction(s)</strong></td><td> {reactions}</td> </tr> </table>""".format( id=self.id, name=self.name, functional=self.functional, address="0x0%x" % id(self), n_reactions=len(self.reactions), reactions=format_long_string( ", ".join(r.id for r in self.reactions), 200), )
def _repr_html_(self): return """ <table> <tr> <td><strong>Gene identifier</strong></td><td>{id}</td> </tr><tr> <td><strong>Name</strong></td><td>{name}</td> </tr><tr> <td><strong>Memory address</strong></td> <td>{address}</td> </tr><tr> <td><strong>Functional</strong></td><td>{functional}</td> </tr><tr> <td><strong>In {n_reactions} reaction(s)</strong></td><td> {reactions}</td> </tr> </table>""".format(id=self.id, name=self.name, functional=self.functional, address='0x0%x' % id(self), n_reactions=len(self.reactions), reactions=format_long_string( ', '.join(r.id for r in self.reactions), 200))
def metabolite_summary(met, solution=None, threshold=0.01, fva=False, floatfmt='.3g'): """Print a summary of the reactions which produce and consume this metabolite solution : cobra.core.Solution A previously solved model solution to use for generating the summary. If none provided (default), the summary method will resolve the model. Note that the solution object must match the model, i.e., changes to the model such as changed bounds, added or removed reactions are not taken into account by this method. threshold : float a percentage value of the maximum flux below which to ignore reaction fluxes fva : float (0->1), or None Whether or not to include flux variability analysis in the output. If given, fva should be a float between 0 and 1, representing the fraction of the optimum objective to be searched. floatfmt : string format method for floats, passed to tabulate. Default is '.3g'. """ if solution is None: met.model.slim_optimize(error_value=None) solution = get_solution(met.model, reactions=met.reactions) rxn_id = list() flux = list() reaction = list() for rxn in met.reactions: rxn_id.append(format_long_string(rxn.id, 10)) flux.append(solution.fluxes[rxn.id] * rxn.metabolites[met]) reaction.append(format_long_string(rxn.reaction, 40 if fva else 50)) flux_summary = pd.DataFrame(data={ "id": rxn_id, "flux": flux, "reaction": reaction}) if fva: fva_results = flux_variability_analysis( met.model, met.reactions, fraction_of_optimum=fva) flux_summary.index = flux_summary["id"] flux_summary["maximum"] = zeros(len(rxn_id)) flux_summary["minimum"] = zeros(len(rxn_id)) for rid, rxn in zip(rxn_id, met.reactions): imax = rxn.metabolites[met] * fva_results.loc[rxn.id, "maximum"] imin = rxn.metabolites[met] * fva_results.loc[rxn.id, "minimum"] flux_summary.loc[rid, "fmax"] = (imax if abs(imin) <= abs(imax) else imin) flux_summary.loc[rid, "fmin"] = (imin if abs(imin) <= abs(imax) else imax) assert flux_summary.flux.sum() < 1E-6, "Error in flux balance" flux_summary = _process_flux_dataframe(flux_summary, fva, threshold, floatfmt) flux_summary['percent'] = 0 total_flux = flux_summary[flux_summary.is_input].flux.sum() flux_summary.loc[flux_summary.is_input, 'percent'] = \ flux_summary.loc[flux_summary.is_input, 'flux'] / total_flux flux_summary.loc[~flux_summary.is_input, 'percent'] = \ flux_summary.loc[~flux_summary.is_input, 'flux'] / total_flux flux_summary['percent'] = flux_summary.percent.apply( lambda x: '{:.0%}'.format(x)) if fva: flux_table = tabulate( flux_summary.loc[:, ['percent', 'flux', 'fva_fmt', 'id', 'reaction']].values, floatfmt=floatfmt, headers=['%', 'FLUX', 'RANGE', 'RXN ID', 'REACTION']).split('\n') else: flux_table = tabulate( flux_summary.loc[:, ['percent', 'flux', 'id', 'reaction']].values, floatfmt=floatfmt, headers=['%', 'FLUX', 'RXN ID', 'REACTION'] ).split('\n') flux_table_head = flux_table[:2] met_tag = "{0} ({1})".format(format_long_string(met.name, 45), format_long_string(met.id, 10)) head = "PRODUCING REACTIONS -- " + met_tag print_(head) print_("-" * len(head)) print_('\n'.join(flux_table_head)) print_('\n'.join( pd.np.array(flux_table[2:])[flux_summary.is_input.values])) print_() print_("CONSUMING REACTIONS -- " + met_tag) print_("-" * len(head)) print_('\n'.join(flux_table_head)) print_('\n'.join( pd.np.array(flux_table[2:])[~flux_summary.is_input.values]))
def model_summary(model, solution=None, threshold=0.01, fva=None, floatfmt='.3g'): """Print a summary of the input and output fluxes of the model. solution : cobra.core.Solution A previously solved model solution to use for generating the summary. If none provided (default), the summary method will resolve the model. Note that the solution object must match the model, i.e., changes to the model such as changed bounds, added or removed reactions are not taken into account by this method. threshold : float a percentage value of the maximum flux below which to ignore reaction fluxes fva : int or None Whether or not to calculate and report flux variability in the output summary floatfmt : string format method for floats, passed to tabulate. Default is '.3g'. """ objective_reactions = linear_reaction_coefficients(model) boundary_reactions = model.exchanges summary_rxns = set(objective_reactions.keys()).union(boundary_reactions) if solution is None: model.slim_optimize(error_value=None) solution = get_solution(model, reactions=summary_rxns) # Create a dataframe of objective fluxes obj_fluxes = pd.DataFrame({key: solution.fluxes[key.id] * value for key, value in iteritems(objective_reactions)}, index=['flux']).T obj_fluxes['id'] = obj_fluxes.apply( lambda x: format_long_string(x.name.id, 15), 1) # Build a dictionary of metabolite production from the boundary reactions metabolite_fluxes = {} for rxn in boundary_reactions: for met, stoich in iteritems(rxn.metabolites): metabolite_fluxes[met] = { 'id': format_long_string(met.id, 15), 'flux': stoich * solution.fluxes[rxn.id]} # Calculate FVA results if requested if fva: fva_results = flux_variability_analysis( model, reaction_list=boundary_reactions, fraction_of_optimum=fva) for rxn in boundary_reactions: for met, stoich in iteritems(rxn.metabolites): imin = stoich * fva_results.loc[rxn.id]['minimum'] imax = stoich * fva_results.loc[rxn.id]['maximum'] # Correct 'max' and 'min' for negative values metabolite_fluxes[met].update({ 'fmin': imin if abs(imin) <= abs(imax) else imax, 'fmax': imax if abs(imin) <= abs(imax) else imin, }) # Generate a dataframe of boundary fluxes metabolite_fluxes = pd.DataFrame(metabolite_fluxes).T metabolite_fluxes = _process_flux_dataframe( metabolite_fluxes, fva, threshold, floatfmt) # Begin building string output table def get_str_table(species_df, fva=False): """Formats a string table for each column""" if not fva: return tabulate(species_df.loc[:, ['id', 'flux']].values, floatfmt=floatfmt, tablefmt='plain').split('\n') else: return tabulate( species_df.loc[:, ['id', 'flux', 'fva_fmt']].values, floatfmt=floatfmt, tablefmt='simple', headers=['id', 'Flux', 'Range']).split('\n') in_table = get_str_table( metabolite_fluxes[metabolite_fluxes.is_input], fva=fva) out_table = get_str_table( metabolite_fluxes[~metabolite_fluxes.is_input], fva=fva) obj_table = get_str_table(obj_fluxes, fva=False) # Print nested output table print_(tabulate( [entries for entries in zip_longest(in_table, out_table, obj_table)], headers=['IN FLUXES', 'OUT FLUXES', 'OBJECTIVES'], tablefmt='simple'))
def metabolite_summary(met, solution=None, threshold=0.01, fva=False, names=False, floatfmt='.3g'): """ Print a summary of the production and consumption fluxes. This method requires the model for which this metabolite is a part to be solved. Parameters ---------- solution : cobra.Solution, optional A previously solved model solution to use for generating the summary. If none provided (default), the summary method will resolve the model. Note that the solution object must match the model, i.e., changes to the model such as changed bounds, added or removed reactions are not taken into account by this method. threshold : float, optional Threshold below which fluxes are not reported. fva : pandas.DataFrame, float or None, optional Whether or not to include flux variability analysis in the output. If given, fva should either be a previous FVA solution matching the model or a float between 0 and 1 representing the fraction of the optimum objective to be searched. names : bool, optional Emit reaction and metabolite names rather than identifiers (default False). floatfmt : string, optional Format string for floats (default '.3g'). """ if names: emit = attrgetter('name') else: emit = attrgetter('id') if solution is None: met.model.slim_optimize(error_value=None) solution = get_solution(met.model, reactions=met.reactions) rxns = sorted(met.reactions, key=attrgetter("id")) rxn_id = list() rxn_name = list() flux = list() reaction = list() for rxn in rxns: rxn_id.append(rxn.id) rxn_name.append(format_long_string(emit(rxn), 10)) flux.append(solution[rxn.id] * rxn.metabolites[met]) txt = rxn.build_reaction_string(use_metabolite_names=names) reaction.append(format_long_string(txt, 40 if fva is not None else 50)) flux_summary = pd.DataFrame( { "id": rxn_name, "flux": flux, "reaction": reaction }, index=rxn_id) if fva is not None: if hasattr(fva, 'columns'): fva_results = fva else: fva_results = flux_variability_analysis(met.model, list(met.reactions), fraction_of_optimum=fva) flux_summary["maximum"] = zeros(len(rxn_id), dtype=float) flux_summary["minimum"] = zeros(len(rxn_id), dtype=float) for rxn in rxns: fmax = rxn.metabolites[met] * fva_results.at[rxn.id, "maximum"] fmin = rxn.metabolites[met] * fva_results.at[rxn.id, "minimum"] if abs(fmin) <= abs(fmax): flux_summary.at[rxn.id, "fmax"] = fmax flux_summary.at[rxn.id, "fmin"] = fmin else: # Reverse fluxes. flux_summary.at[rxn.id, "fmax"] = fmin flux_summary.at[rxn.id, "fmin"] = fmax assert flux_summary["flux"].sum() < 1E-6, "Error in flux balance" flux_summary = _process_flux_dataframe(flux_summary, fva, threshold, floatfmt) flux_summary['percent'] = 0 total_flux = flux_summary.loc[flux_summary.is_input, "flux"].sum() flux_summary.loc[flux_summary.is_input, 'percent'] = \ flux_summary.loc[flux_summary.is_input, 'flux'] / total_flux flux_summary.loc[~flux_summary.is_input, 'percent'] = \ flux_summary.loc[~flux_summary.is_input, 'flux'] / total_flux flux_summary['percent'] = flux_summary.percent.apply( lambda x: '{:.0%}'.format(x)) if fva is not None: flux_table = tabulate( flux_summary. loc[:, ['percent', 'flux', 'fva_fmt', 'id', 'reaction']].values, floatfmt=floatfmt, headers=['%', 'FLUX', 'RANGE', 'RXN ID', 'REACTION']).split('\n') else: flux_table = tabulate( flux_summary.loc[:, ['percent', 'flux', 'id', 'reaction']].values, floatfmt=floatfmt, headers=['%', 'FLUX', 'RXN ID', 'REACTION']).split('\n') flux_table_head = flux_table[:2] met_tag = "{0} ({1})".format(format_long_string(met.name, 45), format_long_string(met.id, 10)) head = "PRODUCING REACTIONS -- " + met_tag print_(head) print_("-" * len(head)) print_('\n'.join(flux_table_head)) print_('\n'.join( pd.np.array(flux_table[2:])[flux_summary.is_input.values])) print_() print_("CONSUMING REACTIONS -- " + met_tag) print_("-" * len(head)) print_('\n'.join(flux_table_head)) print_('\n'.join( pd.np.array(flux_table[2:])[~flux_summary.is_input.values]))
def model_summary(model, solution=None, threshold=0.01, fva=None, names=False, floatfmt='.3g'): """ Print a summary of the input and output fluxes of the model. Parameters ---------- solution: cobra.Solution, optional A previously solved model solution to use for generating the summary. If none provided (default), the summary method will resolve the model. Note that the solution object must match the model, i.e., changes to the model such as changed bounds, added or removed reactions are not taken into account by this method. threshold : float, optional Threshold below which fluxes are not reported. fva : pandas.DataFrame, float or None, optional Whether or not to include flux variability analysis in the output. If given, fva should either be a previous FVA solution matching the model or a float between 0 and 1 representing the fraction of the optimum objective to be searched. names : bool, optional Emit reaction and metabolite names rather than identifiers (default False). floatfmt : string, optional Format string for floats (default '.3g'). """ if names: emit = attrgetter('name') else: emit = attrgetter('id') objective_reactions = linear_reaction_coefficients(model) boundary_reactions = model.exchanges summary_rxns = set(objective_reactions.keys()).union(boundary_reactions) if solution is None: model.slim_optimize(error_value=None) solution = get_solution(model, reactions=summary_rxns) # Create a dataframe of objective fluxes obj_fluxes = pd.DataFrame( { key: solution[key.id] * value for key, value in iteritems(objective_reactions) }, index=['flux']).T obj_fluxes['id'] = obj_fluxes.apply( lambda x: format_long_string(x.name.id, 15), 1) # Build a dictionary of metabolite production from the boundary reactions metabolites = {m for r in boundary_reactions for m in r.metabolites} index = sorted(metabolites, key=attrgetter('id')) metabolite_fluxes = pd.DataFrame( { 'id': [format_long_string(emit(m), 15) for m in index], 'flux': zeros(len(index), dtype=float) }, index=[m.id for m in index]) for rxn in boundary_reactions: for met, stoich in iteritems(rxn.metabolites): metabolite_fluxes.at[met.id, 'flux'] += stoich * solution[rxn.id] # Calculate FVA results if requested if fva is not None: if len(index) != len(boundary_reactions): LOGGER.warning( "There exists more than one boundary reaction per metabolite. " "Please be careful when evaluating flux ranges.") metabolite_fluxes['fmin'] = zeros(len(index), dtype=float) metabolite_fluxes['fmax'] = zeros(len(index), dtype=float) if hasattr(fva, 'columns'): fva_results = fva else: fva_results = flux_variability_analysis( model, reaction_list=boundary_reactions, fraction_of_optimum=fva) for rxn in boundary_reactions: for met, stoich in iteritems(rxn.metabolites): fmin = stoich * fva_results.at[rxn.id, 'minimum'] fmax = stoich * fva_results.at[rxn.id, 'maximum'] # Correct 'max' and 'min' for negative values if abs(fmin) <= abs(fmax): metabolite_fluxes.at[met.id, 'fmin'] += fmin metabolite_fluxes.at[met.id, 'fmax'] += fmax else: metabolite_fluxes.at[met.id, 'fmin'] += fmax metabolite_fluxes.at[met.id, 'fmax'] += fmin # Generate a dataframe of boundary fluxes metabolite_fluxes = _process_flux_dataframe(metabolite_fluxes, fva, threshold, floatfmt) # Begin building string output table def get_str_table(species_df, fva=False): """Formats a string table for each column""" if fva: return tabulate(species_df.loc[:, ['id', 'flux', 'fva_fmt']].values, floatfmt=floatfmt, tablefmt='simple', headers=['id', 'Flux', 'Range']).split('\n') else: return tabulate(species_df.loc[:, ['id', 'flux']].values, floatfmt=floatfmt, tablefmt='plain').split('\n') in_table = get_str_table(metabolite_fluxes[metabolite_fluxes['is_input']], fva=fva is not None) out_table = get_str_table( metabolite_fluxes[~metabolite_fluxes['is_input']], fva=fva is not None) obj_table = get_str_table(obj_fluxes, fva=False) # Print nested output table print_( tabulate([ entries for entries in zip_longest(in_table, out_table, obj_table) ], headers=['IN FLUXES', 'OUT FLUXES', 'OBJECTIVES'], tablefmt='simple'))
def metabolite_summary(met, solution=None, threshold=0.01, fva=False, names=False, floatfmt='.3g'): """ Print a summary of the production and consumption fluxes. This method requires the model for which this metabolite is a part to be solved. Parameters ---------- solution : cobra.Solution, optional A previously solved model solution to use for generating the summary. If none provided (default), the summary method will resolve the model. Note that the solution object must match the model, i.e., changes to the model such as changed bounds, added or removed reactions are not taken into account by this method. threshold : float, optional Threshold below which fluxes are not reported. fva : pandas.DataFrame, float or None, optional Whether or not to include flux variability analysis in the output. If given, fva should either be a previous FVA solution matching the model or a float between 0 and 1 representing the fraction of the optimum objective to be searched. names : bool, optional Emit reaction and metabolite names rather than identifiers (default False). floatfmt : string, optional Format string for floats (default '.3g'). """ if names: emit = attrgetter('name') else: emit = attrgetter('id') if solution is None: met.model.slim_optimize(error_value=None) solution = get_solution(met.model, reactions=met.reactions) rxns = sorted(met.reactions, key=attrgetter("id")) rxn_id = list() rxn_name = list() flux = list() reaction = list() for rxn in rxns: rxn_id.append(rxn.id) rxn_name.append(format_long_string(emit(rxn), 10)) flux.append(solution[rxn.id] * rxn.metabolites[met]) txt = rxn.build_reaction_string(use_metabolite_names=names) reaction.append(format_long_string(txt, 40 if fva is not None else 50)) flux_summary = pd.DataFrame({ "id": rxn_name, "flux": flux, "reaction": reaction }, index=rxn_id) if fva is not None: if hasattr(fva, 'columns'): fva_results = fva else: fva_results = flux_variability_analysis( met.model, list(met.reactions), fraction_of_optimum=fva) flux_summary["maximum"] = zeros(len(rxn_id), dtype=float) flux_summary["minimum"] = zeros(len(rxn_id), dtype=float) for rxn in rxns: fmax = rxn.metabolites[met] * fva_results.at[rxn.id, "maximum"] fmin = rxn.metabolites[met] * fva_results.at[rxn.id, "minimum"] if abs(fmin) <= abs(fmax): flux_summary.at[rxn.id, "fmax"] = fmax flux_summary.at[rxn.id, "fmin"] = fmin else: # Reverse fluxes. flux_summary.at[rxn.id, "fmax"] = fmin flux_summary.at[rxn.id, "fmin"] = fmax assert flux_summary["flux"].sum() < 1E-6, "Error in flux balance" flux_summary = _process_flux_dataframe(flux_summary, fva, threshold, floatfmt) flux_summary['percent'] = 0 total_flux = flux_summary.loc[flux_summary.is_input, "flux"].sum() flux_summary.loc[flux_summary.is_input, 'percent'] = \ flux_summary.loc[flux_summary.is_input, 'flux'] / total_flux flux_summary.loc[~flux_summary.is_input, 'percent'] = \ flux_summary.loc[~flux_summary.is_input, 'flux'] / total_flux flux_summary['percent'] = flux_summary.percent.apply( lambda x: '{:.0%}'.format(x)) if fva is not None: flux_table = tabulate( flux_summary.loc[:, ['percent', 'flux', 'fva_fmt', 'id', 'reaction']].values, floatfmt=floatfmt, headers=['%', 'FLUX', 'RANGE', 'RXN ID', 'REACTION']).split('\n') else: flux_table = tabulate( flux_summary.loc[:, ['percent', 'flux', 'id', 'reaction']].values, floatfmt=floatfmt, headers=['%', 'FLUX', 'RXN ID', 'REACTION'] ).split('\n') flux_table_head = flux_table[:2] met_tag = "{0} ({1})".format(format_long_string(met.name, 45), format_long_string(met.id, 10)) head = "PRODUCING REACTIONS -- " + met_tag print_(head) print_("-" * len(head)) print_('\n'.join(flux_table_head)) print_('\n'.join( pd.np.array(flux_table[2:])[flux_summary.is_input.values])) print_() print_("CONSUMING REACTIONS -- " + met_tag) print_("-" * len(head)) print_('\n'.join(flux_table_head)) print_('\n'.join( pd.np.array(flux_table[2:])[~flux_summary.is_input.values]))
def model_summary(model, solution=None, threshold=0.01, fva=None, names=False, floatfmt='.3g'): """ Print a summary of the input and output fluxes of the model. Parameters ---------- solution: cobra.Solution, optional A previously solved model solution to use for generating the summary. If none provided (default), the summary method will resolve the model. Note that the solution object must match the model, i.e., changes to the model such as changed bounds, added or removed reactions are not taken into account by this method. threshold : float, optional Threshold below which fluxes are not reported. fva : pandas.DataFrame, float or None, optional Whether or not to include flux variability analysis in the output. If given, fva should either be a previous FVA solution matching the model or a float between 0 and 1 representing the fraction of the optimum objective to be searched. names : bool, optional Emit reaction and metabolite names rather than identifiers (default False). floatfmt : string, optional Format string for floats (default '.3g'). """ if names: emit = attrgetter('name') else: emit = attrgetter('id') objective_reactions = linear_reaction_coefficients(model) boundary_reactions = model.exchanges summary_rxns = set(objective_reactions.keys()).union(boundary_reactions) if solution is None: model.slim_optimize(error_value=None) solution = get_solution(model, reactions=summary_rxns) # Create a dataframe of objective fluxes obj_fluxes = pd.DataFrame({key: solution[key.id] * value for key, value in iteritems(objective_reactions)}, index=['flux']).T obj_fluxes['id'] = obj_fluxes.apply( lambda x: format_long_string(x.name.id, 15), 1) # Build a dictionary of metabolite production from the boundary reactions metabolites = {m for r in boundary_reactions for m in r.metabolites} index = sorted(metabolites, key=attrgetter('id')) metabolite_fluxes = pd.DataFrame({ 'id': [format_long_string(emit(m), 15) for m in index], 'flux': zeros(len(index), dtype=float) }, index=[m.id for m in index]) for rxn in boundary_reactions: for met, stoich in iteritems(rxn.metabolites): metabolite_fluxes.at[met.id, 'flux'] += stoich * solution[rxn.id] # Calculate FVA results if requested if fva is not None: if len(index) != len(boundary_reactions): LOGGER.warning( "There exists more than one boundary reaction per metabolite. " "Please be careful when evaluating flux ranges.") metabolite_fluxes['fmin'] = zeros(len(index), dtype=float) metabolite_fluxes['fmax'] = zeros(len(index), dtype=float) if hasattr(fva, 'columns'): fva_results = fva else: fva_results = flux_variability_analysis( model, reaction_list=boundary_reactions, fraction_of_optimum=fva) for rxn in boundary_reactions: for met, stoich in iteritems(rxn.metabolites): fmin = stoich * fva_results.at[rxn.id, 'minimum'] fmax = stoich * fva_results.at[rxn.id, 'maximum'] # Correct 'max' and 'min' for negative values if abs(fmin) <= abs(fmax): metabolite_fluxes.at[met.id, 'fmin'] += fmin metabolite_fluxes.at[met.id, 'fmax'] += fmax else: metabolite_fluxes.at[met.id, 'fmin'] += fmax metabolite_fluxes.at[met.id, 'fmax'] += fmin # Generate a dataframe of boundary fluxes metabolite_fluxes = _process_flux_dataframe( metabolite_fluxes, fva, threshold, floatfmt) # Begin building string output table def get_str_table(species_df, fva=False): """Formats a string table for each column""" if fva: return tabulate( species_df.loc[:, ['id', 'flux', 'fva_fmt']].values, floatfmt=floatfmt, tablefmt='simple', headers=['id', 'Flux', 'Range']).split('\n') else: return tabulate(species_df.loc[:, ['id', 'flux']].values, floatfmt=floatfmt, tablefmt='plain').split('\n') in_table = get_str_table( metabolite_fluxes[metabolite_fluxes['is_input']], fva=fva is not None) out_table = get_str_table( metabolite_fluxes[~metabolite_fluxes['is_input']], fva=fva is not None) obj_table = get_str_table(obj_fluxes, fva=False) # Print nested output table print_(tabulate( [entries for entries in zip_longest(in_table, out_table, obj_table)], headers=['IN FLUXES', 'OUT FLUXES', 'OBJECTIVES'], tablefmt='simple'))