def lp_sampler(model, n_samples=1000, weights=None, constraints=None, select_probability=0.01, futile_cycle_threshold=1e2, variation_threshold=1e-4, merge_keys=False, verbose=True): if not weights: variability = FVA(model, constraints=constraints) weights = {r_id: 1.0 / (ub - lb) for r_id, (lb, ub) in variability.items() if ub is not None and lb is not None and variation_threshold < (ub - lb) < futile_cycle_threshold} samples = [] solver = solver_instance(model) for i in range(n_samples): objective = {r_id: gauss(0, 1)*W for r_id, W in weights.items() if random() < select_probability} sol = pFBA(model, objective=objective, constraints=constraints, solver=solver) if sol.status == Status.OPTIMAL: samples.append(sol.values) if verbose: print('Sampling success rate: {} (of {})'.format(len(samples), n_samples)) if merge_keys: merged = OrderedDict() for r_id in model.reactions: merged[r_id] = [sample[r_id] for sample in samples] samples = merged return samples
def testRun(self): model = load_cbmodel(SMALL_TEST_MODEL, flavor='cobra') solution1 = pFBA(model) solution2 = FBA(model) self.assertEqual(solution1.status, Status.OPTIMAL) self.assertEqual(solution2.status, Status.OPTIMAL) growth1 = solution1.values[model.biomass_reaction] growth2 = solution2.values[model.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 testRun(self): model = load_cbmodel(SMALL_TEST_MODEL, flavor='cobra') solution1 = pFBA(model) solution2 = FBA(model) self.assertEqual(solution1.status, Status.OPTIMAL) self.assertEqual(solution2.status, Status.OPTIMAL) growth1 = solution1.values[model.biomass_reaction] growth2 = solution2.values[model.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 _run_stoic_simutation(model, objective, minimize, constraints, method): if method == 'FBA': solution = FBA(model, objective=objective, minimize=minimize, constraints=constraints) elif method == 'pFBA': solution = pFBA(model, objective=objective, minimize=minimize, constraints=constraints) else: raise Exception("Unknown method to perform the simulation.") return solution.status, solution.values
def eFlux(model, gene_exp, scale_rxn=None, scale_value=None, constraints=None, parsimonious=False): """ Run an E-Flux simulation (Colijn et al, 2009). Arguments: model (CBModel): model gene_exp (dict): transcriptomics data scale_rxn (str): reaction to scale flux vector (optional) scale_value (float): scaling factor (mandatory if scale_rxn is specified) constraints (dict): additional constraints (optional) parsimonious (bool): compute a parsimonious solution (default: False) Returns: Solution: solution """ rxn_exp = gene_to_reaction_expression(model, gene_exp) max_exp = max(rxn_exp.values()) bounds = {} for r_id, reaction in model.reactions.items(): val = rxn_exp[r_id] / max_exp if r_id in rxn_exp else 1 lb2 = -val if reaction.lb is None or reaction.lb < 0 else 0 ub2 = val if reaction.ub is None or reaction.ub > 0 else 0 bounds[r_id] = (lb2, ub2) if constraints: for r_id, x in constraints.items(): lb, ub = x if isinstance(x, tuple) else (x, x) lb2 = -1 if lb is None or lb < 0 else 0 ub2 = 1 if ub is None or ub > 0 else 0 bounds[r_id] = (lb2, ub2) if parsimonious: sol = pFBA(model, constraints=bounds) else: sol = FBA(model, constraints=bounds) if scale_rxn is not None: if sol.values[scale_rxn] != 0: k = abs(scale_value / sol.values[scale_rxn]) else: k = 0 for r_id, val in sol.values.items(): sol.values[r_id] = val * k return sol
def lp_sampler(model, n_samples=1000, weights=None, constraints=None, select_probability=0.01, futile_cycle_threshold=1e2, variation_threshold=1e-4, merge_keys=False, verbose=True): if not weights: variability = FVA(model, constraints=constraints) weights = { r_id: 1.0 / (ub - lb) for r_id, (lb, ub) in variability.items() if ub is not None and lb is not None and variation_threshold < (ub - lb) < futile_cycle_threshold } samples = [] solver = solver_instance(model) for i in range(n_samples): objective = { r_id: gauss(0, 1) * W for r_id, W in weights.items() if random() < select_probability } sol = pFBA(model, objective=objective, constraints=constraints, solver=solver) if sol.status == Status.OPTIMAL: samples.append(sol.values) if verbose: print('Sampling success rate: {} (of {})'.format( len(samples), n_samples)) if merge_keys: merged = OrderedDict() for r_id in model.reactions: merged[r_id] = [sample[r_id] for sample in samples] samples = merged return samples
def medium_test(): SBML_FILE = "../../../examples/models/EC_SC_model.xml" model = load_cbmodel(SBML_FILE) model2 = fix_exchange_reactions_model(model) for r in model2.reactions: print(r) for r_id, rxn in model2.reactions.items(): if r_id.startswith( 'R_EX_'): # ou tambem podes fazer if rxn.is_exchange: rxn.lb = 0 rxn.ub = 1000 \ #if rxn.ub is None else rxn.ub #sol 1 constraints = { 'R_EX_mn2_e__mod1': (-1000, 0), 'R_EX_cobalt2_e__mod1': (-1000, 0), 'R_EX_cl_e__mod1': (-1000, 0), 'R_EX_xyl_D_medium_': (-12, 0), 'R_EX_mobd_e__mod1': (-1000, 0), 'R_EX_mg2_e__mod1': (-1000, 0), 'R_EX_o2_medium_': (-2, 0), 'R_EX_k_medium_': (-1000, 0), 'R_EX_nh4_medium_': (-1000, 0), 'R_EX_fe3_e__mod1': (-1000, 0), 'R_EX_zn2_e__mod1': (-1000, 0), 'R_EX_ca2_e__mod1': (-1000, 0), 'R_EX_so4_medium_': (-1000, 0), 'R_EX_pi_medium_': (-1000, 0), 'R_EX_cu2_e__mod1': (-1000, 0) } #sol2 constraints = { 'R_EX_mn2_e__mod1': (-1000, 0), 'R_EX_cobalt2_e__mod1': (-1000, 0), 'R_EX_cl_e__mod1': (-1000, 0), 'R_EX_mobd_e__mod1': (-1000, 0), 'R_EX_cu_e__mod1': (-1000, 0), 'R_EX_mg2_e__mod1': (-1000, 0), 'R_EX_glc_D_medium_': (-12, 0), 'R_EX_o2_medium_': (-2, 0), 'R_EX_k_medium_': (-1000, 0), 'R_EX_nh4_medium_': (-1000, 0), 'R_EX_fe3_e__mod1': (-1000, 0), 'R_EX_zn2_e__mod1': (-1000, 0), 'R_EX_ca2_e__mod1': (-1000, 0), 'R_EX_so4_medium_': (-1000, 0), 'R_EX_pi_medium_': (-1000, 0) } res = pFBA(model2, objective={"R_BM_total_Synth": 1}, constraints=constraints) #res = FBA(model2, objective={"R_BM_total_Synth": 1}) for r, f in res.values.items(): print(r + " --> " + str(f)) print(res.values["R_EX_succ_medium_"]) print(res.values["R_BM_total_Synth"]) print(res.values["R_EX_glc_D_medium_"])