File: detect.cpp

package info (click to toggle)
osra 2.1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 3,468 kB
  • sloc: cpp: 52,893; sh: 3,085; perl: 390; makefile: 328; python: 279; xml: 169; java: 160
file content (90 lines) | stat: -rw-r--r-- 2,299 bytes parent folder | download | duplicates (2)
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
// g++ -I/usr/local/include/openbabel-2.0/ -static  detect.cpp -o recall -L/usr/local/lib -lopenbabel -lz -linchi
#include <iostream>
#include <string>
#include <set>
#include <dirent.h>
#include <openbabel/obconversion.h>
#include <openbabel/mol.h>

void collect_inchi(std::set<std::string> &inchi1, const  std::string &name1)
{
  OpenBabel::OBConversion obconversion;
  obconversion.SetInFormat("sdf");
  obconversion.SetOutFormat("inchi");
  obconversion.SetOptions("K", obconversion.OUTOPTIONS);
  OpenBabel::OBMol mol;
  bool notatend = obconversion.ReadFile(&mol, name1);
  while (notatend)
    {
      std::string inchi = obconversion.WriteString(&mol);
      if (!inchi.empty())
	inchi1.insert(inchi);
      mol.Clear();
      notatend = obconversion.Read(&mol);
    }
}

void print_errors(const std::set<std::string> &inchi1, const  std::string &name2)
{
  OpenBabel::OBConversion obconversion;
  obconversion.SetInFormat("sdf");
  obconversion.SetOutFormat("inchi");
  obconversion.SetOptions("K", obconversion.OUTOPTIONS);
  OpenBabel::OBMol mol;
  bool notatend = obconversion.ReadFile(&mol, name2);
  int i = 0;
  while (notatend)
    {
      std::string inchi = obconversion.WriteString(&mol);
      if (inchi.empty() || inchi1.find(inchi) == inchi1.end())
	{
	  std::cout << name2 << " " << i << std::endl;
	}
      mol.Clear();
      notatend = obconversion.Read(&mol);
      i++;
    }
}



int main(int argc,char **argv)
{

  if(argc<3)
    {
      std::cerr << "Usage: " << argv[0] <<" ground_truth/ computed/" << std::endl;
      return 1;
    }
  
  OpenBabel::obErrorLog.StopLogging();
 
  std::string folder1(argv[1]);
  std::string folder2(argv[2]);
  DIR *dir;
  struct dirent *ent;
  size_t total = 0, identical = 0, computed = 0;
  if ((dir = opendir (folder1.c_str())) != NULL) 
    {
      while ((ent = readdir (dir)) != NULL) 
	{
	  std::string name1 = folder1 + ent->d_name;
	  std::string name2 = folder2 + ent->d_name;
	  if (name1.size() > 4 && name1.substr(name1.size()-4) == ".sdf")
	    {
	      std::set<std::string> inchi1;
	      collect_inchi(inchi1,name1);
	      print_errors(inchi1,name2);
	    }
	}
      closedir (dir);
    } 
  else 
    {
      std::cerr << "Unable to open directory " << argv[1] << std::endl;
      return 1;
    }


  return(0);
}