public static boolean replaceAtomByAtom ( final IAtomContainer container , final IAtom oldAtom , final IAtom newAtom ) { if ( oldAtom == null ) throw new NullPointerException ( "Atom to be replaced was null!" ) ; if ( newAtom == null ) throw new NullPointerException ( "Replacement atom was null!" ) ; final int idx = container . indexOf ( oldAtom ) ; if ( idx < 0 ) return false ; container . setAtom ( idx , newAtom ) ; List < Sgroup > sgrougs = container . getProperty ( CDKConstants . CTAB_SGROUPS ) ; if ( sgrougs != null ) { boolean updated = false ; List < Sgroup > replaced = new ArrayList <> ( ) ; for ( Sgroup org : sgrougs ) { if ( org . getAtoms ( ) . contains ( oldAtom ) ) { updated = true ; Sgroup cpy = new Sgroup ( ) ; for ( IAtom atom : org . getAtoms ( ) ) { if ( ! oldAtom . equals ( atom ) ) cpy . addAtom ( atom ) ; else cpy . addAtom ( newAtom ) ; } for ( IBond bond : org . getBonds ( ) ) cpy . addBond ( bond ) ; for ( Sgroup parent : org . getParents ( ) ) cpy . addParent ( parent ) ; for ( SgroupKey key : org . getAttributeKeys ( ) ) cpy . putValue ( key , org . getValue ( key ) ) ; replaced . add ( cpy ) ; } else { replaced . add ( org ) ; } } if ( updated ) { container . setProperty ( CDKConstants . CTAB_SGROUPS , Collections . unmodifiableList ( replaced ) ) ; } } return true ; }
Substitute one atom in a container for another adjusting bonds single electrons lone pairs and stereochemistry as required .