RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
SynthonSpaceSearcher.h
Go to the documentation of this file.
1//
2// Copyright (C) David Cosgrove 2024.
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// This file declares an abstract base class for searching a synthon
12// space. Concrete base classes include SynthonSpaceSubstructureSearcher
13// and SynthonSpaceFingerprintSearcher.
14
15#ifndef SYNTHONSPACESEARCHER_H
16#define SYNTHONSPACESEARCHER_H
17
18#include <atomic>
19#include <chrono>
20#include <random>
21
22#include <RDGeneral/export.h>
26#include <boost/spirit/home/support/common_terminals.hpp>
27
28using Clock = std::chrono::steady_clock;
29using TimePoint = std::chrono::time_point<Clock>;
30
31namespace RDKit {
32class ROMol;
33
34namespace SynthonSpaceSearch {
35
36// Abstract base class for searching the SynthonSpace.
38 public:
41 const SynthonSpaceSearchParams &params,
42 SynthonSpace &space);
47
48 virtual ~SynthonSpaceSearcher() = default;
49
52
53 SynthonSpace &getSpace() const { return d_space; }
54 const ROMol &getQuery() const { return d_query; }
55 const SynthonSpaceSearchParams &getParams() const { return d_params; }
56
57 // Do the search of this fragSet against the SynthonSet in the
58 // appropriate way, for example by substructure or fingerprint
59 // similarity.
60 virtual std::vector<std::unique_ptr<SynthonSpaceHitSet>> searchFragSet(
61 const std::vector<std::unique_ptr<ROMol>> &fragSet,
62 const SynthonSet &reaction) const = 0;
63
64 // Make the hit, constructed from a specific combination of
65 // synthons in the hitset, and verify that it matches the
66 // query in the appropriate way. There'll be 1 entry in synthNums
67 // for each synthon list in the hitset. Returns an empty pointer
68 // if the hit isn't accepted for whatever reason.
69 std::unique_ptr<ROMol> buildAndVerifyHit(
70 const SynthonSpaceHitSet *hitset,
71 const std::vector<size_t> &synthNums) const;
72
73 protected:
74 // Checks that the given molecule is definitely a hit according to
75 // the derived class' criteria. This function checks the chiralAtomCount
76 // if appropriate, which required a non-const ROMol.
77 virtual bool verifyHit(ROMol &mol) const;
78
79 // Do a check against number of heavy atoms etc. if options call for it
80 // which can be done without having to build the full molecule from the
81 // synthons. Some of the search methods (fingerprints, for example) can do
82 // additional quick checks on whether this set of synthons can match the query
83 // without building the full molecule.
84 virtual bool quickVerify(const SynthonSpaceHitSet *hitset,
85 const std::vector<size_t> &synthNums) const;
86
87 private:
88 std::unique_ptr<std::mt19937> d_randGen;
89
90 const ROMol &d_query;
91 const SynthonSpaceSearchParams &d_params;
92 SynthonSpace &d_space;
93
94 // Generally, the search needs the query fragmented into no more than
95 // the largest number synthon sets in any reaction. Substructure search
96 // needs more than that, sometimes.
97 virtual unsigned int getNumQueryFragmentsRequired();
98 // Some of the search methods might need extra setup of the fragment
99 // sets. The FingerprintSearcher, for example, needs fingerprints
100 // for all the fragments. The SubstructureSearcher needs connector
101 // regions and information about them.
102 virtual void extraSearchSetup(
103 [[maybe_unused]] std::vector<std::vector<std::unique_ptr<ROMol>>>
104 &fragSets) {}
105
106 std::vector<std::unique_ptr<SynthonSpaceHitSet>> assembleHitSets(
107 const TimePoint *endTime, bool &timedOut, std::uint64_t &totHits);
108
109 std::vector<std::unique_ptr<SynthonSpaceHitSet>> doTheSearch(
110 std::vector<std::vector<std::unique_ptr<ROMol>>> &fragSets,
111 const TimePoint *endTime, bool &timedOut, std::uint64_t &totHits);
112
113 // Build the molecules from the synthons identified in hitsets.
114 // Checks that all the results produced match the
115 // query. Duplicates by name are not returned,
116 // but duplicate SMILES from different reactions will be.
117 // Hitsets will be re-ordered on exit.
118 void buildHits(std::vector<std::unique_ptr<SynthonSpaceHitSet>> &hitsets,
119 const TimePoint *endTime, bool &timedOut,
120 std::vector<std::unique_ptr<ROMol>> &results) const;
121 void buildAllHits(
122 const std::vector<std::unique_ptr<SynthonSpaceHitSet>> &hitsets,
123 const TimePoint *endTime, bool &timedOut,
124 std::vector<std::unique_ptr<ROMol>> &results) const;
125 void makeHitsFromToTry(
126 const std::vector<
127 std::pair<const SynthonSpaceHitSet *, std::vector<size_t>>> &toTry,
128 const TimePoint *endTime, std::vector<std::unique_ptr<ROMol>> &results,
129 std::atomic<std::int64_t> &numHitsFound) const;
130 void processToTrySet(
131 std::vector<std::pair<const SynthonSpaceHitSet *, std::vector<size_t>>>
132 &toTry,
133 const TimePoint *endTime, std::vector<std::unique_ptr<ROMol>> &results,
134 std::atomic<std::int64_t> &numHitsFound) const;
135
136 // get the subset of synthons for the given reaction to use for this
137 // enumeration.
138 std::vector<std::vector<ROMol *>> getSynthonsToUse(
139 const std::vector<boost::dynamic_bitset<>> &synthonsToUse,
140 const std::string &reaction_id) const;
141};
142
143} // namespace SynthonSpaceSearch
144} // namespace RDKit
145#endif // SYNTHONSPACESEARCHER_H
std::chrono::steady_clock Clock
std::chrono::time_point< Clock > TimePoint
contains a class for searching combinatorial libraries in Synthon format such as Enamine REAL.
SynthonSpaceSearcher(const ROMol &query, const SynthonSpaceSearchParams &params, SynthonSpace &space)
SynthonSpaceSearcher(SynthonSpaceSearcher &&other)=delete
void search(const SearchResultCallback &cb)
SynthonSpaceSearcher(const SynthonSpaceSearcher &other)=delete
virtual std::vector< std::unique_ptr< SynthonSpaceHitSet > > searchFragSet(const std::vector< std::unique_ptr< ROMol > > &fragSet, const SynthonSet &reaction) const =0
virtual bool verifyHit(ROMol &mol) const
SynthonSpaceSearcher & operator=(const SynthonSpaceSearcher &other)=delete
const SynthonSpaceSearchParams & getParams() const
std::unique_ptr< ROMol > buildAndVerifyHit(const SynthonSpaceHitSet *hitset, const std::vector< size_t > &synthNums) const
SynthonSpaceSearcher & operator=(SynthonSpaceSearcher &&other)=delete
virtual bool quickVerify(const SynthonSpaceHitSet *hitset, const std::vector< size_t > &synthNums) const
std::function< bool(std::vector< std::unique_ptr< ROMol > > &)> SearchResultCallback
Std stuff.