Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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])
def test_add_metabolites_combine_false(self, core_model):
test_metabolite = Metabolite('test')
for reaction in core_model.reactions:
reaction.add_metabolites({test_metabolite: -66}, combine=False)
assert reaction.metabolites[test_metabolite] == -66
assert core_model.solver.constraints['test'].expression.has(-66. * reaction.forward_variable)
assert core_model.solver.constraints['test'].expression.has(66. * reaction.reverse_variable)
already_included_metabolite = list(reaction.metabolites.keys())[0]
reaction.add_metabolites({already_included_metabolite: 10}, combine=False)
assert reaction.metabolites[already_included_metabolite] == 10
assert core_model.solver.constraints[already_included_metabolite.id].expression.has(
10 * reaction.forward_variable)
assert core_model.solver.constraints[already_included_metabolite.id].expression.has(
-10 * reaction.reverse_variable)
def build_model():
m = Model("Zur et al 2012")
m1 = Metabolite("M1")
m2 = Metabolite("M2")
m3 = Metabolite("M3")
m4 = Metabolite("M4")
m5 = Metabolite("M5")
m6 = Metabolite("M6")
m7 = Metabolite("M7")
m8 = Metabolite("M8")
m9 = Metabolite("M9")
m10 = Metabolite("M10")
r1 = Reaction("R1")
r1.add_metabolites({m3: 1})
r2 = Reaction("R2")
r2.add_metabolites({m1: 1})
r2.gene_reaction_rule = "G1 or G2"
def setUp(self):
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})
model = Model("test model")
model.add_reactions([r1, r2, r3, r4, r5])
conf = {"r1": 1, "r2": -1, "EX_A": 1, "EX_B": 1, "EX_C": 1}
self.model = model
self.conf = conf
def test_impossible_req(self, model):
mod, conf = model
D = Metabolite("D")
mod.add_metabolites([D])
opt = CORDA(mod, conf, met_prod=["D"])
need = opt.associated(["EX_CORDA_0"])
assert len(need) == 0
assert "EX_CORDA_0" in opt.impossible
def build_model():
m = Model("Zur et al 2012")
m1 = Metabolite("M1")
m2 = Metabolite("M2")
m3 = Metabolite("M3")
m4 = Metabolite("M4")
m5 = Metabolite("M5")
m6 = Metabolite("M6")
m7 = Metabolite("M7")
m8 = Metabolite("M8")
m9 = Metabolite("M9")
m10 = Metabolite("M10")
r1 = Reaction("R1")
r1.add_metabolites({m3: 1})
r2 = Reaction("R2")
r2.add_metabolites({m1: 1})
r2.gene_reaction_rule = "G1 or G2"
r3 = Reaction("R3")
r3.add_metabolites({m2: 1})
r3.gene_reaction_rule = "G5"
If subreaction adds a chemical moiety to a macromolecules via a
modification or other means, the biomass contribution of the
modification process should be accounted for and ultimately included
in the reaction it is involved in.
Returns
-------
float
Mass of moiety transferred to macromolecule by subreaction
"""
elements = self.element_contribution
# Create temporary metabolite for calculating formula weight
tmp_met = cobra.Metabolite('mass')
elements_to_formula(tmp_met, elements)
return tmp_met.formula_weight
to_pop.append(name)
for i in to_pop:
possible_names.pop(i)
possible_names = possible_names.keys()
for possible_name in possible_names:
m = data[possible_name] # TODO: generalize
if m.dtype.names is None:
continue
if not set(["rxns", "mets", "S", "lb", "ub"]) \
<= set(m.dtype.names):
continue
model = cobra.Model()
model.id = m["description"][0, 0][0]
model.description = model.id
for i, name in enumerate(m["mets"][0, 0]):
new_metabolite = cobra.Metabolite()
new_metabolite.id = name[0][0]#.encode("ascii", "replace")
try:
new_metabolite.name = m["metNames"][0, 0][i][0][0]
new_metabolite.formula = m["metFormulas"][0][0][i][0][0]
except:
pass
model.add_metabolites([new_metabolite])
for i, name in enumerate(m["rxns"][0, 0]):
new_reaction = cobra.Reaction()
new_reaction.id = name[0][0]#.encode("ascii", "replace")
new_reaction.lower_bound = m["lb"][0, 0][i][0]
new_reaction.upper_bound = m["ub"][0, 0][i][0]
new_reaction.objective_coefficient = m["c"][0, 0][i][0]
try:
new_reaction.name = m["rxnNames"][0, 0][i][0][0]
except:
# problem is:
# max profit
# s.t.
# cone_production_cost*cone_production + popsidle_production_cost*popsicle_production <= starting_budget
# number of cones and popsicles has to be integer...
# first, we'll solve the continuous case just to make sure everything is
# working (it should only make cones)...then we'll tighten the constraints to
# integer... and it should make popsicles.
cobra_model = Model('MILP_implementation_test')
cone_out = Metabolite(id='cone_out', compartment='c')
cone_in = Metabolite(id='cone_in', compartment='c')
cone_consumed = Metabolite(id='cone_consumed', compartment='c')
popsicle_out = Metabolite(id='popsicle_out', compartment='c')
popsicle_in = Metabolite(id='popsicle_in', compartment='c')
popsicle_consumed = Metabolite(id='popsicle_consumed', compartment='c')
the_reactions = []
# SOURCE
Cone_source = Reaction(name='Cone_source')
temp_metabolite_dict = {cone_out: 1}
Cone_source.add_metabolites(temp_metabolite_dict)
the_reactions.append(Cone_source)
Popsicle_source = Reaction(name='Popsicle_source')
temp_metabolite_dict = {popsicle_out: 1}
Popsicle_source.add_metabolites(temp_metabolite_dict)
the_reactions.append(Popsicle_source)
def __init__(self, design_space_model, objective, variables=None, reference_model=None,
exclude=(), normalize_ranges_by=None, points=10):
super(DifferentialFVA, self).__init__()
self.design_space_model = design_space_model
self.design_space_nullspace = nullspace(create_stoichiometric_array(self.design_space_model))
if reference_model is None:
self.reference_model = self.design_space_model.copy()
self.reference_nullspace = self.design_space_nullspace
else:
self.reference_model = reference_model
self.reference_nullspace = nullspace(create_stoichiometric_array(self.reference_model))
if isinstance(objective, Reaction):
self.objective = objective.id
elif isinstance(objective, Metabolite):
try:
self.reference_model.add_boundary(objective, type='demand')
except ValueError:
pass
try:
self.objective = self.design_space_model.add_boundary(objective, type='demand').id
except ValueError:
self.objective = self.design_space_model.reactions.get_by_id("DM_" + objective.id).id
elif isinstance(objective, str):
self.objective = objective
else:
raise ValueError('You need to provide an objective as a Reaction, Metabolite or a reaction id')
if variables is None:
# try to establish the current objective reaction
obj_var_ids = [variable.name for variable in self.design_space_model.objective.expression.free_symbols]