~ubuntu-branches/debian/stretch/openbabel/stretch

« back to all changes in this revision

Viewing changes to src/formats/cacaoformat.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2008-07-22 23:54:58 UTC
  • mfrom: (3.1.10 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080722235458-3o606czluviz4akx
Tags: 2.2.0-2
* Upload to unstable.
* debian/control: Updated descriptions.
* debian/patches/gauss_cube_format.patch: New patch, makes the 
  gaussian cube format available again.
* debian/rules (DEB_DH_MAKESHLIBS_ARGS_libopenbabel3): Removed.
* debian/rules (DEB_CONFIGURE_EXTRA_FLAGS): Likewise.
* debian/libopenbabel3.install: Adjust formats directory to single 
  version hierarchy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
13
GNU General Public License for more details.
14
14
***********************************************************************/
 
15
#include <openbabel/babelconfig.h>
15
16
 
16
 
#include "mol.h"
17
 
#include "math/matrix3x3.h"
18
 
#include "obconversion.h"
19
 
#include "obmolecformat.h"
 
17
#include <openbabel/obmolecformat.h>
 
18
#include <openbabel/math/matrix3x3.h>
20
19
 
21
20
using namespace std;
22
21
namespace OpenBabel
23
22
{
24
23
 
25
 
class CacaoFormat : public OBMoleculeFormat
26
 
{
27
 
public:
 
24
  class CacaoFormat : public OBMoleculeFormat
 
25
  {
 
26
  public:
28
27
    //Register this format type ID
29
28
    CacaoFormat()
30
29
    {
31
 
        OBConversion::RegisterFormat("caccrt",this);
 
30
      OBConversion::RegisterFormat("caccrt",this);
32
31
    }
33
32
 
34
 
  virtual const char* Description() //required
35
 
  {
36
 
    return
37
 
      "Cacao Cartesian format\n \
38
 
       Read Options e.g. -as\n\
39
 
        s  Output single bonds only\n\
40
 
        b  Disable bonding entirely\n\n";
41
 
  };
42
 
 
43
 
  virtual const char* SpecificationURL()
44
 
  {return "http://www.chembio.uoguelph.ca/oakley/310/cacao/cacao.htm";}; //optional
45
 
 
46
 
  //Flags() can return be any the following combined by | or be omitted if none apply
47
 
  // NOTREADABLE  READONEONLY  NOTWRITABLE  WRITEONEONLY
48
 
  virtual unsigned int Flags()
49
 
  {
50
 
    return READONEONLY | WRITEONEONLY;
51
 
  };
52
 
 
53
 
  ////////////////////////////////////////////////////
54
 
  /// The "API" interface functions
55
 
  virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
56
 
  virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
57
 
 
58
 
  static void SetHilderbrandt(OBMol&,vector<OBInternalCoord*>&);
59
 
};
60
 
 
61
 
//Make an instance of the format class
62
 
CacaoFormat theCacaoFormat;
63
 
 
64
 
/////////////////////////////////////////////////////////////////
65
 
bool CacaoFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
66
 
{
67
 
 
68
 
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
 
33
    virtual const char* Description() //required
 
34
    {
 
35
      return
 
36
        "Cacao Cartesian format\n"
 
37
        "Read Options e.g. -as\n"
 
38
        "  s  Output single bonds only\n"
 
39
        "  b  Disable bonding entirely\n\n";
 
40
    };
 
41
 
 
42
    virtual const char* SpecificationURL()
 
43
    {return "http://www.chembio.uoguelph.ca/oakley/310/cacao/cacao.htm";}; //optional
 
44
 
 
45
    //Flags() can return be any the following combined by | or be omitted if none apply
 
46
    // NOTREADABLE  READONEONLY  NOTWRITABLE  WRITEONEONLY
 
47
    virtual unsigned int Flags()
 
48
    {
 
49
      return READONEONLY | WRITEONEONLY;
 
50
    };
 
51
 
 
52
    ////////////////////////////////////////////////////
 
53
    /// The "API" interface functions
 
54
    virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
 
55
    virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
 
56
 
 
57
    static void SetHilderbrandt(OBMol&,vector<OBInternalCoord*>&);
 
58
  };
 
59
 
 
60
  //Make an instance of the format class
 
61
  CacaoFormat theCacaoFormat;
 
62
 
 
63
  /////////////////////////////////////////////////////////////////
 
64
  bool CacaoFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
 
65
  {
 
66
 
 
67
    OBMol* pmol = pOb->CastAndClear<OBMol>();
69
68
    if(pmol==NULL)
70
 
        return false;
 
69
      return false;
71
70
 
72
71
    //Define some references so we can use the old parameter names
73
72
    istream &ifs = *pConv->GetInStream();
85
84
    sscanf(buffer,"%d",&natoms);
86
85
 
87
86
    while (ifs.getline(buffer,BUFF_SIZE) &&!EQn(buffer,"CELL",4))
88
 
        ;
 
87
      ;
89
88
 
90
89
    if (!EQn(buffer,"CELL",4))
91
 
        return(false);
 
90
      return(false);
92
91
    vector<string> vs;
93
92
    tokenize(vs,buffer," \n\t,");
94
93
    if (vs.size() != 7)
95
 
        return(false);
 
94
      return(false);
96
95
 
97
96
    //parse cell values
98
97
    A = atof((char*)vs[1].c_str());
104
103
 
105
104
    OBUnitCell *uc = new OBUnitCell;
106
105
    uc->SetData(A, B, C, Alpha, Beta, Gamma);
 
106
    uc->SetOrigin(fileformatInput);
107
107
    mol.SetData(uc);
108
108
    m = uc->GetOrthoMatrix();
109
109
 
115
115
    mol.BeginModify();
116
116
 
117
117
    for (i = 1; i <= natoms;i++)
118
 
    {
 
118
      {
119
119
        if (!ifs.getline(buffer,BUFF_SIZE))
120
 
            return(false);
 
120
          return(false);
121
121
        tokenize(vs,buffer," \n\t,");
122
122
        if (vs.size() < 4)
123
 
            return(false);
 
123
          return(false);
124
124
        atom = mol.NewAtom();
125
125
 
126
126
        x = atof((char*)vs[1].c_str());
131
131
 
132
132
        atom->SetAtomicNum(etab.GetAtomicNum(vs[0].c_str()));
133
133
        atom->SetVector(v);
134
 
    }
 
134
      }
135
135
 
136
136
    if (!pConv->IsOption("b",OBConversion::INOPTIONS))
137
137
      mol.ConnectTheDots();
140
140
 
141
141
    // clean out remaining blank lines
142
142
    while(ifs.peek() != EOF && ifs.good() && 
143
 
          (ifs.peek() == '\n' || ifs.peek() == '\r'))
 
143
          (ifs.peek() == '\n' || ifs.peek() == '\r'))
144
144
      ifs.getline(buffer,BUFF_SIZE);
145
145
 
146
146
    mol.EndModify();
147
147
    return(true);
148
 
}
149
 
 
150
 
////////////////////////////////////////////////////////////////
151
 
 
152
 
bool CacaoFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
153
 
{
 
148
  }
 
149
 
 
150
  ////////////////////////////////////////////////////////////////
 
151
 
 
152
  bool CacaoFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
 
153
  {
154
154
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
155
155
    if(pmol==NULL)
156
 
        return false;
 
156
      return false;
157
157
 
158
158
    //Define some references so we can use the old parameter names
159
159
    ostream &ofs = *pConv->GetOutStream();
161
161
 
162
162
    OBAtom *atom;
163
163
    char buffer[BUFF_SIZE];
164
 
    vector<OBNodeBase*>::iterator i;
 
164
    vector<OBAtom*>::iterator i;
165
165
 
166
166
    snprintf(buffer, BUFF_SIZE, "%s\n",mol.GetTitle());
167
167
    ofs << buffer;
171
171
    if (!mol.HasData(OBGenericDataType::UnitCell))
172
172
      ofs << "CELL 1.,1.,1.,90.,90.,90.\n";
173
173
    else
174
 
    {
 
174
      {
175
175
        OBUnitCell *uc = (OBUnitCell*)mol.GetData(OBGenericDataType::UnitCell);
176
176
        snprintf(buffer, BUFF_SIZE, "CELL %f,%f,%f,%f,%f,%f\n",
177
 
                uc->GetA(), uc->GetB(), uc->GetC(),
178
 
                uc->GetAlpha(), uc->GetBeta(), uc->GetGamma());
 
177
                 uc->GetA(), uc->GetB(), uc->GetC(),
 
178
                 uc->GetAlpha(), uc->GetBeta(), uc->GetGamma());
179
179
        ofs << buffer;
180
 
    }
 
180
      }
181
181
 
182
182
    for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
183
 
    {
 
183
      {
184
184
        snprintf(buffer,BUFF_SIZE,"%2s %7.4f, %7.4f, %7.4f\n",
185
 
                etab.GetSymbol(atom->GetAtomicNum()),
186
 
                atom->x(),
187
 
                atom->y(),
188
 
                atom->z());
 
185
                 etab.GetSymbol(atom->GetAtomicNum()),
 
186
                 atom->x(),
 
187
                 atom->y(),
 
188
                 atom->z());
189
189
        ofs << buffer;
190
 
    }
 
190
      }
191
191
 
192
192
    return(true);
193
 
}
 
