Commit 118c4b50 authored by eladnoor's avatar eladnoor

adding functions needed for equilibrator online.

parent 2d4f192b
......@@ -26,14 +26,15 @@
from collections import namedtuple
from typing import Tuple
import re
from typing import Tuple, Set
import numpy as np
from equilibrator_cache import Compound
from equilibrator_cache.reaction import Reaction
from equilibrator_cache.thermodynamic_constants import legendre_transform
from . import Q_, R, ureg
from . import Q_, R, ureg, ccache
......@@ -216,11 +217,26 @@ class PhasedCompound(object):
"""Get the compound's MetaNetX ID."""
return self.compound.mnx_id
def id(self) -> int:
"""Get the compound's equilibrator internal ID."""
def formula(self) -> str:
"""Get the chemical formula."""
return self.compound.formula
def html_formula(self) -> str:
"""Get the chemical formula."""
return re.sub(r'(\d+)', r'<sub>\1</sub>', self.compound.formula)
def mass(self) -> float:
"""Get the chemical molecular mass."""
return self.compound.mass
def phase(self) -> str:
"""Get the phase."""
......@@ -377,3 +393,12 @@ class PhasedCompound(object):
"phase": self.phase,
"ln_abundance": self.ln_abundance,
"microspecies": ms_list}
def first_name(self):
# TODO: use MetaNetX database to get the first name
return ccache.get_compound_first_name(self.compound)
def names(self) -> Set[str]:
return ccache.get_compound_names(self.compound)
......@@ -27,7 +27,7 @@
import logging
from typing import Dict, List, Tuple
from typing import Dict, List, Tuple, KeysView, Iterable
import numpy as np
from equilibrator_cache.reaction import Reaction
......@@ -45,7 +45,8 @@ class PhasedReaction(Reaction):
sparse: Dict[Compound, float],
arrow: str = "<=>",
rid: str = None
rid: str = None,
sparse_with_phases: Dict[PhasedCompound, float] = None
"""Create a PhasedReaction object.
......@@ -54,21 +55,31 @@ class PhasedReaction(Reaction):
:param rid: a string of the reaction ID
super(PhasedReaction, self).__init__(sparse, arrow, rid)
self.sparse_with_phases = \
{PhasedCompound(cpd): coeff for cpd, coeff in sparse.items()}
if rid is not None:
self.reaction_id = rid
if sparse_with_phases is not None:
self.sparse_with_phases = sparse_with_phases
self.sparse_with_phases = \
{PhasedCompound(cpd): coeff for cpd, coeff in sparse.items()}
if self.rid is None:
self.reaction_id = 'COCO:R%05d' % PhasedReaction.REACTION_COUNTER
PhasedReaction.REACTION_COUNTER += 1
def clone(self):
"""Clone this reaction object."""
phased_reaction = PhasedReaction(self.sparse, self.arrow, self.rid)
phased_reaction.sparse_with_phases = dict(
return phased_reaction
return PhasedReaction(self.sparse.copy(), self.arrow, self.rid,
def reverse(self):
sparse = {cpd: -coeff
for (cpd, coeff) in self.sparse.items()}
sparse_with_phases = {cpd: -coeff
for (cpd, coeff) in self.sparse_with_phases.items()}
return PhasedReaction(sparse, self.arrow, self.rid,
def set_abundance(self, compound: Compound, abundance: ureg.Quantity):
"""Set the abundance of the compound."""
......@@ -138,20 +149,23 @@ class PhasedReaction(Reaction):
return 0.0
def add_stoichiometry(self,
cpd: Compound,
compound: Compound,
coeff: float) -> None:
"""Add to the stoichiometric coefficient of a compound.
If this compound is not already in the reaction, add it.
super(PhasedReaction, self).add_stoichiometry(cpd, coeff)
super(PhasedReaction, self).add_stoichiometry(compound, coeff)
for phased_compound in self.sparse_with_phases.keys():
if phased_compound.compound == cpd:
self.sparse_with_phases[phased_compound] += coeff
if phased_compound.compound == compound:
if self.sparse_with_phases[phased_compound] == -coeff:
self.sparse_with_phases[phased_compound] += coeff
self.sparse_with_phases[PhasedCompound(cpd)] = coeff
self.sparse_with_phases[PhasedCompound(compound)] = coeff
@ureg.check(None, None, "[concentration]", "[temperature]")
def separate_stored_dg_prime(
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment