File: stereoperceptiontest.cpp

package info (click to toggle)
openbabel 2.3.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,356 kB
  • ctags: 41,971
  • sloc: cpp: 321,256; ansic: 89,228; python: 7,262; perl: 6,418; pascal: 793; sh: 194; xml: 97; ruby: 55; makefile: 48; java: 23
file content (438 lines) | stat: -rw-r--r-- 14,969 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "obtest.h"
#include <openbabel/stereo/tetrahedral.h>
#include <openbabel/stereo/cistrans.h>
#include <openbabel/mol.h>
#include <openbabel/obconversion.h>

using namespace std;
using namespace OpenBabel;

std::string GetFilename(const std::string &filename)
{
  string path = TESTDATADIR + filename;

  return path;
}

std::string test_singleTetrahedral(const std::string &file, 
    const OBTetrahedralStereo::Config &correct)
{
  OBMol mol;
  OBConversion conv;
  conv.SetInFormat("mol");
  conv.SetOutFormat("can");

  const std::string filename = GetFilename(file);
  ifstream ifs(filename.c_str());
  if (!ifs) {
    cerr << "Could not open " << filename << endl;
    return std::string();
  }

  conv.Read(&mol, &ifs);
  ifs.close();

  std::vector<OBGenericData *> stereoData = mol.GetAllData(OBGenericDataType::StereoData);
  OB_ASSERT( stereoData.size() == 1 );

  // compare the stereochemistry
  for (std::vector<OBGenericData*>::iterator data = stereoData.begin(); data != stereoData.end(); ++data) {
    if (((OBStereoBase*)*data)->GetType() == OBStereo::Tetrahedral) {
      OBTetrahedralStereo *ts = dynamic_cast<OBTetrahedralStereo*>(*data);
      OB_ASSERT( ts->GetConfig() == correct );
      if ( ts->GetConfig() != correct ) {
        cout << "found = " << ts->GetConfig() << endl;
        cout << "correct = " << correct << endl;
      }

      OBTetrahedralStereo::Config cfg = ts->GetConfig();
      OB_ASSERT( cfg.specified );
      // change refs 
      OBStereo::Permutate(cfg.refs, 1, 2);
      OB_ASSERT( cfg != correct );
      // change winding
      cfg.winding = (cfg.winding == OBStereo::Clockwise) ? OBStereo::AntiClockwise : OBStereo::Clockwise;
      OB_ASSERT( cfg == correct );
      // change view
      cfg.view = (cfg.view == OBStereo::ViewFrom) ? OBStereo::ViewTowards : OBStereo::ViewFrom;
      OB_ASSERT( cfg != correct );
      cfg.view = (cfg.view == OBStereo::ViewFrom) ? OBStereo::ViewTowards : OBStereo::ViewFrom;
      OB_ASSERT( cfg == correct );
      // change center
      cfg.center = 3994;
      OB_ASSERT( cfg != correct );


    }
  }

  return conv.WriteString(&mol);
}

std::string test_singleCisTrans(const std::string &file, 
    const OBCisTransStereo::Config &correct)
{
  OBMol mol;
  OBConversion conv;
  conv.SetInFormat("mol");
  conv.SetOutFormat("can");

  const std::string filename = GetFilename(file);
  ifstream ifs(filename.c_str());
  if (!ifs) {
    cerr << "Could not open " << filename << endl;
    return std::string();
  }

  conv.Read(&mol, &ifs);
  ifs.close();

  std::vector<OBGenericData *> stereoData = mol.GetAllData(OBGenericDataType::StereoData);
  OB_ASSERT( stereoData.size() == 1 );

  // compare the stereochemistry
  for (std::vector<OBGenericData*>::iterator data = stereoData.begin(); data != stereoData.end(); ++data) {
    if (((OBStereoBase*)*data)->GetType() == OBStereo::CisTrans) {
      OBCisTransStereo *ct = dynamic_cast<OBCisTransStereo*>(*data);
      OB_ASSERT( ct->GetConfig() == correct );
      if ( ct->GetConfig() != correct ) {
        cout << "found = " << ct->GetConfig() << endl;
        cout << "correct = " << correct << endl;
      }
    }
  }

  return conv.WriteString(&mol);
}

std::string readMol(OBMol *pmol, const std::string &file)
{
  OBConversion conv;
  conv.SetInFormat("mol");
  conv.SetOutFormat("can");

  const std::string filename = GetFilename(file);
  ifstream ifs(filename.c_str());
  if (!ifs) {
    cerr << "Could not open " << filename << endl;
    return NULL;
  }

  conv.Read(pmol, &ifs);
  ifs.close();
  return conv.WriteString(pmol);
}

