Coverage Report

Created: 2025-08-03 06:31

/src/openbabel/src/stereo/gen3dstereohelper.cpp
Line
Count
Source (jump to first uncovered line)
1
/**********************************************************************
2
  gen3dstereohelper.cpp - Helper class for 3D coordinate generation
3
4
  Copyright (C) 2020 by Tim Vandermeersch
5
6
  This file is part of the Open Babel project.
7
  For more information, see <http://openbabel.org/>
8
9
  This program is free software; you can redistribute it and/or modify
10
  it under the terms of the GNU General Public License as published by
11
  the Free Software Foundation; either version 2 of the License, or
12
  (at your option) any later version.
13
14
  This program is distributed in the hope that it will be useful,
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
  GNU General Public License for more details.
18
19
  You should have received a copy of the GNU General Public License
20
  along with this program; if not, write to the Free Software
21
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
  02110-1301, USA.
23
 **********************************************************************/
24
25
#include "gen3dstereohelper.h"
26
#include <openbabel/stereo/stereo.h>
27
#include <openbabel/stereo/tetrahedral.h>
28
#include <openbabel/stereo/cistrans.h>
29
#include <openbabel/obconversion.h>
30
#include <openbabel/mol.h>
31
#include <openbabel/bond.h>
32
#include <openbabel/obiter.h>
33
34
namespace OpenBabel {
35
36
  void OBGen3DStereoHelper::Setup(OBMol *mol)
37
0
  {
38
0
    m_unspecifiedTetrahedral.clear();
39
0
    m_unspecifiedCisTrans.clear();
40
41
    // Store canonical SMILES of original molecule
42
0
    OBConversion conv;
43
0
    conv.SetOutFormat("can");
44
0
    m_inputSmiles = conv.WriteString(mol, true);
45
46
    // Keep track of unspecified stereochemistry
47
0
    OBStereoFacade facade(mol);
48
49
0
    std::vector<OBTetrahedralStereo*> tetrahedral = facade.GetAllTetrahedralStereo();
50
0
    for (std::size_t i = 0; i < tetrahedral.size(); ++i) {
51
0
      OBTetrahedralStereo::Config cfg = tetrahedral[i]->GetConfig();
52
0
      if (!cfg.specified)
53
0
        m_unspecifiedTetrahedral.push_back(cfg.center);
54
0
    }
55
56
0
    std::vector<OBCisTransStereo*> cistrans = facade.GetAllCisTransStereo();
57
0
    for (std::size_t i = 0; i < cistrans.size(); ++i) {
58
0
      OBCisTransStereo::Config cfg = cistrans[i]->GetConfig();
59
0
      OBAtom *begin = mol->GetAtomById(cfg.begin);
60
0
      OBAtom *end = mol->GetAtomById(cfg.end);
61
0
      if (!begin || !end)
62
0
        continue;
63
0
      OBBond *bond = mol->GetBond(begin, end);
64
0
      if (!bond)
65
0
        continue;
66
0
      if (!cfg.specified)
67
0
        m_unspecifiedCisTrans.push_back(bond->GetId());
68
0
    }
69
0
  }
70
71
  bool OBGen3DStereoHelper::Check(OBMol *mol)
72
0
  {
73
    // Perceive stereo from 3D coords
74
0
    StereoFrom3D(mol, true); // true  = force
75
76
    // Make sure to respect previously unspecifed stereochemistry
77
0
    OBStereoFacade facade(mol);
78
79
0
    for (std::size_t i = 0; i < m_unspecifiedTetrahedral.size(); ++i) {
80
0
      OBTetrahedralStereo *ts = facade.GetTetrahedralStereo(m_unspecifiedTetrahedral[i]);
81
0
      if (!ts)
82
0
        continue;
83
0
      OBTetrahedralStereo::Config cfg = ts->GetConfig();
84
0
      cfg.specified = false;
85
0
      ts->SetConfig(cfg);
86
0
    }
87
88
0
    for (std::size_t i = 0; i < m_unspecifiedCisTrans.size(); ++i) {
89
0
      OBCisTransStereo *ct = facade.GetCisTransStereo(m_unspecifiedCisTrans[i]);
90
0
      if (!ct)
91
0
        continue;
92
0
      OBCisTransStereo::Config cfg = ct->GetConfig();
93
0
      cfg.specified = false;
94
0
      ct->SetConfig(cfg);
95
0
    }
96
97
    // Generate canonical SMILES with stereochemistry perceived from 3D coords.
98
0
    OBConversion conv;
99
0
    conv.SetOutFormat("can");
100
0
    std::string predictedSmiles = conv.WriteString(mol, true);
101
102
0
    return m_inputSmiles == predictedSmiles;
103
0
  }
104
105
} // namespace OpenBabel