Skip to content

Commit

Permalink
Migrate PartialTChargeMMFF94DescriptorTest to a simpler, InChi input …
Browse files Browse the repository at this point in the history
…based approach

Signed-off-by: John May <john.wilkinsonmay@gmail.com>
  • Loading branch information
mjw99 authored and johnmay committed Jul 18, 2014
1 parent 0e641e2 commit 88a54c6
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 140 deletions.
6 changes: 6 additions & 0 deletions descriptor/qsaratomic/pom.xml
Expand Up @@ -192,6 +192,12 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>cdk-inchi</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Expand Up @@ -26,16 +26,17 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openscience.cdk.CDKConstants;

import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.config.Elements;

import org.openscience.cdk.charges.MMFF94PartialCharges;
import org.openscience.cdk.exception.CDKException;

import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.qsar.IAtomicDescriptor;
import org.openscience.cdk.qsar.result.DoubleResult;

import org.openscience.cdk.inchi.InChIGeneratorFactory;
import org.openscience.cdk.inchi.InChIToStructure;

/**
* TestSuite that runs all QSAR tests.
Expand All @@ -47,7 +48,57 @@ public class PartialTChargeMMFF94DescriptorTest extends AtomicDescriptorTest {

private final double METHOD_ERROR = 0.16;

private final IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();

private final double[] AtomInChIToMMFF94PartialCharges(String InChI) {

InChIGeneratorFactory factory = null;

try {
factory = InChIGeneratorFactory.getInstance();
} catch (CDKException e2) {

e2.printStackTrace();
}

InChIToStructure parser = null;
try {
parser = factory.getInChIToStructure(
InChI, DefaultChemObjectBuilder.getInstance()
);
} catch (CDKException e1) {

e1.printStackTrace();
}

IAtomContainer ac = parser.getAtomContainer();
try {
addExplicitHydrogens(ac);
} catch (Exception e) {

e.printStackTrace();
}

MMFF94PartialCharges mmff = new MMFF94PartialCharges();
try {
mmff.assignMMFF94PartialCharges(ac);
} catch (Exception e) {

e.printStackTrace();
}

double[] testResult = new double[ac.getAtomCount()];
int i = 0;
for (IAtom atom : ac.atoms()) {

//System.out.println(atom.getAtomTypeName() + " " + atom.getProperty("MMFF94charge").toString());
testResult[i] = atom.getProperty("MMFF94charge");
i++;

}

return testResult;

}

/**
* Constructor for the PartialTChargeMMFF94DescriptorTest object
Expand All @@ -72,176 +123,82 @@ public void setUp() throws Exception {
* A unit test for JUnit with Methanol
*/
@Test
public void testPartialTotalChargeDescriptor_Methanol() throws ClassNotFoundException, CDKException, java.lang.Exception {
double [] testResult={0.28,-0.68,0.0,0.0,0.0,0.4};
IAtomicDescriptor descriptor = new PartialTChargeMMFF94Descriptor();

IAtomContainer mol = builder.newInstance(IAtomContainer.class);
IAtom carbon = builder.newInstance(IAtom.class,Elements.CARBON);
IAtom oxygen = builder.newInstance(IAtom.class,Elements.OXYGEN);
// making sure the order matches the test results
mol.addAtom(carbon);
mol.addAtom(oxygen);
mol.addBond(builder.newInstance(IBond.class,carbon, oxygen, CDKConstants.BONDORDER_SINGLE));
addExplicitHydrogens(mol);

for (int i = 0 ; i < mol.getAtomCount() ; i++){
double result= ((DoubleResult)descriptor.calculate(mol.getAtom(i),mol).getValue()).doubleValue();
Assert.assertEquals(testResult[i],result,METHOD_ERROR);
}
public void testPartialTotalChargeDescriptor_Methanol()
throws ClassNotFoundException, CDKException, java.lang.Exception {

double[] expectedResult = { 0.28, -0.68, 0.0, 0.0, 0.0, 0.4 };
double[] actualResult = AtomInChIToMMFF94PartialCharges("InChI=1S/CH4O/c1-2/h2H,1H3");

Assert.assertArrayEquals(expectedResult, actualResult, METHOD_ERROR);

}
/**
* A unit test for JUnit with Methylamine
*/
@Test
public void testPartialTotalChargeDescriptor_Methylamine() throws ClassNotFoundException, CDKException, java.lang.Exception {
double [] testResult={0.27,-0.99,0.0,0.0,0.0,0.36,0.36};
IAtomicDescriptor descriptor = new PartialTChargeMMFF94Descriptor();

IAtomContainer mol = builder.newInstance(IAtomContainer.class);
IAtom carbon = builder.newInstance(IAtom.class,Elements.CARBON);
IAtom nitrogen = builder.newInstance(IAtom.class,Elements.NITROGEN);
// making sure the order matches the test results
mol.addAtom(carbon);
mol.addAtom(nitrogen);
mol.addBond(builder.newInstance(IBond.class,carbon, nitrogen, CDKConstants.BONDORDER_SINGLE));
addExplicitHydrogens(mol);

for (int i = 0 ; i < 7 ; i++){
double result= ((DoubleResult)descriptor.calculate(mol.getAtom(i),mol).getValue()).doubleValue();
Assert.assertEquals(testResult[i],result,METHOD_ERROR);
}
double[] expectedResult={0.27,-0.99,0.0,0.0,0.0,0.36,0.36};
double[] actualResult = AtomInChIToMMFF94PartialCharges("InChI=1S/CH5N/c1-2/h2H2,1H3");

Assert.assertArrayEquals(expectedResult, actualResult, METHOD_ERROR);

}
/**
* A unit test for JUnit with ethoxyethane
*/
@Test
public void testPartialTotalChargeDescriptor_Ethoxyethane() throws ClassNotFoundException, CDKException, java.lang.Exception {
double [] testResult={0.28,-0.56,0.28,};
IAtomicDescriptor descriptor = new PartialTChargeMMFF94Descriptor();

// IAtomContainer mol = sp.parseSmiles("COC");
IAtomContainer mol = builder.newInstance(IAtomContainer.class);
IAtom carbon = builder.newInstance(IAtom.class,Elements.CARBON);
IAtom oxygen = builder.newInstance(IAtom.class,Elements.OXYGEN);
IAtom carbon2 = builder.newInstance(IAtom.class,Elements.CARBON);
// making sure the order matches the test results
mol.addAtom(carbon);
mol.addAtom(oxygen);
mol.addAtom(carbon2);
mol.addBond(builder.newInstance(IBond.class,carbon, oxygen, CDKConstants.BONDORDER_SINGLE));
mol.addBond(builder.newInstance(IBond.class,carbon2, oxygen, CDKConstants.BONDORDER_SINGLE));
addExplicitHydrogens(mol);
double[] expectedResult={0.0,0.0,0.28,0.28,-0.56,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
double[] actualResult = AtomInChIToMMFF94PartialCharges("InChI=1S/C4H10O/c1-3-5-4-2/h3-4H2,1-2H3");

for (int i = 0 ; i < 3 ; i++){
double result= ((DoubleResult)descriptor.calculate(mol.getAtom(i),mol).getValue()).doubleValue();
Assert.assertEquals(testResult[i],result,METHOD_ERROR);
}
Assert.assertArrayEquals(expectedResult, actualResult, METHOD_ERROR);


}
/**
* A unit test for JUnit with Methanethiol
*/
@Test
public void testPartialTotalChargeDescriptor_Methanethiol() throws ClassNotFoundException, CDKException, java.lang.Exception {
double [] testResult={0.23,-0.41,0.0,};
IAtomicDescriptor descriptor = new PartialTChargeMMFF94Descriptor();

IAtomContainer mol = builder.newInstance(IAtomContainer.class);
IAtom carbon = builder.newInstance(IAtom.class,Elements.CARBON);
IAtom sulfur = builder.newInstance(IAtom.class,Elements.SULFUR);
// making sure the order matches the test results
mol.addAtom(carbon);
mol.addAtom(sulfur);
mol.addBond(builder.newInstance(IBond.class,carbon, sulfur, CDKConstants.BONDORDER_SINGLE));
addExplicitHydrogens(mol);

for (int i = 0 ; i < 3 ; i++){
double result= ((DoubleResult)descriptor.calculate(mol.getAtom(i),mol).getValue()).doubleValue();
Assert.assertEquals(testResult[i],result,METHOD_ERROR);
}
double[] expectedResult={0.23,-0.41,0.0,0.0,0.0,0.18};
double[] actualResult = AtomInChIToMMFF94PartialCharges("InChI=1S/CH4S/c1-2/h2H,1H3");

Assert.assertArrayEquals(expectedResult, actualResult, METHOD_ERROR);
}
/**
* A unit test for JUnit with Chloromethane
*/
@Test
public void testPartialTotalChargeDescriptor_Chloromethane() throws ClassNotFoundException, CDKException, java.lang.Exception {
double [] testResult={0.29,-0.29,0.0};
IAtomicDescriptor descriptor = new PartialTChargeMMFF94Descriptor();

// IAtomContainer mol = sp.parseSmiles("CCl");
IAtomContainer mol = builder.newInstance(IAtomContainer.class);
IAtom carbon = builder.newInstance(IAtom.class,Elements.CARBON);
IAtom chlorine = builder.newInstance(IAtom.class,Elements.CHLORINE);
// making sure the order matches the test results
mol.addAtom(carbon);
mol.addAtom(chlorine);
mol.addBond(builder.newInstance(IBond.class,carbon, chlorine, CDKConstants.BONDORDER_SINGLE));
addExplicitHydrogens(mol);

for (int i = 0 ; i < 3 ; i++){
double result= ((DoubleResult)descriptor.calculate(mol.getAtom(i),mol).getValue()).doubleValue();
Assert.assertEquals(testResult[i],result,METHOD_ERROR);
}
double[] expectedResult={0.29,-0.29,0.0,0.0,0.0};
double[] actualResult = AtomInChIToMMFF94PartialCharges("InChI=1S/CH3Cl/c1-2/h1H3");

Assert.assertArrayEquals(expectedResult, actualResult, METHOD_ERROR);

}
/**
* A unit test for JUnit with Benzene
*/
@Test
public void testPartialTotalChargeDescriptor_Benzene() throws ClassNotFoundException, CDKException, java.lang.Exception {
double [] testResult={-0.15,-0.15,-0.15,-0.15,-0.15,-0.15,0.15,0.15,0.15,0.15,0.15,0.15};/* from Merck Molecular Force Field. II. Thomas A. Halgren*/
IAtomicDescriptor descriptor = new PartialTChargeMMFF94Descriptor();

// IAtomContainer mol = sp.parseSmiles("c1ccccc1");
IAtomContainer mol = builder.newInstance(IAtomContainer.class);
for (int i=0; i<6; i++) {
IAtom carbon = builder.newInstance(IAtom.class,Elements.CARBON);
carbon.setFlag(CDKConstants.ISAROMATIC, true);
// making sure the order matches the test results
mol.addAtom(carbon);
}
IBond ringBond = builder.newInstance(IBond.class,mol.getAtom(0), mol.getAtom(1), CDKConstants.BONDORDER_DOUBLE);
ringBond.setFlag(CDKConstants.ISAROMATIC, true);
mol.addBond(ringBond);
ringBond = builder.newInstance(IBond.class,mol.getAtom(1), mol.getAtom(2), CDKConstants.BONDORDER_SINGLE);
ringBond.setFlag(CDKConstants.ISAROMATIC, true);
mol.addBond(ringBond);
ringBond = builder.newInstance(IBond.class,mol.getAtom(2), mol.getAtom(3), CDKConstants.BONDORDER_DOUBLE);
ringBond.setFlag(CDKConstants.ISAROMATIC, true);
mol.addBond(ringBond);
ringBond = builder.newInstance(IBond.class,mol.getAtom(3), mol.getAtom(4), CDKConstants.BONDORDER_SINGLE);
ringBond.setFlag(CDKConstants.ISAROMATIC, true);
mol.addBond(ringBond);
ringBond = builder.newInstance(IBond.class,mol.getAtom(4), mol.getAtom(5), CDKConstants.BONDORDER_DOUBLE);
ringBond.setFlag(CDKConstants.ISAROMATIC, true);
mol.addBond(ringBond);
ringBond = builder.newInstance(IBond.class,mol.getAtom(5), mol.getAtom(0), CDKConstants.BONDORDER_SINGLE);
ringBond.setFlag(CDKConstants.ISAROMATIC, true);
mol.addBond(ringBond);
addExplicitHydrogens(mol);
double[] expectedResult={-0.15,-0.15,-0.15,-0.15,-0.15,-0.15,0.15,0.15,0.15,0.15,0.15,0.15};
double[] actualResult = AtomInChIToMMFF94PartialCharges("InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H");

for (int i = 0 ; i < 12 ; i++){
double result= ((DoubleResult)descriptor.calculate(mol.getAtom(i),mol).getValue()).doubleValue();
Assert.assertEquals(testResult[i],result,METHOD_ERROR);
}
Assert.assertArrayEquals(expectedResult, actualResult, METHOD_ERROR);

}
/**
* A unit test for JUnit with Water
*/
@Test
public void testPartialTotalChargeDescriptor_Water() throws ClassNotFoundException, CDKException, java.lang.Exception {
double [] testResult={-0.86,0.43,0.43};
IAtomicDescriptor descriptor = new PartialTChargeMMFF94Descriptor();

IAtomContainer mol = builder.newInstance(IAtomContainer.class);
IAtom oxygen = builder.newInstance(IAtom.class,Elements.OXYGEN);
// making sure the order matches the test results
mol.addAtom(oxygen);
addExplicitHydrogens(mol);

for (int i = 0 ; i < 3 ; i++){
double result= ((DoubleResult)descriptor.calculate(mol.getAtom(i),mol).getValue()).doubleValue();
Assert.assertEquals(testResult[i],result,METHOD_ERROR);
}
}
double[] expectedResult={-0.86,0.43,0.43};
double[] actualResult = AtomInChIToMMFF94PartialCharges("InChI=1S/H2O/h1H2");

Assert.assertArrayEquals(expectedResult, actualResult, METHOD_ERROR);

}


}

0 comments on commit 88a54c6

Please sign in to comment.