193
  }
194
194
 
195
 
//! \todo Make this method bulletproof. Currently it causes segfaults sometimes
196
 
void CacaoFormat::SetHilderbrandt(OBMol &mol,vector<OBInternalCoord*> &vit)
197
 
{
 
195
  //! \todo Make this method bulletproof. Currently it causes segfaults sometimes
 
196
  void CacaoFormat::SetHilderbrandt(OBMol &mol,vector<OBInternalCoord*> &vit)
 
197
  {
198
198
    // Roundtrip testing shows that some atoms are NULL
199
199
    //  which causes segfaults when dereferencing later
200
200
    //   (e.g. in the last "segment" of this routine)
205
205
    dummy2.SetVector(1.0,0.0,0.0);
206
206
 
207
207
    OBAtom *atom,*a1,*a2,*ref;
208
 
    vector<OBNodeBase*>::iterator ai;
 
208
    vector<OBAtom*>::iterator ai;
209
209
 
210
210
    vit.push_back((OBInternalCoord*)NULL);
211
211
    for (atom = mol.BeginAtom(ai);atom;atom = mol.NextAtom(ai))
212
 
        vit.push_back(new OBInternalCoord (atom));
 
212
      vit.push_back(new OBInternalCoord (atom));
213
213
 
214
214
    vit[1]->_a = &dummy1;
215
215
    vit[1]->_b = &dummy2;
217
217
      vit[2]->_b = &dummy1;
218
218
      vit[2]->_c = &dummy2;
219
219
      if  (vit.size() > 3) {
220
 
        vit[3]->_c = &dummy1;
 
220
        vit[3]->_c = &dummy1;
221
221
      }
222
222
    }
223
223
 
224
224
    unsigned int i,j;
225
225
    for (i = 2;i <= mol.NumAtoms();i++)
226
 
    {
 
226
      {
227
227
        ref = (OBAtom*)NULL;
228
228
        a1 = mol.GetAtom(i);
229
229
        sum = 100.0;
230
230
        for (j = 1;j < i;j++)
231
 
        {
 
231
          {
232
232
            a2 = mol.GetAtom(j);
233
233
            r = (a1->GetVector()-a2->GetVector()).length_2();
234
234
            if ((r < sum) && (vit[j]->_a != a2) && (vit[j]->_b != a2))
235
 
            {
 
235
              {
236
236
                sum = r;
237
237
                ref = a2;
238
 
            }
239
 
        }
 
238
              }
 
239
          }
240
240
        vit[i]->_a = ref;
241
 
    }
 
241
      }
242
242
 
243
243
    for (i = 3;i <= mol.NumAtoms();i++)
244
 
        vit[i]->_b = vit[vit[i]->_a->GetIdx()]->_a;
 
244
      vit[i]->_b = vit[vit[i]->_a->GetIdx()]->_a;
245
245
 
246
246
    for (i = 4;i <= mol.NumAtoms();i++)
247
 
    {
 
247
      {
248
248
        if (vit[i]->_b && vit[i]->_b->GetIdx())
249
 
            vit[i]->_c = vit[vit[i]->_b->GetIdx()]->_b;
 
249
          vit[i]->_c = vit[vit[i]->_b->GetIdx()]->_b;
250
250
        else
251
 
            vit[i]->_c = &dummy1;
252
 
    }
 
251
          vit[i]->_c = &dummy1;
 
252
      }
253
253
 
254
254
    OBAtom *a,*b,*c;
255
255
    vector3 v1,v2;
256
256
    for (i = 2;i <= mol.NumAtoms();i++)
257
 
    {
 
257
      {
258
258
        atom = mol.GetAtom(i);
259
259
        a = vit[i]->_a;
260
260
        b = vit[i]->_b;
267
267
                                        b->GetVector(),
268
268
                                        c->GetVector());
269
269
        vit[i]->_dst = (vit[i]->_a->GetVector() - atom->GetVector()).length();
270
 
    }
 
270
      }
