1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/**********************************************************************
smartstest.cpp - Test SMARTS algorithms and atom typing.
This file is part of the Open Babel project.
For more information, see <http://openbabel.sourceforge.net/>
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
Some portions Copyright (C) 2001-2005 Geoffrey R. Hutchison
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
// used to set import/export for Cygwin DLLs
#ifdef WIN32
#define USING_OBDLL
#endif
#include <openbabel/babelconfig.h>
#include <fstream>
#include <openbabel/mol.h>
#include <openbabel/obconversion.h>
namespace OpenBabel
{
bool SafeOpen(std::ifstream &fs, const char *filename);
bool SafeOpen(std::ofstream &fs, const char *filename);
}
using namespace std;
using namespace OpenBabel;
void GenerateSmartsReference();
#ifdef TESTDATADIR
string testdatadir = TESTDATADIR;
string smarts_file = testdatadir + "smartstest.txt";
string results_file = testdatadir + "smartsresults.txt";
string smilestypes_file = testdatadir + "attype.00.smi";
#else
string smarts_file = "files/smartstest.txt";
string results_file = "files/smartsresults.txt";
string smilestypes_file = "files/attype.00.smi";
#endif
int main(int argc,char *argv[])
{
// turn off slow sync with C-style output (we don't use it anyway).
std::ios::sync_with_stdio(false);
if (argc != 1)
{
if (strncmp(argv[1], "-g", 2))
{
cout << "Usage: smartstest\n";
cout << " Tests Open Babel SMILES/SMARTS pattern matching." << endl;
return 0;
}
else
{
GenerateSmartsReference();
return 0;
}
}
cout << endl << "# Testing SMARTS... \n";
std::ifstream ifs;
if (!SafeOpen(ifs, smarts_file.c_str()))
{
cout << "Bail out! Cannot read " << smarts_file << endl;
return -1; // test failed
}
//read in the SMARTS test patterns
char buffer[BUFF_SIZE];
vector<OBSmartsPattern*> vsp;
for (;ifs.getline(buffer,BUFF_SIZE);)
{
if (buffer[0] == '#') // skip comment line
continue;
OBSmartsPattern *sp = new OBSmartsPattern;
if (sp->Init(buffer))
vsp.push_back(sp);
else
delete sp;
}
ifs.close();
std::ifstream rifs;
if (!SafeOpen(rifs, results_file.c_str()))
{
cout << "Bail out! Cannot read in results file " << results_file << endl;
return -1; // test failed
}
unsigned int npats;
rifs.getline(buffer,BUFF_SIZE);
sscanf(buffer,"%d %*s",&npats);
//make sure the number of SMARTS patterns is the same as in the
//reference data
if (npats != vsp.size())
{
cout << "Bail out! Correct number of patterns not read in" <<
"Read in " << vsp.size() << " expected " << npats << endl;
return -1; // test failed
}
std::ifstream mifs;
if (!SafeOpen(mifs, smilestypes_file.c_str()))
{
cout << "Bail out! Cannot read atom types " << smilestypes_file << endl;
return -1; // test failed
}
unsigned int k;
unsigned int res_line = 0;
OBMol mol;
vector<string> vs;
vector<OBSmartsPattern*>::iterator i;
vector<vector<int> > mlist;
unsigned int currentMol = 0; // each molecule is a separate test
bool molPassed = true;
OBConversion conv(&mifs, &cout);
if (! conv.SetInAndOutFormats("SMI","SMI"))
{
cout << "Bail out! SMILES format is not loaded" << endl;
return -1;
}
//read in molecules, match SMARTS, and compare results to reference data
for (;mifs;)
{
mol.Clear();
conv.Read(&mol);
if (mol.Empty())
continue;
currentMol++;
molPassed = true;
for (i = vsp.begin();i != vsp.end();i++)
{
if (!rifs.getline(buffer,BUFF_SIZE))
{
cout << "Bail out! Error reading reference data" << endl;
return -1; // test failed
}
res_line++;
tokenize(vs,buffer);
(*i)->Match(mol);
mlist = (*i)->GetMapList();
if (mlist.size() != vs.size())
{
cout << "not ok " << currentMol
<< " # number of matches different than reference\n";
cout << "# Expected " << vs.size() << " matches, found "
<< mlist.size() << "\n";
cout << "# Error with molecule " << mol.GetTitle();
cout << "# on pattern " << (*i)->GetSMARTS() << "\n";
if (mlist.size())
cout << "# First match: atom #" << mlist[0][0] << "\n";
molPassed = false;
break;
}
if (mlist.size())
{
for (k = 0;k < vs.size();k++)
{
if (atoi(vs[k].c_str()) != mlist[k][0])
{
cout << "not ok " << currentMol
<< "# matching atom numbers different than reference\n";
cout << "# Expected " << vs[k] << " but found "
<< mlist[k][0] << "\n";
cout << "# Molecule: " << mol.GetTitle() << "\n";
cout << "# Pattern: " << (*i)->GetSMARTS() << "\n";
molPassed = false;
break;
}
}
if (k != vs.size())
{
molPassed = false;
break;
}
}
}
if (molPassed)
cout << "ok " << currentMol << " # molecule passed tests\n";
}
// output the number of tests run
cout << "1.." << currentMol << endl;
// Passed Test
return 0;
}
void GenerateSmartsReference()
{
std::ifstream ifs;
if (!SafeOpen(ifs,smarts_file.c_str()))
return;
char buffer[BUFF_SIZE];
vector<OBSmartsPattern*> vsp;
for (;ifs.getline(buffer,BUFF_SIZE);)
{
if (buffer[0] == '#') // skip comment line
continue;
OBSmartsPattern *sp = new OBSmartsPattern;
if (sp->Init(buffer))
vsp.push_back(sp);
else
delete sp;
}
std::ofstream ofs;
if (!SafeOpen(ofs, results_file.c_str()))
return;
ofs << vsp.size() << " patterns" << endl;
std::ifstream mifs;
if (!SafeOpen(mifs, smilestypes_file.c_str()))
return;
vector<int> vm;
vector<vector<int> > mlist;
vector<vector<int> >::iterator j;
vector<OBSmartsPattern*>::iterator i;
OBMol mol;
OBConversion conv(&mifs, &cout);
if(! conv.SetInAndOutFormats("SMI","SMI"))
{
cerr << "SMILES format is not loaded" << endl;
return;
}
for (;mifs;)
{
mol.Clear();
conv.Read(&mol);
if (mol.Empty())
continue;
for (i = vsp.begin();i != vsp.end();i++)
{
(*i)->Match(mol);
mlist = (*i)->GetMapList();
for (j = mlist.begin();j != mlist.end();j++)
{
sprintf(buffer,"%3d",*(j->begin()));
ofs << buffer;
}
ofs << endl;
}
}
cerr << " SMARTS test results written successfully" << endl;
return;
}
|