12.1 Using NewAtom and NewBond

While using SMILES is a convenient method of specifying a molecule, OEChem contains functions that allow molecules to be constructed from atoms and bonds explicitly. The following example shows how to create the molecule water.

Atoms are created by calling the OEMolBase method, NewAtom, and Bonds are created by calling the OEMolBase method, NewBond. OEMolBase NewAtom takes the atomic number of the atom to create and returns a pointer to the new OEAtomBase, and NewBond takes two OEAtomBases and a integer bond order as arguments, and returns a reference to the new OEBondBase.

The atoms and bonds of a molecule are automatically deleted when their parent molecule is destroyed.

 1 /**************************************************************
 2  * Copyright 2005, OpenEye Scientific Software, Inc.
 3  *************************************************************/
 4
 5 import openeye.oechem.*;
 6
 7 public class CreatingAtomsV1 {
 8   public static void main(String argv[]) {
 9     OEGraphMol mol = new OEGraphMol();
10
11     OEAtomBase o  = mol.NewAtom(8);
12     OEAtomBase h1 = mol.NewAtom(1);
13     OEAtomBase h2 = mol.NewAtom(1);
14
15     OEBondBase b1 = mol.NewBond(o,h1,1);
16     OEBondBase b2 = mol.NewBond(o,h2,1);
17
18     System.out.println("# atoms "+mol.NumAtoms());
19     System.out.println("# bonds "+mol.NumBonds());
20
21     System.out.println(b1.GetBgnIdx()+"-"+b1.GetEndIdx()+" order: "+b1.GetOrder());
22     System.out.println(b2.GetBgnIdx()+"-"+b2.GetEndIdx()+" order: "+b2.GetOrder());
23   }
24 }

Listing:12.1 Creating new atoms and bonds

In the example source code, the atomic numbers of oxygen, 8, and hydrogen, 1, are explicitly encoded in the program. To make this code easier to read and less error prone, OEChem provides symbolic constants for the first 109 elements. This defines the atomic symbols as statics in the class OEElemNo with the appropriate values. The following example uses these constants instead of just numbers.

 1 /**************************************************************
 2  * Copyright 2005, OpenEye Scientific Software, Inc.
 3  *************************************************************/
 4
 5 import openeye.oechem.*;
 6
 7 public class CreatingAtomsV2 {
 8   public static void main(String argv[]) {
 9     OEGraphMol mol = new OEGraphMol();
10
11     OEAtomBase o  = mol.NewAtom(OEElemNo.O);
12     OEAtomBase h1 = mol.NewAtom(OEElemNo.H);
13     OEAtomBase h2 = mol.NewAtom(OEElemNo.H);
14
15     OEBondBase b1 = mol.NewBond(o,h1,1);
16     OEBondBase b2 = mol.NewBond(o,h2,1);
17
18     System.out.println("# atoms "+mol.NumAtoms());
19     System.out.println("# bonds "+mol.NumBonds());
20
21     System.out.println(b1.GetBgnIdx()+"-"+b1.GetEndIdx()+" order: "+b1.GetOrder());
22     System.out.println(b2.GetBgnIdx()+"-"+b2.GetEndIdx()+" order: "+b2.GetOrder());
23   }
24 }

Listing:12.2 Creating new atoms and bonds