271
271
 
272
 
}
273
 
//***************************************************************
274
 
class CacaoInternalFormat : public OBFormat
275
 
{
276
 
public:
 
272
  }
 
273
  //***************************************************************
 
274
  class CacaoInternalFormat : public OBMoleculeFormat
 
275
  {
 
276
  public:
277
277
    //Register this format type ID
278
278
    CacaoInternalFormat()
279
279
    {
280
 
        OBConversion::RegisterFormat("cacint",this);
 
280
      OBConversion::RegisterFormat("cacint",this);
281
281
    }
282
282
 
283
283
    virtual const char* Description() //required
284
284
    {
285
 
        return
286
 
            "Cacao Internal format\n \
 
285
      return
 
286
        "Cacao Internal format\n \
287
287
            No comments yet\n \
288
288
            ";
289
289
    };
290
290
 
291
291
    virtual const char* SpecificationURL(){return
292
 
            "http://www.chembio.uoguelph.ca/oakley/310/cacao/cacao.htm";}; //optional
 
292
        "http://www.chembio.uoguelph.ca/oakley/310/cacao/cacao.htm";}; //optional
293
293
 
294
294
    //Flags() can return be any the following combined by | or be omitted if none apply
295
295
    // NOTREADABLE  READONEONLY  NOTWRITABLE  WRITEONEONLY
