RDKit: TautomerQuery.h Source File

| 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>
20#include <GraphMol/Substruct/SubstructMatch.h>
21#include <DataStructs/ExplicitBitVect.h>
22
23#ifdef RDK_USE_BOOST_SERIALIZATION
24#include <RDGeneral/BoostStartInclude.h>
25#include <boost/serialization/vector.hpp>
26#include <boost/serialization/shared_ptr.hpp>
27#include <boost/serialization/split_member.hpp>
28#include <RDGeneral/BoostEndInclude.h>
29#endif
30
31namespace RDKit {
32
33class RWMol;
34
35 RDKIT_TAUTOMERQUERY_EXPORT bool TautomerQueryCanSerialize();
36
37 class RDKIT_TAUTOMERQUERY_EXPORT TautomerQuery {
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
59 TautomerQuery(const TautomerQuery &other)
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
76 static TautomerQuery *fromMol(
77 const ROMol &molecule,
78 const std::string &tautomerTransformFile = std::string());
79
80 // Substructure search
81 std::vector<MatchVectType> substructOf(
82 const ROMol &mol,
83 const SubstructMatchParameters &params = SubstructMatchParameters(),
84 std::vector<ROMOL_SPTR> *matchingTautomers = nullptr) const;
85
86 // SubstructureMatch
87 bool isSubstructOf(const ROMol &mol, const SubstructMatchParameters &params =
88 SubstructMatchParameters());
89
90 // Query fingerprint
91 ExplicitBitVect *patternFingerprintTemplate(
92 unsigned int fpSize = 2048U) const;
93 // Static method to Fingerprint a target
94 static ExplicitBitVect *patternFingerprintTarget(const ROMol &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
159 RDKIT_TAUTOMERQUERY_EXPORT std::vector<MatchVectType> SubstructMatch(
160 const ROMol &mol, const TautomerQuery &query,
161 const SubstructMatchParameters &params);
162
163} // namespace RDKit
164
165#endif // RDKIT_TAUTOMERQUERY_H
BoostEndInclude.h
BoostStartInclude.h
ExplicitBitVect.h
RDUNUSED_PARAM
#define RDUNUSED_PARAM(x)
Definition Invariant.h:196
PRECONDITION
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
MolPickler.h
ROMol.h
Defines the primary molecule class ROMol as well as associated typedefs.
SubstructMatch.h
ExplicitBitVect
a class for bit vectors that are densely occupied
Definition ExplicitBitVect.h:29
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
RDKit::ROMol
Definition ROMol.h:199
RDKit::RWMol
RWMol is a molecule class that is intended to be edited.
Definition RWMol.h:32
RDKit::TautomerQuery
Definition TautomerQuery.h:37
RDKit::TautomerQuery::patternFingerprintTemplate
ExplicitBitVect * patternFingerprintTemplate(unsigned int fpSize=2048U) const
RDKit::TautomerQuery::serialize
std::string serialize() const
returns a string with a serialized (pickled) representation
RDKit::TautomerQuery::TautomerQuery
TautomerQuery(const TautomerQuery &other)
Copy constructor performs a deep copy.
Definition TautomerQuery.h:59
RDKit::TautomerQuery::getModifiedAtoms
const std::vector< size_t > getModifiedAtoms() const
Definition TautomerQuery.h:104
RDKit::TautomerQuery::getTemplateMolecule
const ROMol & getTemplateMolecule() const
Definition TautomerQuery.h:100
RDKit::TautomerQuery::getModifiedBonds
const std::vector< size_t > getModifiedBonds() const
Definition TautomerQuery.h:106
RDKit::TautomerQuery::TautomerQuery
TautomerQuery(const std::string &pickle)
Definition TautomerQuery.h:72
RDKit::TautomerQuery::isSubstructOf
bool isSubstructOf(const ROMol &mol, const SubstructMatchParameters &params=SubstructMatchParameters())
RDKit::TautomerQuery::initFromString
void initFromString(const std::string &text)
initializes from a string pickle
RDKit::TautomerQuery::getTautomers
const std::vector< ROMOL_SPTR > getTautomers() const
Definition TautomerQuery.h:102
RDKit::TautomerQuery::TautomerQueryMatcher
friend class TautomerQueryMatcher
Definition TautomerQuery.h:117
RDKit::TautomerQuery::TautomerQuery
TautomerQuery(std::vector< ROMOL_SPTR > tautomers, const ROMol *const templateMolecule, std::vector< size_t > modifiedAtoms, std::vector< size_t > modifiedBonds)
RDKit::TautomerQuery::initFromStream
void initFromStream(std::istream &ss)
initializes from a stream pickle
RDKit::TautomerQuery::patternFingerprintTarget
static ExplicitBitVect * patternFingerprintTarget(const ROMol &target, unsigned int fpSize=2048U)
RDKit::TautomerQuery::toStream
void toStream(std::ostream &ss) const
serializes (pickles) to a stream
RDKit::TautomerQuery::fromMol
static TautomerQuery * fromMol(const ROMol &molecule, const std::string &tautomerTransformFile=std::string())
RDKit::TautomerQuery::substructOf
std::vector< MatchVectType > substructOf(const ROMol &mol, const SubstructMatchParameters &params=SubstructMatchParameters(), std::vector< ROMOL_SPTR > *matchingTautomers=nullptr) const
export.h
RDKIT_TAUTOMERQUERY_EXPORT
#define RDKIT_TAUTOMERQUERY_EXPORT
Definition export.h:585
RDKit
Std stuff.
Definition Abbreviations.h:19
RDKit::SubstructMatch
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::TautomerQueryCanSerialize
RDKIT_TAUTOMERQUERY_EXPORT bool TautomerQueryCanSerialize()
RDKit::SubstructMatchParameters
Definition SubstructMatch.h:42