-
Notifications
You must be signed in to change notification settings - Fork 5
/
Main.cpp
388 lines (319 loc) · 10.7 KB
/
Main.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* 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/>.
*/
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <cstdlib>
#include <mcheck.h>
//
// Open Babel
//
#include <openbabel/obconversion.h>
#include <openbabel/mol.h>
#include <openbabel/generic.h>
#include <openbabel/atom.h>
#include <openbabel/bond.h>
#include <openbabel/groupcontrib.h>
//
// This project molecular representation
//
#include "Atom.h"
#include "Bond.h"
#include "Molecule.h"
#include "Rigid.h"
#include "Linker.h"
//
// File processing in / out.
//
#include "OBWriter.h"
#include "Options.h"
#include "Validator.h"
//
// Synthesis-Based Functionality
//
//#include "HyperGraph.h"
#include "EdgeAnnotation.h"
#include "Instantiator.h"
//#include "PebblerHyperGraph.h"
#include "Utilities.h"
#include "IdFactory.h"
#include "Constants.h"
//
// Global set of linkers and rigids read from the input files.
//
std::vector<Linker*> linkers;
std::vector<Rigid*> rigids;
void Cleanup(std::vector<Linker*>& linkers, std::vector<Rigid*>& rigids);
bool splitMolecule(std::ifstream& infile, std::string& name,
std::string& prefix, std::string& suffix)
{
prefix = "";
suffix = "";
std::string line = "";
// Eat #### in large files (if it exists)
eatWhiteLines(infile);
if (infile.peek() == '#')
{
getline(infile, line);
name = line;
eatWhiteLines(infile);
}
getline(infile, line);
prefix += line + '\n';
// Nothing left to read...
if (infile.eof() || infile.fail()) return false;
// Read the prefix (end indicated by END)
while(line.find("END") == std::string::npos)
{
getline(infile, line);
prefix += line + '\n';
}
// Add '$$$$' to the prefix.
// prefix += "\n$$$$";
// Set suffix equal to remainder of the file
while (line.find("$$$$") == std::string::npos)
{
getline(infile, line);
suffix += line + '\n';
}
return true;
}
Molecule* createLocalMolecule(OpenBabel::OBMol* mol, MoleculeT mType,
const std::string& name, std::string& suffix)
{
//
// Add the suffix as comment data to the actual OBMol object.
//
OpenBabel::OBCommentData* cData = new OpenBabel::OBCommentData();
cData->SetAttribute("Comment");
cData->SetData(suffix);
mol->SetData(cData);
//
// Create this particular molecule type based on the name of the file.
//
if (mType == LINKER)
{
return new Linker(mol, name);
}
else if (mType == RIGID)
{
return new Rigid(mol, name);
}
return 0;
}
void addMolecule(char type, Molecule* molecule)
{
if (type == 'l')
{
linkers.push_back(static_cast<Linker*>(molecule));
}
else if (type == 'r')
{
rigids.push_back(static_cast<Rigid*>(molecule));
}
}
void readMoleculeFile(const char* fileName)
{
//
// Input parser conversion functionality for Open babel
//
OpenBabel::OBConversion obConversion;
obConversion.SetInFormat("SDF");
//
// Open the file, split the current molecule into Molecule Data (prefix)
// and Our Data (Suffix)
//
std::ifstream infile;
infile.open(fileName);
std::string name = "UNKNOWN";
std::string prefix = "";
std::string suffix = "";
while(splitMolecule(infile, name, prefix, suffix))
{
//
// If the name of molecule is not given, overwrite it with the name of the file.
//
if (name == "UNKNOWN")
{
name = "#### ";
name += fileName;
name += " ####";
}
if (g_debug_output) std::cerr << "Name: " << std::endl << name << std::endl;
if (g_debug_output) std::cerr << "Prefix: " << std::endl << prefix << std::endl;
if (g_debug_output) std::cerr << "Suffix: " << std::endl << suffix << std::endl;
// Create and parse using Open Babel
OpenBabel::OBMol* mol = new OpenBabel::OBMol();
bool notAtEnd = obConversion.ReadString(mol, prefix);
// Assign all needed data to the molecule (comment data)
Molecule* local = createLocalMolecule(mol, fileName[0] == 'l' ? LINKER : RIGID,
name, suffix);
//std::cerr << *local << std::endl;
// calculate the molecular weight, H donors and acceptors and the plogp
//local->openBabelPredictLipinski();
// add to logfile
if (Molecule::isOpenBabelLipinskiCompliant(*mol))
{
std::ofstream logfile("synth_log_initial_fragments_logfile.txt",
std::ofstream::out | std::ofstream::app); // append
logfile << fileName << "\nMolWt = " << local->getMolWt() << "\n";
logfile << "HBD = " << local->getHBD() << "\n";
logfile << "HBA1 = " << local->getHBA1() << "\n";
logfile << "logP = " << local->getlogP() << "\n";
logfile << std::endl;
logfile.close();
}
else std::cerr << "Main: predictLipinski failed somehow!" << endl;
if (g_debug_output) std::cout << "Local: " << *local << "|" << std::endl;
// Add to the linker or rigid list as needed.
addMolecule(fileName[0], local);
// We don't keep a copy of the OpenBabel molecule anymore.
delete mol;
}
}
//
// Parse each input data files
//
bool readInputFiles(const Options& options)
{
for (std::vector<std::string>::const_iterator it = options.inFiles.begin();
it != options.inFiles.end(); it++)
{
if ((*it)[0] != 'l' && (*it)[0] != 'r')
{
cerr << "Unexpected file prefix: \'" << (*it)[0]
<< "\' with file " << *it << endl;
return false;
}
readMoleculeFile((*it).c_str());
}
return true;
}
int main(int argc, char** argv)
{
if (argc < 2)
{
std::cerr << "Usage: <program> [SDF-file-list] -o <output-file> -v <validation-file>"
<< " -pool <#obgen-threads>" << std::endl;
return 1;
}
//
// Remove log files from a previous run.
//
// system("rm molecules.smi");
// system("rm synth_log_initial_fragments_logfile.txt");
// system("rm ScrubAndExportSMI_logfile.txt");
// system("rm Validation_logfile.txt");
//
// Global options object.
//
Options options(argc, argv);
if (!options.parseCommandLine())
{
std::cerr << "Command-line parsing failed; exiting." << std::endl;
return 1;
}
/*
if (!options.AnalyzeEnvironment())
{
std::cerr << "Environment not set properly; exiting." << std::endl;
return 1;
}
*/
//
// Output command-line option information
//
if (Options::THREADED) std::cout << "Threaded execution." << std::endl;
else if (Options::SERIAL) std::cout << "Serial execution." << std::endl;
else
{
std::cerr << "Neither serial nor threaded specified; exiting." << std::endl;
return 1;
}
// std::cout << "SMI Comparison Level: " << Options::SMI_LEVEL_BOUND << std::endl;
std::cout << "Probability Filtration Level: "
<< Options::PROBABILITY_PRUNE_LEVEL_START << std::endl;
// Printing the specified Tanimoto value to the user.
// std::cerr << "Tanimoto Coefficient Threshold Specified: "
// << Options::TANIMOTO << std::endl;
if (!Options::SMI_ONLY)
{
std::cerr << "OBGEN output thread pool size: "
<< Options::OBGEN_THREAD_POOL_SIZE << std::endl;
}
if (!readInputFiles(options)) return 1;
//
// Bypass synthesis for acquiring information about the input fragments.
//
if (g_calculate_lipinski_descriptors_for_input_fragments_only)
{
std::cout << "Calculated Lipinski Descriptors for input fragments, now exiting early."
<< " (Flag set in Constants.h)" << std:: endl;
return 0;
}
// Output object for the nodes of the hypergraph.
OBWriter* writer = new OBWriter(Options::OBGEN_THREAD_POOL_SIZE);
if (Options::SMI_ONLY) writer->InitializeFile(options.outFileSMI);
else writer->InitializeFile(options.outFile);
if (options.validationFile == "") OBWriter::TurnValidationOff();
// The main object that performs synthesis.
Instantiator instantiator(writer, cout); //, options.validationFile);
// Instantiation build the hypergraph; this is the main data structure for the
// resultant molecules.
// Also creates the hypergraph using threaded or non-threaded techniques.
MoleculeHashHypergraph* graph;
if (Options::THREADED) graph = instantiator.ThreadedInstantiate(linkers, rigids);
else if (Options::SERIAL) graph = instantiator.SerialInstantiate(linkers, rigids);
// std::cout << "Hypergraph contains (" << graph->currentSize()
// << ", " << graph->nonKilledSize()<< ") nodes" << std::endl;
unsigned inc = instantiator.getIncluded();
unsigned exc = instantiator.getExcluded();
std::cout << "Excluded (" << exc << "); Included (" << inc << ") \t Excluded: "
<< ((double)(exc) / (exc + inc)) << "\%" << std::endl;
// External output will always have 0 molecules; uncomment for internal usage and
// accurate numbers.
// std::cout << OBWriter::NumCompliantMolecules()
// << " are Lipinski compliant molecules" << std::endl;
//
// Validate the molecules specified in the validation file (command-line -v)
//
Validator validator(OBWriter::compliantMols);
validator.Validate(options.validationFile);
// Deleting the writer will kill the thread pool.
delete writer;
// For later: pebbling
//PebblerHyperGraph<Molecule, EdgeAnnotationT> pebblerGraph = graph->GetPebblerHyperGraph();
Cleanup(linkers, rigids);
std::cerr << "Exiting the main thread." << std::endl;
//muntrace();
return 0;
}
void Cleanup(std::vector<Linker*>& linkers, std::vector<Rigid*>& rigids)
{
for (int ell = 0; ell < linkers.size(); ell++)
{
delete linkers[ell];
}
for (int r = 0; r < rigids.size(); r++)
{
delete rigids[r];
}
}