Example #1
0
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
Example #2
0
 def testRun(self):
     model = load_cbmodel(SMALL_TEST_MODEL, flavor='cobra')
     write_model_to_file(model, PLAIN_TEXT_COPY)
     model_copy = read_model_from_file(PLAIN_TEXT_COPY, kind='cb')
     solution = FBA(model_copy)
     self.assertEqual(solution.status, Status.OPTIMAL)
     self.assertAlmostEqual(solution.fobj, GROWTH_RATE, places=2)
Example #3
0
 def testRun(self):
     model = load_cbmodel(SMALL_TEST_MODEL, flavor='cobra')
     make_irreversible(model)
     self.assertTrue(all([not reaction.reversible for reaction in model.reactions.values()]))
     solution = FBA(model)
     self.assertEqual(solution.status, Status.OPTIMAL)
     self.assertAlmostEqual(solution.fobj, GROWTH_RATE, places=2)
Example #4
0
 def testRun(self):
     model = load_cbmodel(SMALL_TEST_MODEL, flavor='cobra')
     r_id1 = 'R_PGI'
     r_id2 = 'R_G6PDH2r'
     ratio = 2.0
     model.add_ratio_constraint(r_id1, r_id2, ratio)
     solution = FBA(model, get_shadow_prices=True, get_reduced_costs=True)
     self.assertEqual(solution.status, Status.OPTIMAL)
     self.assertEqual(solution.values[r_id1] / solution.values[r_id2], ratio)
Example #5
0
 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 find_essential_drains(self):
        drains = self.get_drains()
        if self.constraints:
            constraints = {
                d: (StoicConfigurations.DEFAULT_LB,
                    StoicConfigurations.DEFAULT_UB)
                for d in drains if d not in self.constraints.keys()
            }
        else:
            constraints = {
                d: (StoicConfigurations.DEFAULT_LB,
                    StoicConfigurations.DEFAULT_UB)
                for d in drains
            }
        solution = FBA(self.model,
                       objective=self.objective,
                       minimize=self.minimize,
                       constraints=constraints)

        # #which objetive reactions has flux whem drains are all open
        hasFlux = []
        for rId in self.objective.keys():
            if solution.values[rId] > 0:
                hasFlux.append(rId)

        # close each drain and check if the objective reactions have flux
        essential = []
        for d in drains:
            constraints[d] = (0, StoicConfigurations.DEFAULT_UB)
            solution = FBA(self.model,
                           objective=self.objective,
                           minimize=self.minimize,
                           constraints=constraints)
            if (solution.values is None
                    or any([solution.values[rId] == 0 for rId in hasFlux])):
                essential.append(d)
            constraints[d] = (StoicConfigurations.DEFAULT_LB,
                              StoicConfigurations.DEFAULT_UB)

        return essential
Example #7
0
def cbm_simualtion_iAF():
    SBML_FILE = "../../../examples/models/Ec_iAF1260.xml"
    model = load_cbmodel(SBML_FILE, flavor="cobra")
    constraints = {'R_Ec_biomass_iAF1260_core_59p81M': (0.55, 9999)}

    res = FBA(model, objective={"R_EX_succ_e": 1}, constraints=constraints)

    for r, f in res.values.items():
        if f != 0:
            print(r + " --> " + str(f))

    print(res.values["R_EX_succ_e"])
    print(res.values["R_Ec_biomass_iAF1260_core_59p81M"])
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
Example #9
0
def cbm_simualtion():
    SBML_FILE = "../../../examples/models/iMM904.xml"
    model = load_cbmodel(SBML_FILE, flavor="fbc2")

    constraints = {
        'R_EX_so4_e': (-10000, 0),
        'R_EX_o2_e': (-50, 0),
        'R_EX_gam6p_e': (-10, 0),
        'R_EX_melib_e': (-5, 0)
    }
    constraints.update({"R_BIOMASS_SC5_notrace": (0.21, 9999)})
    res = FBA(model, objective={"R_EX_etoh_e": 1}, constraints=constraints)
    print(res.values["R_EX_etoh_e"])
    print(res.values["R_BIOMASS_SC5_notrace"])
    print(res.values["R_EX_so4_e"])
    print(res.values["R_EX_gam6p_e"])
    print(res.values["R_EX_o2_e"])
    print(res.values["R_EX_melib_e"])
Example #10
0
def GIMME(model, gene_exp, cutoff=25, growth_frac=0.9, constraints=None, parsimonious=False):
    """ Run a GIMME simulation (Becker and Palsson, 2008).

    Arguments:
        model (CBModel): model
        gene_exp (dict): transcriptomics data
        cutoff (int): percentile cuttof (default: 25)
        growth_frac (float): minimum growth requirement (default: 0.9)
        constraints (dict): additional constraints
        parsimonious (bool): compute a parsimonious solution (default: False)

    Returns:
        Solution: solution
    """

    rxn_exp = gene_to_reaction_expression(model, gene_exp, or_func=max)
    threshold = percentile(rxn_exp.values(), cutoff)
    coeffs = {r_id: threshold-val for r_id, val in rxn_exp.items() if val < threshold}

    solver = solver_instance(model)

    wt_solution = FBA(model, constraints=constraints, solver=solver)

    if not constraints:
        constraints = {}

    biomass = model.biomass_reaction
    constraints[biomass] = (growth_frac * wt_solution.values[biomass], None)

    for r_id in model.reactions:
        if model.reactions[r_id].reversible:
            pos, neg = r_id + '+', r_id + '-'
            solver.add_variable(pos, 0, None, persistent=False, update_problem=False)
            solver.add_variable(neg, 0, None, persistent=False, update_problem=False)
    solver.update()

    for r_id in model.reactions:
        if model.reactions[r_id].reversible:
            pos, neg = r_id + '+', r_id + '-'
            solver.add_constraint('c' + pos, {r_id: -1, pos: 1}, '>', 0, persistent=False, update_problem=False)
            solver.add_constraint('c' + neg, {r_id: 1, neg: 1}, '>', 0, persistent=False, update_problem=False)
    solver.update()

    objective = dict()
    for r_id, val in coeffs.items():
        if model.reactions[r_id].reversible:
            pos, neg = r_id + '+', r_id + '-'
            objective[pos] = val
            objective[neg] = val
        else:
            objective[r_id] = val

    solution = solver.solve(objective, minimize=True, constraints=constraints)

    if parsimonious:
        pre_solution = solution

        solver.add_constraint('obj', objective, '=', pre_solution.fobj)
        objective = dict()

        for r_id in model.reactions:
            if model.reactions[r_id].reversible:
                pos, neg = r_id + '+', r_id + '-'
                objective[pos] = 1
                objective[neg] = 1
            else:
                objective[r_id] = 1

        solution = solver.solve(objective, minimize=True, constraints=constraints)
        solver.remove_constraint('obj')
        solution.pre_solution = pre_solution

    return solution
Example #11
0
 def testRun(self):
     model = load_cbmodel(SMALL_TEST_MODEL, flavor='cobra')
     solution = FBA(model, get_shadow_prices=True, get_reduced_costs=True)
     self.assertEqual(solution.status, Status.OPTIMAL)
     self.assertAlmostEqual(solution.fobj, GROWTH_RATE, places=2)
Example #12
0
 def testRun(self):
     model = load_cbmodel(SMALL_TEST_MODEL, flavor='cobra')
     simplify(model)
     solution = FBA(model)
     self.assertEqual(solution.status, Status.OPTIMAL)
     self.assertAlmostEqual(solution.fobj, GROWTH_RATE, places=2)