21.5 Calculating Molecular Weight of a Compound

The following example demonstrates how to use OEChem's periodic table functions to perform the common task of determining the molecular weight of a compound. Average molecular weight is commonly used in filtering (Lipinski's Rules) and as a descriptor in QSAR. The use of inaccurate values for molecular weight in these applications may help explain their limited success.

 1 /**************************************************************
 2  * Copyright 2005, OpenEye Scientific Software, Inc.
 3  *************************************************************/
 4 import openeye.oechem.*;
 5
 6 public class MolecularWeight {
 7   private static double CalculateMolecularWeight(OEMolBase mol) {
 8     double wgt=0.0;
 9     int impH = 0;
10     for (OEAtomBaseIter aiter=mol.GetAtoms();aiter.hasNext();) {
11       OEAtomBase atom = aiter.next();
12       int elem = atom.GetAtomicNum();
13       int mass = atom.GetIsotope();
14       impH += atom.GetImplicitHCount();
15       if (elem!=0 && mass!=0 && oechem.OEIsCommonIsotope(elem,mass)) {
16         wgt += oechem.OEGetIsotopicWeight(elem,mass);
17       }
18       else {
19         wgt += oechem.OEGetAverageWeight(elem);
20       }
21     }
22     wgt += impH * oechem.OEGetAverageWeight(1);
23     return wgt;
24   }
25   public static void main(String argv[]) {
26     oemolistream ifs = new oemolistream();
27     if (ifs.open(argv[0])) {
28       OEGraphMol mol = new OEGraphMol();
29       while (oechem.OEReadMolecule(ifs, mol)) {
30         System.out.print(mol.GetTitle()+" ");
31         System.out.println("mw= "+CalculateMolecularWeight(mol));
32       }
33     }
34   }
35 }
36

Listing:21.1 Calculating Molecular Weight