How to use the cobra.Reaction function in cobra

To help you get started, we’ve selected a few cobra examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github opencobra / driven / tests / assets / blazier_et_al_2012.py View on Github external
r2 = Reaction("R2")
    r2.add_metabolites({m1: -1, m2: 1})
    r2.gene_reaction_rule = "Gene2"

    r3 = Reaction("R3")
    r3.add_metabolites({m2: -1, m3: 1})
    r3.gene_reaction_rule = "Gene3"

    r4 = Reaction("R4")
    r4.add_metabolites({m3: -1})

    r5 = Reaction("R5")
    r5.add_metabolites({m4_e: -1, m4: 1})

    r6 = Reaction("R6")
    r6.add_metabolites({m4: -1, m5: 1})
    r6.gene_reaction_rule = "Gene6"

    r7 = Reaction("R7")
    r7.add_metabolites({m5: -1, m2: 1})
    r7.lower_bound = -r7.upper_bound
    r7.gene_reaction_rule = "Gene7"

    r8 = Reaction("R8")
    r8.add_metabolites({m5: -1})

    m.add_reactions([r1, r2, r3, r4, r5, r6, r7, r8])

    EX_M1_e = m.add_boundary(m1_e)
    EX_M1_e.lower_bound = -10
github biosustain / cameo / tests / test_solver_based_model.py View on Github external
def test_get_reaction_for(self, core_model):
        with core_model:
            for r in core_model.reactions:
                assert isinstance(get_reaction_for(core_model, r.id), cobra.Reaction)
                assert isinstance(get_reaction_for(core_model, r), cobra.Reaction)
            for m in core_model.metabolites:
                assert isinstance(get_reaction_for(core_model, m.id), cobra.Reaction)
                assert isinstance(get_reaction_for(core_model, m), cobra.Reaction)

        with pytest.raises(TypeError):
            get_reaction_for(core_model, None)
        with pytest.raises(KeyError):
            get_reaction_for(core_model, "blablabla")
        with pytest.raises(KeyError):
            get_reaction_for(core_model, "accoa_lp_c_lp_", add=False)
github opencobra / cobrapy / cobra / oven / unit_tests.py View on Github external
def setUp(self):
        CobraTestCase.setUp(self)
        self.old_solution = self.model.solution.f
        self.infeasible_problem = cobra.Model()
        metabolite_1 = cobra.Metabolite("met1")
        metabolite_2 = cobra.Metabolite("met2")
        reaction_1 = cobra.Reaction("rxn1")
        reaction_2 = cobra.Reaction("rxn2")
        reaction_1.add_metabolites({metabolite_1: 1})
        reaction_2.add_metabolites({metabolite_2: 1})
        reaction_1.lower_bound = 1
        reaction_2.upper_bound = 2
        self.infeasible_problem.add_reactions([reaction_1, reaction_2])
github opencobra / driven / tests / assets / blazier_et_al_2012.py View on Github external
m1_e = Metabolite("M1_e")
    m1 = Metabolite("M1")
    m2 = Metabolite("M2")
    m3 = Metabolite("M3")
    m4_e = Metabolite("M4_e")
    m4 = Metabolite("M4")
    m5 = Metabolite("M5")

    r1 = Reaction("R1")
    r1.add_metabolites({m1_e: -1, m1: 1})

    r2 = Reaction("R2")
    r2.add_metabolites({m1: -1, m2: 1})
    r2.gene_reaction_rule = "Gene2"

    r3 = Reaction("R3")
    r3.add_metabolites({m2: -1, m3: 1})
    r3.gene_reaction_rule = "Gene3"

    r4 = Reaction("R4")
    r4.add_metabolites({m3: -1})

    r5 = Reaction("R5")
    r5.add_metabolites({m4_e: -1, m4: 1})

    r6 = Reaction("R6")
    r6.add_metabolites({m4: -1, m5: 1})
    r6.gene_reaction_rule = "Gene6"

    r7 = Reaction("R7")
    r7.add_metabolites({m5: -1, m2: 1})
    r7.lower_bound = -r7.upper_bound
github biosustain / cameo / tests / test_solver_based_model.py View on Github external
def tiny_toy_model(request):
    tiny = Model("Toy Model")
    m1 = Metabolite("M1")
    d1 = Reaction("ex1")
    d1.add_metabolites({m1: -1})
    d1.upper_bound = 0
    d1.lower_bound = -1000
    tiny.add_reactions([d1])
    tiny.solver = request.param
    return tiny
github opencobra / driven / tests / assets / zur_et_al_2010.py View on Github external
r9 = Reaction("R9")
    r9.add_metabolites({m5: -1})

    r10 = Reaction("R10")
    r10.add_metabolites({m6: -1, m9: 1})
    r10.gene_reaction_rule = "G7"

    r11 = Reaction("R11")
    r11.add_metabolites({m7: -1})

    r12 = Reaction("R12")
    r12.add_metabolites({m8: -1})
    r12.gene_reaction_rule = "G4"

    r13 = Reaction("R13")
    r13.add_metabolites({m9: -1})

    m.add_reactions([r1, r2, r3, r4, r5, r6, r7, r8])

    m.objective = r4

    return m
