RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
TautomerQuery.h
Go to the documentation of this file.
1//
2// Created by Gareth Jones on 5/7/2020.
3//
4// Copyright 2020-2022 Schrodinger, Inc and other RDKit contributors
5// @@ All Rights Reserved @@
6// This file is part of the RDKit.
7// The contents are covered by the terms of the BSD license
8// which is included in the file license.txt, found at the root
9// of the RDKit source tree.
10
11#include <RDGeneral/export.h>
12
13#ifndef RDKIT_TAUTOMERQUERY_H
14#define RDKIT_TAUTOMERQUERY_H
15
16#include <GraphMol/ROMol.h>
17#include <GraphMol/MolPickler.h>
18#include <vector>
19#include <span>
22
23#ifdef RDK_USE_BOOST_SERIALIZATION
25#include <boost/serialization/vector.hpp>
26#include <boost/serialization/shared_ptr.hpp>
27#include <boost/serialization/split_member.hpp>
29#endif
30
31namespace RDKit {
32
33class RWMol;
34
36
38 private:
39 // Tautomers of the query
40 std::vector<ROMOL_SPTR> d_tautomers;
41 // Template query for substructure search
42 std::unique_ptr<const ROMol> d_templateMolecule;
43 // Tautomeric bonds and atoms
44 std::vector<size_t> d_modifiedAtoms;
45 std::vector<size_t> d_modifiedBonds;
46
47 // tests if a match to the template matches a specific tautomer
48 bool matchTautomer(const ROMol &mol, const ROMol &tautomer,
49 const std::span<const unsigned int> &match,
50 const SubstructMatchParameters &params) const;
51
52 public:
53 TautomerQuery(std::vector<ROMOL_SPTR> tautomers,
54 const ROMol *const templateMolecule,
55 std::vector<size_t> modifiedAtoms,
56 std::vector<size_t> modifiedBonds);
57
58 //! Copy constructor performs a deep copy
60 : d_templateMolecule(other.d_templateMolecule
61 ? new ROMol(*other.d_templateMolecule)
62 : nullptr),
63 d_modifiedAtoms(other.d_modifiedAtoms),
64 d_modifiedBonds(other.d_modifiedBonds) {
65 PRECONDITION(other.d_templateMolecule != nullptr, "Null template");
66 for (auto taut : other.d_tautomers) {
67 PRECONDITION(taut.get() != nullptr, "Null tautomer");
68 d_tautomers.push_back(boost::make_shared<ROMol>(*taut));
69 }
70 }
71
72 TautomerQuery(const std::string &pickle) { initFromString(pickle); }
73
74 // Factory to build TautomerQuery
75 // Caller owns the memory
77 const ROMol &molecule,
78 const std::string &tautomerTransformFile = std::string());
79
80 // Substructure search
81 std::vector<MatchVectType> substructOf(
82 const ROMol &mol,
84 std::vector<ROMOL_SPTR> *matchingTautomers = nullptr) const;
85
86 // SubstructureMatch
87 bool isSubstructOf(const ROMol &mol, const SubstructMatchParameters &params =
89
90 // Query fingerprint
92 unsigned int fpSize = 2048U) const;
93 // Static method to Fingerprint a target
95 unsigned int fpSize = 2048U);
96
97 // accessors
98
99 // pointer is owned by TautomerQuery
100 const ROMol &getTemplateMolecule() const { return *d_templateMolecule; }
101
102 const std::vector<ROMOL_SPTR> getTautomers() const { return d_tautomers; }
103
104 const std::vector<size_t> getModifiedAtoms() const { return d_modifiedAtoms; }
105
106 const std::vector<size_t> getModifiedBonds() const { return d_modifiedBonds; }
107
108 //! serializes (pickles) to a stream
109 void toStream(std::ostream &ss) const;
110 //! returns a string with a serialized (pickled) representation
111 std::string serialize() const;
112 //! initializes from a stream pickle
113 void initFromStream(std::istream &ss);
114 //! initializes from a string pickle
115 void initFromString(const std::string &text);
116
117 friend class TautomerQueryMatcher;
118
119#ifdef RDK_USE_BOOST_SERIALIZATION
120 template <class Archive>
121 void save(Archive &ar, const unsigned int version) const {
122 RDUNUSED_PARAM(version);
123 std::vector<std::string> pkls;
124 for (const auto &taut : d_tautomers) {
125 std::string pkl;
126 MolPickler::pickleMol(*taut, pkl, PicklerOps::AllProps);
127 pkls.push_back(pkl);
128 }
129 ar << pkls;
130 std::string molpkl;
131 MolPickler::pickleMol(*d_templateMolecule, molpkl, PicklerOps::AllProps);
132 ar << molpkl;
133 ar << d_modifiedAtoms;
134 ar << d_modifiedBonds;
135 }
136
137 template <class Archive>
138 void load(Archive &ar, const unsigned int version) {
139 RDUNUSED_PARAM(version);
140
141 std::vector<std::string> pkls;
142 ar >> pkls;
143 d_tautomers.clear();
144 for (const auto &pkl : pkls) {
145 d_tautomers.push_back(ROMOL_SPTR(new ROMol(pkl)));
146 }
147 std::string molpkl;
148 ar >> molpkl;
149 d_templateMolecule.reset(new ROMol(molpkl));
150
151 ar >> d_modifiedAtoms;
152 ar >> d_modifiedBonds;
153 }
154 BOOST_SERIALIZATION_SPLIT_MEMBER()
155#endif
156};
157
158// so we can use the templates in Code/GraphMol/Substruct/SubstructMatch.h
160 const ROMol &mol, const TautomerQuery &query,
161 const SubstructMatchParameters &params);
162
163} // namespace RDKit
164
165#endif // RDKIT_TAUTOMERQUERY_H
#define RDUNUSED_PARAM(x)
Definition Invariant.h:197
#define PRECONDITION(expr, mess)
Definition Invariant.h:109
Defines the primary molecule class ROMol as well as associated typedefs.
a class for bit vectors that are densely occupied
ExplicitBitVect * patternFingerprintTemplate(unsigned int fpSize=2048U) const
std::string serialize() const
returns a string with a serialized (pickled) representation
TautomerQuery(const TautomerQuery &other)
Copy constructor performs a deep copy.
const std::vector< size_t > getModifiedAtoms() const
const ROMol & getTemplateMolecule() const
const std::vector< size_t > getModifiedBonds() const
TautomerQuery(const std::string &pickle)
bool isSubstructOf(const ROMol &mol, const SubstructMatchParameters &params=SubstructMatchParameters())
void initFromString(const std::string &text)
initializes from a string pickle
const std::vector< ROMOL_SPTR > getTautomers() const
TautomerQuery(std::vector< ROMOL_SPTR > tautomers, const ROMol *const templateMolecule, std::vector< size_t > modifiedAtoms, std::vector< size_t > modifiedBonds)
void initFromStream(std::istream &ss)
initializes from a stream pickle
static ExplicitBitVect * patternFingerprintTarget(const ROMol &target, unsigned int fpSize=2048U)
void toStream(std::ostream &ss) const
serializes (pickles) to a stream
static TautomerQuery * fromMol(const ROMol &molecule, const std::string &tautomerTransformFile=std::string())
std::vector< MatchVectType > substructOf(const ROMol &mol, const SubstructMatchParameters &params=SubstructMatchParameters(), std::vector< ROMOL_SPTR > *matchingTautomers=nullptr) const
#define RDKIT_TAUTOMERQUERY_EXPORT
Definition export.h:553
Std stuff.
RDKIT_SUBSTRUCTMATCH_EXPORT std::vector< MatchVectType > SubstructMatch(const ROMol &mol, const ROMol &query, const SubstructMatchParameters &params=SubstructMatchParameters())
Find a substructure match for a query in a molecule.
RDKIT_TAUTOMERQUERY_EXPORT bool TautomerQueryCanSerialize()