20.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.

# ch20-2.py
from openeye.oechem import *

def CalculateMolecularWeight(mol):
    result = 0.0
    for atom in mol.GetAtoms():
        elem = atom.GetAtomicNum()
        mass = atom.GetIsotope()
        if (elem != 0 and mass != 0):
            result += OEGetIsotopicWeight(elem,mass)
        else:
            result += OEGetAverageWeight(elem)
    return result

ims = oemolistream("drugs.sdf");
for mol in ims.GetOEMols():
	print mol.GetTitle(), " mw = ", CalculateMolecularWeight(mol)