~ubuntu-branches/ubuntu/feisty/openbabel/feisty

« back to all changes in this revision

Viewing changes to src/formats/bgfformat.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2006-05-14 19:46:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060514194601-h3j1wovvc42cigxl
Tags: 2.0.1-1
* New upstream release. (Closes: #341628)
* debian/patches/04_zipstream_fix.diff: Removed, applied upstream.
* debian/rules (DEB_MAKE_CHECK_TARGET): Readded.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
 
 
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.
 
9
 
 
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
***********************************************************************/
 
15
 
 
16
#include "mol.h"
 
17
#include "obconversion.h"
 
18
#include "obmolecformat.h"
 
19
 
 
20
using namespace std;
 
21
namespace OpenBabel
 
22
{
 
23
 
 
24
class BGFFormat : public OBMoleculeFormat
 
25
{
 
26
public:
 
27
    //Register this format type ID
 
28
    BGFFormat()
 
29
    {
 
30
        OBConversion::RegisterFormat("bgf",this);
 
31
    }
 
32
 
 
33
    virtual const char* Description() //required
 
34
    {
 
35
        return
 
36
            "MSI BGF format\n \
 
37
            No comments yet\n";
 
38
    };
 
39
 
 
40
  virtual const char* SpecificationURL()
 
41
  {return "";}; //optional
 
42
 
 
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()
 
46
    {
 
47
        return READONEONLY | WRITEONEONLY;
 
48
    };
 
49
 
 
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);
 
55
};
 
56
//***
 
57
 
 
58
//Make an instance of the format class
 
59
BGFFormat theBGFFormat;
 
60
 
 
61
/////////////////////////////////////////////////////////////////
 
62
bool BGFFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
 
63
{
 
64
 
 
65
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
 
66
    if(pmol==NULL)
 
67
        return false;
 
68
 
 
69
    //Define some references so we can use the old parameter names
 
70
    istream &ifs = *pConv->GetInStream();
 
71
    OBMol &mol = *pmol;
 
72
    mol.SetTitle( pConv->GetTitle()); //default title is the filename
 
73
    mol.BeginModify();
 
74
 
 
75
    char buffer[BUFF_SIZE];
 
76
    char tmp[15],tmptyp[15];
 
77
 
 
78
    while (ifs.getline(buffer,BUFF_SIZE))
 
79
        if (EQn(buffer,"FORMAT",6))
 
80
            break;
 
81
 
 
82
    ttab.SetFromType("DRE");
 
83
    ttab.SetToType("INT");
 
84
    OBAtom *atom;
 
85
    double x,y,z,chrg;
 
86
    for (;;)
 
87
    {
 
88
        if (!ifs.getline(buffer,BUFF_SIZE))
 
89
            break;
 
90
        if (EQn(buffer,"FORMAT",6))
 
91
            break;
 
92
 
 
93
        sscanf(buffer,"%*s %*s %*s %*s %*s %*s %lf %lf %lf %s %*s %*s %lf",
 
94
               &x,&y,&z,
 
95
               tmptyp,
 
96
               &chrg);
 
97
        atom = mol.NewAtom();
 
98
 
 
99
        ttab.Translate(tmp,tmptyp);
 
100
        atom->SetType(tmp);
 
101
 
 
102
        CleanAtomType(tmptyp);
 
103
        atom->SetAtomicNum(etab.GetAtomicNum(tmptyp));
 
104
 
 
105
        atom->SetVector(x,y,z);
 
106
    }
 
107
    unsigned int i;
 
108
    vector<int> vtmp;
 
109
    vector<vector<int> > vcon;
 
110
    vector<vector<int> > vord;
 
111
 
 
112
    for (i = 0; i < mol.NumAtoms();i++)
 
113
    {
 
114
        vcon.push_back(vtmp);
 
115
        vord.push_back(vtmp);
 
116
    }
 
117
 
 
118
    unsigned int bgn;
 
119
    vector<string> vs;
 
120
    for (;;)
 
121
    {
 
122
        if (!ifs.getline(buffer,BUFF_SIZE) || EQn(buffer,"END",3))
 
123
            break;
 
124
 
 
125
        tokenize(vs,buffer);
 
126
        if (vs.empty() || vs.size() < 3 || vs.size() > 10)
 
127
            continue;
 
128
 
 
129
        if (EQn(buffer,"CONECT",6))
 
130
        {
 
131
            bgn = atoi((char*)vs[1].c_str()) - 1;
 
132
            if (bgn < 1 || bgn > mol.NumAtoms())
 
133
                continue;
 
134
            for (i = 2;i < vs.size();i++)
 
135
            {
 
136
                vcon[bgn].push_back(atoi((char*)vs[i].c_str()));
 
137
                vord[bgn].push_back(1);
 
138
            }
 
139
        }
 
140
        else
 
141
            if (EQn(buffer,"ORDER",5))
 
142
            {
 
143
                bgn = atoi((char*)vs[1].c_str()) - 1;
 
144
                if (bgn < 1 || bgn > mol.NumAtoms())
 
145
                    continue;
 
146
                if (vs.size() > vord[bgn].size()+2)
 
147
                    continue;
 
148
                for (i = 2;i < vs.size();i++)
 
149
                    vord[bgn][i-2] = atoi((char*)vs[i].c_str());
 
150
            }
 
151
    }
 
152
 
 
153
    unsigned int j;
 
154
    for (i = 1;i <= mol.NumAtoms();i++)
 
155
        if (!vcon[i - 1].empty())
 
156
            for (j = 0;j < vcon[i - 1].size();j++)
 
157
            {
 
158
                mol.AddBond(i,vcon[i - 1][j],vord[i - 1][j]);
 
159
            }
 
160
 
 
161
    //load up the next line after the END marker
 
162
    ifs.getline(buffer,BUFF_SIZE);
 
163
 
 
164
    mol.EndModify();
 
165
    return(true);
 
166
}
 
167
 
 
168
////////////////////////////////////////////////////////////////
 
169
 
 
170
bool BGFFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
 
171
{
 
172
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
 
173
    if(pmol==NULL)
 
174
        return false;
 
175
 
 
176
    //Define some references so we can use the old parameter names
 
177
    ostream &ofs = *pConv->GetOutStream();
 
178
    OBMol &mol = *pmol;
 
179
 
 
180
    vector<OBNodeBase*>::iterator i;
 
181
    int max_val;
 
182
    OBAtom *atom;
 
183
    char buffer[BUFF_SIZE];
 
184
    char elmnt_typ[5], dreid_typ[5], atm_sym[10], max_val_str[5];
 
185
 
 
186
    mol.Kekulize();
 
187
 
 
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;
 
195
 
 
196
    ttab.SetFromType("INT");
 
197
 
 
198
    for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
 
199
    {
 
200
        strcpy(elmnt_typ,etab.GetSymbol(atom->GetAtomicNum()));
 
201
        ToUpper(elmnt_typ);
 
202
 
 
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);
 
208
        if (max_val == 0)
 
209
            max_val = 1;
 
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",
 
212
                "HETATM",
 
213
                atom->GetIdx(),
 
214
                atm_sym,
 
215
                "RES",
 
216
                "A",
 
217
                "444",
 
218
                atom->GetX(),
 
219
                atom->GetY(),
 
220
                atom->GetZ(),
 
221
                dreid_typ,
 
222
                max_val,
 
223
                0,
 
224
                atom->GetPartialCharge());
 
225
        ofs << buffer << endl;
 
226
    }
 