296
296
    virtual unsigned int Flags()
297
297
    {
298
 
        return NOTREADABLE | WRITEONEONLY;
 
298
      return NOTREADABLE | WRITEONEONLY;
299
299
    };
300
300
 
301
301
    ////////////////////////////////////////////////////
302
302
    /// The "API" interface functions
303
303
    virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
304
 
 
305
 
    ////////////////////////////////////////////////////
306
 
    virtual bool WriteChemObject(OBConversion* pConv)
307
 
    {
308
 
        //Retrieve the target OBMol
309
 
        OBBase* pOb = pConv->GetChemObject();
310
 
        OBMol* pmol = dynamic_cast<OBMol*> (pOb);
311
 
        bool ret=false;
312
 
        if(pmol)
313
 
            ret=WriteMolecule(pmol,pConv);
314
 
        delete pOb;
315
 
        return ret;
316
 
    };
317
 
};
318
 
 
319
 
//Make an instance of the format class
320
 
CacaoInternalFormat theCacaoInternalFormat;
321
 
 
322
 
bool CacaoInternalFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
323
 
{
 
304
  };
 
305
 
 
306
  //Make an instance of the format class
 
307
  CacaoInternalFormat theCacaoInternalFormat;
 
308
 
 
309
  bool CacaoInternalFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
 
310
  {
324
311
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
325
312
    if(pmol==NULL)
326
 
        return false;
 
313
      return false;
327
314
 
328
315
    //Define some references so we can use the old parameter names
329
316
    ostream &ofs = *pConv->GetOutStream();
334
321
    char tmptype[16],buffer[BUFF_SIZE];
335
322
 
336
323
    if (mol.Empty())
337
 
        return(false);
 
324
      return(false);
338
325
 
339
326
    //translate first atom to origin
340
327
    v = mol.GetAtom(1)->GetVector();
352
339
    snprintf(buffer, BUFF_SIZE, "0.,0.,0., %s\n",tmptype);
353
340
    ofs << buffer;
354
341
    for (i = 2; i <= mol.NumAtoms(); i++)
355
 
    {
 
342
      {
356
343
        strncpy(tmptype,etab.GetSymbol(mol.GetAtom(i)->GetAtomicNum()), sizeof(tmptype));
357
344
        tmptype[sizeof(tmptype) - 1] = '\0';
358
345
 
359
346
        if (vit[i]->_tor < 0.0)
360
 
            vit[i]->_tor += 360.0;
 
347
          vit[i]->_tor += 360.0;
361
348
        snprintf(buffer, BUFF_SIZE, "%2d,%d,%2s%7.3f,%7.3f,%7.3f",
362
 
                vit[i]->_a->GetIdx(),i,tmptype,
363
 
                vit[i]->_dst,
364
 
                vit[i]->_ang,
365
 
                vit[i]->_tor);
 
349
                 vit[i]->_a->GetIdx(),i,tmptype,
 
350
                 vit[i]->_dst,
 
351
                 vit[i]->_ang,
 
352
                 vit[i]->_tor);
366
353
        ofs << buffer << endl;
367
 
    }
 
354
      }
368
355
 
369
356
    vector<OBInternalCoord*>::iterator j;
370
357
    for (j = vit.begin();j != vit.end();j++)
371
 
        if (*j)
 
358
      if (*j)
372
359
        {
373
 
            delete *j;
374
 
            *j = NULL;
 
360
          delete *j;
 
361
          *j = NULL;
375
362
        }
376
363
 
377
364
    return(true);
378
 
}
 
365
  }
379
366
 
380
367
} //namespace OpenBabel