Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Set atomic numbers when creating an element from a symbol
Change-Id: Ibc96f55efb6922113e652b9ea7177390ec421199
Signed-off-by: John May <john.wilkinsonmay@gmail.com>
  • Loading branch information
egonw authored and johnmay committed Aug 6, 2013
1 parent a621922 commit c34d8c7
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/main/org/openscience/cdk/Element.java
Expand Up @@ -25,6 +25,7 @@
package org.openscience.cdk;

import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.tools.periodictable.PeriodicTable;

import java.io.Serializable;

Expand Down Expand Up @@ -95,6 +96,7 @@ public Element(IElement element) {
public Element(String symbol) {
this();
this.symbol = symbol;
this.atomicNumber = PeriodicTable.getAtomicNumber(symbol);
}

/**
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.openscience.cdk.interfaces.IAtomType;
import org.openscience.cdk.interfaces.IBond.Order;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
import org.openscience.cdk.tools.manipulator.BondManipulator;
Expand Down Expand Up @@ -129,7 +130,9 @@ public void startElement(String uri, String local,

private void startAtomTypeElement(String local, Attributes atts) {
if ("AtomType".equals(local)) {
currentAtomType = builder.newInstance(IAtomType.class, "H");
IElement bootstrapHydrogen = builder.newInstance(IElement.class, "H");
bootstrapHydrogen.setAtomicNumber(null);
currentAtomType = builder.newInstance(IAtomType.class, bootstrapHydrogen);
currentAtomType.setAtomTypeName(atts.getValue("rdf:ID"));
piBondCount = 0;
neighborCount = 0;
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.openscience.cdk.interfaces.IAtomType;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.tools.periodictable.PeriodicTable;

/**
* @cdk.module isomorphism
Expand Down Expand Up @@ -131,6 +132,7 @@ public abstract class QueryAtom extends QueryChemObject implements IQueryAtom {
public QueryAtom(String symbol, IChemObjectBuilder builder) {
this(builder);
this.symbol = symbol;
this.atomicNumber = PeriodicTable.getAtomicNumber(symbol);
}

public QueryAtom(IChemObjectBuilder builder) {
Expand Down
Expand Up @@ -830,9 +830,10 @@ public static IAtomContainer getAtomContainer(IMolecularFormula formula, IAtomCo

for (IIsotope isotope : formula.isotopes()) {
int occur = formula.getIsotopeCount(isotope);
for (int i = 0; i < occur; i++)
atomContainer.addAtom(formula.getBuilder().newInstance(IAtom.class,isotope));

for (int i = 0; i < occur; i++) {
IAtom atom = formula.getBuilder().newInstance(IAtom.class,isotope);
atomContainer.addAtom(atom);
}
}
return atomContainer;
}
Expand Down
9 changes: 8 additions & 1 deletion src/test/org/openscience/cdk/ElementTest.java
Expand Up @@ -62,7 +62,14 @@ public IChemObject newTestObject() {
IElement e = new Element("C");
Assert.assertEquals("C", e.getSymbol());
}


@Test public void testElement_X() {
IElement e = new Element("X");
Assert.assertEquals("X", e.getSymbol());
// and it should not throw exceptions
Assert.assertNull(e.getAtomicNumber());
}

@Test public void testElement_String_Integer() {
IElement e = new Element("H", 1);
Assert.assertEquals("H", e.getSymbol());
Expand Down
9 changes: 8 additions & 1 deletion src/test/org/openscience/cdk/debug/DebugElementTest.java
Expand Up @@ -58,7 +58,14 @@ public IChemObject newTestObject() {
IElement e = new DebugElement("C");
Assert.assertEquals("C", e.getSymbol());
}


@Test public void testElement_X() {
IElement e = new DebugElement("X");
Assert.assertEquals("X", e.getSymbol());
// and it should not throw exceptions
Assert.assertNull(e.getAtomicNumber());
}

@Test public void testDebugElement_String_int() {
IElement e = new DebugElement("H", 1);
Assert.assertEquals("H", e.getSymbol());
Expand Down
9 changes: 8 additions & 1 deletion src/test/org/openscience/cdk/silent/ElementTest.java
Expand Up @@ -62,7 +62,14 @@ public IChemObject newTestObject() {
IElement e = new Element("C");
Assert.assertEquals("C", e.getSymbol());
}


@Test public void testElement_X() {
IElement e = new Element("X");
Assert.assertEquals("X", e.getSymbol());
// and it should not throw exceptions
Assert.assertNull(e.getAtomicNumber());
}

@Test public void testElement_String_Integer() {
IElement e = new Element("H", 1);
Assert.assertEquals("H", e.getSymbol());
Expand Down
Expand Up @@ -714,6 +714,28 @@ public void testGetAtomContainer_String_IChemObjectBuilder() {
Assert.assertEquals(6, atomContainer.getAtomCount());
}

/**
* @cdk.bug 1296
*/
@Test
public void testGetAtomContainer_AddsAtomicNumbers(){
IMolecularFormula mf2 = new MolecularFormula();
mf2.addIsotope(builder.newInstance(IIsotope.class,"C"),2);
mf2.addIsotope(builder.newInstance(IIsotope.class,"H"),4);
IAtomContainer ac = MolecularFormulaManipulator.getAtomContainer(
mf2, builder.newInstance(IAtomContainer.class)
);
Assert.assertEquals(6, ac.getAtomCount());
Assert.assertNotNull(ac.getAtom(0).getAtomicNumber());
for (IAtom atom : ac.atoms()) {
if ("C".equals(atom.getSymbol()))
Assert.assertEquals(6, atom.getAtomicNumber().intValue());
else if ("H".equals(atom.getSymbol()))
Assert.assertEquals(1, atom.getAtomicNumber().intValue());
else
Assert.fail("Unexcepted element: " + atom.getSymbol());
}
}

@Test
public void testMolecularFormulaIAtomContainer_to_IAtomContainer2(){
Expand Down

0 comments on commit c34d8c7

Please sign in to comment.