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

#include "oechem.h"
#include <iostream>

using namespace OEChem;
using namespace OESystem;
using namespace std;

float GetMaxX(const OEMolBase &mol)
{
  OEIter<OEAtomBase> atom;
  float xyz[3];
  float maxX = 0.0f;
  bool first = true;
  for(atom = mol.GetAtoms();atom;++atom)
  {
    mol.GetCoords(atom,xyz);
    if(first)
    {
      maxX = xyz[0];
      first = false;
    }
    else
      if(xyz[0] > maxX)
        maxX = xyz[0];
  }
  return maxX;
}

int main(int, char ** argv)
{
  OEIter<OEMCMolBase> mol;
  OEIter<OEConfBase> conf;

  oemolistream ims(argv[1]);

  std::string maxconf;
  float tmpx = 0.0f, maxX = 0.0f;

  for (mol=ims.GetMCMolBases(); mol; ++mol)
  {
    for(conf = mol->GetConfs(); conf; ++conf)
    {
      tmpx = GetMaxX(*conf);
      if(tmpx > maxX)
      {
        if(!maxconf.empty())
        {
          cerr << conf->GetTitle() << " has a larger value of x than " <<
                  maxconf << endl;
        }
        maxconf = conf->GetTitle();
        maxX = tmpx;
      }
    }
  }

  return 0;
}

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.