private void placeSgroupBrackets ( IAtomContainer mol ) { List < Sgroup > sgroups = mol . getProperty ( CDKConstants . CTAB_SGROUPS ) ; if ( sgroups == null ) return ; // index all crossing bonds final Multimap < IBond , Sgroup > bondMap = HashMultimap . create ( ) ; final Map < IBond , Integer > counter = new HashMap <> ( ) ; for ( Sgroup sgroup : sgroups ) { if ( ! hasBrackets ( sgroup ) ) continue ; for ( IBond bond : sgroup . getBonds ( ) ) { bondMap . put ( bond , sgroup ) ; counter . put ( bond , 0 ) ; } } sgroups = new ArrayList <> ( sgroups ) ; // place child sgroups first Collections . sort ( sgroups , new Comparator < Sgroup > ( ) { @ Override public int compare ( Sgroup o1 , Sgroup o2 ) { if ( o1 . getParents ( ) . isEmpty ( ) != o2 . getParents ( ) . isEmpty ( ) ) { if ( o1 . getParents ( ) . isEmpty ( ) ) return + 1 ; return - 1 ; } return 0 ; } } ) ; for ( Sgroup sgroup : sgroups ) { if ( ! hasBrackets ( sgroup ) ) continue ; final Set < IAtom > atoms = sgroup . getAtoms ( ) ; final Set < IBond > xbonds = sgroup . getBonds ( ) ; // clear all the existing brackets sgroup . putValue ( SgroupKey . CtabBracket , null ) ; // assign brackets to crossing bonds if ( xbonds . size ( ) >= 2 ) { // check for vertical alignment boolean vert = true ; for ( IBond bond : xbonds ) { final double theta = angle ( bond ) ; if ( Math . abs ( Math . toDegrees ( theta ) ) > 40 && Math . abs ( Math . toDegrees ( theta ) ) < 140 ) { vert = false ; break ; } } for ( IBond bond : xbonds ) sgroup . addBracket ( newCrossingBracket ( bond , bondMap , counter , vert ) ) ; } // <= 1 crossing bonds so simply wrap the entire fragment else { IAtomContainer tmp = mol . getBuilder ( ) . newInstance ( IAtomContainer . class ) ; for ( IAtom atom : atoms ) tmp . addAtom ( atom ) ; double [ ] minmax = GeometryUtil . getMinMax ( tmp ) ; double padding = 0.7 * bondLength ; sgroup . addBracket ( new SgroupBracket ( minmax [ 0 ] - padding , minmax [ 1 ] - padding , minmax [ 0 ] - padding , minmax [ 3 ] + padding ) ) ; sgroup . addBracket ( new SgroupBracket ( minmax [ 2 ] + padding , minmax [ 1 ] - padding , minmax [ 2 ] + padding , minmax [ 3 ] + padding ) ) ; } } }
Place and update brackets for polymer Sgroups .