File: example1.cpp

package info (click to toggle)
openbabel 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 37,188 kB
  • ctags: 47,193
  • sloc: cpp: 237,858; ansic: 85,555; cs: 22,219; java: 14,377; sh: 9,876; perl: 5,432; python: 4,319; pascal: 793; makefile: 683; xml: 97; ruby: 54
file content (44 lines) | stat: -rw-r--r-- 1,111 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <openbabel/obconversion.h>
#include <openbabel/mol.h>
#include <openbabel/obiter.h>

using namespace std;
using namespace OpenBabel;

int main(int argc,char **argv){

if(argc<2){
cout << "Usage: ProgrameName InputFileName";
return 1;
}

ifstream ifs(argv[1]);
if(!ifs){
cout << "Cannot open input file";
return 1;
}

OpenBabel::OBConversion conv(&ifs, &cout);
if(conv.SetInAndOutFormats("mol","sdf")){
  OpenBabel::OBMol mol;
  if(conv.Read(&mol)){
    cout << "Mol.wt: "<<mol.GetMolWt()<<endl;//works even with implicit hydrogen
    mol.AddHydrogens(false,false); //ensure that hydrogens are all explicit
    cout << "No. of atoms: " << mol.NumAtoms()<<endl;
    cout << "No. of hvy atoms: " << mol.NumHvyAtoms() << endl;
    cout << "No. of bonds: " << mol.NumBonds() << endl;
    double exactMass = 0.0;
    FOR_ATOMS_OF_MOL(a, mol){
      cout << "iterating..."<<endl;
      exactMass += a->GetExactMass();
    }
    cout << "Exact Mass: " << exactMass << endl;
  }
  else
    cout << "can't conv.read()."<<endl;
}
else
  cout << "Incorrect in/out format." << endl;
return 0;
}