Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User defined constraints #1228

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ ipython_config.py
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/

# Gradle
.idea/**/gradle.xml
Expand Down
16 changes: 3 additions & 13 deletions .idea/cobrapy.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions src/cobra/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import logging
from copy import copy, deepcopy
from functools import partial
from typing import List
from warnings import warn

import optlang
from optlang import Constraint, Variable
from optlang.container import Container
from optlang.symbolics import Basic, Zero

from cobra.core.configuration import Configuration
Expand Down Expand Up @@ -60,6 +63,8 @@ class Model(Object):
groups : DictList
A DictList where the key is the group identifier and the value a
Group
user_defined_const : UserDefinedConstraint
A Dictlist to store UserDefinedConstraints
solution : Solution
The last obtained solution from optimizing the model.

Expand Down Expand Up @@ -97,6 +102,8 @@ def __init__(self, id_or_model=None, name=None):
self.reactions = DictList() # A list of cobra.Reactions
self.metabolites = DictList() # A list of cobra.Metabolites
self.groups = DictList() # A list of cobra.Groups
self.user_defined_const = Container() # A list of UserDefinedConstraints
self._const_ids = set()
# genes based on their ids {Gene.id: Gene}
self._compartments = {}
self._contexts = []
Expand Down Expand Up @@ -326,6 +333,7 @@ def copy(self):
"notes",
"annotation",
"groups",
"user_defined_const",
}
for attr in self.__dict__:
if attr not in do_not_copy_by_ref:
Expand Down Expand Up @@ -403,6 +411,8 @@ def copy(self):
new_objects.append(new_object)
new_group.add_members(new_objects)

new.user_defined_const = deepcopy(self.user_defined_const)

try:
new._solver = deepcopy(self.solver)
# Cplex has an issue with deep copies
Expand Down Expand Up @@ -870,6 +880,77 @@ def get_associated_groups(self, element):
# check whether the element is associated with the model
return [g for g in self.groups if element in g.members]

def add_user_defined_constraints(self, constraints: List[Constraint]) -> None:
"""Adds User defined constraints (FBC V3) to the model

Parameters
----------
constraints: list
a list of UserDefinedConstraints
"""

if isinstance(constraints, Constraint):
logger.warning(
f"The constraints passed must be inside a list: {constraints}")
constraints = [constraints]
if not isinstance(constraints, list):
# if single Constraint, convert to a list
raise TypeError(
f"The constraints passed must be inside a list: {constraints}"
)

list_of_var_cons = []
for constraint in constraints:
constraint: "Constraint"
if not isinstance(constraint, Constraint):
raise TypeError(
f"The user defined constraints passed must be of "
f"type 'Constraints': {constraint}"
)

variable_substituions = dict()
for variable in constraint.variables:
if self.variables.get(variable.name, None):
variable_substituions[variable] = self.variables.get(variable.name)
if variable.name in self.reactions:
variable_substituions[variable] = self.reactions.get_by_id(variable.name).flux_expression
constraint._expression = constraint.expression.xreplace(variable_substituions)

list_of_var_cons.append(constraint)
self.user_defined_const.has_key(constraint.name) or self.user_defined_const.append(constraint)
self.add_cons_vars(list_of_var_cons)

def remove_user_defined_constraints(self, constraints: List[Constraint]) -> None:
"""Remove the constraints from the model

Parameters
----------
constraints: list
a list of UserDefinedConstraints
"""
if isinstance(constraints, Constraint):
logger.warning(
f"The constraints passed must be inside a list: {constraints}")
constraints = [constraints]
if not isinstance(constraints, list):
# if single Constraint, convert to a list
raise TypeError(
f"The constraints passed must be inside a list: {constraints}"
)

cons_to_remove = []
for constraint in constraints:
constraint: "Constraint"
if not isinstance(constraint, Constraint):
raise TypeError(
f"The user defined constraints passed must be of "
f"type 'Constraint': {constraint}"
)
self._const_ids.remove(constraint.name)
cons_to_remove = self.constraints[constraint.name]
self.user_defined_const.__delitem__(constraint.name)
self.remove_cons_vars(cons_to_remove)

def add_cons_vars(self, what, **kwargs):
"""Add constraints and variables to the model's mathematical problem.

Expand Down
Loading