Skip to content

Commit ff8a51a

Browse files
committed
Modified WeightDescriptor calculate method to return natural mass. Updated unit tests
1 parent 7a18f1a commit ff8a51a

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

descriptor/qsarmolecular/src/main/java/org/openscience/cdk/qsar/descriptors/molecular/WeightDescriptor.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
*/
1919
package org.openscience.cdk.qsar.descriptors.molecular;
2020

21+
import java.io.IOException;
22+
2123
import org.openscience.cdk.CDKConstants;
2224
import org.openscience.cdk.config.Isotopes;
2325
import org.openscience.cdk.exception.CDKException;
26+
import org.openscience.cdk.interfaces.IAtom;
2427
import org.openscience.cdk.interfaces.IAtomContainer;
2528
import org.openscience.cdk.interfaces.IIsotope;
2629
import org.openscience.cdk.qsar.AbstractMolecularDescriptor;
@@ -140,47 +143,53 @@ private DescriptorValue getDummyDescriptorValue(Exception e) {
140143
}
141144

142145
/**
143-
* Calculate the weight of specified element type in the supplied {@link IAtomContainer}.
146+
* Calculate the natural weight of specified elements type in the supplied {@link IAtomContainer}.
144147
*
145148
* @param container The AtomContainer for which this descriptor is to be calculated. If 'H'
146149
* is specified as the element symbol make sure that the AtomContainer has hydrogens.
147-
*@return The total weight of atoms of the specified element type
150+
*@return The total natural weight of atoms of the specified element type
148151
*/
149152
@Override
150153
public DescriptorValue calculate(IAtomContainer container) {
151154
double weight = 0;
155+
double hydrogenNaturalMass;
156+
try {
157+
IIsotope hydrogen = Isotopes.getInstance().getMajorIsotope("H");
158+
hydrogenNaturalMass = Isotopes.getInstance().getNaturalMass(hydrogen);
159+
} catch (IOException e) {
160+
return getDummyDescriptorValue(e);
161+
}
162+
152163
if (elementName.equals("*")) {
153164
try {
154-
for (int i = 0; i < container.getAtomCount(); i++) {
155-
//logger.debug("WEIGHT: "+container.getAtomAt(i).getSymbol() +" " +IsotopeFactory.getInstance().getMajorIsotope( container.getAtomAt(i).getSymbol() ).getExactMass());
156-
weight += Isotopes.getInstance().getMajorIsotope(container.getAtom(i).getSymbol()).getExactMass();
157-
Integer hcount = container.getAtom(i).getImplicitHydrogenCount();
158-
if (hcount == CDKConstants.UNSET) hcount = 0;
159-
weight += (hcount * 1.00782504);
165+
for (IAtom atom : container.atoms()) {
166+
weight += Isotopes.getInstance().getNaturalMass(atom);
167+
Integer implicitHydrogenCount = atom.getImplicitHydrogenCount();
168+
if (implicitHydrogenCount == CDKConstants.UNSET) {
169+
implicitHydrogenCount = 0;
170+
}
171+
weight += (implicitHydrogenCount * hydrogenNaturalMass);
160172
}
161173
} catch (Exception e) {
162174
return getDummyDescriptorValue(e);
163175
}
164176
} else if (elementName.equals("H")) {
165177
try {
166-
IIsotope h = Isotopes.getInstance().getMajorIsotope("H");
167-
for (int i = 0; i < container.getAtomCount(); i++) {
168-
if (container.getAtom(i).getSymbol().equals(elementName)) {
169-
weight += Isotopes.getInstance().getMajorIsotope(container.getAtom(i).getSymbol())
170-
.getExactMass();
178+
for (IAtom atom : container.atoms()) {
179+
if (atom.getSymbol().equals(elementName)) {
180+
weight += hydrogenNaturalMass;
171181
} else {
172-
weight += (container.getAtom(i).getImplicitHydrogenCount() * h.getExactMass());
182+
weight += (atom.getImplicitHydrogenCount() * hydrogenNaturalMass);
173183
}
174184
}
175185
} catch (Exception e) {
176186
return getDummyDescriptorValue(e);
177187
}
178188
} else {
179189
try {
180-
for (int i = 0; i < container.getAtomCount(); i++) {
181-
if (container.getAtom(i).getSymbol().equals(elementName)) {
182-
weight += Isotopes.getInstance().getMajorIsotope(container.getAtom(i).getSymbol())
183-
.getExactMass();
190+
for (IAtom atom : container.atoms()) {
191+
if (atom.getSymbol().equals(elementName)) {
192+
weight += Isotopes.getInstance().getNaturalMass(atom);
184193
}
185194
}
186195
} catch (Exception e) {
@@ -195,7 +204,7 @@ public DescriptorValue calculate(IAtomContainer container) {
195204

196205
/**
197206
* Returns the specific type of the DescriptorResult object.
198-
*
207+
*
199208
* The return value from this method really indicates what type of result will
200209
* be obtained from the {@link org.openscience.cdk.qsar.DescriptorValue} object. Note that the same result
201210
* can be achieved by interrogating the {@link org.openscience.cdk.qsar.DescriptorValue} object; this method

descriptor/qsarmolecular/src/test/java/org/openscience/cdk/qsar/descriptors/molecular/WeightDescriptorTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public void setUp() throws Exception {
4545
}
4646

4747
@Test
48-
public void testWeightDescriptor() throws ClassNotFoundException, CDKException, java.lang.Exception {
48+
public void testWeightDescriptor() throws Exception {
4949
Object[] params = {"*"};
5050
descriptor.setParameters(params);
51-
SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
52-
IAtomContainer mol = sp.parseSmiles("CCC");
53-
Assert.assertEquals(44.06, ((DoubleResult) descriptor.calculate(mol).getValue()).doubleValue(), 0.1);
51+
SmilesParser smilesParser = new SmilesParser(DefaultChemObjectBuilder.getInstance());
52+
IAtomContainer mol = smilesParser.parseSmiles("CCC");
53+
Assert.assertEquals(44.095, ((DoubleResult) descriptor.calculate(mol).getValue()).doubleValue(), 0.001);
5454
}
5555

5656
/**
@@ -63,7 +63,7 @@ public void testNoHydrogens() throws Exception {
6363
IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();
6464
IAtomContainer mol = builder.newInstance(IAtomContainer.class);
6565
mol.addAtom(builder.newInstance(IAtom.class, "C"));
66-
Assert.assertEquals(12.00, ((DoubleResult) descriptor.calculate(mol).getValue()).doubleValue(), 0.1);
66+
Assert.assertEquals(12.0107, ((DoubleResult) descriptor.calculate(mol).getValue()).doubleValue(), 0.0001);
6767
}
6868

6969
/**
@@ -84,7 +84,7 @@ public void testExplicitHydrogens() throws Exception {
8484
mol.addBond(0, 2, Order.SINGLE);
8585
mol.addBond(0, 3, Order.SINGLE);
8686
mol.addBond(0, 4, Order.SINGLE);
87-
Assert.assertEquals(16.01, ((DoubleResult) descriptor.calculate(mol).getValue()).doubleValue(), 0.1);
87+
Assert.assertEquals(16.042, ((DoubleResult) descriptor.calculate(mol).getValue()).doubleValue(), 0.001);
8888
}
8989

9090
/**
@@ -98,7 +98,7 @@ public void testImplicitHydrogens() throws Exception {
9898
IAtomContainer mol = builder.newInstance(IAtomContainer.class);
9999
mol.addAtom(builder.newInstance(IAtom.class, "C"));
100100
mol.getAtom(0).setImplicitHydrogenCount(4);
101-
Assert.assertEquals(16.01, ((DoubleResult) descriptor.calculate(mol).getValue()).doubleValue(), 0.1);
101+
Assert.assertEquals(16.042, ((DoubleResult) descriptor.calculate(mol).getValue()).doubleValue(), 0.001);
102102
}
103103

104104
}

0 commit comments

Comments
 (0)