def degrees_of_freedom(model): """ Return the degrees of freedom, i.e. number of "free variables". Parameters ---------- model : cobra.Model The metabolic model under investigation. Notes: ------ This specifically refers to the dimensionality of the right nullspace of the S matrix, as dim(Null(S)) corresponds directly to the number of free variables in the system [1]_. The forumla used calculates this using the rank-nullity theorem [2]_. References: ----------- .. [1] Fukuda, K. & Terlaky, T. Criss-cross methods: A fresh view on pivot algorithms. Mathematical Programming 79, 369-395 (1997). .. [2] Alama, J. The Rank+Nullity Theorem. Formalized Mathematics 15, (2007). """ s_matrix, _, _ = con_helpers.stoichiometry_matrix( model.metabolites, model.reactions ) return s_matrix.shape[1] - matrix_rank(model)
def degrees_of_freedom(model): """ Return the degrees of freedom, i.e., number of "free variables". Parameters ---------- model : cobra.Model The metabolic model under investigation. Notes ----- This specifically refers to the dimensionality of the (right) null space of the stoichiometric matrix, as dim(Null(S)) corresponds directly to the number of free variables in the system [1]_. The formula used calculates this using the rank-nullity theorem [2]_. References ---------- .. [1] Fukuda, K. & Terlaky, T. Criss-cross methods: A fresh view on pivot algorithms. Mathematical Programming 79, 369-395 (1997). .. [2] Alama, J. The Rank+Nullity Theorem. Formalized Mathematics 15, (2007). """ s_matrix, _, _ = con_helpers.stoichiometry_matrix( model.metabolites, model.reactions ) return s_matrix.shape[1] - matrix_rank(model)
def absolute_extreme_coefficient_ratio(model): """Return the absolute max and absolute non-zero min coefficients.""" # S-Matrix with absolute values: s_matrix, _, _ = con_helpers.stoichiometry_matrix(model.metabolites, model.reactions) abs_matrix = np.absolute(s_matrix) absolute_max_coef = np.amax(abs_matrix) absolute_non_zero_min_coef = abs_matrix[abs_matrix > 0].min() return (absolute_max_coef, absolute_non_zero_min_coef)
def matrix_rank(model): """ Return the rank of the model's stoichiometric matrix. Parameters ---------- model : cobra.Model The metabolic model under investigation. """ s_matrix, _, _ = con_helpers.stoichiometry_matrix(model.metabolites, model.reactions) return con_helpers.rank(s_matrix)
def absolute_extreme_coefficient_ratio(model): """ Return the maximum and minimum absolute, non-zero coefficients. Parameters ---------- model : cobra.Model The metabolic model under investigation. """ s_matrix, _, _ = con_helpers.stoichiometry_matrix(model.metabolites, model.reactions) abs_matrix = np.abs(s_matrix) return abs_matrix.max(), abs_matrix[abs_matrix > 0].min()
def matrix_rank(model): """ Return the rank of the model's stoichiometric matrix. Parameters ---------- model : cobra.Model The metabolic model under investigation. """ s_matrix, _, _ = con_helpers.stoichiometry_matrix( model.metabolites, model.reactions ) return con_helpers.rank(s_matrix)
def number_steady_state_flux_solutions(model): """ Return the amount of steady-state flux solutions of this model. Parameters ---------- model : cobra.Model The metabolic model under investigation. """ s_matrix, _, _ = con_helpers.stoichiometry_matrix( model.metabolites, model.reactions ) n_matrix = con_helpers.nullspace(s_matrix) return n_matrix.shape[1]
def number_independent_conservation_relations(model): """ Return the amount of conserved metabolic pools. Parameters ---------- model : cobra.Model The metabolic model under investigation. """ s_matrix, _, _ = con_helpers.stoichiometry_matrix( model.metabolites, model.reactions ) ln_matrix = con_helpers.nullspace(s_matrix.T) return ln_matrix.shape[1]
def absolute_extreme_coefficient_ratio(model): """ Return the maximum and minimum absolute, non-zero coefficients. Parameters ---------- model : cobra.Model The metabolic model under investigation. """ s_matrix, _, _ = con_helpers.stoichiometry_matrix( model.metabolites, model.reactions ) abs_matrix = np.abs(s_matrix) return abs_matrix.max(), abs_matrix[abs_matrix > 0].min()
def number_independent_conservation_relations(model): """ Return the number of conserved metabolite pools. This number is given by the left null space of the stoichiometric matrix. Parameters ---------- model : cobra.Model The metabolic model under investigation. """ s_matrix, _, _ = con_helpers.stoichiometry_matrix(model.metabolites, model.reactions) left_ns = con_helpers.nullspace(s_matrix.T) return left_ns.shape[1] if len(left_ns) > 1 else 0
def number_independent_conservation_relations(model): """ Return the number of conserved metabolite pools. This number is given by the left null space of the stoichiometric matrix. Parameters ---------- model : cobra.Model The metabolic model under investigation. """ s_matrix, _, _ = con_helpers.stoichiometry_matrix( model.metabolites, model.reactions ) ln_matrix = con_helpers.nullspace(s_matrix.T) return ln_matrix.shape[1]
def find_inconsistent_min_stoichiometry(model, atol=1e-13): """ Detect inconsistent minimal net stoichiometries. Parameters ---------- model : cobra.Model The metabolic model under investigation. atol : float, optional Values below the absolute tolerance are treated as zero. Expected to be very small but larger than zero. Notes ----- See [1]_ section 3.3 for a complete description of the algorithm. References ---------- .. [1] Gevorgyan, A., M. G Poolman, and D. A Fell. "Detection of Stoichiometric Inconsistencies in Biomolecular Models." Bioinformatics 24, no. 19 (2008): 2245. """ if check_stoichiometric_consistency(model): return set() Model, Constraint, Variable, Objective = con_helpers.get_interface(model) unconserved_mets = find_unconserved_metabolites(model) LOGGER.info("model has %d unconserved metabolites", len(unconserved_mets)) internal_rxns = con_helpers.get_internals(model) internal_mets = set(met for rxn in internal_rxns for met in rxn.metabolites) get_id = attrgetter("id") reactions = sorted(internal_rxns, key=get_id) metabolites = sorted(internal_mets, key=get_id) stoich, met_index, rxn_index = con_helpers.stoichiometry_matrix( metabolites, reactions) left_ns = con_helpers.nullspace(stoich.T) # deal with numerical instabilities left_ns[np.abs(left_ns) < atol] = 0.0 LOGGER.info("nullspace has dimension %d", left_ns.shape[1]) inc_minimal = set() (problem, indicators) = con_helpers.create_milp_problem(left_ns, metabolites, Model, Variable, Constraint, Objective) LOGGER.debug(str(problem)) cuts = list() for met in unconserved_mets: row = met_index[met] if (left_ns[row] == 0.0).all(): LOGGER.debug("%s: singleton minimal unconservable set", met.id) # singleton set! inc_minimal.add((met, )) continue # expect a positive mass for the unconserved metabolite problem.variables[met.id].lb = 1e-3 status = problem.optimize() while status == "optimal": LOGGER.debug("%s: status %s", met.id, status) LOGGER.debug("sum of all primal values: %f", sum(problem.primal_values.values())) LOGGER.debug("sum of binary indicators: %f", sum(var.primal for var in indicators)) solution = [ model.metabolites.get_by_id(var.name[2:]) for var in indicators if var.primal > 0.2 ] LOGGER.debug("%s: set size %d", met.id, len(solution)) inc_minimal.add(tuple(solution)) if len(solution) == 1: break cuts.append( con_helpers.add_cut(problem, indicators, len(solution) - 1, Constraint)) status = problem.optimize() LOGGER.debug("%s: last status %s", met.id, status) # reset problem.variables[met.id].lb = 0.0 problem.remove(cuts) cuts.clear() return inc_minimal
def matrix_rank(model): """Return the rank of the model's stoichiometric matrix.""" s_matrix, _, _ = con_helpers.stoichiometry_matrix(model.metabolites, model.reactions) return con_helpers.rank(s_matrix)
def number_steady_state_flux_solutions(model): """Return the amount of steady-state flux solutions of this model.""" s_matrix, _, _ = con_helpers.stoichiometry_matrix(model.metabolites, model.reactions) n_matrix = con_helpers.nullspace_basis(s_matrix) return n_matrix.shape[1]
def number_independent_conservation_relations(model): """Return the amount of conserved metabolic pools.""" s_matrix, _, _ = con_helpers.stoichiometry_matrix(model.metabolites, model.reactions) ln_matrix = con_helpers.nullspace_basis(s_matrix.T) return ln_matrix.shape[1]