Skip to content

Commit

Permalink
Only store the atomic number of an atom and not it's redundant symbol.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Jun 27, 2018
1 parent c2f7763 commit e9327af
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.exception.NoSuchAtomTypeException;
Expand Down Expand Up @@ -280,8 +281,7 @@ public IAtomType[] getAtomTypes(String symbol) {
logger.debug("Request for atomtype for symbol ", symbol);
List<IAtomType> atomList = new ArrayList<IAtomType>();
for (IAtomType atomType : atomTypes.values()) {
// logger.debug(" does symbol match for: ", atomType);
if (atomType.getSymbol().equals(symbol)) {
if (Objects.equals(atomType.getSymbol(), symbol)) {
atomList.add(atomType);
}
}
Expand Down
29 changes: 18 additions & 11 deletions base/data/src/main/java/org/openscience/cdk/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.openscience.cdk;

import com.google.common.base.Objects;
import org.openscience.cdk.config.Elements;
import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.tools.periodictable.PeriodicTable;

Expand Down Expand Up @@ -60,18 +61,14 @@ public class Element extends ChemObject implements Serializable, IElement, Clone
*/
private static final long serialVersionUID = 3062529834691231436L;

/** The element symbol for this element as listed in the periodic table. */
protected String symbol;

/** The atomic number for this element giving their position in the periodic table. */
protected Integer atomicNumber = (Integer) CDKConstants.UNSET;
protected Integer atomicNumber = null;

/**
* Constructs an empty Element.
*/
public Element() {
super();
this.symbol = null;
}

/**
Expand All @@ -83,7 +80,6 @@ public Element() {
*/
public Element(IElement element) {
super(element);
this.symbol = element.getSymbol();
this.atomicNumber = element.getAtomicNumber();
}

Expand All @@ -94,7 +90,8 @@ public Element(IElement element) {
* @param symbol The element symbol that this element should have.
*/
public Element(String symbol) {
this(symbol, PeriodicTable.getAtomicNumber(symbol));
super();
setSymbolInternal(symbol);
}

/**
Expand All @@ -105,7 +102,6 @@ public Element(String symbol) {
* @param atomicNumber The atomicNumber of this element.
*/
public Element(String symbol, Integer atomicNumber) {
this.symbol = symbol;
this.atomicNumber = atomicNumber;
}

Expand Down Expand Up @@ -153,7 +149,11 @@ public void setAtomicNumber(Integer atomicNumber) {
*/
@Override
public String getSymbol() {
return this.symbol;
if (atomicNumber == null)
return null;
if (atomicNumber == 0)
return "R";
return Elements.ofNumber(atomicNumber).symbol();
}

/**
Expand All @@ -165,10 +165,17 @@ public String getSymbol() {
*/
@Override
public void setSymbol(String symbol) {
this.symbol = symbol;
setSymbolInternal(symbol);
notifyChanged();
}

private void setSymbolInternal(String symbol) {
if (symbol == null)
this.atomicNumber = null;
else
this.atomicNumber = Elements.ofString(symbol).number();
}

@Override
public String toString() {
StringBuffer resultString = new StringBuffer(32);
Expand Down Expand Up @@ -206,6 +213,6 @@ public boolean compare(Object object) {
return false;
}
Element elem = (Element) object;
return Objects.equal(atomicNumber, elem.atomicNumber) && Objects.equal(symbol, elem.symbol);
return Objects.equal(atomicNumber, elem.atomicNumber);
}
}
4 changes: 2 additions & 2 deletions base/data/src/main/java/org/openscience/cdk/PseudoAtom.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public PseudoAtom(String label) {
*/
public PseudoAtom(IElement element) {
super(element);
setAtomicNumber(0);
if (element instanceof IPseudoAtom) {
this.label = ((IPseudoAtom) element).getLabel();
} else {
super.symbol = "R";
this.label = element.getSymbol();
this.label = "R";
}
}

Expand Down
8 changes: 5 additions & 3 deletions base/data/src/test/java/org/openscience/cdk/ElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testElement_String() {
@Test
public void testElement_X() {
IElement e = new Element("X");
Assert.assertEquals("X", e.getSymbol());
Assert.assertEquals("R", e.getSymbol());
// and it should not throw exceptions
Assert.assertNotNull(e.getAtomicNumber());
Assert.assertThat(e.getAtomicNumber(), is(0));
Expand Down Expand Up @@ -108,8 +108,10 @@ public void compareDiffSymbol() {

@Test
public void compareDiffAtomicNumber() {
Element e1 = new Element(new String("H"), 1);
Element e2 = new Element(new String("H"), null);
Element e1 = new Element();
Element e2 = new Element();
e1.setAtomicNumber(1);
e1.setAtomicNumber(2);
Assert.assertFalse(e1.compare(e2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testDebugElement_String() {
@Test
public void testElement_X() {
IElement e = new DebugElement("X");
Assert.assertEquals("X", e.getSymbol());
Assert.assertEquals("R", e.getSymbol());
// and it should not throw exceptions
Assert.assertNotNull(e.getAtomicNumber());
Assert.assertThat(e.getAtomicNumber(), is(0));
Expand Down
29 changes: 18 additions & 11 deletions base/silent/src/main/java/org/openscience/cdk/silent/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.google.common.base.Objects;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.config.Elements;
import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.tools.periodictable.PeriodicTable;

Expand Down Expand Up @@ -56,18 +57,14 @@ public class Element extends ChemObject implements Serializable, IElement, Clone
*/
private static final long serialVersionUID = 3062529834691231436L;

/** The element symbol for this element as listed in the periodic table. */
protected String symbol;

/** The atomic number for this element giving their position in the periodic table. */
protected Integer atomicNumber = (Integer) CDKConstants.UNSET;
protected Integer atomicNumber = null;

/**
* Constructs an empty Element.
*/
public Element() {
super();
this.symbol = null;
}

/**
Expand All @@ -79,7 +76,6 @@ public Element() {
*/
public Element(IElement element) {
super(element);
this.symbol = element.getSymbol();
this.atomicNumber = element.getAtomicNumber();
}

Expand All @@ -90,7 +86,8 @@ public Element(IElement element) {
* @param symbol The element symbol that this element should have.
*/
public Element(String symbol) {
this(symbol, PeriodicTable.getAtomicNumber(symbol));
this();
setSymbolInternal(symbol);
}

/**
Expand All @@ -101,7 +98,6 @@ public Element(String symbol) {
* @param atomicNumber The atomicNumber of this element.
*/
public Element(String symbol, Integer atomicNumber) {
this.symbol = symbol;
this.atomicNumber = atomicNumber;
}

Expand Down Expand Up @@ -148,7 +144,11 @@ public void setAtomicNumber(Integer atomicNumber) {
*/
@Override
public String getSymbol() {
return this.symbol;
if (atomicNumber == null)
return null;
if (atomicNumber == 0)
return "R";
return Elements.ofNumber(atomicNumber).symbol();
}

/**
Expand All @@ -160,7 +160,14 @@ public String getSymbol() {
*/
@Override
public void setSymbol(String symbol) {
this.symbol = symbol;
setSymbolInternal(symbol);
}

private void setSymbolInternal(String symbol) {
if (symbol == null)
this.atomicNumber = null;
else
this.atomicNumber = Elements.ofString(symbol).number();
}

@Override
Expand Down Expand Up @@ -200,6 +207,6 @@ public boolean compare(Object object) {
return false;
}
Element elem = (Element) object;
return Objects.equal(atomicNumber, elem.atomicNumber) && Objects.equal(symbol, elem.symbol);
return Objects.equal(atomicNumber, elem.atomicNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public PseudoAtom(String label) {
*/
public PseudoAtom(IElement element) {
super(element);
setAtomicNumber(0);
if (element instanceof IPseudoAtom) {
this.label = ((IPseudoAtom) element).getLabel();
} else {
super.symbol = "R";
this.label = element.getSymbol();
this.label = "R";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testElement_String() {
@Test
public void testElement_X() {
IElement e = new Element("X");
Assert.assertEquals("X", e.getSymbol());
Assert.assertEquals("R", e.getSymbol());
// and it should not throw exceptions
Assert.assertNotNull(e.getAtomicNumber());
Assert.assertThat(e.getAtomicNumber(), is(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void testSetSymbol_String() {
@Test
public void testGetSymbol() {
IElement e = (IElement) newChemObject();
e.setSymbol("X");
Assert.assertEquals("X", e.getSymbol());
e.setSymbol("Ir");
Assert.assertEquals("Ir", e.getSymbol());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ public void testPseudoAtomLabels() throws Exception {
molecule = reader.read(molecule);
reader.close();
assertTrue(molecule.getAtom(4) instanceof IPseudoAtom);
Assert.assertEquals("Gln", molecule.getAtom(4).getSymbol());
Assert.assertEquals("R", molecule.getAtom(4).getSymbol());
IPseudoAtom pa = (IPseudoAtom) molecule.getAtom(4);
Assert.assertEquals("Gln", pa.getLabel());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testPseudoAtomLabels() throws Exception {
molecule = reader.read(molecule);
reader.close();
Assert.assertTrue(molecule.getAtom(9) instanceof IPseudoAtom);
Assert.assertEquals("Leu", molecule.getAtom(9).getSymbol());
Assert.assertEquals("R", molecule.getAtom(9).getSymbol());
IPseudoAtom pa = (IPseudoAtom) molecule.getAtom(9);
Assert.assertEquals("Leu", pa.getLabel());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public void readAtomClass() throws Exception {
@Test
public void erroneousLabels_tRNA() throws Exception {
IAtomContainer ac = convert("[tRNA]CC");
assertThat(ac.getAtom(0).getSymbol(), is("*"));
assertThat(ac.getAtom(0).getSymbol(), is("R"));
assertThat(ac.getAtom(0), is(instanceOf(IPseudoAtom.class)));
assertThat(((IPseudoAtom) ac.getAtom(0)).getLabel(), is("tRNA"));
}
Expand All @@ -449,7 +449,7 @@ public void erroneousLabels_tRNA() throws Exception {
@Test
public void erroneousLabels_nested() throws Exception {
IAtomContainer ac = convert("[now-[this]-is-mean]CC");
assertThat(ac.getAtom(0).getSymbol(), is("*"));
assertThat(ac.getAtom(0).getSymbol(), is("R"));
assertThat(ac.getAtom(0), is(instanceOf(IPseudoAtom.class)));
assertThat(((IPseudoAtom) ac.getAtom(0)).getLabel(), is("now-[this]-is-mean"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class PharmacophoreAtom extends Atom {

private String smarts;
private int[] matchingAtoms;
private String symbol;

/**
* Create a pharmacophore group.
Expand Down Expand Up @@ -107,6 +108,16 @@ public String getSmarts() {
return smarts;
}

@Override
public void setSymbol(String symbol) {
this.symbol = symbol;
}

@Override
public String getSymbol() {
return symbol;
}

/**
* Set the atoms of a target molecule that correspond to this group.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class PharmacophoreQueryAtom extends Atom implements IQueryAtom {

private String smarts;
private IQueryAtomContainer[] compiledSmarts;
private String symbol;

/**
* Creat a new query pharmacophore group
Expand All @@ -52,7 +53,7 @@ public class PharmacophoreQueryAtom extends Atom implements IQueryAtom {
* @param smarts The SMARTS pattern to be used for matching
*/
public PharmacophoreQueryAtom(String symbol, String smarts) {
setSymbol(symbol);
this.symbol = symbol;
this.smarts = smarts;
// Note that we allow a special form of SMARTS where the | operator
// represents logical or of multi-atom groups (as opposed to ','
Expand All @@ -63,6 +64,22 @@ public PharmacophoreQueryAtom(String symbol, String smarts) {
compiledSmarts[i] = SMARTSParser.parse(subSmarts[i], null);
}

/**
* {@inheritDoc}
*/
@Override
public String getSymbol() {
return this.symbol;
}

/**
* {@inheritDoc}
*/
@Override
public void setSymbol(String symbol) {
this.symbol = symbol;
}

/**
* Get the SMARTS pattern for this pharmacophore group.
*
Expand Down

0 comments on commit e9327af

Please sign in to comment.