Skip to content

Commit

Permalink
fix: set objective direction
Browse files Browse the repository at this point in the history
  • Loading branch information
hredestig committed Apr 18, 2017
1 parent b95142d commit da79b8a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions cobra/io/sbml3.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def ns(query):
OBJECTIVES_XPATH = ns("fbc:objective[@fbc:id='%s']/"
"fbc:listOfFluxObjectives/"
"fbc:fluxObjective")
LONG_SHORT_DIRECTION = {'maximize': 'max', 'minimize': 'min'}
SHORT_LONG_DIRECTION = {'min': 'minimize', 'max': 'maximize'}

if _with_lxml:
RDF_ANNOTATION_XPATH = ("sbml:annotation/rdf:RDF/"
"rdf:Description[@rdf:about=$metaid]/"
Expand Down Expand Up @@ -365,8 +368,13 @@ def process_gpr(sub_xml):
if obj_list is None:
warn("listOfObjectives element not found")
return model
target_objective = get_attrib(obj_list, "fbc:activeObjective")
obj_query = OBJECTIVES_XPATH % target_objective
target_objective_id = get_attrib(obj_list, "fbc:activeObjective")
target_objective = obj_list.find(
ns("fbc:objective[@fbc:id='{}']".format(target_objective_id)))
obj_direction_long = get_attrib(target_objective, "fbc:type")
obj_direction = LONG_SHORT_DIRECTION[obj_direction_long]

obj_query = OBJECTIVES_XPATH % target_objective_id
coefficients = {}
for sbml_objective in obj_list.findall(obj_query):
rxn_id = clip(get_attrib(sbml_objective, "fbc:reaction"), "R_")
Expand All @@ -380,6 +388,7 @@ def process_gpr(sub_xml):
except ValueError as e:
warn(str(e))
set_objective(model, coefficients)
model.solver.objective.direction = obj_direction
return model


Expand Down Expand Up @@ -412,7 +421,8 @@ def model_to_xml(cobra_model, units=True):
set_attrib(obj_list_tmp, "fbc:activeObjective", "obj")
obj_list_tmp = SubElement(obj_list_tmp, ns("fbc:objective"))
set_attrib(obj_list_tmp, "fbc:id", "obj")
set_attrib(obj_list_tmp, "fbc:type", "maximize")
set_attrib(obj_list_tmp, "fbc:type",
SHORT_LONG_DIRECTION[cobra_model.objective.direction])
flux_objectives_list = SubElement(obj_list_tmp,
ns("fbc:listOfFluxObjectives"))

Expand Down
1 change: 1 addition & 0 deletions cobra/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class TestCobraIO:
def compare_models(cls, name, model1, model2):
assert len(model1.reactions) == len(model2.reactions)
assert len(model1.metabolites) == len(model2.metabolites)
assert model1.objective.direction == model2.objective.direction
for attr in ("id", "name", "lower_bound", "upper_bound",
"objective_coefficient", "gene_reaction_rule"):
assert getattr(model1.reactions[0], attr) == getattr(
Expand Down

0 comments on commit da79b8a

Please sign in to comment.