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() = default;
54 TautomerQuery(std::vector<ROMOL_SPTR> tautomers,
55 const ROMol *const templateMolecule,
56 std::vector<size_t> modifiedAtoms,
57 std::vector<size_t> modifiedBonds);
58
59 //! Copy constructor performs a deep copy
61 : d_templateMolecule(other.d_templateMolecule
62 ? new ROMol(*other.d_templateMolecule)
63 : nullptr),
64 d_modifiedAtoms(other.d_modifiedAtoms),
65 d_modifiedBonds(other.d_modifiedBonds) {
66 PRECONDITION(other.d_templateMolecule != nullptr, "Null template");
67 for (auto taut : other.d_tautomers) {
68 PRECONDITION(taut.get() != nullptr, "Null tautomer");
69 d_tautomers.push_back(boost::make_shared<ROMol>(*taut));
70 }
71 }
72
73 TautomerQuery(const std::string &pickle) { initFromString(pickle); }
74
75 // Factory to build TautomerQuery
76 // Caller owns the memory
78 const ROMol &molecule,
79 const std::string &tautomerTransformFile = std::string());
80
81 // Substructure search
82 std::vector<MatchVectType> substructOf(
83 const ROMol &mol,
85 std::vector<ROMOL_SPTR> *matchingTautomers = nullptr) const;
86
87 // SubstructureMatch
88 bool isSubstructOf(const ROMol &mol, const SubstructMatchParameters &params =
90
91 // Query fingerprint
93 unsigned int fpSize = 2048U) const;
94 // Static method to Fingerprint a target
96 unsigned int fpSize = 2048U);
97
98 // accessors
99
100 // pointer is owned by TautomerQuery
101 const ROMol &getTemplateMolecule() const { return *d_templateMolecule; }
102
103 const std::vector<ROMOL_SPTR> getTautomers() const { return d_tautomers; }
104
105 const std::vector<size_t> getModifiedAtoms() const { return d_modifiedAtoms; }
106
107 const std::vector<size_t> getModifiedBonds() const { return d_modifiedBonds; }
108
109 //! serializes (pickles) to a stream
110 void toStream(std::ostream &ss) const;
111 //! returns a string with a serialized (pickled) representation
112 std::string serialize() const;
113 //! initializes from a stream pickle
114 void initFromStream(std::istream &ss);
115 //! initializes from a string pickle
116 void initFromString(const std::string &text);
117
119
120#ifdef RDK_USE_BOOST_SERIALIZATION
121 template <class Archive>
122 void save(Archive &ar, const unsigned int version) const {
123 RDUNUSED_PARAM(version);
124 std::vector<std::string> pkls;
125 for (const auto &taut : d_tautomers) {
126 std::string pkl;
127 MolPickler::pickleMol(*taut, pkl, PicklerOps::AllProps);
128 pkls.push_back(pkl);
129 }
130 ar << pkls;
131 std::string molpkl;
132 MolPickler::pickleMol(*d_templateMolecule, molpkl, PicklerOps::AllProps);
133 ar << molpkl;
134 ar << d_modifiedAtoms;
135 ar << d_modifiedBonds;
136 }
137
138 template <class Archive>
139 void load(Archive &ar, const unsigned int version) {
140 RDUNUSED_PARAM(version);
141
142 std::vector<std::string> pkls;
143 ar >> pkls;
144 d_tautomers.clear();
145 for (const auto &pkl : pkls) {
146 d_tautomers.push_back(ROMOL_SPTR(new ROMol(pkl)));
147 }
148 std::string molpkl;
149 ar >> molpkl;
150 d_templateMolecule.reset(new ROMol(molpkl));
151
152 ar >> d_modifiedAtoms;
153 ar >> d_modifiedBonds;
154 }
155 BOOST_SERIALIZATION_SPLIT_MEMBER()
156#endif
157};
158
159// so we can use the templates in Code/GraphMol/Substruct/SubstructMatch.h
161 const ROMol &mol, const TautomerQuery &query,
162 const SubstructMatchParameters &params);
163
164} // namespace RDKit
165
166#endif // RDKIT_TAUTOMERQUERY_H
#define RDUNUSED_PARAM(x)
Definition Invariant.h:196
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
Defines the primary molecule class ROMol as well as associated typedefs.
a class for bit vectors that are densely occupied
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
RWMol is a molecule class that is intended to be edited.
Definition RWMol.h:32
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
friend class TautomerQueryMatcher
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:741
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()