private static IMolecularFormula getMolecularFormula ( String stringMF , IMolecularFormula formula , boolean assumeMajorIsotope ) { if ( stringMF . contains ( "." ) || stringMF . contains ( "(" ) || stringMF . length ( ) > 0 && stringMF . charAt ( 0 ) >= ' ' && stringMF . charAt ( 0 ) <= ' ' ) stringMF = simplifyMolecularFormula ( stringMF ) ; // Extract charge from String when contains []X- format Integer charge = null ; if ( ( stringMF . contains ( "[" ) && stringMF . contains ( "]" ) ) && ( stringMF . contains ( "+" ) || stringMF . contains ( HYPHEN_STR ) || stringMF . contains ( MINUS_STR ) ) ) { charge = extractCharge ( stringMF ) ; stringMF = cleanMFfromCharge ( stringMF ) ; } if ( stringMF . isEmpty ( ) ) return null ; int len = stringMF . length ( ) ; CharIter iter = new CharIter ( ) ; iter . str = stringMF ; while ( iter . pos < len ) { if ( ! parseIsotope ( iter , formula , assumeMajorIsotope ) ) { LoggingToolFactory . createLoggingTool ( MolecularFormulaManipulator . class ) . error ( "Could not parse MF: " + iter . str ) ; return null ; } } if ( charge != null ) formula . setCharge ( charge ) ; return formula ; }
Add to an instance of IMolecularFormula the elements extracts form molecular formula string . The string is immediately analyzed and a set of Nodes is built based on this analysis . The hydrogens are assumed to be implicit . The boolean indicates if the major isotope is to be assumed or if no assumption is to be made .