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

« back to all changes in this revision

Viewing changes to src/formats/xml/cdxmlformat.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:
 
1
/**********************************************************************
 
2
Copyright (C) 2006 by Geoff Hutchison
 
3
 
 
4
This file is part of the Open Babel project.
 
5
For more information, see <http://openbabel.sourceforge.net/>
 
6
 
 
7
This program is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU General Public License as published by
 
9
the Free Software Foundation version 2 of the License.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
***********************************************************************/
 
16
 
 
17
#include <openbabel/babelconfig.h>
 
18
#include <openbabel/xml.h>
 
19
 
 
20
#ifdef WIN32
 
21
#pragma warning (disable : 4800)
 
22
#endif
 
23
 
 
24
using namespace std;
 
25
namespace OpenBabel
 
26
{
 
27
 
 
28
class ChemDrawXMLFormat : public XMLMoleculeFormat
 
29
{
 
30
public:
 
31
        ChemDrawXMLFormat(): Order (-1)
 
32
        {
 
33
                OBConversion::RegisterFormat("cdxml", this, "chemical/x-cdxml");
 
34
                XMLConversion::RegisterXMLFormat(this, false, "http://www.camsoft.com/xml/cdxml.dtd");
 
35
                XMLConversion::RegisterXMLFormat(this);
 
36
        }
 
37
        virtual const char* NamespaceURI()const{return "http://www.cambridgesoft.com/xml/cdxml.dtd";}
 
38
  virtual const char* Description()
 
39
  {
 
40
      return " \
 
41
ChemDraw CDXML format \n \
 
42
Minimal support of chemical structure information only.\n \
 
43
\n";
 
44
};
 
45
 
 
46
  virtual const char* GetMIMEType() 
 
47
  { return "chemical/x-cdxml"; };
 
48
 
 
49
  virtual const char* SpecificationURL()
 
50
  {return "http://www.cambridgesoft.com/services/documentation/sdk/chemdraw/cdx/";}
 
51
 
 
52
 
 
53
  virtual unsigned int Flags()
 
54
  {
 
55
      return READXML;
 
56
  };
 
57
 
 
58
    virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
 
59
        virtual bool DoElement(const string& name);
 
60
        virtual bool EndElement(const string& name);
 
61
 
 
62
        // EndTag is used so that the stream buffer is is filled with the XML from
 
63
        // complete objects, as far as possible. 
 
64
        virtual const char* EndTag(){ return "/fragment>"; };
 
65
 
 
66
    //atoms and bonds might have no content, so EndElement is not always called
 
67
    // that's why we need to ensure that atoms and bonds are really added.
 
68
    void EnsureEndElement(void);
 
69
 
 
70
private:
 
71
  OBAtom _tempAtom; //!< A temporary atom as the atom tag is read
 
72
  int Begin, End, Order, Flag; // Data for current bond
 
73
  map <int, int> atoms; // maps chemdraw atom id to openbabel idx.
 
74
  int _offset; // used to ensure that atoms have different ids.
 
75
  double _scale; // current scale
 
76
};
 
77
 
 
78
////////////////////////////////////////////////////////////////////
 
79
 
 
80
ChemDrawXMLFormat theChemDrawXMLFormat;
 
81
 
 
82
////////////////////////////////////////////////////////////////////
 
83
 
 
84
bool ChemDrawXMLFormat::DoElement(const string& name)
 
85
{
 
86
  string buf;
 
87
  if(name=="fragment") 
 
88
  {
 
89
    //This is the start of the molecule we are extracting and it will
 
90
    //be put into the OBMol* _pmol declared in the parent class.
 
91
    //initialise everything
 
92
    _tempAtom.Clear();
 
93
    atoms.clear();
 
94
 
 
95
    _pmol->SetDimension(2);
 
96
    _pmol->BeginModify();
 
97
  }
 
98
  else if(name=="n")
 
99
  {
 
100
    EnsureEndElement();
 
101
    buf = _pxmlConv->GetAttribute("Type");
 
102
    if (buf.length())
 
103
    {
 
104
      if (buf != "Unspecified" && buf != "Element")
 
105
      {
 
106
        cerr << "CDXML Format: Node type \"" << buf <<
 
107
                  "\" is not currently supported." << endl;
 
108
        return false; // FIXME: use as many types as possible
 
109
          }
 
110
    }
 
111
    _tempAtom.SetAtomicNum(6); // default is carbon
 
112
    buf = _pxmlConv->GetAttribute("id");
 
113
    if (buf.length())
 
114
      _tempAtom.SetIdx(atoi(buf.c_str()));
 
115
    buf = _pxmlConv->GetAttribute("Element");
 
116
    if (buf.length())
 
117
      _tempAtom.SetAtomicNum(atoi(buf.c_str()));
 
118
 
 
119
    buf = _pxmlConv->GetAttribute("p"); // coords
 
120
    if (buf.length())
 
121
    {
 
122
      double x = 0., y = 0.;
 
123
      sscanf(buf.c_str(), "%lf %lf", &x, &y);
 
124
      _tempAtom.SetVector(x, y, 0.);
 
125
    }
 
126
    buf = _pxmlConv->GetAttribute("Charge");
 
127
    if (buf.length())
 
128
      _tempAtom.SetFormalCharge(atoi(buf.c_str()));
 
129
  }
 
130
  else if(name=="b")
 
131
  {
 
132
    EnsureEndElement();
 
133
        bool invert_ends = false;
 
134
        Begin = End = Flag = 0;
 
135
    buf = _pxmlConv->GetAttribute("Order");
 
136
    if (buf.length())
 
137
      Order = atoi(buf.c_str());
 
138
    else
 
139
      Order = 1; //default value
 
140
    buf = _pxmlConv->GetAttribute("Display");
 
141
    if (buf.length())
 
142
    {
 
143
          if (buf == "WedgeEnd")
 
144
      {
 
145
        invert_ends = true;
 
146
        Flag = OB_WEDGE_BOND;
 
147
          }
 
148
      else if (buf == "WedgeBegin")
 
149
        Flag = OB_WEDGE_BOND;
 
150
      else if (buf == "WedgedHashBegin")
 
151
      {
 
152
        invert_ends = true;
 
153
        Flag = OB_HASH_BOND;
 
154
          }
 
155
      else if (buf == "Hash" || buf == "WedgedHashEnd")
 
156
        Flag = OB_HASH_BOND;
 
157
    }
 
158
    buf = _pxmlConv->GetAttribute("B");
 
159
    if (buf.length())
 
160
    {
 
161
      if (invert_ends)
 
162
        End = atoms[atoi(buf.c_str())];
 
163
      else
 
164
        Begin = atoms[atoi(buf.c_str())];
 
165
    }
 
166
    buf = _pxmlConv->GetAttribute("E");
 
167
    if (buf.length())
 
168
    {
 
169
      if (invert_ends)
 
170
        Begin = atoms[atoi(buf.c_str())];
 
171
      else
 
172
        End = atoms[atoi(buf.c_str())];
 
173
    }
 
174
  }
 
175
 
 
176
  return true;
 
177
}
 
178
 
 
179
bool ChemDrawXMLFormat::EndElement(const string& name)
 
180
{
 
181
  //unsigned int i;
 
182
  if(name=="n")
 
183
  {
 
184
    _pmol->AddAtom(_tempAtom);
 
185
    atoms[_tempAtom.GetIdx()] = _pmol->NumAtoms();
 
186
    _tempAtom.Clear();
 
187
  }
 
188
  else if(name=="b")
 
189
  {
 
190
    _pmol->AddBond(Begin, End, Order, Flag);
 
191
        Order = -1;
 
192
  }
 
193
  else if(name=="fragment") //this is the end of the molecule we are extracting
 
194
  {
 
195
    EnsureEndElement();
 
196
    _pmol->EndModify();
 
197
    atoms.clear();
 
198
    return false;//means stop parsing
 
199
  }
 
200
  return true;
 
201
}       
 
202
 
 
203
void ChemDrawXMLFormat::EnsureEndElement(void)
 
204
{
 
205
  if (_tempAtom.GetAtomicNum() != 0)
 
206
  {
 
207
    _pmol->AddAtom(_tempAtom);
 
208
    atoms[_tempAtom.GetIdx()] = _pmol->NumAtoms();
 
209
    _tempAtom.Clear();
 
210
  }
 
211
  else if (Order >= 0)
 
212
  {
 
213
    _pmol->AddBond(Begin, End, Order, Flag);
 
214
        Order = -1;
 
215
  }
 
216
}
 
217
 
 
218
bool ChemDrawXMLFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
 
219
{
 
220
  static const xmlChar C_MOLECULE[]         = "fragment";
 
221
  static const xmlChar C_CDXML[]            = "CDXML";
 
222
  static const xmlChar C_BONDLENGTH[]       = "BondLength";
 
223
  static const xmlChar C_PAGE[]             = "page";
 
224
  static const xmlChar C_ATOM[]             = "n";
 
225
  static const xmlChar C_BOND[]             = "b";
 
226
  static const xmlChar C_ID[]               = "id";
 
227
 
 
228
  static const xmlChar C_CHARGE[]           = "Charge";
 
229
  static const xmlChar C_COORDS[]           = "p";
 
230
  static const xmlChar C_ELEMENT[]          = "Element";
 
231
  static const xmlChar C_ORDER[]            = "Order";
 
232
  static const xmlChar C_BEGIN[]            = "B";
 
233
  static const xmlChar C_END[]              = "E";
 
234
  static const xmlChar C_DISPLAY[]          = "Display";
 
235
 
 
236
  _pxmlConv = XMLConversion::GetDerived(pConv,false);
 
237
  if(!_pxmlConv)
 
238
    return false;
 
239
 
 
240
  OBMol* pmol = dynamic_cast<OBMol*>(pOb);
 
241
  if(pmol==NULL)
 
242
        return false;
 
243
  OBMol &mol = *pmol;
 
244
 
 
245
  OBBond *pbond;
 
246
  vector<OBBond*>::iterator j;
 
247
  if(_pxmlConv->GetOutputIndex() == 1)
 
248
  {
 
249
    xmlTextWriterStartDocument(writer(), NULL, NULL, NULL);
 
250
    xmlTextWriterWriteDTD(writer(), BAD_CAST "CDXML", NULL, BAD_CAST "http://www.camsoft.com/xml/cdxml.dtd", NULL);
 
251
    xmlTextWriterStartElement(writer(), C_CDXML);
 
252
    xmlTextWriterWriteFormatAttribute(writer(), C_BONDLENGTH , "30");
 
253
    xmlTextWriterStartElement(writer(), C_PAGE); // put everything on one page
 
254
    // now guess the average bond size for the first molecule and scale to 30.
 
255
    _scale = 0.;
 
256
    if (mol.NumBonds())
 
257
    {
 
258
      for (pbond = mol.BeginBond(j);pbond;pbond = mol.NextBond(j))
 
259
        _scale += pbond->GetLength();
 
260
      _scale /= mol.NumBonds();
 
261
        }
 
262
    else
 
263
      _scale = 1.; // FIXME: what happens if the molecule has no bond?
 
264
    _scale = 30. / _scale;
 
265
    _offset = 0;
 
266
  }
 
267
        
 
268
  xmlTextWriterStartElement(writer(), C_MOLECULE);
 
269
 
 
270
  OBAtom *patom;
 
271
  vector<OBAtom*>::iterator i;
 
272
  int n;
 
273
  for (patom = mol.BeginAtom(i);patom;patom = mol.NextAtom(i))
 
274
  {
 
275
    xmlTextWriterStartElement(writer(), C_ATOM);
 
276
 
 
277
    xmlTextWriterWriteFormatAttribute(writer(), C_ID , "%d", patom->GetIdx() + _offset);
 
278
    xmlTextWriterWriteFormatAttribute(writer(), C_COORDS , "%f %f", patom->GetX() * _scale, patom->GetY() * _scale);
 
279
        n = patom->GetAtomicNum();
 
280
    if (n != 6)
 
281
    {
 
282
      xmlTextWriterWriteFormatAttribute(writer(), C_ELEMENT , "%d", n);
 
283
    }
 
284
    n = patom->GetFormalCharge();
 
285
    if (n != 0)
 
286
    {
 
287
      xmlTextWriterWriteFormatAttribute(writer(), C_CHARGE , "%d", n);
 
288
    }
 
289
    xmlTextWriterEndElement(writer());
 
290
  }
 
291
 
 
292
  for (pbond = mol.BeginBond(j);pbond;pbond = mol.NextBond(j))
 
293
  {
 
294
    xmlTextWriterStartElement(writer(), C_BOND);
 
295
        patom = pbond->GetBeginAtom();
 
296
    xmlTextWriterWriteFormatAttribute(writer(), C_BEGIN , "%d", patom->GetIdx() + _offset);
 
297
        patom = pbond->GetEndAtom();
 
298
    xmlTextWriterWriteFormatAttribute(writer(), C_END , "%d", patom->GetIdx() + _offset);
 
299
        n = pbond->GetBO();
 
300
    if (n != 1)
 
301
    {
 
302
      xmlTextWriterWriteFormatAttribute(writer(), C_ORDER , "%d", n);
 
303
    }
 
304
    if (pbond->IsWedge())
 
305
      xmlTextWriterWriteFormatAttribute(writer(), C_DISPLAY , "WedgeBegin");
 
306
    else if (pbond->IsHash())
 
307
      xmlTextWriterWriteFormatAttribute(writer(), C_DISPLAY , "WedgedHashEnd");
 
308
    xmlTextWriterEndElement(writer());
 
309
  }
 
310
  _offset += mol.NumAtoms ();
 
311
 
 
312
  xmlTextWriterEndElement(writer());//molecule
 
313
 
 
314
  if(_pxmlConv->IsLast())
 
315
  {
 
316
    xmlTextWriterEndDocument(writer()); // page
 
317
    xmlTextWriterEndDocument(writer()); //document
 
318
    OutputToStream();
 
319
  }
 
320
  return true;
 
321
}
 
322
 
 
323
}//namespace