~ubuntu-branches/ubuntu/hardy/openbabel/hardy

« back to all changes in this revision

Viewing changes to test/ringtest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-08-06 17:28:40 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070806172840-22hoqb3ve230qav1
Tags: 2.1.1-0ubuntu1
* New upstream release
* New so version 2

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
For more information, see <http://openbabel.sourceforge.net/>
6
6
 
7
7
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
8
 
Some portions Copyright (C) 2001-2006 Geoffrey R. Hutchison
 
8
Some portions Copyright (C) 2001-2005 Geoffrey R. Hutchison
9
9
 
10
10
This program is free software; you can redistribute it and/or modify
11
11
it under the terms of the GNU General Public License as published by
17
17
GNU General Public License for more details.
18
18
***********************************************************************/
19
19
 
20
 
#include <fstream>
21
 
 
22
20
// used to set import/export for Cygwin DLLs
23
21
#ifdef WIN32
24
22
#define USING_OBDLL
25
23
#endif
26
24
 
27
 
#include "babelconfig.h"
28
 
#include "mol.h"
29
 
#include "obconversion.h"
 
25
#include <openbabel/babelconfig.h>
 
26
 
 
27
#include <fstream>
 
28
 
 
29
#include <openbabel/mol.h>
 
30
#include <openbabel/obconversion.h>
30
31
 
31
32
namespace OpenBabel
32
33
{
33
 
bool SafeOpen(std::ifstream &fs, char *filename);
34
 
bool SafeOpen(std::ofstream &fs, char *filename);
 
34
  bool SafeOpen(std::ifstream &fs, const char *filename);
 
35
  bool SafeOpen(std::ofstream &fs, const char *filename);
35
36
}
36
37
 
37
38
using namespace std;
39
40
 
40
41
void GenerateRingReference();
41
42
 
42
 
int main(int argc,char *argv[])
43
 
