psamm.formula – Chemical compound formula

Parser and representation of chemical formulas.

Chemical formulas (Formula) are represented as a number of FormulaElements with associated counts. A Formula is itself a FormulaElement so a formula can contain subformulas. This allows some simple structure to be represented.

class psamm.formula.Atom(symbol)

Represent an atom in a chemical formula

>>> hydrogen = Atom.H
>>> oxygen = Atom.O
>>> str(oxygen | 2*hydrogen)
'H2O'
__ge__(other)

x.__ge__(y) <==> x>=y

__gt__(other)

x.__gt__(y) <==> x>y

__le__(other)

x.__le__(y) <==> x<=y

symbol

Atom symbol

>>> Atom.H.symbol
'H'
class psamm.formula.Formula(values={})

Representation of a chemial formula

This is represented as a number of FormulaElements with associated counts.

>>> f = Formula({Atom.C: 6, Atom.H: 12, Atom.O: 6})
>>> str(f)
'C6H12O6'
__mul__(other)

Multiply formula element by other

__or__(other)

Merge formulas into one formula

__unicode__()

Return formula represented using Hill notation system

>>> str(Formula({Atom.C: 6, Atom.H: 12, Atom.O: 6}))
'C6H12O6'
classmethod balance(lhs, rhs)

Return formulas that need to be added to balance given formulas

Given complete formulas for right side and left side of a reaction, calculate formulas for the missing compounds on both sides. Return as a left, right tuple. Formulas can be flattened before balancing to disregard grouping structure.

flattened()

Return formula where subformulas have been flattened

>>> str(Formula.parse('(CH2)(CH2)2').flattened())
'C3H6'
items()

Iterate over (FormulaElement, value)-pairs

classmethod parse(s)

Parse a formula string (e.g. C6H10O2)

class psamm.formula.FormulaElement

Base class representing elements of a formula

__add__(other)

Add formula elements creating subformulas

__mul__(other)

Multiply formula element by other

__or__(other)

Merge formula elements into one formula

repeat(count)

Repeat formula element by creating a subformula

substitute(mapping)

Return formula element with substitutions performed

variables()

Iterator over variables in formula element

class psamm.formula.Radical(symbol)

Represents a radical or other unknown subformula

symbol

Radical symbol

>>> Radical('R1').symbol
'R1'