Skip to content

Commit

Permalink
For fun, a chiral center finder, and a couple of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gilleain committed May 12, 2010
1 parent 1308f09 commit 14196cb
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/signature/chemistry/ChiralCenterFinder.java
@@ -0,0 +1,37 @@
package signature.chemistry;

import java.util.ArrayList;
import java.util.List;

public class ChiralCenterFinder {

public static List<Integer> findTetrahedralChiralCenters(Molecule molecule) {
List<Integer> chiralCenterIndices = new ArrayList<Integer>();
MoleculeSignature molSig = new MoleculeSignature(molecule);
List<String> signatureStrings = molSig.getVertexSignatureStrings();
for (int i = 0; i < molecule.getAtomCount(); i++) {
int[] connected = molecule.getConnected(i);
if (connected.length < 4) {
continue;
} else {
String s0 = signatureStrings.get(connected[0]);
String s1 = signatureStrings.get(connected[1]);
String s2 = signatureStrings.get(connected[2]);
String s3 = signatureStrings.get(connected[3]);
if (s0.equals(s1)
|| s0.equals(s2)
|| s0.equals(s3)
|| s1.equals(s2)
|| s1.equals(s3)
|| s2.equals(s3)) {
continue;
} else {
chiralCenterIndices.add(i);
}
}
}

return chiralCenterIndices;
}

}
12 changes: 12 additions & 0 deletions src/main/java/signature/chemistry/Molecule.java
Expand Up @@ -239,10 +239,22 @@ public void addAtom(int i, String symbol) {
this.atoms.add(new Atom(i, symbol));
}

public void addMultipleAtoms(int count, String symbol) {
for (int i = 0; i < count; i++) {
addAtom(symbol);
}
}

public void addSingleBond(int atomNumberA, int atomNumberB) {
this.addBond(atomNumberA, atomNumberB, 1);
}

public void addMultipleSingleBonds(int i, int...js) {
for (int j : js) {
addSingleBond(i, j);
}
}

public void addBond(int atomNumberA, int atomNumberB, int order) {
Atom a = this.atoms.get(atomNumberA);
Atom b = this.atoms.get(atomNumberB);
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/signature/chemistry/ChiralityTest.java
@@ -0,0 +1,78 @@
package signature.chemistry;

import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import signature.SymmetryClass;

public class ChiralityTest {

@Test
public void spiranTest() {
Molecule mol = new Molecule();
mol.addAtom("N");
mol.addMultipleAtoms(12, "C");
mol.addMultipleAtoms(24, "H");
mol.addMultipleSingleBonds(0, 1, 4, 5, 8); // central N
mol.addMultipleSingleBonds(1, 2, 33, 34); // ring 1 - CA
mol.addMultipleSingleBonds(2, 3, 11, 15); // ring 1 - CB
mol.addMultipleSingleBonds(3, 4, 12, 16); // ring 1 - CB
mol.addMultipleSingleBonds(4, 35, 36); // ring 1 - CA
mol.addMultipleSingleBonds(5, 6, 29, 30); // ring 2 - CA
mol.addMultipleSingleBonds(6, 7, 9, 13); // ring 2 - CB
mol.addMultipleSingleBonds(7, 8, 10, 14); // ring 2 - CB
mol.addMultipleSingleBonds(8, 31, 32); // ring 2 - CA
mol.addMultipleSingleBonds(9, 17, 18, 19); // CH3 A
mol.addMultipleSingleBonds(10, 20, 21, 22); // CH3 B
mol.addMultipleSingleBonds(11, 23, 24, 25); // CH3 C
mol.addMultipleSingleBonds(12, 26, 27, 28); // CH3 D

// 7 symmetry classes - central N, CA, CB, CH3-C, HA, HB, CH3-H
MoleculeSignature molSig = new MoleculeSignature(mol);
List<SymmetryClass> symmetryClasses = molSig.getSymmetryClasses();
Assert.assertEquals(7, symmetryClasses.size());

List<Integer> tetraChiralCenters =
ChiralCenterFinder.findTetrahedralChiralCenters(mol);
Assert.assertEquals(4, tetraChiralCenters.size());
Assert.assertEquals(2, (int)tetraChiralCenters.get(0));
Assert.assertEquals(3, (int)tetraChiralCenters.get(1));
Assert.assertEquals(6, (int)tetraChiralCenters.get(2));
Assert.assertEquals(7, (int)tetraChiralCenters.get(3));
}

@Test
public void dichlorocyclopropaneTest() {
Molecule mol = new Molecule();
mol.addAtom("C");
mol.addAtom("C");
mol.addAtom("C");
mol.addAtom("Cl");
mol.addAtom("Cl");
mol.addAtom("H");
mol.addAtom("H");
mol.addAtom("H");
mol.addAtom("H");
mol.addMultipleSingleBonds(0, 1, 2, 3, 5);
mol.addMultipleSingleBonds(1, 2, 4, 6);
mol.addMultipleSingleBonds(2, 7, 8);

// 5 symmetry classes - the non-chiral carbon, its hydrogens, the chiral
// carbons, thier hydrogens, and the chlorines.
MoleculeSignature molSig = new MoleculeSignature(mol);
List<SymmetryClass> symmetryClasses = molSig.getSymmetryClasses();
// System.out.println(symmetryClasses);
Assert.assertEquals(5, symmetryClasses.size());

// only two possible chiral centers
List<Integer> tetraChiralCenters =
ChiralCenterFinder.findTetrahedralChiralCenters(mol);
Assert.assertEquals(2, tetraChiralCenters.size());
Assert.assertEquals(0, (int)tetraChiralCenters.get(0));
Assert.assertEquals(1, (int)tetraChiralCenters.get(1));
}

}

1 comment on commit 14196cb

@egonw
Copy link

@egonw egonw commented on 14196cb May 12, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For fun?!? I love this! Gilleain++

Please sign in to comment.