psamm.reaction – Reaction equations and compounds

Definitions related to reaction equations and parsing of such equations.

class psamm.reaction.Compound(name, compartment=None, arguments=())

Represents a compound in a reaction equation

A compound is a named entity in the reaction equations representing a chemical compound. A compound can represent a generalized chemical entity (e.g. polyphosphate) and the arguments can be used to instantiate a specific chemical entity (e.g. polyphosphate(3)) by passing a number as an argument or a partially specified entity by passing an expression (e.g. polyphosphate(n)).


Expression argument for generalized compounds


Compartment of compound


Return an instance of this compound in the specified compartment

>>> Compound('H+').in_compartment('e')
Compound('H+', 'e')

Name of compound


Translate compound name using given function

>>> Compound('Pb').translate(lambda x: x.lower())
class psamm.reaction.Direction

Directionality of reaction equation.


Return the flipped version of this direction.


Whether this direction includes forward direction.


Whether this direction includes reverse direction.


Return string symbol for direction.

class psamm.reaction.Reaction(*args)

Reaction equation representation.

Each compound is associated with a stoichiometric value and the reaction has a Direction. The reaction is created in one of the three following ways.

It can be created from a direction and two iterables of compound, value pairs representing the left-hand side and the right-hand side of the reaction:

>>> r = Reaction(Direction.Both, [(Compound('A'), 1), (Compound('B', 2))],
                                 [(Compound('C'), 1)])
>>> str(r)
'|A| + (2) |B| <=> |C|'

It can also be created from a single dict or iterable of compound, value pairs where the left-hand side compounds have negative values and the right-hand side compounds have positive values:

>>> r = Reaction(Direction.Forward, {
        Compound('A'): -1,
        Compound('B'): -2,
        Compound('C'): 1
>>> str(r)
'|A| + (2) |B| <=> |C|'

Lastly, the reaction can be created from an existing reaction object, creating a copy of that reaction.

>>> r = Reaction(Direction.Forward, {Compound('A'): -1, Compound('B'): 1})
>>> r2 = Reaction(r)
>>> str(r2)
'|A| => |B|'

Reactions can be added to produce combined reactions.

>>> r = Reaction(Direction.Forward, {Compound('A'): -1, Compound('B'): 1})
>>> s = Reaction(Direction.Forward, {Compound('B'): -1, Compound('C'): 1})
>>> str(r + s)
'|A| => |C|'

Reactions can also be multiplied by a number to produce a new reaction with scaled stoichiometry.

>>> r = Reaction(Direction.Forward, {Compound('A'): -1, Compound('B'): 2})
>>> str(2 * r)
'(2) |A| => (4) |B|'

Multiplying with a negative value will also flip the reaction, and as a special case, negating a reaction will simply flip it.

>>> r = Reaction(Direction.Forward, {Compound('A'): -1, Compound('B'): 2})
>>> str(r)
'|A| => (2) |B|'
>>> str(-r)
'(2) |B| <= |A|'

Sequence of compounds on both sides of the reaction equation

The sign of the stoichiometric values reflect whether the compound is on the left-hand side (negative) or the right-hand side (positive).


Direction of reaction equation


Compounds on the left-hand side of the reaction equation.


Return normalized reaction

The normalized reaction will be bidirectional or a forward reaction (i.e. reverse reactions are flipped).


Compounds on the right-hand side of the reaction equation.


Return reaction where compound names have been translated.

For each compound the translate function is called with the compound name and the returned value is used as the new compound name. A new reaction is returned with the substituted compound names.