Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added a helper class to get atom number for the heavy atoms, using th…
…e InChI algorithm

Signed-off-by: Rajarshi  Guha <rajarshi.guha@gmail.com>
  • Loading branch information
egonw authored and rajarshi committed Jul 14, 2011
1 parent 90371fd commit abcc00a
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 1 deletion.
@@ -0,0 +1,68 @@
/* Copyright (C) 2011 Egon Willighagen <egonw@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.graph.invariant;

import net.sf.jniinchi.INCHI_RET;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.inchi.InChIGenerator;
import org.openscience.cdk.inchi.InChIGeneratorFactory;
import org.openscience.cdk.interfaces.IAtomContainer;

/**
* Tool for calculating atom numbers using the InChI algorithm.
*
* @cdk.module inchi
* @cdk.githash
*/
@TestClass("org.openscience.cdk.graph.invariant.InChINumbersToolsTest")
public class InChINumbersTools {

/**
* Makes an array containing the InChI atom numbers of the non-hydrogen
* atoms in the atomContainer. It returns zero for all hydrogens.
*
* @param atomContainer The {@link IAtomContainer} to analyze.
* @return The number from 1 to the number of heavy atoms.
* @throws CDKException When the InChI could not be generated
*/
@TestMethod("testSimpleNumbering,testHydrogens,testGlycine")
public static long[] getNumbers(IAtomContainer atomContainer)
throws CDKException {
long[] atomNumbers = new long[atomContainer.getAtomCount()];
InChIGeneratorFactory factory = InChIGeneratorFactory.getInstance();
InChIGenerator gen = factory.getInChIGenerator(atomContainer);
if (gen.getReturnStatus() != INCHI_RET.OKAY)
throw new CDKException("Could not generate InChI.");
String aux = gen.getAuxInfo();
aux = aux.substring(aux.indexOf("/N:") + 3);
String numberStringAux = aux.substring(0, aux.indexOf("/"));
String[] numberStrings = numberStringAux.split("\\,");
int i = 0;
for (String numberString : numberStrings) {
i++;
atomNumbers[Integer.valueOf(numberString)-1] = i;
}
return atomNumbers;
}

}

@@ -0,0 +1,82 @@
/* Copyright (C) 2011 Egon Willighagen <egonw@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.graph.invariant;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.Atom;
import org.openscience.cdk.CDKTestCase;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.smiles.SmilesParser;

/**
* @cdk.module test-inchi
*/
public class InChINumbersToolsTest extends CDKTestCase {

@Test
public void testSimpleNumbering() throws CDKException{
IAtomContainer container = new Molecule();
container.addAtom(new Atom("O"));
container.addAtom(new Atom("C"));
container.addBond(0, 1, IBond.Order.SINGLE);
long[] numbers = InChINumbersTools.getNumbers(container);
Assert.assertEquals(2, numbers.length);
Assert.assertEquals(2, numbers[0]);
Assert.assertEquals(1, numbers[1]);
}

@Test
public void testHydrogens() throws CDKException{
IAtomContainer container = new Molecule();
container.addAtom(new Atom("H"));
container.addAtom(new Atom("C"));
container.addBond(0, 1, IBond.Order.SINGLE);
container.addAtom(new Atom("H"));
container.addBond(1, 2, IBond.Order.SINGLE);
container.addAtom(new Atom("H"));
container.addBond(1, 3, IBond.Order.SINGLE);
container.addAtom(new Atom("H"));
container.addBond(1, 4, IBond.Order.SINGLE);
long[] numbers = InChINumbersTools.getNumbers(container);
Assert.assertEquals(5, numbers.length);
Assert.assertEquals(0, numbers[0]);
Assert.assertEquals(1, numbers[1]);
Assert.assertEquals(0, numbers[2]);
Assert.assertEquals(0, numbers[3]);
Assert.assertEquals(0, numbers[4]);
}

@Test
public void testGlycine() throws Exception {
SmilesParser parser = new SmilesParser(DefaultChemObjectBuilder.getInstance());
IAtomContainer atomContainer = parser.parseSmiles("C(C(=O)O)N");
long[] numbers = InChINumbersTools.getNumbers(atomContainer);
Assert.assertEquals(5, numbers.length);
Assert.assertEquals(1, numbers[0]);
Assert.assertEquals(2, numbers[1]);
Assert.assertEquals(4, numbers[2]);
Assert.assertEquals(5, numbers[3]);
Assert.assertEquals(3, numbers[4]);
}
}
4 changes: 3 additions & 1 deletion src/test/org/openscience/cdk/modulesuites/MinchiTests.java
Expand Up @@ -23,6 +23,7 @@
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.openscience.cdk.coverage.InchiCoverageTest;
import org.openscience.cdk.graph.invariant.InChINumbersToolsTest;
import org.openscience.cdk.inchi.InChIGeneratorTest;
import org.openscience.cdk.inchi.InChIToStructureTest;

Expand All @@ -35,6 +36,7 @@
@SuiteClasses(value={
InchiCoverageTest.class,
InChIGeneratorTest.class,
InChIToStructureTest.class
InChIToStructureTest.class,
InChINumbersToolsTest.class
})
public class MinchiTests {}

0 comments on commit abcc00a

Please sign in to comment.