@@ -1599,7 +1599,7 @@ static Map<IBond, IAtomContainer> ringPreferenceMap(IAtomContainer container,
15991599
16001600 final List <IAtomContainer > rings = AtomContainerSetManipulator .getAllAtomContainers (smallest );
16011601
1602- Collections .sort (rings , new RingBondOffsetComparator ());
1602+ Collections .sort (rings , new RingBondOffsetComparator (container ));
16031603
16041604 final Map <IBond , IAtomContainer > ringMap = new HashMap <IBond , IAtomContainer >();
16051605
@@ -1689,29 +1689,52 @@ static final class RingBondOffsetComparator implements Comparator<IAtomContainer
16891689 }
16901690 }
16911691
1692+ private final boolean hasMetal ;
1693+
16921694 /**
16931695 * Create a new comparator.
16941696 */
1695- RingBondOffsetComparator () {}
1697+ RingBondOffsetComparator (IAtomContainer mol ) {
1698+ hasMetal = hasMetal (mol );
1699+ }
1700+
1701+ RingBondOffsetComparator () {
1702+ hasMetal = false ;
1703+ }
1704+
1705+ private static boolean hasMetal (IAtomContainer mol ) {
1706+ for (IAtom atom : mol .atoms ())
1707+ if (Elements .isMetal (atom ))
1708+ return true ;
1709+ return false ;
1710+ }
16961711
16971712 /**
16981713 *{@inheritDoc}
16991714 */
17001715 @ Override
1701- public int compare (IAtomContainer containerA , IAtomContainer containerB ) {
1716+ public int compare (IAtomContainer ringa , IAtomContainer ringb ) {
1717+
1718+ // non-metal rings (e.g. carbo/hetro cycles first)
1719+ if (hasMetal ) {
1720+ int cmp = Boolean .compare (hasMetal (ringa ),
1721+ hasMetal (ringb ));
1722+ if (cmp != 0 )
1723+ return cmp ;
1724+ }
17021725
1703- // first order by size
1704- int sizeCmp = Ints .compare (sizePreference (containerA .getAtomCount ()),
1705- sizePreference (containerB .getAtomCount ()));
1726+ // order by size 6,5,7,4,3,rest
1727+ int sizeCmp = Ints .compare (sizePreference (ringa .getAtomCount ()),
1728+ sizePreference (ringb .getAtomCount ()));
17061729 if (sizeCmp != 0 ) return sizeCmp ;
17071730
17081731 // now order by number of double bonds
1709- int piBondCmp = Ints .compare (nDoubleBonds (containerA ), nDoubleBonds (containerB ));
1732+ int piBondCmp = Ints .compare (nDoubleBonds (ringa ), nDoubleBonds (ringb ));
17101733 if (piBondCmp != 0 ) return -piBondCmp ;
17111734
17121735 // order by element frequencies, all carbon rings are preferred
1713- int [] freqA = countLightElements (containerA );
1714- int [] freqB = countLightElements (containerB );
1736+ int [] freqA = countLightElements (ringa );
1737+ int [] freqB = countLightElements (ringb );
17151738
17161739 for (Elements element : Arrays .asList (Elements .Carbon , Elements .Nitrogen , Elements .Oxygen , Elements .Sulfur ,
17171740 Elements .Phosphorus )) {
0 commit comments