babel.cpp
Go to the documentation of this file.
1 #include "babel.h"
2 
12 extern "C" {
13  #include "defines.h"
14  #include "error.h"
15  #include "boolean.h"
16 }
17 
18 #include <openbabel/mol.h>
19 #include <openbabel/obconversion.h>
20 #include <openbabel/forcefield.h>
21 #include <openbabel/oberror.h>
22 
23 #include <iostream>
24 
25 using namespace OpenBabel;
26 using namespace std;
27 
30 OBERROR extern OBMessageHandler obErrorLog;
31 
41 typedef struct {
42  OBMol mol;
43  OBForceField *ff;
44 } TMolInfo;
45 
46 TMolecule *ReadMolecule(char *fname)
47 {
48  TMolInfo *m;
49  OBConversion conv;
50  OBFormat *format;
51 
52  // Avoid warning messages
53  OpenBabel::obErrorLog.SetOutputLevel(obError);
54 
55  // Define the molecular information
56  m=new(TMolInfo);
57 
58  // Read the molecule
59  format=conv.FormatFromExt(fname);
60  if (!format)
61  Error("Unknown extension file");
62  if (!conv.SetInFormat(format))
63  Error("Unknown file format");
64  if (!conv.ReadFile(&(m->mol),fname))
65  Error("Error reading the input file");
66 
67  m->ff=NULL;
68 
69  return((void*)m);
70 }
71 
72 void WriteMolecule(char *fname,TMolecule *ml)
73 {
74  OBConversion conv;
75  OBFormat *format;
76  TMolInfo *m;
77 
78  m=(TMolInfo *)ml;
79 
80  // Avoid warning messages
81  OpenBabel::obErrorLog.SetOutputLevel(obError);
82 
83  // Write the molecule
84  format=conv.FormatFromExt(fname);
85  if (!format)
86  Error("Unknown extension file");
87  if (!conv.SetOutFormat(format))
88  Error("Unknown file format");
89  if (!conv.WriteFile(&(m->mol),fname))
90  Error("Error writting the output file");
91 }
92 
93 unsigned int nAtoms(TMolecule *ml)
94 {
95  TMolInfo *m;
96 
97  m=(TMolInfo *)ml;
98 
99  return(m->mol.NumAtoms());
100 }
101 
102 unsigned int GetAtomicNumber(unsigned int na,TMolecule *ml)
103 {
104  TMolInfo *m;
105  OBAtom *a;
106 
107  m=(TMolInfo *)ml;
108 
109  a=m->mol.GetAtom(na+1);
110 
111  return(a->GetAtomicNum());
112 }
113 
114 unsigned int GetAtomResidue(unsigned int na,TMolecule *ml)
115 {
116  TMolInfo *m;
117  OBAtom *a;
118  OBResidue *r;
119 
120  m=(TMolInfo *)ml;
121 
122  a=m->mol.GetAtom(na+1);
123  r=a->GetResidue();
124 
125  if (r==NULL)
126  return(NO_UINT);
127  else
128  return(r->GetNum());
129 }
130 
131 char GetAtomChain(unsigned int na,TMolecule *ml)
132 {
133  TMolInfo *m;
134  OBAtom *a;
135  OBResidue *r;
136 
137  m=(TMolInfo *)ml;
138 
139  a=m->mol.GetAtom(na+1);
140  r=a->GetResidue();
141 
142  if (r==NULL)
143  return('-');
144  else
145  return(r->GetChain());
146 }
147 
148 boolean IsAtomInProline(unsigned int na,TMolecule *ml)
149 {
150  TMolInfo *m;
151  OBAtom *a;
152  OBResidue *r;
153 
154  m=(TMolInfo *)ml;
155 
156  a=m->mol.GetAtom(na+1);
157  r=a->GetResidue();
158 
159  if (r==NULL)
160  return(FALSE);
161  else
162  return(r->GetName().compare("PRO")==0);
163 }
164 
165 double VdWRadius(unsigned int na,TMolecule *ml)
166 {
167  TMolInfo *m;
168  OBAtom *a;
169 
170  m=(TMolInfo *)ml;
171 
172  a=m->mol.GetAtom(na+1); // In babel atoms are numbered from 1
173 
174  return(etab.GetVdwRad(a->GetAtomicNum()));
175 }
176 
177 boolean HasBond(unsigned int na1,unsigned int na2,TMolecule *ml)
178 {
179  TMolInfo *m;
180  OBAtom *a,*n;
181  OBBondIterator ait;
182  boolean found;
183 
184  m=(TMolInfo *)ml;
185 
186  a=m->mol.GetAtom(na1+1); // In babel atoms are numbered from 1
187 
188  n=a->BeginNbrAtom(ait);
189  found=false;
190  while ((!found)&&(n!=NULL))
191  {
192  found=(n->GetIdx()-1==na2);
193  n=a->NextNbrAtom(ait);
194  }
195 
196  if (found)
197  return(TRUE);
198  else
199  return(FALSE);
200 }
201 
202 void AddBond(unsigned int na1,unsigned int na2,TMolecule *ml)
203 {
204  TMolInfo *m;
205 
206  m=(TMolInfo *)ml;
207 
208  m->mol.AddBond(na1+1,na2+1,1);
209 }
210 
211 void RemoveBond(unsigned int na1,unsigned int na2,TMolecule *ml)
212 {
213  TMolInfo *m;
214  OBAtom *a;
215  OBBond *b;
216  OBBondIterator bit;
217  boolean found;
218 
219  m=(TMolInfo *)ml;
220 
221  a=m->mol.GetAtom(na1+1); // In babel atoms are numbered from 1
222 
223  b=a->BeginBond(bit);
224  found=false;
225  while ((!found)&&(b!=NULL))
226  {
227  found=(((b->GetBeginAtomIdx()-1==na1)&&(b->GetEndAtomIdx()-1==na2))||
228  ((b->GetBeginAtomIdx()-1==na2)&&(b->GetEndAtomIdx()-1==na1)));
229  if (!found)
230  b=a->NextBond(bit);
231  }
232 
233  if (found)
234  m->mol.DeleteBond(b);
235 }
236 
237 unsigned int GetFirstNeighbour(unsigned int na,boolean *fix,TBondIterator **it,TMolecule *ml)
238 {
239  TMolInfo *m;
240  OBAtom *a,*n;
241  OBBondIterator *ait;
242 
243  m=(TMolInfo *)ml;
244 
245  ait=new OBBondIterator;
246 
247  a=m->mol.GetAtom(na+1); // In babel atoms are numbered from 1
248  n=a->BeginNbrAtom(*ait);
249  *it=(void *)ait;
250 
251  if (n==NULL)
252  {
253  *fix=FALSE;
254  return(NO_UINT);
255  }
256  else
257  {
258  /* double bonds fix the rotation */
259  *fix=((*(*ait))->IsDouble());
260  return(n->GetIdx()-1); // In babel atoms are numbered from 1
261  }
262 }
263 
264 unsigned int GetNextNeighbour(unsigned int na,boolean *fix,TBondIterator *it,TMolecule *ml)
265 {
266  TMolInfo *m;
267  OBAtom *a,*n;
268  OBBondIterator *ait;
269 
270  m=(TMolInfo *)ml;
271  ait=(OBBondIterator *)it;
272 
273  a=m->mol.GetAtom(na+1); // In babel atoms are numbered from 1
274  n=a->NextNbrAtom(*ait);
275 
276  if (n==NULL)
277  {
278  *fix=FALSE;
279  return(NO_UINT);
280  }
281  else
282  {
283  /* double bonds fix the rotation */
284  /* hidrogens can rotate but since there is nothing else attached to them
285  the rotation has no effect (due to the symmetry of hydrogens represented
286  as balls) */
287  *fix=((*(*ait))->IsDouble());
288  return(n->GetIdx()-1); // In babel atoms are numbered from 1
289  }
290 }
291 
293 {
294  OBBondIterator *ait;
295 
296  ait=(OBBondIterator *)it;
297 
298  delete ait;
299 }
300 
301 void GetAtomCoordinates(double *pos,TMolecule *ml)
302 {
303  TMolInfo *m;
304  double *p;
305  OBAtomIterator i;
306 
307  m=(TMolInfo *)ml;
308 
309  p=pos;
310  for(i=m->mol.BeginAtoms();i!=m->mol.EndAtoms();++i)
311  {
312  *p=(*i)->x();p++;
313  *p=(*i)->y();p++;
314  *p=(*i)->z();p++;
315  }
316 }
317 
318 void SetAtomCoordinates(double *pos,TMolecule *ml)
319 {
320  TMolInfo *m;
321 
322  m=(TMolInfo *)ml;
323 
324  m->mol.SetCoordinates(pos);
325 }
326 
328 {
329  TMolInfo *m;
330 
331  m=(TMolInfo *)ml;
332 
333  if (m->ff==NULL)
334  {
335  m->ff=OBForceField::FindType("MMFF94");
336 
337  if (!m->ff->Setup(m->mol))
338  Error("Could not setup forcefield.");
339  }
340 
341  m->ff->SetCoordinates(m->mol);
342 
343  return(m->ff->Energy(false));
344 }
345 
347 {
348  TMolInfo *m;
349 
350  m=(TMolInfo *)ml;
351 
352  delete m;
353 }
Definition of the boolean type.
boolean HasBond(unsigned int na1, unsigned int na2, TMolecule *ml)
Determines is a given bond exists.
Definition: babel.cpp:177
void DeleteMolecule(TMolecule *ml)
Destructor.
Definition: babel.cpp:346
void SetAtomCoordinates(double *pos, TMolecule *ml)
Sets the positions of the atoms in the molecule.
Definition: babel.cpp:318
unsigned int nAtoms(TMolecule *ml)
Number of atoms in a molecule.
Definition: babel.cpp:93
#define FALSE
FALSE.
Definition: boolean.h:30
unsigned int GetAtomicNumber(unsigned int na, TMolecule *ml)
Returns the atomic number of a given atom.
Definition: babel.cpp:102
boolean IsAtomInProline(unsigned int na, TMolecule *ml)
Identifies atoms in proline residues.
Definition: babel.cpp:148
void GetAtomCoordinates(double *pos, TMolecule *ml)
Gets the positions of the atoms in the molecule.
Definition: babel.cpp:301
void WriteMolecule(char *fname, TMolecule *ml)
Writes the molecule information to a file.
Definition: babel.cpp:72
#define TRUE
TRUE.
Definition: boolean.h:21
void Error(const char *s)
General error function.
Definition: error.c:80
void AddBond(unsigned int na1, unsigned int na2, TMolecule *ml)
Adds a bond between two atoms.
Definition: babel.cpp:202
void TBondIterator
Iterator over the neighbours of a given atom.
Definition: babel.h:26
Summarizes the information relating a given molecule.
Definition: babel.cpp:41
Error and warning functions.
unsigned int GetAtomResidue(unsigned int na, TMolecule *ml)
Returns the residue of a given atom.
Definition: babel.cpp:114
void RemoveBond(unsigned int na1, unsigned int na2, TMolecule *ml)
Removes a bond between two atoms.
Definition: babel.cpp:211
void DeleteBondIterator(TBondIterator *it)
Deletes the bond iterator defined in GetFirstNeighbour.
Definition: babel.cpp:292
unsigned int GetNextNeighbour(unsigned int na, boolean *fix, TBondIterator *it, TMolecule *ml)
Gets the identifier of the next neighbour for a given atom in a molecule.
Definition: babel.cpp:264
Definitions of constants and macros used in several parts of the cuik library.
Minimalistic Cuik-OpenBabel interface.
char GetAtomChain(unsigned int na, TMolecule *ml)
Returns a char identifying the chain of the atom.
Definition: babel.cpp:131
TMolecule * ReadMolecule(char *fname)
Reads the molecule information from a file.
Definition: babel.cpp:46
double VdWRadius(unsigned int na, TMolecule *ml)
Returns the Van der Waals radius for a given atom.
Definition: babel.cpp:165
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
unsigned int GetFirstNeighbour(unsigned int na, boolean *fix, TBondIterator **it, TMolecule *ml)
Gets the identifier of the first neighbour for a given atom in a molecule.
Definition: babel.cpp:237
void TMolecule
The molecule handler type.
Definition: babel.h:33
OBForceField * ff
Definition: babel.cpp:43
double ComputeEnergy(TMolecule *ml)
Computes the potential energy of a molecule.
Definition: babel.cpp:327
OBMol mol
Definition: babel.cpp:42