File: obdistgen.cpp

package info (click to toggle)
openbabel 3.1.1%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 259,620 kB
  • sloc: cpp: 361,957; python: 11,640; ansic: 6,470; perl: 6,010; pascal: 793; php: 529; sh: 226; xml: 97; ruby: 64; makefile: 45; java: 23
file content (124 lines) | stat: -rw-r--r-- 3,522 bytes parent folder | download | duplicates (5)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**********************************************************************
obgen.cpp - test program for SMILES 3D coordinate generation
          - using distance geometry

Copyright (C) 2006 Tim Vandermeersch
Some portions Copyright (C) 2006-2012 Geoffrey R. Hutchison

This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
***********************************************************************/

// used to set import/export for Cygwin DLLs
#ifdef WIN32
#define USING_OBDLL
#endif

#include <openbabel/babelconfig.h>
#include <openbabel/base.h>
#include <openbabel/mol.h>
#include <openbabel/obconversion.h>
#include <openbabel/forcefield.h>
#include <openbabel/distgeom.h>

using namespace std;
using namespace OpenBabel;

// PROTOTYPES /////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
//! \brief  Generate rough 3D coordinates for SMILES (or other 0D files).
//
int main(int argc,char **argv)
{
  char *program_name= argv[0];
  int c;
  string basename, filename = "";

  if (argc < 2) {
    cout << "Usage: obdistgen <filename>" << endl;
    cout << endl;
    exit(-1);
  } else {
    basename = filename = argv[1];
    size_t extPos = filename.rfind('.');

    if (extPos!= string::npos) {
      basename = filename.substr(0, extPos);
    }
  }

  // Find Input filetype
  OBConversion conv;
  OBFormat *format_in = conv.FormatFromExt(filename.c_str());
  OBFormat *format_out = conv.FindFormat("sdf");

  if (!format_in || !format_out || !conv.SetInAndOutFormats(format_in, format_out)) {
    cerr << program_name << ": cannot read input/output format!" << endl;
    exit (-1);
  }

  ifstream ifs;
  ofstream ofs;

  // Read the file
  ifs.open(filename.c_str());
  if (!ifs) {
    cerr << program_name << ": cannot read input file!" << endl;
    exit (-1);
  }

  OBMol mol;
  OBForceField* pFF = OBForceField::FindForceField("mmff94");

  for (c=1;;c++) {
      mol.Clear();
      if (!conv.Read(&mol, &ifs))
        break;
      if (mol.Empty())
        break;

      // hydrogens must be added before Setup(mol) is called
      //  to ensure stereochemistry
      mol.AddHydrogens();

      OBDistanceGeometry dg;
      dg.Setup(mol);

      for (unsigned int i = 1; i < 2; ++i) {
        //        cout << i << endl;
        dg.AddConformer();
      }
      dg.GetConformers(mol);
      //      cout << " Conformers: " << mol.NumConformers() << endl;
      // Check the energies
      pFF->Setup(mol);
      // load through all the conformers
      unsigned int minConf = 0;
      double e, minE = 1.0e10;
      for (unsigned int n = 0; n < mol.NumConformers();++n)
        {
          mol.SetConformer(n);
          pFF->SetCoordinates(mol);
          e = pFF->Energy(false);
          //          cout << " Conformer: " << n << " " << e << endl;
          if (e < minE) {
            minE = e;
            minConf = n;
          }
        }
      mol.SetConformer(minConf);
      conv.Write(&mol, &cout);
  } // end for loop

  return(0);
}