The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Chemistry::File::Formula - Molecular formula reader/formatter

SYNOPSIS

    use Chemistry::File::Formula;

    my $mol = Chemistry::Mol->parse("H2O");
    print $mol->print(format => formula);
    print $mol->formula;    # this is a shorthand for the above 
    print $mol->print(format => formula, 
        formula_format => "%s%d{<sub>%d</sub>});

DESCRIPTION

This module converts a molecule object to a string with the formula. It registers the 'formula' format with Chemistry::Mol. Besides its obvious use, it is included in the Chemistry::Mol distribution because it is a very simple example of a Chemistry::File derived I/O module.

The format can be specified as a printf-like string with the following control sequences, which are specified with the formula_format parameter to $mol->print or $mol->write.

%s symbol
%D number of atoms
%d number of atoms, included only when it is greater than one
%d{substr} substr is only included when number of atoms is greater than one
%j{substr} substr is inserted between the formatted string for each element. (The 'j' stands for 'joiner'). The format should have only one joiner, and its location in the string doesn't matter.
%% a percent sign

If no format is specified, the default is "%s%d". Some examples follow. Let's assume that the formula is C2H6O, as it would be formatted by default.

%s%D

Like the default, but include explicit indices for all atoms. The formula would be formatted as "C2H6O1"

%s%d{<sub>%d</sub>}

HTML format. The output would be "C<sub>2</sub>H<sub>6</sub>O".

%D %s%j{, }

Use a comma followed by a space as a joiner. The output would be "2 C, 6 H, 1 O".

Parsing Formulas

Formulas can also be parsed back into Chemistry::Mol objects. The formula may have parentheses and square or triangular brackets, and it may have the following abbreviations:

    Me => '(CH3)',
    Et => '(CH3CH2)',
    Bu => '(C4H9)',
    Bn => '(C6H5CH2)',
    Cp => '(C5H5)',
    Ph => '(C6H5)',
    Bz => '(C6H5CO)',

The formula may also be preceded by a number, which multiplies the whole formula. Some examples of valid formulas:

CH3(CH2)3CH3. Equivalent to C5H12.
C6H3Me3. Equivalent to C9H12.
2Cu[NH3]4(NO3)2. Equivalent to Cu2H24N12O12.
2C(C[C5]4)3. Equivalent to C152 (kind of silly example...).
2C(C(C(C)5)4)3. Equivalent to C152.
C 1 0 H 2 2. Equivalent to C10H22. Note that whitespace is completely ignored.

When a formula is parsed, a molecule object is created which consists of the set of the atoms in the formula (no bonds or coordinates, of course). The atoms are created in alphabetical order, so the molecule object for C2H5Br would have the atoms in the following sequence: Br, C, C, H, H, H, H, H.

VERSION

0.26

SEE ALSO

Chemistry::Mol, Chemistry::File

The PerlMol website http://www.perlmol.org/

AUTHOR

Ivan Tubert-Brohman <itub@cpan.org>.

Formula parsing code contributed by Brent Gregersen.