1818 */
1919package org .openscience .cdk .qsar .descriptors .molecular ;
2020
21+ import java .io .IOException ;
22+
2123import org .openscience .cdk .CDKConstants ;
2224import org .openscience .cdk .config .Isotopes ;
2325import org .openscience .cdk .exception .CDKException ;
26+ import org .openscience .cdk .interfaces .IAtom ;
2427import org .openscience .cdk .interfaces .IAtomContainer ;
2528import org .openscience .cdk .interfaces .IIsotope ;
2629import 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
0 commit comments