{
44
 
    if (argc != 1)
45
 
    {
46
 
        if (strncmp(argv[1], "-g", 2))
47
 
        {
48
 
            cout << "Usage: ringtest" << endl;
49
 
            cout << "   Tests Open Babel ring perception testing." << endl;
50
 
            return 0;
51
 
        }
52
 
        else
53
 
        {
54
 
            GenerateRingReference();
55
 
            return 0;
56
 
        }
57
 
    }
58
 
 
59
 
    cout << endl << "Testing RINGS..." << endl;
60
 
 
61
43
#ifdef TESTDATADIR
62
 
 
63
 
    string testdatadir = TESTDATADIR;
64
 
    string results_file = testdatadir + "ringresults.txt";
65
 
    string smilestypes_file = testdatadir + "attype.00.smi";
 
44
  string testdatadir = TESTDATADIR;
 
45
  string results_file = testdatadir + "ringresults.txt";
 
46
  string smilestypes_file = testdatadir + "attype.00.smi";
66
47
#else
67
 
 
68
 
    string results_file = "ringresults.txt";
69
 
    string smilestypes_file = "attype.00.smi";
 
48
  string results_file = "files/ringresults.txt";
 
49
  string smilestypes_file = "files/attype.00.smi";
70
50
#endif
71
51
 
72
 
    std::ifstream mifs;
73
 
    if (!SafeOpen(mifs, (char*)smilestypes_file.c_str()))
74
 
    {
75
 
        return -1; // test failed
76
 
    }
77
 
 
78
 
    std::ifstream rifs;
79
 
    if (!SafeOpen(rifs, (char*)results_file.c_str()))
80
 
    {
81
 
        return -1; // test failed
82
 
    }
83
 
 
84
 
    unsigned int size;
85
 
    OBBond *bond;
86
 
    OBAtom *atom;
87
 
    int count;
88
 
    char buffer[BUFF_SIZE];
89
 
    vector<string> vs;
90
 
    vector<OBRing*> vr;
91
 
    vector<bool> vb;
92
 
    vector<int> vi;
93
 
    OBMol mol;
94
 
    vector<string>::iterator i;
95
 
    vector<OBEdgeBase*>::iterator j;
96
 
    vector<OBNodeBase*>::iterator k;
97
 
    vector<OBRing*>::iterator m;
98
 
    OBConversion conv(&mifs, &cout);
99
 
 
100
 
    if(! conv.SetInAndOutFormats("SMI","SMI"))
101
 
    {
102
 
        ThrowError("SMILES format is not loaded");
103
 
        return -1;
104
 
    }
105
 
 
106
 
    for (;mifs;)
107
 
    {
108
 
        mol.Clear();
109
 
        conv.Read(&mol);
110
 
        if (mol.Empty())
111
 
            continue;
112
 
        if (!rifs.getline(buffer,BUFF_SIZE))
113
 
        {
114
 
            ThrowError("error reading reference data");
115
 
            return -1; // test failed
116
 
        }
117
 
 
118
 
        vb.clear();
119
 
        vb.resize(mol.NumBonds(),false);
120
 
        //check ring bonds
121
 
        tokenize(vs,buffer);
122
 
        for (i = vs.begin();i != vs.end();i++)
123
 
            vb[atoi((char*)i->c_str())] = true;
124
 
 
125
 
        for (bond = mol.BeginBond(j);bond;bond = mol.NextBond(j))
126
 
            if (vb[bond->GetIdx()] != bond->IsInRing())
127
 
            {
128
 
                ThrowError("ring bond data different than reference");
129
 
                ThrowError((char*)mol.GetTitle());
130
 
                return -1; // test failed
131
 
            }
132
 
 
133
 
        vr = mol.GetSSSR();
134
 
        if (!rifs.getline(buffer,BUFF_SIZE))
135
 
        {
136
 
            ThrowError("error reading reference data");
137
 
            return -1; // test failed
138
 
        }
139
 
        sscanf(buffer,"%d",&size);
140
 
        if (vr.size() != size) //check SSSR size
141
 
        {
142
 
            ThrowError("SSSR size different than reference");
143
 
            ThrowError((char*)mol.GetTitle());
144
 
            return -1; // test failed
145
 
        }
146
 
 
147
 
        if (!rifs.getline(buffer,BUFF_SIZE))
148
 
        {
149
 
            ThrowError("error reading reference data");
150
 
            return -1; // test failed
151
 
        }
152
 
 
153
 
        tokenize(vs,buffer);
154
 
        i = vs.begin();
155
 
        for (atom = mol.BeginAtom(k);atom;atom = mol.NextAtom(k))
156
 
        {
157
 
            if (i == vs.end())
158
 
            {
159
 
                ThrowError("Error in SSSR count");
160
 
                ThrowError((char*)mol.GetTitle());
161
 
                return -1; // test failed
162
 
            }
163
 
            count = 0;
164
 
            for (m = vr.begin();m != vr.end();m++)
165
 
                if ((*m)->_pathset[atom->GetIdx()])
166
 
                    count++;
167
 
 
168
 
            if (atoi((char*)i->c_str()) != count)
169
 
            {
170
 
                ThrowError("Ring membership test failed");
171
 
                ThrowError((char*)mol.GetTitle());
172
 
                return -1; // test failed
173
 
            }
174
 
 
175
 
            i++;
176
 
        }
177
 
 
178
 
 
179
 
    }
180
 
 
181
 
    // Passed tests
182
 
    return 0;
 
52
int main(int argc,char *argv[])
 
53
{
 
54
  // turn off slow sync with C-style output (we don't use it anyway).
 
55
  std::ios::sync_with_stdio(false);
 
56
 
 
57
  if (argc != 1)
 
58
    {
 
59
      if (strncmp(argv[1], "-g", 2))
 
60
        {
 
61
          cout << "Usage: ringtest" << endl;
 
62
          cout << "   Tests Open Babel ring perception testing." << endl;
 
63
          return 0;
 
64
        }
 
65
      else
 
66
        {
 
67
          GenerateRingReference();
 
68
          return 0;
 
69
        }
 
70
    }
 
71
 
 
72
  cout << "# Testing ring perception..." << endl;
 
73
 
 
74
  std::ifstream mifs;
 
75
  if (!SafeOpen(mifs, smilestypes_file.c_str()))
 
76
    {
 
77
      cout << "Bail out! Cannot read file " << smilestypes_file << endl;
 
78
      return -1; // test failed
 
79
    }
 
80
 
 
81
  std::ifstream rifs;
 
82
  if (!SafeOpen(rifs, results_file.c_str()))
 
83
    {
 
84
      cout << "Bail out! Cannot read file " << results_file << endl;
 
85
      return -1; // test failed
 
86
    }
 
87
 
 
88
  unsigned int size;
 
89
  OBBond *bond;
 
90
  OBAtom *atom;
 
91
  int count;
 
92
  char buffer[BUFF_SIZE];
 
93
  vector<string> vs;
 
94
  vector<OBRing*> vr;
 
95
  vector<bool> vb;
 
96
  vector<int> vi;
 
97
  OBMol mol;
 
98
  vector<string>::iterator i;
 
99
  vector<OBEdgeBase*>::iterator j;
 
100
  vector<OBNodeBase*>::iterator k;
 
101
  vector<OBRing*>::iterator m;
 
102
  OBConversion conv(&mifs, &cout);
 
103
  unsigned int currentTest = 0;
 
104
 
 
105
  if(! conv.SetInAndOutFormats("SMI","SMI"))
 
106
    {
 
107
      cout << "Bail out! SMILES format is not loaded" << endl;
 
108
      return -1;
 
109
    }
 
110
 
 
111
  for (;mifs;)
 
112
    {
 
113
      mol.Clear();
 
114
      conv.Read(&mol);
 
115
      if (mol.Empty())
 
116
        continue;
 
117
      if (!rifs.getline(buffer,BUFF_SIZE))
 
118
        {
 
119
          cout << "Bail out! error reading reference data" << endl;
 
120
          return -1; // test failed
 
121
        }
 
122
 
 
123
      vb.clear();
 
124
      vb.resize(mol.NumBonds(),false);
 
125
      //check ring bonds
 
126
      tokenize(vs,buffer);
 
127
      for (i = vs.begin();i != vs.end();i++)
 
128
        vb[atoi(i->c_str())] = true;
 
129
 
 
130
      for (bond = mol.BeginBond(j);bond;bond = mol.NextBond(j))
 
131
        {
 
132
          if (vb[bond->GetIdx()] != bond->IsInRing())
 
133
            {
 
134
              cout << "not ok " << ++currentTest
 
135
                   << " # ring bond data different than reference\n";
 
136
              cout << "# Molecule: " << mol.GetTitle() << "\n";
 
137
            }
 
138
          else
 
139
            cout << "ok " << ++currentTest
 
140
                 << " # correct ring bond data\n";
 
141
        }
 
142
 
 
143
      vr = mol.GetSSSR();
 
144
      if (!rifs.getline(buffer,BUFF_SIZE))
 
145
        {
 
146
          cout << "Bail out! error reading reference data\n";
 
147
          return -1; // test failed
 
148
        }
 
149
      sscanf(buffer,"%d",&size);
 
150
      if (vr.size() != size) //check SSSR size
 
151
        {
 
152
          cout << "not ok " << ++currentTest 
 
153
               << " # SSSR size different than reference\n";
 
154
          cout << "# Molecule: " << mol.GetTitle() << "\n";
 
155
        }
 
156
      else
 
157
        cout << "ok " << ++currentTest
 
158
             << " # SSSR size matches reference\n";
 
159
 
 
160
      if (!rifs.getline(buffer,BUFF_SIZE))
 
161
        {
 
162
          cout << "Bail out! error reading reference data" << endl;
 
163
          return -1; // test failed
 
164
        }
 
165
 
 
166
      tokenize(vs,buffer);
 
167
      i = vs.begin();
 
168
      for (atom = mol.BeginAtom(k);atom;atom = mol.NextAtom(k))
 
169
        {
 
170
          if (i == vs.end())
 
171
            {
 
172
              cout << "not ok " << ++currentTest << " # error in SSSR count\n";
 
173
              cout << "# Molecule: " << mol.GetTitle() << "\n";
 
174
            }
 
175
          else
 
176
            cout << "ok " << ++currentTest << " # correct SSSR count\n";
 
177
 
 
178
          count = 0;
 
179
          for (m = vr.begin();m != vr.end();m++)
 
180
            if ((*m)->_pathset[atom->GetIdx()])
 
181
              count++;
 
182
 
 
183
          if (atoi(i->c_str()) != count)
 
184
            {
 
185
              cout << "not ok " << ++currentTest << "# ring membership test failed\n";
 
186
              cout << "# Molecule: " << mol.GetTitle() << "\n";
 
187
            }
 
188
          else
 
189
            cout << "ok " << ++currentTest << " # ring membership passed\n";
 
190
 
 
191
          i++;
 
192
        }
 
193
 
 
194
 
 
195
    }
 
196
 
 
197
  // return number of tests run
 
198
  cout << "1.." << currentTest << endl;
 
199
 
 
200
  // Passed tests
 
201
  return 0;
183
202
}
184
203
 
185
204
void GenerateRingReference()
186
205
{
187
 
    std::ifstream ifs;
188
 
    if (!SafeOpen(ifs,"attype.00.smi"))
189
 
        return;
190
 
 
191
 
    std::ofstream ofs;
192
 
    if (!SafeOpen(ofs,"ringresults.txt"))
193
 
        return;
194
 
 
195
 
    int count;
196
 
    OBAtom *atom;
197
 
    OBBond *bond;
198
 
    char buffer[BUFF_SIZE];
199
 
    vector<OBRing*> vr;
200
 
    vector<OBEdgeBase*>::iterator i;
201
 
    vector<OBNodeBase*>::iterator j;
202
 
    vector<OBRing*>::iterator k;
203
 
    OBMol mol;
204
 
    OBConversion conv(&ifs, &cout);
205
 
 
206
 
    if(! conv.SetInAndOutFormats("SMI","SMI"))
 
206
  std::ifstream ifs;
 
207
 
 
208
  if (!SafeOpen(ifs,smilestypes_file.c_str()))
 
209
    return;
 
210
 
 
211
  std::ofstream ofs;
 
212
  if (!SafeOpen(ofs,results_file.c_str()))
 
213
    return;
 
214
 
 
215
  int count;
 
216
  OBAtom *atom;
 
217
  OBBond *bond;
 
218
  char buffer[BUFF_SIZE];
 
219
  vector<OBRing*> vr;
 
220
  vector<OBEdgeBase*>::iterator i;
 
221
  vector<OBNodeBase*>::iterator j;
 
222
  vector<OBRing*>::iterator k;
 
223
  OBMol mol;
 
224
  OBConversion conv(&ifs, &cout);
 
225
 
 
226
  if(! conv.SetInAndOutFormats("SMI","SMI"))
207
227
    {
208
 
        ThrowError("SMILES format is not loaded");
209
 
        return;
 
228
      cerr << " SMILES format is not loaded" << endl;
 
229
      return;
210
230
    }
211
231
 
212
 
    for (;ifs;)
 
232
  for (;ifs;)
213
233
    {
214
 
        mol.Clear();
215
 
        conv.Read(&mol);
216
 
        if (mol.Empty())
217
 
            continue;
218
 
 
219
 
        //write out ring bonds
220
 
        for (bond = mol.BeginBond(i);bond;bond = mol.NextBond(i))
221
 
            if (bond->IsInRing())
222
 
            {
223
 
                sprintf(buffer,"%3d",bond->GetIdx());
224
 
                ofs << buffer;
225
 
            }
226
 
        ofs << endl;
227
 
 
228
 
        vr = mol.GetSSSR();
229
 
        //write the total number of rings
230
 
        ofs << vr.size() << endl;
231
 
 
232
 
        //write the number of rings that each atom is a member of
233
 
        for (atom = mol.BeginAtom(j);atom;atom = mol.NextAtom(j))
234
 
        {
235
 
            count = 0;
236
 
            for (k = vr.begin();k != vr.end();k++)
237
 
                if ((*k)->_pathset[atom->GetIdx()])
238
 
                    count++;
239
 
 
240
 
            sprintf(buffer,"%3d",count);
 
234
      mol.Clear();
 
235
      conv.Read(&mol);
 
236
      if (mol.Empty())
 
237
        continue;
 
238
 
 
239
      //write out ring bonds
 
240
      for (bond = mol.BeginBond(i);bond;bond = mol.NextBond(i))
 
241
        if (bond->IsInRing())
 
242
          {
 
243
            sprintf(buffer,"%3d",bond->GetIdx());
241
244
            ofs << buffer;
 
245
          }
 
246
      ofs << endl;
 
247
 
 
248
      vr = mol.GetSSSR();
 
249
      //write the total number of rings
 
250
      ofs << vr.size() << endl;
 
251
 
 
252
      //write the number of rings that each atom is a member of
 
253
      for (atom = mol.BeginAtom(j);atom;atom = mol.NextAtom(j))
 
254
        {
 
255
          count = 0;
 
256
          for (k = vr.begin();k != vr.end();k++)
 
257
            if ((*k)->_pathset[atom->GetIdx()])
 
258
              count++;
 
259
 
 
260
          sprintf(buffer,"%3d",count);
 
261
          ofs << buffer;
242
262
        }
243
 
        ofs << endl;
 
263
      ofs << endl;
244
264
 
245
265
    }
246
266
 
247
 
    ThrowError("Ring perception test results written successfully");
 
267
  cerr << " Ring perception test results written successfully" << endl;
 
268
  return;
248
269
}
249
270