Coverage Report

Created: 2025-12-05 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openbabel/src/stereo/facade.cpp
Line
Count
Source
1
/**********************************************************************
2
  stereofacade.cpp - OBStereoFacade
3
4
  Copyright (C) 2009 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
#include <openbabel/stereo/tetrahedral.h>
25
#include <openbabel/stereo/cistrans.h>
26
#include <openbabel/stereo/squareplanar.h>
27
#include <openbabel/mol.h>
28
#include <openbabel/atom.h>
29
#include <openbabel/bond.h>
30
#include <openbabel/obiter.h>
31
32
namespace OpenBabel {
33
34
  unsigned int OBStereoFacade::NumTetrahedralStereo()
35
0
  {
36
0
    EnsureInit();
37
0
    return static_cast<unsigned int> (m_tetrahedralMap.size());
38
0
  }
39
40
  unsigned int OBStereoFacade::NumCisTransStereo()
41
0
  {
42
0
    EnsureInit();
43
0
    return static_cast<unsigned int> (m_cistransMap.size());
44
0
  }
45
46
  unsigned int OBStereoFacade::NumSquarePlanarStereo()
47
0
  {
48
0
    EnsureInit();
49
0
    return static_cast<unsigned int> (m_squarePlanarMap.size());
50
0
  }
51
52
  std::vector<OBTetrahedralStereo*> OBStereoFacade::GetAllTetrahedralStereo()
53
0
  {
54
0
    EnsureInit();
55
56
0
    typedef std::map<unsigned long, OBTetrahedralStereo*>::iterator Iter;
57
0
    std::vector<OBTetrahedralStereo*> result;
58
0
    for (Iter it = m_tetrahedralMap.begin(); it != m_tetrahedralMap.end(); ++it)
59
0
      result.push_back(it->second);
60
61
0
    return result;
62
0
  }
63
64
  std::vector<OBCisTransStereo*> OBStereoFacade::GetAllCisTransStereo()
65
0
  {
66
0
    EnsureInit();
67
68
0
    typedef std::map<unsigned long, OBCisTransStereo*>::iterator Iter;
69
0
    std::vector<OBCisTransStereo*> result;
70
0
    for (Iter it = m_cistransMap.begin(); it != m_cistransMap.end(); ++it)
71
0
      result.push_back(it->second);
72
73
0
    return result;
74
0
  }
75
76
  std::vector<OBSquarePlanarStereo*> OBStereoFacade::GetAllSquarePlanarStereo()
77
0
  {
78
0
    EnsureInit();
79
80
0
    typedef std::map<unsigned long, OBSquarePlanarStereo*>::iterator Iter;
81
0
    std::vector<OBSquarePlanarStereo*> result;
82
0
    for (Iter it = m_squarePlanarMap.begin(); it != m_squarePlanarMap.end(); ++it)
83
0
      result.push_back(it->second);
84
85
0
    return result;
86
0
  }
87
88
  bool OBStereoFacade::HasTetrahedralStereo(unsigned long atomId)
89
0
  {
90
0
    EnsureInit();
91
0
    if (m_tetrahedralMap.find(atomId) != m_tetrahedralMap.end())
92
0
      return true;
93
0
    return false;
94
0
  }
95
96
  bool OBStereoFacade::HasCisTransStereo(unsigned long bondId)
97
0
  {
98
0
    EnsureInit();
99
0
    if (m_cistransMap.find(bondId) != m_cistransMap.end())
100
0
      return true;
101
0
    return false;
102
0
  }
103
104
  bool OBStereoFacade::HasSquarePlanarStereo(unsigned long atomId)
105
0
  {
106
0
    EnsureInit();
107
0
    if (m_squarePlanarMap.find(atomId) != m_squarePlanarMap.end())
108
0
      return true;
109
0
    return false;
110
0
  }
111
112
  OBTetrahedralStereo* OBStereoFacade::GetTetrahedralStereo(unsigned long atomId)
113
0
  {
114
0
    if (!HasTetrahedralStereo(atomId))
115
0
      return nullptr;
116
0
    return m_tetrahedralMap[atomId];
117
0
  }
118
119
  OBCisTransStereo* OBStereoFacade::GetCisTransStereo(unsigned long bondId)
120
0
  {
121
0
    if (!HasCisTransStereo(bondId))
122
0
      return nullptr;
123
0
    return m_cistransMap[bondId];
124
0
  }
125
126
  OBSquarePlanarStereo* OBStereoFacade::GetSquarePlanarStereo(unsigned long atomId)
127
0
  {
128
0
    if (!HasSquarePlanarStereo(atomId))
129
0
      return nullptr;
130
0
    return m_squarePlanarMap[atomId];
131
0
  }
132
133
  void OBStereoFacade::InitMaps()
134
0
  {
135
0
    if (m_perceive && !m_mol->HasChiralityPerceived())
136
0
      PerceiveStereo(m_mol);
137
138
0
    std::vector<OBGenericData *> stereoData = m_mol->GetAllData(OBGenericDataType::StereoData);
139
140
0
    std::vector<OBGenericData*>::iterator data;
141
0
    for (data = stereoData.begin(); data != stereoData.end(); ++data) {
142
0
      OBStereo::Type type = ((OBStereoBase*)*data)->GetType();
143
0
      if (type == OBStereo::Tetrahedral) {
144
0
        OBTetrahedralStereo *ts = dynamic_cast<OBTetrahedralStereo*>(*data);
145
0
        OBTetrahedralStereo::Config config = ts->GetConfig();
146
0
        if (config.center == OBStereo::NoRef)
147
0
          continue;
148
0
        m_tetrahedralMap[config.center] = ts;
149
0
      } else
150
0
      if (type == OBStereo::SquarePlanar) {
151
0
        OBSquarePlanarStereo *sp = dynamic_cast<OBSquarePlanarStereo*>(*data);
152
0
        OBSquarePlanarStereo::Config config = sp->GetConfig();
153
0
        if (config.center == OBStereo::NoRef)
154
0
          continue;
155
0
        m_squarePlanarMap[config.center] = sp;
156
0
      } else
157
0
      if (type == OBStereo::CisTrans) {
158
0
        OBCisTransStereo *ct = dynamic_cast<OBCisTransStereo*>(*data);
159
0
        OBCisTransStereo::Config config = ct->GetConfig();
160
        // find the bond id from begin & end atom ids
161
0
        unsigned long id = OBStereo::NoRef;
162
0
        OBAtom *a = m_mol->GetAtomById(config.begin);
163
0
        if (!a)
164
0
          continue;
165
0
        FOR_BONDS_OF_ATOM (bond, a) {
166
0
          unsigned long beginId = bond->GetBeginAtom()->GetId();
167
0
          unsigned long endId = bond->GetEndAtom()->GetId();
168
0
          if ((beginId == config.begin && endId == config.end) ||
169
0
              (beginId == config.end && endId == config.begin)) {
170
0
            id = bond->GetId();
171
0
            break;
172
0
          }
173
0
        }
174
0
        if (id == OBStereo::NoRef)
175
0
          continue;
176
0
        m_cistransMap[id] = ct;
177
0
      }
178
0
    }
179
180
0
    m_init = true;
181
0
  }
182
183
  template<>
184
  bool OBStereoFacade::HasStereo<OBStereo::Tetrahedral>(unsigned long id)
185
0
  {
186
0
    return HasTetrahedralStereo(id);
187
0
  }
188
189
  template<>
190
  bool OBStereoFacade::HasStereo<OBStereo::CisTrans>(unsigned long id)
191
0
  {
192
0
    return HasCisTransStereo(id);
193
0
  }
194
195
  template<>
196
  OBTetrahedralStereo* OBStereoFacade::GetStereo<OBTetrahedralStereo>(unsigned long id)
197
0
  {
198
0
    return GetTetrahedralStereo(id);
199
0
  }
200
201
  template<>
202
  OBCisTransStereo* OBStereoFacade::GetStereo<OBCisTransStereo>(unsigned long id)
203
0
  {
204
0
    return GetCisTransStereo(id);
205
0
  }
206
207
208
209
210
}
211