RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
Subgraphs.h
Go to the documentation of this file.
1//
2// Copyright (C) 2003-2026 Greg Landrum and other RDKit contributors
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10
11/*! \file Subgraphs.h
12
13 \brief functionality for finding subgraphs and paths in molecules
14
15 Difference between _subgraphs_ and _paths_ :
16 Subgraphs are potentially branched, whereas paths (in our
17 terminology at least) cannot be. So, the following graph:
18\verbatim
19 C--0--C--1--C--3--C
20 |
21 2
22 |
23 C
24\endverbatim
25 has 3 _subgraphs_ of length 3: (0,1,2),(0,1,3),(2,1,3)
26 but only 2 _paths_ of length 3: (0,1,3),(2,1,3)
27*/
28#include <RDGeneral/export.h>
29#ifndef RD_SUBGRAPHS_H
30#define RD_SUBGRAPHS_H
31
32#include <vector>
33#include <list>
34#include <map>
35#include <unordered_map>
36#include <boost/dynamic_bitset.hpp>
37
38namespace RDKit {
39class ROMol;
40// NOTE: before replacing the defn of PATH_TYPE: be aware that
41// we do occasionally use reverse iterators on these things, so
42// replacing with a slist would probably be a bad idea.
43typedef std::vector<int> PATH_TYPE;
44typedef std::list<PATH_TYPE> PATH_LIST;
45typedef PATH_LIST::const_iterator PATH_LIST_CI;
46
47typedef std::map<int, PATH_LIST> INT_PATH_LIST_MAP;
48typedef INT_PATH_LIST_MAP::const_iterator INT_PATH_LIST_MAP_CI;
49typedef INT_PATH_LIST_MAP::iterator INT_PATH_LIST_MAP_I;
50
51// --- --- --- --- --- --- --- --- --- --- --- --- ---
52//
53//
54// --- --- --- --- --- --- --- --- --- --- --- --- ---
55
56//! \brief find all bond subgraphs in a range of sizes
57/*!
58 * \param mol - the molecule to be considered
59 * \param lowerLen - the minimum subgraph size to find
60 * \param upperLen - the maximum subgraph size to find
61 * \param useHs - if set, hydrogens in the graph will be considered
62 * eligible to be in paths. NOTE: this will not add
63 * Hs to the graph.
64 * \param rootedAtAtom - if non-negative, only subgraphs that start at
65 * this atom will be returned.
66 * \param ignoreAtoms - if provided, any subgraph that contains any of
67 * the atoms in this set will be ignored
68 *
69 * The result is a map from subgraph size -> list of paths
70 * (i.e. list of list of bond indices)
71 */
73 const ROMol &mol, unsigned int lowerLen, unsigned int upperLen,
74 bool useHs = false, int rootedAtAtom = -1,
75 boost::dynamic_bitset<> *ignoreAtoms = nullptr);
76
77//! \brief find all bond subgraphs of a particular size
78/*!
79 * \param mol - the molecule to be considered
80 * \param targetLen - the length of the subgraphs to be returned
81 * \param useHs - if set, hydrogens in the graph will be considered
82 * eligible to be in paths. NOTE: this will not add
83 * Hs to the graph.
84 * \param rootedAtAtom - if non-negative, only subgraphs that start at
85 * this atom will be returned.
86 * \param ignoreAtoms - if provided, any subgraph that contains any of
87 * the atoms in this set will be ignored
88 *
89 *
90 * The result is a list of paths (i.e. list of list of bond indices)
91 */
93 const ROMol &mol, unsigned int targetLen, bool useHs = false,
94 int rootedAtAtom = -1, boost::dynamic_bitset<> *ignoreAtoms = nullptr);
95
96//! \brief find unique bond subgraphs of a particular size
97/*!
98 * \param mol - the molecule to be considered
99 * \param targetLen - the length of the subgraphs to be returned
100 * \param useHs - if set, hydrogens in the graph will be considered
101 * eligible to be in paths. NOTE: this will not add
102 * Hs to the graph.
103 * \param useBO - if set, bond orders will be considered when uniquifying
104 * the paths
105 * \param rootedAtAtom - if non-negative, only subgraphs that start at
106 * \param ignoreAtoms - if provided, any subgraph that contains any of
107 * the atoms in this set will be ignored
108 *
109 * The result is a list of paths (i.e. list of list of bond indices)
110 */
112 const ROMol &mol, unsigned int targetLen, bool useHs = false,
113 bool useBO = true, int rootedAtAtom = -1,
114 boost::dynamic_bitset<> *ignoreAtoms = nullptr);
115//! \brief find all paths of a particular size
116/*!
117 * \param mol - the molecule to be considered
118 * \param targetLen - the length of the paths to be returned
119 * \param useBonds - if set, the path indices will be bond indices,
120 * not atom indices
121 * \param useHs - if set, hydrogens in the graph will be considered
122 * eligible to be in paths. NOTE: this will not add
123 * Hs to the graph.
124 * \param rootedAtAtom - if non-negative, only subgraphs that start at
125 * this atom will be returned.
126 * \param onlyShortestPaths - if set then only paths which are <= the shortest
127 * path between the begin and end atoms will be
128 * included in the results
129 * \param ignoreAtoms - if provided, any subgraph that contains any of
130 * the atoms in this set will be ignored
131 *
132 * The result is a list of paths (i.e. list of list of bond indices)
133 */
135 const ROMol &mol, unsigned int targetLen, bool useBonds = true,
136 bool useHs = false, int rootedAtAtom = -1, bool onlyShortestPaths = false,
137 boost::dynamic_bitset<> *ignoreAtoms = nullptr);
139 const ROMol &mol, unsigned int lowerLen, unsigned int upperLen,
140 bool useBonds = true, bool useHs = false, int rootedAtAtom = -1,
141 bool onlyShortestPaths = false,
142 boost::dynamic_bitset<> *ignoreAtoms = nullptr);
143
144//! \brief Find bond subgraphs of a particular radius around an atom.
145//! Return empty result if there is no bond at the requested radius.
146/*!
147 * \param mol - the molecule to be considered
148 * \param radius - the radius of the subgraphs to be considered
149 * \param rootedAtAtom - the atom to consider
150 * \param useHs - if set, hydrogens in the graph will be considered
151 * eligible to be in paths. NOTE: this will not add
152 * Hs to the graph.
153 * \param enforceSize - If false, all the bonds within the requested radius
154 * (<= radius) is collected. Otherwise, at least one bond
155 * located at the requested radius must be found and
156 * added. \param atomMap - Optional: If provided, it will measure the minimum
157 * distance of the atom from the rooted atom (start with 0 from the rooted
158 * atom). The result is a pair of the atom ID and the distance. The result is a
159 * path (a vector of bond indices)
160 */
162 const ROMol &mol, unsigned int radius, unsigned int rootedAtAtom,
163 bool useHs = false, bool enforceSize = true,
164 std::unordered_map<unsigned int, unsigned int> *atomMap = nullptr);
165
166} // namespace RDKit
167
168#endif
#define RDKIT_SUBGRAPHS_EXPORT
Definition export.h:701
Std stuff.
std::list< PATH_TYPE > PATH_LIST
Definition Subgraphs.h:44
RDKIT_SUBGRAPHS_EXPORT PATH_TYPE findAtomEnvironmentOfRadiusN(const ROMol &mol, unsigned int radius, unsigned int rootedAtAtom, bool useHs=false, bool enforceSize=true, std::unordered_map< unsigned int, unsigned int > *atomMap=nullptr)
Find bond subgraphs of a particular radius around an atom. Return empty result if there is no bond at...
RDKIT_SUBGRAPHS_EXPORT INT_PATH_LIST_MAP findAllPathsOfLengthsMtoN(const ROMol &mol, unsigned int lowerLen, unsigned int upperLen, bool useBonds=true, bool useHs=false, int rootedAtAtom=-1, bool onlyShortestPaths=false, boost::dynamic_bitset<> *ignoreAtoms=nullptr)
std::vector< int > PATH_TYPE
Definition Subgraphs.h:43
RDKIT_SUBGRAPHS_EXPORT PATH_LIST findAllPathsOfLengthN(const ROMol &mol, unsigned int targetLen, bool useBonds=true, bool useHs=false, int rootedAtAtom=-1, bool onlyShortestPaths=false, boost::dynamic_bitset<> *ignoreAtoms=nullptr)
find all paths of a particular size
std::map< int, PATH_LIST > INT_PATH_LIST_MAP
Definition Subgraphs.h:47
RDKIT_SUBGRAPHS_EXPORT INT_PATH_LIST_MAP findAllSubgraphsOfLengthsMtoN(const ROMol &mol, unsigned int lowerLen, unsigned int upperLen, bool useHs=false, int rootedAtAtom=-1, boost::dynamic_bitset<> *ignoreAtoms=nullptr)
find all bond subgraphs in a range of sizes
RDKIT_SUBGRAPHS_EXPORT PATH_LIST findUniqueSubgraphsOfLengthN(const ROMol &mol, unsigned int targetLen, bool useHs=false, bool useBO=true, int rootedAtAtom=-1, boost::dynamic_bitset<> *ignoreAtoms=nullptr)
find unique bond subgraphs of a particular size
INT_PATH_LIST_MAP::iterator INT_PATH_LIST_MAP_I
Definition Subgraphs.h:49
RDKIT_SUBGRAPHS_EXPORT PATH_LIST findAllSubgraphsOfLengthN(const ROMol &mol, unsigned int targetLen, bool useHs=false, int rootedAtAtom=-1, boost::dynamic_bitset<> *ignoreAtoms=nullptr)
find all bond subgraphs of a particular size
INT_PATH_LIST_MAP::const_iterator INT_PATH_LIST_MAP_CI
Definition Subgraphs.h:48
PATH_LIST::const_iterator PATH_LIST_CI
Definition Subgraphs.h:45