1
/**********************************************************************
2
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
3
Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
4
Some portions Copyright (C) 2004 by Chris Morley
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation version 2 of the License.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
***********************************************************************/
17
#include "obconversion.h"
18
#include "obmolecformat.h"
24
class BGFFormat : public OBMoleculeFormat
27
//Register this format type ID
30
OBConversion::RegisterFormat("bgf",this);
33
virtual const char* Description() //required
40
virtual const char* SpecificationURL()
41
{return "";}; //optional
43
//Flags() can return be any the following combined by | or be omitted if none apply
44
// NOTREADABLE READONEONLY NOTWRITABLE WRITEONEONLY
45
virtual unsigned int Flags()
47
return READONEONLY | WRITEONEONLY;
50
//*** This section identical for most OBMol conversions ***
51
////////////////////////////////////////////////////
52
/// The "API" interface functions
53
virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
54
virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
58
//Make an instance of the format class
59
BGFFormat theBGFFormat;
61
/////////////////////////////////////////////////////////////////
62
bool BGFFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
65
OBMol* pmol = dynamic_cast<OBMol*>(pOb);
69
//Define some references so we can use the old parameter names
70
istream &ifs = *pConv->GetInStream();
72
mol.SetTitle( pConv->GetTitle()); //default title is the filename
75
char buffer[BUFF_SIZE];
76
char tmp[15],tmptyp[15];
78
while (ifs.getline(buffer,BUFF_SIZE))
79
if (EQn(buffer,"FORMAT",6))
82
ttab.SetFromType("DRE");
83
ttab.SetToType("INT");
88
if (!ifs.getline(buffer,BUFF_SIZE))
90
if (EQn(buffer,"FORMAT",6))
93
sscanf(buffer,"%*s %*s %*s %*s %*s %*s %lf %lf %lf %s %*s %*s %lf",
99
ttab.Translate(tmp,tmptyp);
102
CleanAtomType(tmptyp);
103
atom->SetAtomicNum(etab.GetAtomicNum(tmptyp));
105
atom->SetVector(x,y,z);
109
vector<vector<int> > vcon;
110
vector<vector<int> > vord;
112
for (i = 0; i < mol.NumAtoms();i++)
114
vcon.push_back(vtmp);
115
vord.push_back(vtmp);
122
if (!ifs.getline(buffer,BUFF_SIZE) || EQn(buffer,"END",3))
126
if (vs.empty() || vs.size() < 3 || vs.size() > 10)
129
if (EQn(buffer,"CONECT",6))
131
bgn = atoi((char*)vs[1].c_str()) - 1;
132
if (bgn < 1 || bgn > mol.NumAtoms())
134
for (i = 2;i < vs.size();i++)
136
vcon[bgn].push_back(atoi((char*)vs[i].c_str()));
137
vord[bgn].push_back(1);
141
if (EQn(buffer,"ORDER",5))
143
bgn = atoi((char*)vs[1].c_str()) - 1;
144
if (bgn < 1 || bgn > mol.NumAtoms())
146
if (vs.size() > vord[bgn].size()+2)
148
for (i = 2;i < vs.size();i++)
149
vord[bgn][i-2] = atoi((char*)vs[i].c_str());
154
for (i = 1;i <= mol.NumAtoms();i++)
155
if (!vcon[i - 1].empty())
156
for (j = 0;j < vcon[i - 1].size();j++)
158
mol.AddBond(i,vcon[i - 1][j],vord[i - 1][j]);
161
//load up the next line after the END marker
162
ifs.getline(buffer,BUFF_SIZE);
168
////////////////////////////////////////////////////////////////
170
bool BGFFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
172
OBMol* pmol = dynamic_cast<OBMol*>(pOb);
176
//Define some references so we can use the old parameter names
177
ostream &ofs = *pConv->GetOutStream();
180
vector<OBNodeBase*>::iterator i;
183
char buffer[BUFF_SIZE];
184
char elmnt_typ[5], dreid_typ[5], atm_sym[10], max_val_str[5];
188
ofs << "BIOGRF 200" << endl;
189
sprintf(buffer,"DESCRP %s",mol.GetTitle());
190
ofs << buffer << endl;
191
sprintf(buffer,"REMARK BGF file created by Open Babel %s",BABEL_VERSION);
192
ofs << buffer << endl;
193
ofs << "FORCEFIELD DREIDING " << endl;
194
ofs << "FORMAT ATOM (a6,1x,i5,1x,a5,1x,a3,1x,a1,1x,a5,3f10.5,1x,a5,i3,i2,1x,f8.5)" << endl;
196
ttab.SetFromType("INT");
198
for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
200
strcpy(elmnt_typ,etab.GetSymbol(atom->GetAtomicNum()));
203
ttab.SetToType("DRE");
204
ttab.Translate(dreid_typ,atom->GetType());
205
ttab.SetToType("HAD");
206
ttab.Translate(max_val_str,atom->GetType());
207
max_val = atoi(max_val_str);
210
sprintf(atm_sym,"%s%d",elmnt_typ,atom->GetIdx());
211
sprintf(buffer,"%6s %5d %-5s %3s %1s %5s%10.5f%10.5f%10.5f %-5s%3d%2d %8.5f",
224
atom->GetPartialCharge());
225
ofs << buffer << endl;
227
sprintf(buffer,"FORMAT CONECT (a6,12i6)\n");
228
ofs << buffer << endl;
231
vector<OBEdgeBase*>::iterator j;
232
for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
233
if (atom->GetValence())
235
sprintf(buffer,"CONECT%6d",atom->GetIdx());
237
for (nbr = atom->BeginNbrAtom(j);nbr;nbr = atom->NextNbrAtom(j))
239
sprintf(buffer,"%6d",nbr->GetIdx());
244
sprintf(buffer,"ORDER %6d",atom->GetIdx());
246
for (nbr = atom->BeginNbrAtom(j);nbr;nbr = atom->NextNbrAtom(j))
248
sprintf(buffer,"%6d",(*j)->GetBO());
254
sprintf(buffer,"END");
255
ofs << buffer << endl;
259
} //namespace OpenBabel