public static IAtomContainerSet detect ( IAtomContainer ac ) { IAtomContainerSet piSystemSet = ac . getBuilder ( ) . newInstance ( IAtomContainerSet . class ) ; for ( int i = 0 ; i < ac . getAtomCount ( ) ; i ++ ) { IAtom atom = ac . getAtom ( i ) ; atom . setFlag ( CDKConstants . VISITED , false ) ; } for ( int i = 0 ; i < ac . getAtomCount ( ) ; i ++ ) { IAtom firstAtom = ac . getAtom ( i ) ; // if this atom was already visited in a previous DFS, continue if ( firstAtom . getFlag ( CDKConstants . VISITED ) || checkAtom ( ac , firstAtom ) == - 1 ) { continue ; } IAtomContainer piSystem = ac . getBuilder ( ) . newInstance ( IAtomContainer . class ) ; Stack < IAtom > stack = new Stack < IAtom > ( ) ; piSystem . addAtom ( firstAtom ) ; stack . push ( firstAtom ) ; firstAtom . setFlag ( CDKConstants . VISITED , true ) ; // Start DFS from firstAtom while ( ! stack . empty ( ) ) { //boolean addAtom = false; IAtom currentAtom = stack . pop ( ) ; List < IAtom > atoms = ac . getConnectedAtomsList ( currentAtom ) ; List < IBond > bonds = ac . getConnectedBondsList ( currentAtom ) ; for ( int j = 0 ; j < atoms . size ( ) ; j ++ ) { IAtom atom = atoms . get ( j ) ; IBond bond = bonds . get ( j ) ; if ( ! atom . getFlag ( CDKConstants . VISITED ) ) { int check = checkAtom ( ac , atom ) ; if ( check == 1 ) { piSystem . addAtom ( atom ) ; piSystem . addBond ( bond ) ; continue ; // do not mark atom as visited if cumulative double bond } else if ( check == 0 ) { piSystem . addAtom ( atom ) ; piSystem . addBond ( bond ) ; stack . push ( atom ) ; } atom . setFlag ( CDKConstants . VISITED , true ) ; } // close rings with one bond else if ( ! piSystem . contains ( bond ) && piSystem . contains ( atom ) ) { piSystem . addBond ( bond ) ; } } } if ( piSystem . getAtomCount ( ) > 2 ) { piSystemSet . addAtomContainer ( piSystem ) ; } } return piSystemSet ; }
Detect all conjugated pi systems in an AtomContainer . This method returns a AtomContainerSet with Atom and Bond objects from the original AtomContainer . The aromaticity has to be known before calling this method .