Skip to content

Commit

Permalink
Intercept pseudo atoms and default null hydrogen counts to 0. The CDK…
Browse files Browse the repository at this point in the history
… will leave the nulls in place for pseudo atoms so we need this special exception,

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Oct 14, 2013
1 parent 68e654f commit ded2e30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/main/org/openscience/cdk/smiles/CDKToBeam.java
Expand Up @@ -39,6 +39,7 @@
import uk.ac.ebi.beam.Atom;
import uk.ac.ebi.beam.AtomBuilder;
import uk.ac.ebi.beam.Bond;
import uk.ac.ebi.beam.Element;
import uk.ac.ebi.beam.Graph;
import uk.ac.ebi.beam.Configuration;
import uk.ac.ebi.beam.Edge;
Expand Down Expand Up @@ -151,16 +152,25 @@ Graph toBeamGraph(IAtomContainer ac) {
@TestMethod("aliphaticAtom,aromaticAtom") Atom toBeamAtom(final IAtom a) {

final boolean aromatic = a.getFlag(CDKConstants.ISAROMATIC);
final Integer hCount = checkNotNull(a.getImplicitHydrogenCount(),
"An atom had an undefined number of implicit hydrogens");
final Integer charge = a.getFormalCharge();
final String symbol = checkNotNull(a.getSymbol(),
"An atom had an undefined symbol");

AtomBuilder ab = aromatic ? AtomBuilder.aromatic(symbol)
.hydrogens(hCount)
: AtomBuilder.aliphatic(symbol)
.hydrogens(hCount);
Element element = Element.ofSymbol(symbol);
if (element == null)
element = Element.Unknown;

AtomBuilder ab = aromatic ? AtomBuilder.aromatic(element)
: AtomBuilder.aliphatic(element);


// CDK leaves nulls on pseudo atoms - we need to check this special case
Integer hCount = a.getImplicitHydrogenCount();
if (element == Element.Unknown) {
ab.hydrogens(hCount != null ? hCount : 0);
} else {
ab.hydrogens(checkNotNull(hCount, "One or more atoms had an undefined number of implicit hydrogens"));
}

if (charge != null)
ab.charge(charge);
Expand Down
11 changes: 11 additions & 0 deletions src/test/org/openscience/cdk/smiles/CDKToBeamTest.java
Expand Up @@ -160,6 +160,17 @@ public void water_Atom() throws Exception {
assertThat(new CDKToBeam().toBeamAtom(a).isotope(),
is(-1));
}

// special check that a CDK pseudo atom will default to 0 hydrogens if
// the hydrogens are set to null
@Test public void pseudoAtom_nullH() throws Exception {
assertThat(new CDKToBeam().toBeamAtom(new Atom("R")).hydrogens(),
is(0));
assertThat(new CDKToBeam().toBeamAtom(new Atom("*")).hydrogens(),
is(0));
assertThat(new CDKToBeam().toBeamAtom(new Atom("R1")).hydrogens(),
is(0));
}

@SuppressWarnings("unchecked")
@Test(expected = IllegalArgumentException.class)
Expand Down

0 comments on commit ded2e30

Please sign in to comment.