Skip to content

Commit

Permalink
Mask hydrogens in canonical labelling.
Browse files Browse the repository at this point in the history
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Jul 15, 2014
1 parent 5b4c073 commit 17a518e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Expand Up @@ -354,6 +354,25 @@ private static int charge(IAtom atom) {
return 0;
}

/**
* Locate explicit hydrogens that are attached to exactly one other atom.
*
* @param ac a structure
* @return binary set of terminal hydrogens
*/
static boolean[] terminalHydrogens(final IAtomContainer ac, final int[][] g) {

final boolean[] hydrogens = new boolean[ac.getAtomCount()];

// we specifically don't check for null atomic number, this must be set.
// if not, something major is wrong
for (int i = 0; i < ac.getAtomCount(); i++) {
hydrogens[i] = ac.getAtom(i).getAtomicNumber() == 1 && g[i].length == 1;
}

return hydrogens;
}

/** The first 2229 primes. */
private static final int[] PRIMES = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,
Expand Down
Expand Up @@ -82,6 +82,31 @@ public class CanonTest {
assertThat(act, is(exp));
}

@Test public void terminalExplicitHydrogensAreNotIncluded() throws Exception {
IAtomContainer m = smi("C/C=C(/C)C[H]");
boolean[] mask = Canon.terminalHydrogens(m, GraphUtil.toAdjList(m));
assertThat(mask, is(new boolean[]{false, false, false, false, false, true}));
}

@Test public void bridgingExplicitHydrogensAreIncluded() throws Exception {
IAtomContainer m = smi("B1[H]B[H]1");
boolean[] mask = Canon.terminalHydrogens(m, GraphUtil.toAdjList(m));
assertThat(mask, is(new boolean[]{false, false, false, false}));
}

@Test public void explicitHydrogensIonsAreIncluded() throws Exception {
IAtomContainer m = smi("[H+]");
boolean[] mask = Canon.terminalHydrogens(m, GraphUtil.toAdjList(m));
assertThat(mask, is(new boolean[]{false}));
}

@Test public void molecularHydrogensAreNotIncluded() throws Exception {
IAtomContainer m = smi("[H][H]");
boolean[] mask = Canon.terminalHydrogens(m, GraphUtil.toAdjList(m));
assertThat(mask, is(new boolean[]{true, true}));
}


static final SmilesParser sp = new SmilesParser(SilentChemObjectBuilder.getInstance());

static IAtomContainer smi(String smi) throws Exception {
Expand Down

0 comments on commit 17a518e

Please sign in to comment.