Created
April 8, 2009 14:49
-
-
Save gilleain/91809 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stereo; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.openscience.cdk.graph.invariant.MorganNumbersTools; | |
import org.openscience.cdk.interfaces.IAtom; | |
import org.openscience.cdk.interfaces.IAtomContainer; | |
import org.openscience.cdk.interfaces.IAtomParity; | |
public class StereoDetector { | |
/** | |
* Find stereo centers based on the connectivity alone, using Morgan numbers | |
* | |
* @param container | |
* the atom container to search | |
* | |
* @return a list of IAtomParity objects representing possible stereocenters | |
*/ | |
public static List<IAtomParity> findStereoCenters(IAtomContainer container) { | |
List<IAtomParity> centers = new ArrayList<IAtomParity>(); | |
long[] morganNumbers = MorganNumbersTools.getMorganNumbers(container); | |
for (IAtom atom : container.atoms()) { | |
List<IAtom> neighbours = container.getConnectedAtomsList(atom); | |
if (neighbours.size() != 4) continue; | |
IAtom first = neighbours.get(0); | |
IAtom second = neighbours.get(1); | |
IAtom third = neighbours.get(2); | |
IAtom fourth = neighbours.get(3); | |
// a 'half-matrix' all-v-all comparison | |
if (isSame(first, second, container, morganNumbers) | |
|| isSame(first, third, container, morganNumbers) | |
|| isSame(first, fourth, container, morganNumbers) | |
|| isSame(second, third, container, morganNumbers) | |
|| isSame(second, fourth, container, morganNumbers) | |
|| isSame(third, fourth, container, morganNumbers)) { | |
continue; | |
} else { | |
int parity = -1; // hmmm. | |
IAtomParity center = | |
container.getBuilder().newAtomParity( | |
atom, first, second, third, fourth, parity); | |
centers.add(center); | |
} | |
} | |
return centers; | |
} | |
public static boolean isSame(IAtom a, IAtom b, IAtomContainer container, long[] morganNumbers) { | |
int indexA = container.getAtomNumber(a); | |
int indexB = container.getAtomNumber(b); | |
long morganNumberA = morganNumbers[indexA]; | |
long morganNumberB = morganNumbers[indexB]; | |
return morganNumberA == morganNumberB | |
&& a.getSymbol().equals(b.getSymbol()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment