File: canonconsistenttest.cpp

package info (click to toggle)
openbabel 2.4.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 58,308 kB
  • sloc: cpp: 459,210; ansic: 90,514; php: 13,963; python: 7,899; perl: 6,518; pascal: 793; sh: 179; xml: 97; ruby: 64; makefile: 46; java: 23; cs: 14
file content (106 lines) | stat: -rw-r--r-- 2,836 bytes parent folder | download | duplicates (5)
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
#include "obtest.h"
#include <openbabel/mol.h>
#include <openbabel/obconversion.h>

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
using namespace OpenBabel;

bool mdoMultiMoleculeFile(const std::string &filename)
{
  std::ifstream ifs;
  ifs.open(filename.c_str());
  OB_REQUIRE( ifs );

  OBMol mol;
  OBConversion conv(&ifs, &cout);
  OBFormat *format = conv.FormatFromExt(filename.c_str());
  OBFormat *canSMI = conv.FindFormat("can");
  OBFormat *smi    = conv.FindFormat("smi");
  OB_REQUIRE(format);
  OB_REQUIRE(canSMI);
  OB_REQUIRE(smi);

  OB_REQUIRE(conv.SetInAndOutFormats(format, canSMI)); 

  string output, roundtrip; // first output string, then the roundtrip
  OBMol round2; // result of reading first output as canonical SMILES
  OBConversion conv2; // duplicate to prevent having to constantly change formats
  OB_REQUIRE(conv2.SetInAndOutFormats(smi, canSMI));

  bool result = true;
  conv.SetInStream(&ifs);
  int testCount = 0;
  int failed = 0;

  while (1) {
    if (!conv.Read(&mol)) {
      // failed read, try again
      if (!conv.Read(&mol))
        break; // we tried twice, so break
    }
    testCount++;
    if (testCount % 1000 == 0)
      cout << testCount << " completed" << endl;

    mol.SetTitle("");
    output = conv.WriteString(&mol, true); // trim whitespace
    OB_REQUIRE(conv2.ReadString(&round2, output));
    round2.SetTitle("");
    roundtrip = conv2.WriteString(&round2, true);
    if (roundtrip != output) {
      failed++;
      result = false;
      if (strcasecmp(output.c_str(), roundtrip.c_str()) != 0)
        cout << "Failed roundtrip: \n  " << output << " -> \n  " << roundtrip << "\n";
      else
        cout << "Failed aromaticity: \n " << output << "\n";
    }
  }
  cout << "PASSED TESTS: " << testCount - failed << "/" << testCount << endl;
  return result;
}


int canonconsistenttest(int argc, char* argv[])
{
  int defaultchoice = 1;
  int choice = defaultchoice;

  if (argc > 1) {
    if(sscanf(argv[1], "%d", &choice) != 1) {
      printf("Couldn't parse that input as a number\n");
      return -1;
    }
  }

  // Define location of file formats for testing
#ifdef FORMATDIR
  char env[BUFF_SIZE];
  snprintf(env, BUFF_SIZE, "BABEL_LIBDIR=%s", FORMATDIR);
  putenv(env);
#endif

  switch(choice) {
    case 1:
      OB_ASSERT( mdoMultiMoleculeFile(OBTestUtil::GetFilename("forcefield.sdf")) );
      break;
    case 2:
      OB_ASSERT( mdoMultiMoleculeFile(OBTestUtil::GetFilename("filterset.sdf")) );
      break;
    case 3:
      OB_ASSERT( mdoMultiMoleculeFile(OBTestUtil::GetFilename("cantest.sdf")) );
      break;
    default:
      cout << "Test numer " << choice << " does not exist!\n";
      return -1;
  }
//  OB_ASSERT( doMultiMoleculeFile(GetFilename("cansmi-roundtrip.smi")) );

  return 0;
}