forked from wcatykid/Graph-Based-Molecular-Synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instantiator.h
190 lines (145 loc) · 5.99 KB
/
Instantiator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* This file is part of esynth.
*
* esynth is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* esynth is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with esynth. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _INSTANTIATOR_GUARD
#define _INSTANTIATOR_GUARD 1
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <memory>
#include <pthread.h>
#include "Molecule.h"
#include "Rigid.h"
#include "Linker.h"
#include "MoleculeHashHypergraph.h"
#include "EdgeAnnotation.h"
#include "IdFactory.h"
#include "OBWriter.h"
#include "TimedHashMap.h"
#include "bloom_filter.hpp"
// threads require a struct to pass multiple arguments
struct Instantiator_ProcessLevel_Thread_Args
{
int m; // level number
MoleculeHashHypergraph* graph; // The single hypergraph used by all levels.
void* this_pointer; // this pointer to calling class (Instantiator)
};
class Instantiator
{
private:
// Create necessary synthesis containers and init the linkers and rigids
void InitializeBaseMolecules(const std::vector<Rigid*>& rigids,
const std::vector<Linker*>& linkers,
std::vector<Molecule*>& baseMolecules);
void InitializeSynthesis(std::vector<Linker*>& linkers, std::vector<Rigid*>& rigids);
// To generate unique molecular ids
IdFactory moleculeIDFactory;
// Contains all processed clauses and relationships amongst the clauses
MoleculeHashHypergraph* graph;
// debug stream
std::ostream& ds;
void HandleNewMolecules(std::queue<Molecule*>& worklist,
pthread_mutex_t* wl_lock,
bloom_filter* const levelFilter,
std::vector<EdgeAggregator*>* newEdges);
void SynthesizeWithMolecule(const Molecule* const currentMol, int level);
void AddEdge(const std::vector<unsigned int>& antecedent,
unsigned int consequent,
EdgeAnnotationT* const annotation);
std::pair<unsigned int, bool> AddNode(MinimalMolecule* const mol, unsigned int level);
/*void ProcessLevel(std::vector<Molecule*>& baseMols,
std::queue<Molecule*>& inSet,
std::queue<Molecule*>& outSet,
pthread_mutex_t& in_lock,
pthread_mutex_t& out_lock,
bool* previousLevelComplete,
bool* thisLevelComplete);*/
void InitOverallFilter();
void InitLevelFilters();
// Lock the hypergraph (for adding)
pthread_mutex_t graph_lock;
// A list of locks for all of the producer-consumer queues.
pthread_mutex_t* queue_locks;
// All of the hierarchical level threads.
pthread_t* queue_threads;
// Indicator that a level has completed processing.
bool* completed_level;
// The actual producer-consumer queue for each level.
std::queue<Molecule*>* level_queues;
// A bloom filter for each level beyond.
std::vector<bloom_filter*> filters;
// A bloom filter for each level beyond.
bloom_filter* overall_filter;
// array of args for each level thread
Instantiator_ProcessLevel_Thread_Args *arg_pointer;
// set of linkers and rigids (1-molecules)
std::vector<Molecule*> baseMolecules;
// Molecules per level (count) for debug
int* moleculeLevelCount;
unsigned long long overallMoleculeCount;
// For output of molecules on the fly.
OBWriter* const writer;
// How many molecules were excluded using probabilistic techniques
unsigned excluded;
// The maximum number of molecules allowable in a queue.
static const unsigned MAX_QUEUE_SIZES[22];
// The expected number of molecules in a level, at maximum.
static const unsigned long long LEVEL_SIZES[22];
// On the fly validation of molecules synthesized;
// Exits if the validation molecule was generated.
void Validate(const std::string& syn_smi) const;
// The molecule to validate
std::string validation_smi;
public:
Instantiator(OBWriter*const obWriter, std::ostream& out = std::cout);
~Instantiator()
{
delete[] level_queues;
delete[] moleculeLevelCount;
delete graph;
// Delete the Bloom filters.
delete overall_filter;
for (std::vector<bloom_filter*>::iterator it = filters.begin(); it != filters.end(); it++)
{
if (*it != 0) delete *it;
}
filters.clear();
//
// Threaded destruction
//
if (Options::THREADED)
{
delete[] queue_locks;
delete[] queue_threads;
delete[] completed_level;
delete[] arg_pointer;
}
}
// Main instantiation function for all linkers and rigidss; worklist technique to construct the graph
MoleculeHashHypergraph* ThreadedInstantiate(std::vector<Linker*>& linkers,
std::vector<Rigid*>& rigids);
// Main instantiation function for all linkers and rigidss; worklist technique to construct the graph
MoleculeHashHypergraph* SerialInstantiate(std::vector<Linker*>& linkers,
std::vector<Rigid*>& rigids);
// Recursive assistant for serial processing.
void SerialInstantiateHelper(int level, unsigned& processedMols);
unsigned getIncluded() const { return overallMoleculeCount; }
unsigned getExcluded() const { return excluded; }
// thread must be implemented as friend class
friend void *ProcessLevel(void * args); // worker thread
};
#endif