public static boolean adjustProtonation ( IMolecularFormula mf , int hcnt ) { if ( mf == null ) throw new NullPointerException ( "No formula provided" ) ; if ( hcnt == 0 ) return false ; // no protons to add final IChemObjectBuilder bldr = mf . getBuilder ( ) ; final int chg = mf . getCharge ( ) != null ? mf . getCharge ( ) : 0 ; IIsotope proton = null ; int pcount = 0 ; for ( IIsotope iso : mf . isotopes ( ) ) { if ( "H" . equals ( iso . getSymbol ( ) ) ) { final int count = mf . getIsotopeCount ( iso ) ; if ( count < hcnt ) continue ; // acceptable if ( proton == null && ( iso . getMassNumber ( ) == null || iso . getMassNumber ( ) == 1 ) ) { proton = iso ; pcount = count ; } // better else if ( proton != null && iso . getMassNumber ( ) != null && iso . getMassNumber ( ) == 1 && proton . getMassNumber ( ) == null ) { proton = iso ; pcount = count ; } } } if ( proton == null && hcnt < 0 ) { return false ; } else if ( proton == null && hcnt > 0 ) { proton = bldr . newInstance ( IIsotope . class , "H" ) ; proton . setMassNumber ( 1 ) ; } mf . removeIsotope ( proton ) ; if ( pcount + hcnt > 0 ) mf . addIsotope ( proton , pcount + hcnt ) ; mf . setCharge ( chg + hcnt ) ; return true ; }
Adjust the protonation of a molecular formula . This utility method adjusts the hydrogen isotope count and charge at the same time .