File: inchiwrite.cpp

package info (click to toggle)
openbabel 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 37,188 kB
  • ctags: 47,193
  • sloc: cpp: 237,858; ansic: 85,555; cs: 22,219; java: 14,377; sh: 9,876; perl: 5,432; python: 4,319; pascal: 793; makefile: 683; xml: 97; ruby: 54
file content (123 lines) | stat: -rw-r--r-- 3,607 bytes parent folder | download
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
/**********************************************************************
 inchiwrite.cpp - tests for writing InChIs

 This file is part of the Open Babel project.
 For more information, see <http://openbabel.sourceforge.net/>

 Copyright (C) 2007 by Chris Morley

 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 <iostream>
#include <fstream>
#include <sstream>
#include <openbabel/babelconfig.h>
#include <openbabel/mol.h>
#include <openbabel/obconversion.h>

using namespace std;
namespace OpenBabel{
extern string GetInChI(istream& is);
}
using namespace OpenBabel;

int main(int argc, char* argv[])
{
  if(argc !=3)
  {
    cout << "Usage:  inchiwrite Testmols.xxx Results.txt\n"
    "Testmols.xxx can be any format but must have the appropriate extension.\n"
    "Results.txt is a text file with the correct the correct InChIs.\n"
    "It can contain other text interspersed." << endl;
    return 1;
  }
  ifstream ifs(argv[1]);
  if(!ifs) { 
    cout << "# Cannot open " << argv[1];
    return 1;
  }
  ifstream results(argv[2]);
  if(!results) {
    cout << "# Cannot open " << argv[2];
    return 1;
  }

  stringstream ssout;
  OBConversion conv(&ifs, &ssout);
  OBFormat* pFormat = conv.FormatFromExt(argv[1]);
  if(!pFormat)
  {
    cout << "# Skipped. Did not recognize the format of " << argv[1] << endl;
    return 1;
  }
  conv.SetInFormat(pFormat);
  if(!conv.SetOutFormat("inchi"))
  {  
    cout << "# Skipped. Did not find InChI Format" << endl;
    return 1;
  }

  conv.AddOption("w",OBConversion::OUTOPTIONS); //suppress routine warnings

  /*In OB version 2.2.0 a set of 'recommended' InChI options is used by default.
    The compiled Windows program wInChI.exe, which is used to generate the results file
    was not available for InChI version 1.02, the beta of which is used in OB.
    So the following option turns off all the InChI options. This needs to be revisted
    when InChI 1.02 is released.
  */
  conv.AddOption("n",OBConversion::OUTOPTIONS);

  int count = conv.Convert();
  if(count<=0) {
    cout << "# Skipped. Did not convert any molecules" << endl;
    return 2;
  }

  cout << "1.." << count << " # molecules converted\n";

  //Compare ssout with the correct InChIs
  int nfail=0;
  int n, skipped=0;
  for(n=1;ssout;++n)
  {
    string correct = GetInChI(results);
    string generated;
    ssout >> generated;
    if(correct.empty() || generated.empty())
      break;
    if(correct.size()<9)//skip molecules with an empty InChI
    {
      skipped++;
      cout << "ok " << n << " # ignored empty InChI\n";
      continue;
    }

    if(generated != correct)
    {
      cout << "Not ok " << n << " # Mismatch in molecule #" 
           << n << ". generated / correct\n"
           << "# " << generated << "\n# " << correct << '\n';
      nfail++;
    }
    else
      cout << "ok " << n << " # InChI matched expected\n";
  }
  cout << n-1-skipped<< " molecules compared" << endl;
  cout << "# " << skipped << " # empty molecules skipped" << endl;
  cout << nfail << " failures" << endl;
  return 0;
}