def gpr_eflux(model, gene_exp, scale_rxn, scale_value, constraints=None, parsimonious=False): max_exp = max(gene_exp.values()) bounds = {} for r_id, (lb, ub) in model.bounds.items(): if r_id.startswith('u_'): gene = r_id[len('u_'):] val = gene_exp[gene] / max_exp if gene in gene_exp else 1 bounds[r_id] = (0, val) else: ub2 = 1 if ub is None or ub > 0 else 0 bounds[r_id] = (0, ub2) if constraints: constraints = model.convert_constraints(constraints) for r_id, x in constraints.items(): ub = x[1] if isinstance(x, tuple) else x ub2 = 1 if ub is None or ub > 0 else 0 bounds[r_id] = (0, ub2) if parsimonious: sol = pFBA(model, constraints=bounds) else: sol = FBA(model, constraints=bounds) sol.values_converted = model.convert_fluxes(sol.values) k = abs(scale_value / sol.values_converted[scale_rxn]) for r_id, val in sol.values_converted.items(): sol.values_converted[r_id] = val * k return sol
def gpr_GIMME(model, gene_exp, cutoff=25, growth_frac=0.9, constraints=None, parsimonious=False): threshold = percentile(gene_exp.values(), cutoff) coeffs = { 'u_' + gene: threshold - val for gene, val in gene_exp.items() if val < threshold } if constraints: constraints = model.convert_constraints(constraints) else: constraints = {} pre_solution = FBA(model, constraints=constraints) biomass = model.detect_biomass_reaction() constraints[biomass] = (growth_frac * pre_solution.values[biomass], None) if parsimonious: solution = pFBA(model, objective=coeffs, minimize=True, constraints=constraints) else: solution = FBA(model, objective=coeffs, minimize=True, constraints=constraints) solution.values_converted = model.convert_fluxes(solution.values) return solution
def gene_pFBA(model, objective=None, minimize=False, constraints=None, solver=None): reactions = [r_id for r_id in model.reactions if r_id.startswith('u_')] if constraints: constraints = model.convert_constraints(constraints) sol = pFBA(model, objective, minimize, constraints, reactions, solver) sol.values_converted = model.convert_fluxes(sol.values) return sol
def testRun(self): model = load_sbml_model(SMALL_TEST_MODEL, kind=GPR_CONSTRAINED) fix_bigg_model(model) solution1 = pFBA(model) solution2 = FBA(model) self.assertEqual(solution1.status, Status.OPTIMAL) self.assertEqual(solution2.status, Status.OPTIMAL) growth1 = solution1.values[model.detect_biomass_reaction()] growth2 = solution2.values[model.detect_biomass_reaction()] self.assertAlmostEqual(growth1, growth2, places=4) norm1 = sum([abs(solution1.values[r_id]) for r_id in model.reactions]) norm2 = sum([abs(solution2.values[r_id]) for r_id in model.reactions]) self.assertLessEqual(norm1, norm2 + 1e-6)
def gpr_GIMME(model, gene_exp, cutoff=25, growth_frac=0.9, constraints=None, parsimonious=False): threshold = percentile(gene_exp.values(), cutoff) coeffs = {'u_' + gene: threshold-val for gene, val in gene_exp.items() if val < threshold} if constraints: constraints = model.convert_constraints(constraints) else: constraints = {} pre_solution = FBA(model, constraints=constraints) biomass = model.detect_biomass_reaction() constraints[biomass] = (growth_frac * pre_solution.values[biomass], None) if parsimonious: solution = pFBA(model, objective=coeffs, minimize=True, constraints=constraints) else: solution = FBA(model, objective=coeffs, minimize=True, constraints=constraints) solution.values_converted = model.convert_fluxes(solution.values) return solution
def arFBA(model, reference_constraints, perturbed_constraints, weights): """ Run an allosteric regulated FBA (arFBA) simulation arguments: - model: an instance of AllostericModel (generated by the SBML loader) - reference_constraints: dict of reaction id to (lower bound, upper bound) - perturbed_constraints: dict of reaction id to (lower bound, upper bound) - weights: dict of (metabolite id, reaction id) to float (weighting factor for each allosteric interaction) returns: - flux distribution: dict of reaction id to float """ TOL = 1e-6 M = 1e3 solution_ref = pFBA(model, constraints=reference_constraints) v0 = solution_ref.values t0 = compute_turnover(model, v0) model_irrev, mapping = build_perturbed_model(model, perturbed_constraints) solver = solver_instance() solver.build_problem(model_irrev) m_r_lookup = model_irrev.metabolite_reaction_lookup_table() reg_m_r_lookup = model_irrev.metabolite_reaction_regulatory_lookup_table() regulators = [m_id for m_id, targets in reg_m_r_lookup.items() if len(targets) > 0] for m_id in regulators: solver.add_variable('t_' + m_id, 0, None, persistent=False, update_problem=False) for fwd_id, bwd_id in mapping.values(): solver.add_variable('y_' + fwd_id, vartype=VarType.BINARY, persistent=False, update_problem=False) solver.add_variable('y_' + bwd_id, vartype=VarType.BINARY, persistent=False, update_problem=False) for (m_id, r_id), kind in model.regulation.items(): if v0[r_id] > TOL and t0[m_id] > TOL: diff_pos = 'd+_{}_{}'.format(m_id, r_id) diff_neg = 'd-_{}_{}'.format(m_id, r_id) solver.add_variable(diff_pos, 0, None, persistent=False, update_problem=False) solver.add_variable(diff_neg, 0, None, persistent=False, update_problem=False) solver.update() for m_id in regulators: lhs = {r_id: coeff for r_id, coeff in m_r_lookup[m_id].items() if coeff > 0} lhs['t_' + m_id] = -1 solver.add_constraint('ct_' + m_id, lhs.items(), persistent=False, update_problem=False) for r_id, (fwd_id, bwd_id) in mapping.items(): solver.add_constraint('c_' + fwd_id, {fwd_id: 1, 'y_' + fwd_id: -M}.items(), '<', 0, persistent=False, update_problem=False) solver.add_constraint('c_' + bwd_id, {bwd_id: 1, 'y_' + bwd_id: -M}.items(), '<', 0, persistent=False, update_problem=False) solver.add_constraint('rev_' + r_id, {'y_' + fwd_id: 1, 'y_' + bwd_id: 1}.items(), '<', 1, persistent=False, update_problem=False) for (m_id, r_id), kind in model.regulation.items(): if v0[r_id] > TOL and t0[m_id] > TOL: diff_pos = 'd+_{}_{}'.format(m_id, r_id) diff_neg = 'd-_{}_{}'.format(m_id, r_id) if r_id in mapping: fwd_id, bwd_id = mapping[r_id] if kind == 1: lhs = {diff_pos: 1, fwd_id: -1/v0[r_id], bwd_id: -1/v0[r_id], 't_' + m_id: 1/t0[m_id]} solver.add_constraint('c' + diff_pos, lhs.items(), '>', 0, persistent=False, update_problem=False) lhs = {diff_neg: 1, fwd_id: 1/v0[r_id], bwd_id: 1/v0[r_id], 't_' + m_id: -1/t0[m_id]} solver.add_constraint('c' + diff_neg, lhs.items(), '>', 0, persistent=False, update_problem=False) else: lhs = {diff_pos: 1, fwd_id: -1/v0[r_id], bwd_id: -1/v0[r_id], 't_' + m_id: -1/t0[m_id]} solver.add_constraint('c' + diff_pos, lhs.items(), '>', -2, persistent=False, update_problem=False) lhs = {diff_neg: 1, fwd_id: 1/v0[r_id], bwd_id: 1/v0[r_id], 't_' + m_id: 1/t0[m_id]} solver.add_constraint('c' + diff_neg, lhs.items(), '>', 2, persistent=False, update_problem=False) else: if kind == 1: lhs = {diff_pos: 1, r_id: -1/v0[r_id], 't_' + m_id: 1/t0[m_id]} solver.add_constraint('c' + diff_pos, lhs.items(), '>', 0, persistent=False, update_problem=False) lhs = {diff_neg: 1, r_id: 1/v0[r_id], 't_' + m_id: -1/t0[m_id]} solver.add_constraint('c' + diff_neg, lhs.items(), '>', 0, persistent=False, update_problem=False) else: lhs = {diff_pos: 1, r_id: -1/v0[r_id], 't_' + m_id: -1/t0[m_id]} solver.add_constraint('c' + diff_pos, lhs.items(), '>', -2, persistent=False, update_problem=False) lhs = {diff_neg: 1, r_id: 1/v0[r_id], 't_' + m_id: 1/t0[m_id]} solver.add_constraint('c' + diff_neg, lhs.items(), '>', 2, persistent=False, update_problem=False) solver.update() objective = {r_id: -1 for r_id in model_irrev.reactions} for (m_id, r_id) in model.regulation.keys(): if (m_id, r_id) in weights and v0[r_id] > TOL and t0[m_id] > TOL: diff_pos = 'd+_{}_{}'.format(m_id, r_id) diff_neg = 'd-_{}_{}'.format(m_id, r_id) objective[diff_pos] = -weights[(m_id, r_id)] objective[diff_neg] = -weights[(m_id, r_id)] solution = solver.solve_lp(objective) if solution.status == Status.OPTIMAL: v = merge_fluxes(model, mapping, solution.values) else: v = None return v