本文整理汇总了Python中cobra.Model.add_reactions方法的典型用法代码示例。如果您正苦于以下问题:Python Model.add_reactions方法的具体用法?Python Model.add_reactions怎么用?Python Model.add_reactions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cobra.Model
的用法示例。
在下文中一共展示了Model.add_reactions方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_change_coefficient
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def test_change_coefficient(self):
solver = self.solver
c = Metabolite("c")
c._bound = 6
x = Reaction("x")
x.lower_bound = 1.
y = Reaction("y")
y.lower_bound = 0.
x.add_metabolites({c: 1})
#y.add_metabolites({c: 1})
z = Reaction("z")
z.add_metabolites({c: 1})
z.objective_coefficient = 1
m = Model("test_model")
m.add_reactions([x, y, z])
# change an existing coefficient
lp = solver.create_problem(m)
solver.solve_problem(lp)
sol1 = solver.format_solution(lp, m)
solver.change_coefficient(lp, 0, 0, 2)
solver.solve_problem(lp)
sol2 = solver.format_solution(lp, m)
self.assertAlmostEqual(sol1.f, 5.0)
self.assertAlmostEqual(sol2.f, 4.0)
# change a new coefficient
z.objective_coefficient = 0.
y.objective_coefficient = 1.
lp = solver.create_problem(m)
solver.change_coefficient(lp, 0, 1, 2)
solver.solve_problem(lp)
solution = solver.format_solution(lp, m)
self.assertAlmostEqual(solution.x_dict["y"], 2.5)
示例2: test_solve_mip
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def test_solve_mip(self):
solver = self.solver
if not hasattr(solver, "_SUPPORTS_MILP") or not solver._SUPPORTS_MILP:
self.skipTest("no milp support")
cobra_model = Model('MILP_implementation_test')
constraint = Metabolite("constraint")
constraint._bound = 2.5
x = Reaction("x")
x.lower_bound = 0.
x.objective_coefficient = 1.
x.add_metabolites({constraint: 2.5})
y = Reaction("y")
y.lower_bound = 0.
y.objective_coefficient = 1.
y.add_metabolites({constraint: 1.})
cobra_model.add_reactions([x, y])
float_sol = solver.solve(cobra_model)
# add an integer constraint
y.variable_kind = "integer"
int_sol = solver.solve(cobra_model)
self.assertAlmostEqual(float_sol.f, 2.5)
self.assertAlmostEqual(float_sol.x_dict["y"], 2.5)
self.assertEqual(int_sol.status, "optimal")
self.assertAlmostEqual(int_sol.f, 2.2)
self.assertAlmostEqual(int_sol.x_dict["y"], 2.0)
示例3: test_loopless
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def test_loopless(self):
try:
solver = get_solver_name(mip=True)
except:
self.skipTest("no MILP solver found")
test_model = Model()
test_model.add_metabolites(Metabolite("A"))
test_model.add_metabolites(Metabolite("B"))
test_model.add_metabolites(Metabolite("C"))
EX_A = Reaction("EX_A")
EX_A.add_metabolites({test_model.metabolites.A: 1})
DM_C = Reaction("DM_C")
DM_C.add_metabolites({test_model.metabolites.C: -1})
v1 = Reaction("v1")
v1.add_metabolites({test_model.metabolites.A: -1, test_model.metabolites.B: 1})
v2 = Reaction("v2")
v2.add_metabolites({test_model.metabolites.B: -1, test_model.metabolites.C: 1})
v3 = Reaction("v3")
v3.add_metabolites({test_model.metabolites.C: -1, test_model.metabolites.A: 1})
DM_C.objective_coefficient = 1
test_model.add_reactions([EX_A, DM_C, v1, v2, v3])
feasible_sol = construct_loopless_model(test_model).optimize()
v3.lower_bound = 1
infeasible_sol = construct_loopless_model(test_model).optimize()
self.assertEqual(feasible_sol.status, "optimal")
self.assertEqual(infeasible_sol.status, "infeasible")
示例4: test_inequality
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def test_inequality(self):
# The space enclosed by the constraints is a 2D triangle with
# vertexes as (3, 0), (1, 2), and (0, 1)
solver = self.solver
# c1 encodes y - x > 1 ==> y > x - 1
# c2 encodes y + x < 3 ==> y < 3 - x
c1 = Metabolite("c1")
c2 = Metabolite("c2")
x = Reaction("x")
x.lower_bound = 0
y = Reaction("y")
y.lower_bound = 0
x.add_metabolites({c1: -1, c2: 1})
y.add_metabolites({c1: 1, c2: 1})
c1._bound = 1
c1._constraint_sense = "G"
c2._bound = 3
c2._constraint_sense = "L"
m = Model()
m.add_reactions([x, y])
# test that optimal values are at the vertices
m.change_objective("x")
self.assertAlmostEqual(solver.solve(m).f, 1.0)
self.assertAlmostEqual(solver.solve(m).x_dict["y"], 2.0)
m.change_objective("y")
self.assertAlmostEqual(solver.solve(m).f, 3.0)
self.assertAlmostEqual(solver.solve(m, objective_sense="minimize").f,
1.0)
示例5: minimized_shuffle
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def minimized_shuffle(small_model):
model = small_model.copy()
chosen = sample(list(set(model.reactions) - set(model.exchanges)), 10)
new = Model("minimized_shuffle")
new.add_reactions(chosen)
LOGGER.debug("'%s' has %d metabolites, %d reactions, and %d genes.",
new.id, new.metabolites, new.reactions, new.genes)
return new
示例6: tiny_toy_model
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def tiny_toy_model():
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.objective = 'ex1'
return tiny
示例7: test_quadratic
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def test_quadratic(self):
solver = self.solver
if not hasattr(solver, "set_quadratic_objective"):
self.skipTest("no qp support")
c = Metabolite("c")
c._bound = 2
x = Reaction("x")
x.objective_coefficient = -0.5
x.lower_bound = 0.
y = Reaction("y")
y.objective_coefficient = -0.5
y.lower_bound = 0.
x.add_metabolites({c: 1})
y.add_metabolites({c: 1})
m = Model()
m.add_reactions([x, y])
lp = self.solver.create_problem(m)
quadratic_obj = scipy.sparse.eye(2) * 2
solver.set_quadratic_objective(lp, quadratic_obj)
solver.solve_problem(lp, objective_sense="minimize")
solution = solver.format_solution(lp, m)
self.assertEqual(solution.status, "optimal")
# Respecting linear objectives also makes the objective value 1.
self.assertAlmostEqual(solution.f, 1.)
self.assertAlmostEqual(solution.x_dict["y"], 1.)
self.assertAlmostEqual(solution.x_dict["y"], 1.)
# When the linear objectives are removed the objective value is 2.
solver.change_variable_objective(lp, 0, 0.)
solver.change_variable_objective(lp, 1, 0.)
solver.solve_problem(lp, objective_sense="minimize")
solution = solver.format_solution(lp, m)
self.assertEqual(solution.status, "optimal")
self.assertAlmostEqual(solution.f, 2.)
# test quadratic from solve function
solution = solver.solve(m, quadratic_component=quadratic_obj,
objective_sense="minimize")
self.assertEqual(solution.status, "optimal")
self.assertAlmostEqual(solution.f, 1.)
c._bound = 6
z = Reaction("z")
x.objective_coefficient = 0.
y.objective_coefficient = 0.
z.lower_bound = 0.
z.add_metabolites({c: 1})
m.add_reaction(z)
solution = solver.solve(m, quadratic_component=scipy.sparse.eye(3),
objective_sense="minimize")
# should be 12 not 24 because 1/2 (V^T Q V)
self.assertEqual(solution.status, "optimal")
self.assertAlmostEqual(solution.f, 6)
self.assertAlmostEqual(solution.x_dict["x"], 2, places=6)
self.assertAlmostEqual(solution.x_dict["y"], 2, places=6)
self.assertAlmostEqual(solution.x_dict["z"], 2, places=6)
示例8: convert_to_cobra_model
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def convert_to_cobra_model(the_network):
""" Take a generic NAMpy model and convert to
a COBRA model. The model is assumed to be monopartite.
You need a functional COBRApy for this.
Arguments:
the_network
kwargs:
flux_bounds
"""
continue_flag = True
try:
from cobra import Model, Reaction, Metabolite
except:
print 'This function requires a functional COBRApy, exiting ...'
if continue_flag:
__default_objective_coefficient = 0
if 'flux_bounds' in kwargs:
flux_bounds = kwargs['flux_bounds']
else:
flux_bounds = len(the_nodes)
metabolite_dict = {}
for the_node in the_network.nodetypes[0].nodes:
the_metabolite = Metabolite(the_node.id)
metabolite_dict.update({the_node.id: the_metabolite})
cobra_reaction_list = []
for the_edge in the_network.edges:
the_reaction = Reaction(the_edge.id)
cobra_reaction_list.append(the_reaction)
the_reaction.upper_bound = flux_bounds
the_reaction.lower_bound = -1 * flux_bounds
cobra_metabolites = {}
the_metabolite_id_1 = the_edge._nodes[0].id
the_metabolite_id_2 = the_edge._nodes[1].id
cobra_metabolites[metabolite_dict[the_metabolite_id_1]] = 1.
cobra_metabolites[metabolite_dict[the_metabolite_id_2]] = -1.
reaction.add_metabolites(cobra_metabolites)
reaction.objective_coefficient = __default_objective_coefficient
cobra_model = Model(model_id)
cobra_model.add_reactions(cobra_reaction_list)
return cobra_model
else:
return None
示例9: build_model
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def build_model():
m = Model("Blazier et al 2012")
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
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
EX_M4_e = m.add_boundary(m4_e)
EX_M4_e.lower_bound = -10
m.objective = r4
return m
示例10: TestCobraSolver
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
class TestCobraSolver(TestCase):
def setUp(self):
self.model = create_test_model()
initialize_growth_medium(self.model, "MgM")
self.old_solution = 0.320064
self.infeasible_model = Model()
metabolite_1 = Metabolite("met1")
# metabolite_2 = Metabolite("met2")
reaction_1 = Reaction("rxn1")
reaction_2 = Reaction("rxn2")
reaction_1.add_metabolites({metabolite_1: 1})
reaction_2.add_metabolites({metabolite_1: 1})
reaction_1.lower_bound = 1
reaction_2.upper_bound = 2
self.infeasible_model.add_reactions([reaction_1, reaction_2])
示例11: model
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def model():
A = Metabolite("A")
B = Metabolite("B")
C = Metabolite("C")
r1 = Reaction("r1")
r1.add_metabolites({A: -1, C: 1})
r2 = Reaction("r2")
r2.add_metabolites({B: -1, C: 1})
r3 = Reaction("EX_A")
r3.add_metabolites({A: 1})
r4 = Reaction("EX_B")
r4.add_metabolites({B: 1})
r5 = Reaction("EX_C")
r5.add_metabolites({C: -1})
mod = Model("test model")
mod.add_reactions([r1, r2, r3, r4, r5])
conf = {"r1": 1, "r2": -1, "EX_A": 1, "EX_B": 1, "EX_C": 1}
return (mod, conf)
示例12: test_solve_mip
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def test_solve_mip(self):
solver = self.solver
cobra_model = Model('MILP_implementation_test')
constraint = Metabolite("constraint")
constraint._bound = 2.5
x = Reaction("x")
x.lower_bound = 0.
x.objective_coefficient = 1.
x.add_metabolites({constraint: 2.5})
y = Reaction("y")
y.lower_bound = 0.
y.objective_coefficient = 1.
y.add_metabolites({constraint: 1.})
cobra_model.add_reactions([x, y])
float_sol = solver.solve(cobra_model)
# add an integer constraint
y.variable_kind = "integer"
int_sol = solver.solve(cobra_model)
self.assertAlmostEqual(float_sol.f, 2.5)
self.assertAlmostEqual(float_sol.x_dict["y"], 2.5)
self.assertAlmostEqual(int_sol.f, 2.2)
self.assertAlmostEqual(int_sol.x_dict["y"], 2.0)
示例13: test_remove_genes
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
def test_remove_genes(self):
m = Model("test")
m.add_reactions([Reaction("r" + str(i + 1)) for i in range(8)])
self.assertEqual(len(m.reactions), 8)
rxns = m.reactions
rxns.r1.gene_reaction_rule = "(a and b) or (c and a)"
rxns.r2.gene_reaction_rule = "(a and b and d and e)"
rxns.r3.gene_reaction_rule = "(a and b) or (b and c)"
rxns.r4.gene_reaction_rule = "(f and b) or (b and c)"
rxns.r5.gene_reaction_rule = "x"
rxns.r6.gene_reaction_rule = "y"
rxns.r7.gene_reaction_rule = "x or z"
rxns.r8.gene_reaction_rule = ""
self.assertIn("a", m.genes)
self.assertIn("x", m.genes)
delete.remove_genes(m, ["a"], remove_reactions=False)
self.assertNotIn("a", m.genes)
self.assertIn("x", m.genes)
self.assertEqual(rxns.r1.gene_reaction_rule, "")
self.assertEqual(rxns.r2.gene_reaction_rule, "")
self.assertEqual(rxns.r3.gene_reaction_rule, "b and c")
self.assertEqual(rxns.r4.gene_reaction_rule, "(f and b) or (b and c)")
self.assertEqual(rxns.r5.gene_reaction_rule, "x")
self.assertEqual(rxns.r6.gene_reaction_rule, "y")
self.assertEqual(rxns.r7.genes, {m.genes.x, m.genes.z})
self.assertEqual(rxns.r8.gene_reaction_rule, "")
delete.remove_genes(m, ["x"], remove_reactions=True)
self.assertEqual(len(m.reactions), 7)
self.assertNotIn("r5", m.reactions)
self.assertNotIn("x", m.genes)
self.assertEqual(rxns.r1.gene_reaction_rule, "")
self.assertEqual(rxns.r2.gene_reaction_rule, "")
self.assertEqual(rxns.r3.gene_reaction_rule, "b and c")
self.assertEqual(rxns.r4.gene_reaction_rule, "(f and b) or (b and c)")
self.assertEqual(rxns.r6.gene_reaction_rule, "y")
self.assertEqual(rxns.r7.gene_reaction_rule, "z")
self.assertEqual(rxns.r7.genes, {m.genes.z})
self.assertEqual(rxns.r8.gene_reaction_rule, "")
示例14: len
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
# print reaction.print_values()
# if reaction.check_mass_balance() != []: print reaction, "is not balanced" # throws error sometimes!
if care_generics and cyc.reaction_is_generic(org, r, generic_exceptions, substitutions):
r_generic += 1
specific_reactions = cyc.reaction_generic_specified(
org,
r,
reaction,
generic_exceptions,
substitutions,
cyc.get_generic_assignment("./conf/generic_assignment.txt"),
formula_dic,
)
model.add_reactions(specific_reactions)
# print "\nabstract reaction:", str(r), reaction.reaction, "\n\tadded", len(specific_reactions), "specific reactions"
print "\nabstract reaction:", str(r), "\n\tadded", len(specific_reactions), "specific reactions"
if len(specific_reactions) == 0:
r_ignored_set.add(str(r))
# print >>r_ignored, str(r), reaction.reaction
generics = cyc.reaction_get_generic(org, r, generic_exceptions, substitutions)
if r.in_pathway != None: # remember missed pathways
plist = r.in_pathway if isinstance(r.in_pathway, list) else [r.in_pathway]
for path in plist:
# p_ignored_set |= set((map(str, plist)))
if p_ignored_set.has_key(str(path)):
p_ignored_set[str(path)] = p_ignored_set[str(path)] | generics
else:
p_ignored_set[str(path)] = generics
for g in generics: # remember missed reactions
示例15: print
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_reactions [as 别名]
# We need to use a solver that supports quadratic programming, such as gurobi
# or cplex. If a solver which supports quadratic programming is installed, this
# function will return its name.
print(solvers.get_solver_name(qp=True))
# Prints:
# gurobi
c = Metabolite("c")
c._bound = 2
x = Reaction("x")
y = Reaction("y")
x.add_metabolites({c: 1})
y.add_metabolites({c: 1})
m = Model()
m.add_reactions([x, y])
sol = m.optimize(quadratic_component=Q, objective_sense="minimize")
sol.x_dict
# Output:
# {'x': 1.0, 'y': 1.0}
# Suppose we change the problem to have a mixed linear and quadratic objective.
#
# > **min** $\frac{1}{2}\left(x^2 + y^2 \right) - y$
#
# > *subject to*
#
# > $x + y = 2$
#
# > $x \ge 0$
#