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