std::string test_singleUnspecifiedTetrahedral(const std::string &file, 
    unsigned long center)
{
  OBMol mol;
  string retval = readMol(&mol, file);
  if (retval.size() > 0) {
    OBStereoFacade stereo(&mol);
    OB_ASSERT( stereo.HasTetrahedralStereo(center) );
    OBTetrahedralStereo *ts = stereo.GetTetrahedralStereo(center);
    OB_ASSERT( ts->GetConfig().specified == false );
  }

  return retval;
}

std::string test_singleUnknownTetrahedral(const std::string &file, 
    unsigned long center)
{
  OBMol mol;
  string retval = readMol(&mol, file);
  if (retval.size() > 0) {
    OBStereoFacade stereo(&mol);
    OB_ASSERT( stereo.HasTetrahedralStereo(center) );
    OBTetrahedralStereo *ts = stereo.GetTetrahedralStereo(center);
    OBTetrahedralStereo::Config cfg = ts->GetConfig();
    OB_ASSERT( cfg.specified == true );
    OB_ASSERT( cfg.winding == OBStereo::UnknownWinding );
  }

  return retval;
}

void test_noStereo(const std::string &file)
{
  OBMol mol;
  OBConversion conv;
  conv.SetInFormat("mol");
  conv.SetOutFormat("can");

  const std::string filename = GetFilename(file);
  ifstream ifs(filename.c_str());
  if (!ifs) {
    cerr << "Could not open " << filename << endl;
    return;
  }

  conv.Read(&mol, &ifs);
  ifs.close();

  // perceive stereochemistry from 3D
  StereoFrom3D(&mol);

  std::vector<OBGenericData *> stereoData = mol.GetAllData(OBGenericDataType::StereoData);
  OB_ASSERT( stereoData.size() == 0 );
} 


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

  // There are several cases to test:

  //////////////////////////////////////////////////////////////////////////////
  // 
  // 1      StereoFrom3D for tetrahedral atoms
  //
  //////////////////////////////////////////////////////////////////////////////

  // 1.1    Input molecule with 4 refs (explicit H)
 
  // 1.1.1  234 == 234  @
  string smiles3D_1 = test_singleTetrahedral("stereo/tetrahedral3D_1.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, 4), OBStereo::AntiClockwise));
  // 1.1.2  234 == 234  @@
  string smiles3D_2 = test_singleTetrahedral("stereo/tetrahedral3D_2.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, 4), OBStereo::Clockwise));
  
  // 1.1.3 compare using ImplicitRef
  string smiles3D_5 = test_singleTetrahedral("stereo/tetrahedral3D_1.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, OBStereo::ImplicitRef), OBStereo::AntiClockwise));
   
  // 1.2    Input molecule with 3 refs (implicit H)
  
  // 1.2.1  23H == 23H  @@
  string smiles3D_3 = test_singleTetrahedral("stereo/tetrahedral3D_3.mol",
      OBTetrahedralStereo::Config(0, 1, OBStereo::MakeRefs(2, 3, OBStereo::ImplicitRef), OBStereo::Clockwise));
  // 1.2.2  23H == 23H  @
  string smiles3D_4 = test_singleTetrahedral("stereo/tetrahedral3D_4.mol",
      OBTetrahedralStereo::Config(0, 1, OBStereo::MakeRefs(2, 3, OBStereo::ImplicitRef), OBStereo::AntiClockwise));
  
  // 1.2.3 compare using explicit id 
  string smiles3D_6 = test_singleTetrahedral("stereo/tetrahedral3D_4.mol",
      OBTetrahedralStereo::Config(0, 1, OBStereo::MakeRefs(2, 3, 4), OBStereo::AntiClockwise));

  OB_ASSERT( smiles3D_1 == smiles3D_2 );
  OB_ASSERT( smiles3D_1 == smiles3D_3 );
  OB_ASSERT( smiles3D_1 == smiles3D_4 );
  OB_ASSERT( smiles3D_1 == smiles3D_5 );
  OB_ASSERT( smiles3D_1 == smiles3D_6 );
  
  cout << smiles3D_1 << endl;

  //////////////////////////////////////////////////////////////////////////////
  // 
  // 2      StereoFrom3D for cis/trans bonds
  //
  //////////////////////////////////////////////////////////////////////////////

  // 2.1    Input molecule with 4 refs (explicit H)
 
  // 2.1.1  F      H   2      5
  //         \    /
  //          C==C       1  3
  //         /    \     
  //        H      F   0      4
  string smiles7 = test_singleCisTrans("stereo/cistrans3D_1.mol",
      OBCisTransStereo::Config(1, 3, OBStereo::MakeRefs(0, 2, 4, 5), OBStereo::ShapeZ));
  // implicit refs
  test_singleCisTrans("stereo/cistrans3D_1.mol", OBCisTransStereo::Config(1, 3, 
        OBStereo::MakeRefs(OBStereo::ImplicitRef, 2, 4, 5), OBStereo::ShapeZ));
  test_singleCisTrans("stereo/cistrans3D_1.mol", OBCisTransStereo::Config(1, 3, 
        OBStereo::MakeRefs(0, 2, 4, OBStereo::ImplicitRef), OBStereo::ShapeZ));
  test_singleCisTrans("stereo/cistrans3D_1.mol", OBCisTransStereo::Config(1, 3, 
        OBStereo::MakeRefs(OBStereo::ImplicitRef, 2, 4, OBStereo::ImplicitRef), OBStereo::ShapeZ));
 
  // 2.1.2  F      F   2      4
  //         \    /
  //          C==C       1  3
  //         /    \     
  //        H      H   0      5
  string smiles8 = test_singleCisTrans("stereo/cistrans3D_4.mol",
      OBCisTransStereo::Config(1, 3, OBStereo::MakeRefs(0, 2, 4, 5), OBStereo::ShapeU));
  // implicit refs
  test_singleCisTrans("stereo/cistrans3D_4.mol", OBCisTransStereo::Config(1, 3, 
        OBStereo::MakeRefs(OBStereo::ImplicitRef, 2, 4, 5), OBStereo::ShapeU));
  test_singleCisTrans("stereo/cistrans3D_4.mol", OBCisTransStereo::Config(1, 3, 
        OBStereo::MakeRefs(0, 2, 4, OBStereo::ImplicitRef), OBStereo::ShapeU));
  test_singleCisTrans("stereo/cistrans3D_4.mol", OBCisTransStereo::Config(1, 3, 
        OBStereo::MakeRefs(OBStereo::ImplicitRef, 2, 4, OBStereo::ImplicitRef), OBStereo::ShapeU));
 
  // 2.2    Input molecule with 3 refs (implicit H)

  // 2.2.1  F          1      
  //         \    
  //          C==C       0  2
  //              \     
  //               F          3
  string smiles9 = test_singleCisTrans("stereo/cistrans3D_2.mol", OBCisTransStereo::Config(0, 2, 
      OBStereo::MakeRefs(1, OBStereo::ImplicitRef, 3, OBStereo::ImplicitRef), OBStereo::ShapeU));

  // 2.2.1  F      F   2      4
  //         \    /
  //          C==C       1  3
  //         /
  //        H          0
  string smiles10 = test_singleCisTrans("stereo/cistrans3D_5.mol", OBCisTransStereo::Config(1, 3, 
      OBStereo::MakeRefs(0, 2, 4, OBStereo::ImplicitRef), OBStereo::ShapeU));

  OB_ASSERT( smiles7 == smiles9 );
  OB_ASSERT( smiles8 == smiles10 );
  
  cout << smiles7 << endl;

  // 3      No stereochemistry

  // 3.1    H      H   3      2
  //         \    /
  //          C==C       0  1
  //              \     
  //               F          4
  test_noStereo("stereo/cistrans3D_3.mol");

  //////////////////////////////////////////////////////////////////////////////
  // 
  // 3      StereoFrom2D for tetrahedral atoms
  //
  //////////////////////////////////////////////////////////////////////////////
  
  // 3.1    Input molecule with 4 refs (2x in plane, one behind plane, one in front of plane)

  // 3.1.1  Input molecule with 4 refs (2x in plane bond, hash & wedge bond)
  //
  //         F   I         C-F : hash (from C to F)
  //          \ /          C-I : wedge (from C to F)
  //           C
  //          / \
  //        Br   Cl
  string smiles2D_1 = test_singleTetrahedral("stereo/tetrahedral2D_1.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, 4), OBStereo::AntiClockwise));
 
  // 3.1.2  Input molecule with 4 refs (2x in plane bond, 'real' hash & 'inverted' hash bond)
  //
  //         F   I         C-F : 'real' hash (from C to F)
  //          \ /          C-I : 'inverted' hash (from I to C) = 'real' wedge
  //           C
  //          / \
  //        Br   Cl
  string smiles2D_2 = test_singleTetrahedral("stereo/tetrahedral2D_4.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, 4), OBStereo::AntiClockwise));
 
  // 3.1.3  Input molecule with 4 refs (2x in plane bond, 'real' wedge & 'inverted' wedge bond)
  //
  //         F   I         C-F : 'inverted' wedge (from F to C) = 'real' hash
  //          \ /          C-I : 'real' wedge (from C to I)
  //           C
  //          / \
  //        Br   Cl
  string smiles2D_3 = test_singleTetrahedral("stereo/tetrahedral2D_5.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, 4), OBStereo::AntiClockwise));

  OB_ASSERT( smiles2D_1 == smiles2D_2 );
  OB_ASSERT( smiles2D_1 == smiles2D_3 );
  
  cout << smiles2D_1 << endl;
  
  // 3.2    Input molecule with 3 refs (2x in plane, one behind plane or in front of plane)
 
  // 3.2.1  Input molecule with 3 refs (2x in plane bond, real wedge bond)
  //
  //           F           C-F : 'real' wedge (from C to F)
  //           |           
  //           C
  //          / \
  //        Br   Cl
  string smiles2D_4 = test_singleTetrahedral("stereo/tetrahedral2D_2.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, OBStereo::ImplicitRef), 
      OBStereo::AntiClockwise));
 
  // 3.2.2  Input molecule with 3 refs (2x in plane bond, inverted wedge bond)
  //
  //           F           C-F : 'inverted' wedge (from F to C)
  //           |           
  //           C
  //          / \
  //        Br   Cl
  
  /* No support for inverted bonds in 'tip-only' convention
  string smiles2D_5 = test_singleTetrahedral("stereo/tetrahedral2D_6.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, OBStereo::ImplicitRef), 
      OBStereo::Clockwise));*/
 
  // 3.2.3  Input molecule with 3 refs (2x in plane bond, real hash bond)
  //
  //           F           C-F : 'real' hash (from C to F)
  //           |           
  //           C
  //          / \
  //        Br   Cl
  string smiles2D_6 = test_singleTetrahedral("stereo/tetrahedral2D_3.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, OBStereo::ImplicitRef), 
      OBStereo::Clockwise));
 
  // 3.2.3  Input molecule with 3 refs (2x in plane bond, real hash bond)
  //
  //           F           C-F : 'inverted' hash (from F to C)
  //           |           
  //           C
  //          / \
  //        Br   Cl
  /* No support for inverted bonds in 'tip-only' convention
  string smiles2D_7 = test_singleTetrahedral("stereo/tetrahedral2D_7.mol",
      OBTetrahedralStereo::Config(1, 0, OBStereo::MakeRefs(2, 3, OBStereo::ImplicitRef), 
      OBStereo::AntiClockwise));

  OB_ASSERT( smiles2D_4 == smiles2D_7 );
  OB_ASSERT( smiles2D_5 == smiles2D_6 );*/

  cout << smiles2D_4 << endl;

 // Input molecule with unknown stereochemistry

  string smiles2D_8 = test_singleUnknownTetrahedral("stereo/tetrahedral2D_8.mol", 1);
  cout << smiles2D_8 << endl;

 // Input molecule with unspecified stereochemistry

  string smiles2D_9 = test_singleUnspecifiedTetrahedral("stereo/tetrahedral2D_9.mol", 1);
  cout << smiles2D_9 << endl;

  //////////////////////////////////////////////////////////////////////////////
  // 
  // 4      StereoFrom2D for cis/trans bonds
  //
  //////////////////////////////////////////////////////////////////////////////
 
  //  C     
  //   \    
  //    C==C 
  //        \
  //         C
  string cistrans2D_1 = test_singleCisTrans("stereo/cistrans2D_1.mol",
      OBCisTransStereo::Config(0, 1, OBStereo::MakeRefs(2, OBStereo::ImplicitRef, 
      3, OBStereo::ImplicitRef), OBStereo::ShapeU));

  //  
  //  C      C
  //   \    / 
  //    C==C
  //        
  string cistrans2D_2 = test_singleCisTrans("stereo/cistrans2D_2.mol",
      OBCisTransStereo::Config(0, 1, OBStereo::MakeRefs(2, OBStereo::ImplicitRef, 
      OBStereo::ImplicitRef, 3), OBStereo::ShapeU));



  return 0;
}