github biosustain / cameo / tests / test_flux_analysis.py View on Github external
def test_flux_variability_sequential_remove_cycles(self, core_model):
        original_objective = core_model.objective
        fva_solution = flux_variability_analysis(core_model, fraction_of_optimum=0.999999419892,
                                                 remove_cycles=True,
                                                 view=SequentialView())
        assert REFERENCE_FVA_SOLUTION_ECOLI_CORE['upper_bound']['FRD7'] > 666.
        assert round(abs(fva_solution['upper_bound']['FRD7'] - 0.), 7) == 0
        for key in fva_solution.data_frame.index:
            if REFERENCE_FVA_SOLUTION_ECOLI_CORE['lower_bound'][key] > -666:
                assert abs(
                    fva_solution['lower_bound'][key] - REFERENCE_FVA_SOLUTION_ECOLI_CORE['lower_bound'][key]) < 0.0001
            if REFERENCE_FVA_SOLUTION_ECOLI_CORE['upper_bound'][key] < 666:
                assert abs(
                    fva_solution['upper_bound'][key] - REFERENCE_FVA_SOLUTION_ECOLI_CORE['upper_bound'][key]) < 0.0001

        cycle_reac = Reaction("minus_PGI")  # Create fake cycle
        cycle_reac.lower_bound = -1000
        core_model.add_reaction(cycle_reac)
        cycle_reac.add_metabolites({met: -c for met, c in core_model.reactions.PGI.metabolites.items()})
        fva_solution = flux_variability_analysis(core_model, remove_cycles=False, reactions=["PGI"])
        assert fva_solution.data_frame.loc["PGI", "upper_bound"] == 1000
        fva_solution = flux_variability_analysis(core_model, remove_cycles=True, reactions=["PGI"])
        assert fva_solution.data_frame.loc["PGI", "upper_bound"] < 666
        assert original_objective == core_model.objective
github opencobra / driven / src / driven / omics / gim3e.py View on Github external
for met in model.metabolites:
        turnover_met = cobra.Metabolite("TM_{}".format(met.id))
        turnover_mets.append(turnover_met)
    model.add_metabolites(turnover_mets)

    # Add turnover metabolites to reactions
    for rxn in model.reactions:
        mets_to_add = {}
        for key, value in six.iteritems(rxn.metabolites):
            turnover_met = model.metabolites.get_by_id("TM_{}".format(key.id))
            mets_to_add[turnover_met] = abs(value)
        rxn.add_metabolites(mets_to_add)

    # Add sink reactions
    for met in model.metabolites.query("^TM_"):
        sink_rxn = cobra.Reaction("SK_{}".format(met.id))
        # TAKEN FROM THE ORIGINAL IMPLEMENTATION:
        # Since both creation and consumption of
        # the real metabolite drive the turnover,
        # we require 2 units of metabolite per
        # 1 unit of flux sink so this matches
        # the turnover through the real metabolite.
        # METABOLOMICS DATA:
        sink_rxn.add_metabolites({met: -2})
        if met.id[3:] in media_metabolites:
            sink_rxn.lower_bound = penalty_fraction * tolerance
        else:
            sink_rxn.lower_bound = 0.0
        model.add_reaction(sink_rxn)
    # TRANSCRIPTOMICS DATA:
    # Add penalty coefficients
    coefficients = {rxn_id: exp_max - expression for rxn_id, expression in
github biosustain / cameo / cameo / flux_analysis / structural.py View on Github external
def _add_target_constraints(self, c):
        """Add the target constraints to the dual model"""
        targets = self._target_constraints

        w_reactions = []
        for i, target in enumerate(targets):
            assert isinstance(target, optlang.interface.Constraint)
            if not target.is_Linear:
                raise ValueError("Target constraints must be linear.")
            if (target.lb is None and target.ub is None) or (target.lb is not None and target.ub is not None):
                raise ValueError("Target constraints must be one-sided inequalities.")
            coefficients_dict = target.expression.as_coefficients_dict()
            w_reac = Reaction("w_" + str(i))
            w_reac.lower_bound = c
            if target.ub is not None:
                coefficients = {
                    self._dual_model.metabolites.get_by_id(var.name): coef for var, coef in coefficients_dict.items()
                    if var.name in self._dual_model.metabolites}
            elif target.lb is not None:
                coefficients = {
                    self._dual_model.metabolites.get_by_id(var.name): -coef for var, coef in coefficients_dict.items()
                    if var.name in self._dual_model.metabolites}
            w_reac.add_metabolites(coefficients)
            w_reactions.append(w_reac)
        self._dual_model.add_reactions(w_reactions)

        return None
github opencobra / driven / driven / flux_analysis / fit_objective / evaluators.py View on Github external
def _set_objective(model, coefficients, candidates, use_reactions):
    if use_reactions:
        obj = Add([Mul([Real(coeff), react.flux_expression]) for react, coeff in zip(candidates, coefficients)])
    else:
        obj = Reaction("make_metabolites")
        obj.add_metabolites({met: coeff for met, coeff in zip(candidates, coefficients) if coeff != 0})
        model.add_reactions([obj])
    model.objective = obj