Skip to content

Commit

Permalink
Method to extract substructures
Browse files Browse the repository at this point in the history
Signed-off-by: Rajarshi Guha <rajarshi.guha@gmail.com>
  • Loading branch information
gilleain authored and rajarshi committed Jan 17, 2011
1 parent 7c6a0c5 commit cfa5c93
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -69,6 +70,39 @@
*/
@TestClass("org.openscience.cdk.tools.manipulator.AtomContainerManipulatorTest")
public class AtomContainerManipulator {

/**
* Extract a substructure from an atom container, in the form of a new
* cloned atom container with only the atoms with indices in atomIndices and
* bonds that connect these atoms.
* <p/>
* Note that this may result in a disconnected atom container.
*
* @param atomContainer the source container to extract from
* @param atomIndices the indices of the substructure
* @return a cloned atom container with a substructure of the source
* @throws CloneNotSupportedException if the source container cannot be cloned
*/
@TestMethod("testExtractSubstructure")
public static IAtomContainer extractSubstructure(
IAtomContainer atomContainer, int... atomIndices) throws CloneNotSupportedException {
IAtomContainer substructure = (IAtomContainer) atomContainer.clone();
int numberOfAtoms = substructure.getAtomCount();
IAtom[] atoms = new IAtom[numberOfAtoms];
for (int atomIndex = 0; atomIndex < numberOfAtoms; atomIndex++) {
atoms[atomIndex] = substructure.getAtom(atomIndex);
}

Arrays.sort(atomIndices);
for (int index = 0; index < numberOfAtoms; index++) {
if (Arrays.binarySearch(atomIndices, index) < 0) {
IAtom atom = atoms[index];
substructure.removeAtomAndConnectedElectronContainers(atom);
}
}

return substructure;
}

/**
* Returna an atom in an atomcontainer identified by id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public void setUp()
ac = MoleculeFactory.makeAlphaPinene();
}

@Test
public void testExtractSubstructure() throws CloneNotSupportedException {
IAtomContainer source = MoleculeFactory.makeEthylCyclohexane();
IAtomContainer ringSubstructure =
AtomContainerManipulator.extractSubstructure(source, 0, 1, 2, 3, 4, 5);
Assert.assertEquals(6, ringSubstructure.getAtomCount());
Assert.assertEquals(6, ringSubstructure.getBondCount());
}

@Test
public void testGetTotalHydrogenCount_IAtomContainer() throws IOException, ClassNotFoundException, CDKException {
Expand Down

0 comments on commit cfa5c93

Please sign in to comment.