Skip to content
Open
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
32 changes: 32 additions & 0 deletions doc/thermoDB.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ with :any:`zlib.decompress` then load the result into memory with
calling :any:`open` with the ``b`` flag, otherwise Python will try to
decode it as unicode and raise an exception !

Building a database from the ModelSEEDDatabase
----------------------------------------------

:mod:`pytfa.thermo.modelseed` builds a thermodynamic database from a local
clone of the `ModelSEEDDatabase <https://github.com/ModelSEED/ModelSEEDDatabase>`_,
located with the ``db_path`` argument, the ``MODELSEED_DB_PATH`` environment
variable, or the configuration of modelseedpy (``pip install .[modelseed]``)::

from pytfa.io import apply_compartment_data
from pytfa.thermo.modelseed import annotate_seed_ids, \
build_thermo_from_modelseed, build_compartment_data

annotate_seed_ids(cobra_model)
thermo_data = build_thermo_from_modelseed(cobra_model)
tmodel = pytfa.ThermoModel(thermo_data, cobra_model)
apply_compartment_data(tmodel, build_compartment_data(tmodel))

* ``annotate_seed_ids`` sets the ``seed_id`` annotation from ModelSEED
metabolite ids (``cpd00002_c0``) or from ``seed.compound`` annotations.
* The energies are the ``Group contribution`` estimates of the compounds, in
kcal/mol. Each is paired with the protonation state of the metabolite in the
model, shifted along the compound's pKas so that its transformed energy is
unchanged. :meth:`~pytfa.ThermoModel.prepare` then leaves the reactions as the
model balances them.
* The pKas are Marvin's acidic pKas.
* There are no cues: reaction errors combine the compound errors in quadrature.
* ``build_compartment_data`` defaults to modelseedpy's values: pH 7 and -160 mV
in the cytosol, pH 6.5 and 0 mV in the extracellular space, and
concentrations between 1e-6 and 0.02 M.

See ``tutorials/tutorial_modelseed.py``.

Structure of a thermodynamic database
-------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion pytfa/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def print_info(self):

info = pd.DataFrame(columns=['value'])
info.loc['name'] = self.name
info.loc['description'] = self.description
# cobra models no longer have a description
info.loc['description'] = getattr(self, 'description', '')
info.loc['num constraints'] = n_constraints
info.loc['num variables'] = n_variables
info.loc['num metabolites'] = n_metabolites
Expand Down
4 changes: 3 additions & 1 deletion pytfa/io/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ def get_solver_string(model):

def obj_to_dict(model):
if model.objective.expression:
# The coefficients dict of the expression also holds its constant term
return {x.name: float(v)
for x,v in model.objective.expression.as_coefficients_dict().items()}
for x,v in model.objective.get_linear_coefficients(
model.objective.variables).items()}
else:
return 0

Expand Down
8 changes: 4 additions & 4 deletions pytfa/optim/relaxation.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def relax_dgo(tmodel, reactions_to_ignore=(), solver=None, in_place = False):
# Create a copy of the cobra_model on which we will perform the slack addition
slack_model = deepcopy(tmodel)
slack_model.solver = solver
slack_model.name = 'SlackModel '+tmodel.name
slack_model.id = 'SlackModel_'+tmodel.id
slack_model.name = 'SlackModel {}'.format(tmodel.name)
slack_model.id = 'SlackModel_{}'.format(tmodel.id)

# Ensure the lazy updates are all done
slack_model.repair()
Expand All @@ -79,8 +79,8 @@ def relax_dgo(tmodel, reactions_to_ignore=(), solver=None, in_place = False):
# Create a copy that will receive the relaxation
relaxed_model = deepcopy(tmodel)
relaxed_model.solver = solver
relaxed_model.name = 'RelaxedModel '+tmodel.name
relaxed_model.id = 'RelaxedModel_'+tmodel.id
relaxed_model.name = 'RelaxedModel {}'.format(tmodel.name)
relaxed_model.id = 'RelaxedModel_{}'.format(tmodel.id)
relaxed_model.repair()
else:
relaxed_model = slack_model
Expand Down
12 changes: 6 additions & 6 deletions pytfa/optim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ def symbol_sum(variables):
:return:
"""

from sympy import Add

# optlang's add builds symengine expressions when optlang uses symengine
from optlang.symbolics import add, Basic

k=0
# If we encounter a zero, which is a special type, increase k
while isinstance(variables[k], Zero) and k<len(variables):
Expand All @@ -119,12 +120,11 @@ def symbol_sum(variables):
return 0

if isinstance(variables[k], GenericVariable):
return Add(*[x.variable for x in variables])
return add([x.variable for x in variables])
elif isinstance(variables[k], optlang.interface.Variable) or \
isinstance(variables[k], sympy.Mul) or \
isinstance(variables[k], sympy.Add) or \
isinstance(variables[k], (Basic, sympy.Basic)) or \
isinstance(variables[k], Number):
return Add(*variables)
return add(variables)
else:
raise ValueError('Arguments should be of type Number, sympy.Add, or sympy.Mul, '
'or optlang.Variable, or GenericVariable')
Expand Down
Loading