File: aliastest.cpp

package info (click to toggle)
openbabel 3.1.1%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 259,620 kB
  • sloc: cpp: 361,957; python: 11,640; ansic: 6,470; perl: 6,010; pascal: 793; php: 529; sh: 226; xml: 97; ruby: 64; makefile: 45; java: 23
file content (116 lines) | stat: -rw-r--r-- 3,537 bytes parent folder | download | duplicates (4)
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
/**********************************************************************
aliastest.cpp - unit testing code for alias.h and alias.cpp
Copyright (C) 2019 by Alex Ustinov

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.
***********************************************************************/
#include "obtest.h"

#include <openbabel/mol.h>
#include <openbabel/obconversion.h>
#include <openbabel/atom.h>
#include <openbabel/obiter.h>
#include <openbabel/alias.h>

using namespace std;
using namespace OpenBabel;

// a class for a test case should contain smiles of the molecule, number of aliases that
// are expected to be produced, and number of unaliasable atoms that are not parts of aliases
// note this test uses standard superatom.txt file included in OpenBabel

class AliasTestExample {
  string _smiles;
  unsigned int _num_aliases;
  unsigned int _num_nonaliased;
public:
  AliasTestExample(const string smiles, const unsigned int num_aliases, const unsigned int num_nonaliased):
    _smiles(smiles), _num_aliases(num_aliases), _num_nonaliased(num_nonaliased) {};

  // this will create aliases in the molecule, then remove all atoms present in aliases,
  // count aliases and atoms that are not present in aliases, and compare with the expected result
  void test() {
    OBMol mol;
    AliasData ad;
    OBConversion conv;
    OB_REQUIRE( conv.SetInFormat("smi") );
    OB_REQUIRE( conv.ReadString(&mol, _smiles) );
    ad.AddAliases(&mol);
    AliasData::RevertToAliasForm(mol);
    unsigned int alias_count = 0;
    unsigned int nonaliased_count = 0;

    FOR_ATOMS_OF_MOL(a, mol)
    {
      AliasData* ad = nullptr;
      if ( static_cast<AliasData*>(a->GetData(AliasDataType)) )
        alias_count++;
      else
        nonaliased_count++;
    }
    //cout << "Testing smiles " << _smiles << endl;
    //cout << "number of aliases " << alias_count << ", number of nonaliased atoms " << nonaliased_count << endl;

    OB_ASSERT( nonaliased_count == _num_nonaliased );
    OB_ASSERT( alias_count == _num_aliases );
  }
};

void testAliases()
{
  AliasTestExample test_set[] = {
    // methyl tert-butyl ether -> MeOtBu
    AliasTestExample("COC(C)(C)C", 2, 0),
    // diethylmalonate -> EtO2CCCO2Et
    AliasTestExample("CCOC(=O)CC(=O)OCC", 2, 1),
    // thioanisole -> PhSMe
    AliasTestExample("c1ccccc1SC", 1, 6),
    // oxalate dianion -> -O2CCO2-
    AliasTestExample("[O-]C(=O)C(=O)[O-]", 2, 0),
    // diphenyl - nothing to alias
    AliasTestExample("c1ccccc1c2ccccc2", 0, 12)
  };

  for (auto i : test_set) {
    i.test();
  }
  cout << endl;
}

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

  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;
    }
  }
  switch(choice) {
  case 1:
    testAliases();
    break;
  default:
    cout << "Test number " << choice << " does not exist!\n";
    return -1;
  }

  return 0;
}