public function getMolecularFormula()
    {
        $acids = str_split($this->getSequence(), 1);

        $frequency = array(
            'C' => 0,
            'H' => 0,
            'N' => 0,
            'O' => 0,
            'S' => 0
        );

        foreach ($acids as $acid) {
            $composition = AminoAcidComposition::getFormula($acid);

            $matches = array();
            preg_match_all('/([A-Z][a-z]?)([0-9]*)/', $composition, $matches, PREG_SET_ORDER);

            foreach ($matches as $match) {
                $chemical = $match[1];
                $count = $match[2];

                if (! isset($frequency[$chemical])) {
                    $frequency[$chemical] = 0;
                }

                $frequency[$chemical] += $count == '' ? 1 : $count;
            }
        }

        // Remove hydrogen and oxygen from C-TERM
        $frequency['H'] -= count($acids) - 1;
        $frequency['O'] -= count($acids) - 1;

        // Remove hydrogen from N-TERM.
        $frequency['H'] -= count($acids) - 1;

        $formula = '';
        foreach ($frequency as $chemical => $count) {
            if ($count == 0) {
                continue;
            }

            $formula .= $chemical;

            if ($count > 1) {
                $formula .= $count;
            }
        }

        return $formula;
    }