Skip to content

Commit

Permalink
fixing add reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
the-code-magician committed Sep 4, 2014
1 parent b216a3b commit a51c9a7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cameo/flux_analysis/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def lmoma(model, reference=None, cache={}, volatile=True, *args, **kwargs):
solution = model.solve()
return solution
except SolveError as e:
print "lmoma could not determine an optimal solution for objective %s" % model.objective
#print "lmoma could not determine an optimal solution for objective %s" % model.objective
raise e

finally:
Expand Down
7 changes: 3 additions & 4 deletions cameo/solver_based_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,11 @@ def add_reactions(self, reaction_list):
if reaction.reversibility and self._reversible_encoding == "split":
reaction_variable = self.solver.interface.Variable(reaction.id, lb=0, ub=reaction._upper_bound)
aux_var = self.solver.interface.Variable(reaction._get_reverse_id(), lb=0, ub=-reaction._lower_bound)
self.solver.add(aux_var)
self.solver._add_variable(aux_var)
else:
reaction_variable = self.solver.interface.Variable(reaction.id, lb=reaction._lower_bound,
ub=reaction._upper_bound)
self.solver.add(reaction_variable)
self.solver._add_variable(reaction_variable)

for metabolite, coeff in reaction.metabolites.iteritems():
if metabolite.id in constr_terms:
Expand All @@ -528,8 +528,7 @@ def add_reactions(self, reaction_list):
try:
self.solver.constraints[met_id] += expr
except KeyError:
super(SolverBasedModel, self).add_metabolites([metabolites[met_id]])
self.solver.add(self.solver.interface.Constraint(expr, name=met_id, lb=0, ub=0))
self.solver._add_constraint(self.solver.interface.Constraint(expr, name=met_id, lb=0, ub=0))

def remove_reactions(self, the_reactions):
for reaction in the_reactions:
Expand Down
4 changes: 2 additions & 2 deletions cameo/strain_design/heuristic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ def heuristic_method(self, heuristic_method):
def _evaluator(self):
raise NotImplementedError

def run(self, view=config.default_view, **kwargs):
def run(self, view=config.default_view, maximize=True, **kwargs):
t = time.time()
print time.strftime("Starting optimization at %a, %d %b %Y %H:%M:%S", time.localtime(t))
res = self.heuristic_method.evolve(generator=self._generator,
maximize=True,
maximize=maximize,
view=view,
evaluator=self._evaluator,
**kwargs)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_solver_based_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest

import os
from cobra import Metabolite
from optlang import Objective
from cameo import load_model
from cameo.exceptions import UndefinedSolution
Expand Down Expand Up @@ -182,7 +183,6 @@ def test_all_objects_point_to_all_other_correct_objects_after_copy(self):
self.assertEqual(reaction2.get_model(), model)
self.assertEqual(reaction2, model.reactions.get_by_id(reaction2.id))


def test_remove_reactions(self):
reactions_to_remove = self.model.reactions[10:30]
self.assertTrue(all([reaction.get_model() is self.model for reaction in reactions_to_remove]))
Expand All @@ -203,6 +203,12 @@ def test_add_demand(self):
self.model.solver.variables["DemandReaction_" + metabolite.id] in self.model.solver.constraints[
metabolite.id].expression)

def test_add_demand_for_non_existing_metabolite(self):
metabolite = Metabolite(id="a_metabolite")
self.model.add_demand(metabolite)
self.assertTrue(self.model.solver.variables["DM_" + metabolite.id]
in self.model.solver.constraints[metabolite.id].expression)

def test_objective(self):
obj = self.model.objective
self.assertEqual(
Expand Down

0 comments on commit a51c9a7

Please sign in to comment.