static IAtomContainer apply ( IAtomContainer container ) { int n = container . getAtomCount ( ) ; int [ ] valences = new int [ n ] ; Map < IAtom , Integer > atomToIndex = Maps . newHashMapWithExpectedSize ( n ) ; for ( IAtom atom : container . atoms ( ) ) atomToIndex . put ( atom , atomToIndex . size ( ) ) ; // compute the bond order sums for ( IBond bond : container . bonds ( ) ) { int u = atomToIndex . get ( bond . getBegin ( ) ) ; int v = atomToIndex . get ( bond . getEnd ( ) ) ; int bondOrder = bond . getOrder ( ) . numeric ( ) ; valences [ u ] += bondOrder ; valences [ v ] += bondOrder ; } for ( int i = 0 ; i < n ; i ++ ) { IAtom atom = container . getAtom ( i ) ; Integer charge = atom . getFormalCharge ( ) ; Integer element = atom . getAtomicNumber ( ) ; if ( element == null ) continue ; // unset = 0 in this case charge = charge == null ? 0 : charge ; int explicit = valences [ i ] ; // if there was a valence read from the mol file use that otherwise // use the default value from the valence model to set the correct // number of implied hydrogens if ( atom . getValency ( ) != null ) { atom . setImplicitHydrogenCount ( atom . getValency ( ) - explicit ) ; } else { int implicit = implicitValence ( element , charge , valences [ i ] ) ; atom . setImplicitHydrogenCount ( implicit - explicit ) ; atom . setValency ( implicit ) ; } } return container ; }
Apply the MDL valence model to the provided atom container .