Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/org/openscience/cdk/graph/invariant/InChINumbersTools.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
|
82 changes: 82 additions & 0 deletions
82
src/test/org/openscience/cdk/graph/invariant/InChINumbersToolsTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
} | ||
} |
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