5.4.3 Use of the conformers as first-class objects

Alternatively, a programmer may wish to use the conformers as first class objects rather than via the state of the OEMCMolBase. This allows one to have multiple conformation objects at once and to treat the OEMCMolBase as a container of single-conformer molecules. The example below shows the use of the conformers as first class objects. Each conformer is represented by an OEConfBase which inherits from the OEMolBase object. Thus, each conformer can be treated as an independent molecule with respect to its coordinates as shown in the example code below.

#!/usr/bin/env python
# ch5-3.py
from openeye.oechem import *
import os, sys

def GetMaxX(mol):
   maxX = 0.0
   first = 1
   for atom in mol.GetAtoms():
      xyz = mol.GetCoords(atom)
      if first:
         maxX = xyz[0]
         first = None
      else:
         if xyz[0]>maxX:
            maxX = xyz[0]

   return maxX

ifs = oemolistream(sys.argv[1])

maxconf = None
xmax = 0.0

for mol in ifs.GetOEMols():
   for conf in mol.GetConfs():
      xtmp = GetMaxX(conf)
      if xtmp > xmax:
         if maxconf:
            print conf.GetTitle(),"has larger x than",maxconf
         maxconf = conf.GetTitle()
         xmax = GetMaxX(conf)

In the listing above, the function GetMaxX returns the maximum x-coordinate of a molecule. The main routine loops over all of the conformers of each molecule and compares the maximum x-coordinate to a running maximum of the x-coordinate of every conformer. If there is a new maximum, the associated conformer is stored and the user is notified via cerr.