Coverage Report

Created: 2026-07-16 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openbabel/src/stereo/gen3dstereohelper.cpp
Line
Count
Source
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
0
    m_inputHasStereoNotation = false;
41
42
    // Store canonical SMILES of original molecule
43
0
    OBConversion conv;
44
0
    conv.SetOutFormat("can");
45
0
    m_inputSmiles = conv.WriteString(mol, true);
46
47
    // Check if the input SMILES contains any stereo notation
48
    // (@ for tetrahedral, / \ for cis/trans)
49
0
    m_inputHasStereoNotation = (m_inputSmiles.find('@') != std::string::npos ||
50
0
                                m_inputSmiles.find('/') != std::string::npos ||
51
0
                                m_inputSmiles.find('\\') != std::string::npos);
52
53
    // Keep track of unspecified stereochemistry
54
0
    OBStereoFacade facade(mol);
55
56
0
    std::vector<OBTetrahedralStereo*> tetrahedral = facade.GetAllTetrahedralStereo();
57
0
    for (std::size_t i = 0; i < tetrahedral.size(); ++i) {
58
0
      OBTetrahedralStereo::Config cfg = tetrahedral[i]->GetConfig();
59
0
      if (!cfg.specified)
60
0
        m_unspecifiedTetrahedral.push_back(cfg.center);
61
0
    }
62
63
0
    std::vector<OBCisTransStereo*> cistrans = facade.GetAllCisTransStereo();
64
0
    for (std::size_t i = 0; i < cistrans.size(); ++i) {
65
0
      OBCisTransStereo::Config cfg = cistrans[i]->GetConfig();
66
0
      OBAtom *begin = mol->GetAtomById(cfg.begin);
67
0
      OBAtom *end = mol->GetAtomById(cfg.end);
68
0
      if (!begin || !end)
69
0
        continue;
70
0
      OBBond *bond = mol->GetBond(begin, end);
71
0
      if (!bond)
72
0
        continue;
73
0
      if (!cfg.specified)
74
0
        m_unspecifiedCisTrans.push_back(bond->GetId());
75
0
    }
76
0
  }
77
78
  bool OBGen3DStereoHelper::Check(OBMol *mol)
79
0
  {
80
    // If the input SMILES has no stereo notation, any 3D geometry is acceptable.
81
    // The E/Z configuration might change during optimization, but that's fine
82
    // since no stereo was specified in the input.
83
0
    if (!m_inputHasStereoNotation)
84
0
      return true;
85
86
    // Perceive stereo from 3D coords
87
0
    StereoFrom3D(mol, true); // true  = force
88
89
    // Make sure to respect previously unspecifed stereochemistry
90
0
    OBStereoFacade facade(mol);
91
92
0
    for (std::size_t i = 0; i < m_unspecifiedTetrahedral.size(); ++i) {
93
0
      OBTetrahedralStereo *ts = facade.GetTetrahedralStereo(m_unspecifiedTetrahedral[i]);
94
0
      if (!ts)
95
0
        continue;
96
0
      OBTetrahedralStereo::Config cfg = ts->GetConfig();
97
0
      cfg.specified = false;
98
0
      ts->SetConfig(cfg);
99
0
    }
100
101
0
    for (std::size_t i = 0; i < m_unspecifiedCisTrans.size(); ++i) {
102
0
      OBCisTransStereo *ct = facade.GetCisTransStereo(m_unspecifiedCisTrans[i]);
103
0
      if (!ct)
104
0
        continue;
105
0
      OBCisTransStereo::Config cfg = ct->GetConfig();
106
0
      cfg.specified = false;
107
0
      ct->SetConfig(cfg);
108
0
    }
109
110
    // Generate canonical SMILES with stereochemistry perceived from 3D coords.
111
0
    OBConversion conv;
112
0
    conv.SetOutFormat("can");
113
0
    std::string predictedSmiles = conv.WriteString(mol, true);
114
115
0
    return m_inputSmiles == predictedSmiles;
116
0
  }
117
118
} // namespace OpenBabel