227
    sprintf(buffer,"FORMAT CONECT (a6,12i6)\n");
 
228
    ofs << buffer << endl;
 
229
 
 
230
    OBAtom *nbr;
 
231
    vector<OBEdgeBase*>::iterator j;
 
232
    for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
 
233
        if (atom->GetValence())
 
234
        {
 
235
            sprintf(buffer,"CONECT%6d",atom->GetIdx());
 
236
            ofs << buffer;
 
237
            for (nbr = atom->BeginNbrAtom(j);nbr;nbr = atom->NextNbrAtom(j))
 
238
            {
 
239
                sprintf(buffer,"%6d",nbr->GetIdx());
 
240
                ofs << buffer;
 
241
            }
 
242
            ofs << endl;
 
243
 
 
244
            sprintf(buffer,"ORDER %6d",atom->GetIdx());
 
245
            ofs << buffer;
 
246
            for (nbr = atom->BeginNbrAtom(j);nbr;nbr = atom->NextNbrAtom(j))
 
247
            {
 
248
                sprintf(buffer,"%6d",(*j)->GetBO());
 
249
                ofs << buffer;
 
250
            }
 
251
            ofs << endl;
 
252
        }
 
253
 
 
254
    sprintf(buffer,"END");
 
255
    ofs << buffer << endl;
 
256
    return(true);
 
257
}
 
258
 
 
259
} //namespace OpenBabel