10.7 Using NewConf

The most common method to create conformers in a molecule is by reading a molecule from a file (see chapter ``Reading and Writing Molecules''). However, when manipulating molecules it is often necessary to create conformers on-the-fly. In OEChem, this is done with the OEMCMolBase::NewConf function. There are four prominent overloads of NewConf. All of the overloads create conformers with capacity to store coordinates for the current number of atoms in the molecule. NewAtom and NewBond adjust this capacity as necessary. The default OEMCMolBase constructor puts the molecule in a state with a single empty conformer (as does the OEMCMolBase::Clear function). The DeleteConfs function of OEMCMolBase removes all of the conformers of the molecule.

 1 #include "openeye.h"
 2 #include "oechem.h"
 3 #include "oesystem.h"
 4
 5 using namespace OEChem;
 6 using namespace std;
 7
 8 int main()
 9 {
10   OEMol mol;
11
12   cerr << "Default NumConfs = " << mol.NumConfs() << endl;
13
14   mol.NewConf();
15
16   cerr << "After One Additional, NumConfs = " << mol.NumConfs() << endl;
17
18   mol.DeleteConfs();
19
20   cerr << "After Deletion, NumConfs = " << mol.NumConfs() << endl;
21
22   return 0;
23 }

Listing:10.5 Deleting Conformers

The code above will produce the output:

1
2
0

The overloads of the NewConf function are:

NewConf()
NewConf(const float *coords)
NewConf(const OEMolBase *mol)
NewConf(const OEConfBase *conf)

The NewConf function returns a pointer to the newly created conformer. For example:

OEConfBase conf = NewConf();
will allow the newly created conformer to be accessed through conf.

If NewConf with no arguments has been called, a new empty conformer is created. The coordinates of the atoms for this conformer can be set using the OEConfBase::SetCoords function, e.g. conf.SetCoords(*coords). See the chapter entitled Coordinate Handling or the API manual for more information on this function.

The version of NewConf which takes a pointer to an OEMolBase copies the coordinates of the passed molecule into the new conformer. NewConf is expecting that the molecule passed has the same graph as the OEMCMolBase which is the parent of the new conformer. It is important to note that this version of NewConf can take any instance of an OEMolBase, such as an OEGraphMol or an OEMol . When an OEMol is passed to NewConf, the coordinates of the newly created conformer will come from the active conformation of the molecule passed.

Finally, there is an overload which takes a pointer to a conformer. This version of NewConf behaves in the same manner as NewConf(const OEMolBase *mol) , such that the coordinates are copied into the new conformer and the graph of the passed molecule is expected to be the same as the parent molecule creating the new conformer.

 1 #include "openeye.h"
 2 #include "oesystem.h"
 3 #include "oechem.h"
 4
 5 #include <iostream>
 6 using namespace std;
 7
 8 using namespace OESystem;
 9 using namespace OEChem;
10
11 void GetGoodMol(OEMCMolBase &destination,const OEMCMolBase &source)
12 {
13   destination = (OEMolBase&)source;
14   destination.DeleteConfs();
15   destination.SetTitle("");
16   OEConfBase *newConf;
17   char buf[2048];
18
19   OEIter<OEConfBase> conf;
20   for(conf = source.GetConfs();conf;++conf)
21   {
22     if(conf->GetEnergy() < 100.0f)
23     {
24       newConf = destination.NewConf(conf);
25       sprintf(buf,"Low Energy Conformer: energy = %.3f",newConf->GetEnergy());
26       newConf->SetTitle(buf);
27     }
28   }
29
30   if(destination.NumConfs() == 0)
31     destination.NewConf();
32 }
33
34 int main()
35 {
36   OEMol mol;
37   OEMol goodmol;
38
39   oemolistream mis("input.oeb");
40   oemolostream mos("output.sdf");
41
42   while(OEReadMolecule(mis,mol))
43   {
44     GetGoodMol(goodmol,mol);
45     OEWriteMolecule(mos,goodmol);
46   }
47
48   return 0;
49 }

Listing:10.6 Use of NewConf to copy conformers

The example above demonstrates copying conformers from one OEMol to another using the NewConf and DeleteConfs functions. The main routine reads all of the molecules from the file ``input.oeb'' and writes the molecules with only their low-energy conformations to ``output.sdf'' . The function GetGoodMol generates a destination molecule that contains only the low-energy conformations of the source molecule. The title of each new conformer is set to reflect its energy.

As noted in the API reference, DeleteConfs is a somewhat dangerous function. OEMols are constructed with a conformer and need at least one conformer for proper function. If you delete all of the conformers of an OEMCMolBase , it is critical that you add at least one conformer before passing the molecule on to other OEChem functions. Note that in GetGoodMol, if no conformers are copied, a single empty conformer is created before the function returns.