$darkmode
DENOPTIM
FragmenterTask.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.fragmenter;
20
21import java.io.File;
22import java.io.IOException;
23import java.util.logging.FileHandler;
24import java.util.logging.Handler;
25import java.util.logging.Level;
26import java.util.logging.Logger;
27import java.util.logging.SimpleFormatter;
28
29import denoptim.io.DenoptimIO;
30import denoptim.programs.fragmenter.FragmenterParameters;
31import denoptim.task.Task;
32import denoptim.utils.TaskUtils;
33
46public class FragmenterTask extends Task
47{
51 private File inputFile;
52
57 private File preliminaryResults;
58
62 protected String results = null;
63
68
72 private Logger logger = null;
73
77 private String logFilePathname = "unset";
78
79//------------------------------------------------------------------------------
80
92 int id) throws SecurityException, IOException
93 {
95 this.id = id;
96 this.inputFile = inputFile;
97 this.settings = settings;
98 this.logger = Logger.getLogger("FragmenterTask-"+id);
99
100 //Create the task-specific logger
101 int n = logger.getHandlers().length;
102 for (int i=0; i<n; i++)
103 {
104 logger.removeHandler(logger.getHandlers()[0]);
105 }
106
107 this.logFilePathname = getLogFileName(settings,id);
108 FileHandler fileHdlr = new FileHandler(logFilePathname);
109 SimpleFormatter formatterTxt = new SimpleFormatter();
110 fileHdlr.setFormatter(formatterTxt);
111 logger.setUseParentHandlers(false);
112 logger.addHandler(fileHdlr);
113 logger.setLevel(settings.getLogger().getLevel());
114 String header = "Started logging for FragmenterTask-"+id ;
115 logger.log(Level.INFO,header);
116 }
117
118//------------------------------------------------------------------------------
119
128 {
129 return settings.getWorkDirectory() + DenoptimIO.FS
130 + "structuresBatch-" + i + ".sdf";
131 }
132
133//------------------------------------------------------------------------------
134
138 public String getLogFilePathname()
139 {
140 return logFilePathname;
141 }
142
143//------------------------------------------------------------------------------
144
148 public String getResultFile()
149 {
150 return results;
151 }
152
153//------------------------------------------------------------------------------
154
164 {
165 return settings.getWorkDirectory() + DenoptimIO.FS
166 + "structuresNoMissingAtoms-" + i + ".sdf";
167 }
168
169//------------------------------------------------------------------------------
170
179 {
180 return settings.getWorkDirectory() + DenoptimIO.FS
181 + "structuresPreFiltered-" + i + ".sdf";
182 }
183
184//------------------------------------------------------------------------------
185
194 {
195 return settings.getWorkDirectory() + DenoptimIO.FS
196 + "Fragments-" + i + ".sdf";
197 }
198
199//------------------------------------------------------------------------------
200
208 {
209 return settings.getWorkDirectory() + DenoptimIO.FS
210 + "Fragments.sdf";
211 }
212
213//------------------------------------------------------------------------------
214
222 {
223 return settings.getWorkDirectory() + DenoptimIO.FS
224 + "Results.sdf";
225 }
226
227//------------------------------------------------------------------------------
228
235 private static String getLogFileName(FragmenterParameters settings, int i)
236 {
237 return settings.getWorkDirectory() + DenoptimIO.FS
238 + "FragmenterTask-" + i + ".log";
239 }
240
241//------------------------------------------------------------------------------
242
247 @Override
248 public Object call() throws Exception
249 {
250 // Preliminary check for missing atoms by elemental analysis
252 {
253 logger.log(Level.INFO,"Starting elemental analysis");
254 File newResultsFile = new File(getConfirmedFormulaFileName(settings,
255 id));
257 newResultsFile, logger);
258 preliminaryResults = newResultsFile;
259 } else {
261 }
262
263 // Pre-fragmentation filter
264 if (settings.doPreFilter())
265 {
266 logger.log(Level.INFO,"Pre-filtering structures");
267 File newResultsFile = new File(getPreFilteredFileName(settings, id));
269 settings.getPreFiltrationSMARTS(), newResultsFile, logger);
270 preliminaryResults = newResultsFile;
271 }
272
273 // Fragmentation of structures
275 {
276 logger.log(Level.INFO,"Fragmentation of structures");
277 File newResultsFile = new File(getFragmentsFileName(settings, id));
278 boolean producedSomething = FragmenterTools.fragmentation(inputFile,
279 settings, newResultsFile,logger);
280 if (!producedSomething)
281 {
282 completed = true;
283 return null;
284 }
285 preliminaryResults = newResultsFile;
286 }
287
289 {
290 logger.log(Level.INFO,"Filtering fragments");
291 File newResultsFile = new File(getFragmentsFileName(settings, id));
293 newResultsFile, logger);
294 preliminaryResults = newResultsFile;
295 }
296
297 // Final message
298 results = preliminaryResults.getAbsolutePath();
299 if (settings.getNumTasks()>1)
300 {
301 logger.log(Level.INFO,"Fragmenter task " + id + " completed.");
302 } else {
303 logger.log(Level.INFO,"Results available in "+results);
304 }
305
306 // We stop the logger's file handler to remove the lock file.
307 for (Handler h : logger.getHandlers())
308 {
309 if (h instanceof FileHandler) {
310 logger.removeHandler(h);
311 h.close();
312 }
313 }
314
315 completed = true;
316 return results;
317 }
318
319//------------------------------------------------------------------------------
320
321}
Task that performs the various steps in the process that prepares chemical structured to be chopped,...
FragmenterParameters settings
Settings for the calculation of the fitness.
File preliminaryResults
File containing the latest results, though not the final results.
String logFilePathname
Pathname to thread-specific log.
static String getLogFileName(FragmenterParameters settings, int i)
Builds the pathname of the log file for this task.
String results
The data structure holding the results of this task.
static String getFragmentsFileName(FragmenterParameters settings, int i)
Builds the pathname of the structure file meant to hold fragments resulting from this task.
static String getPreFilteredFileName(FragmenterParameters settings, int i)
Builds the pathname of the structure file meant to hold structures that survive the pre-filtering ste...
static String getInputFileName(FragmenterParameters settings, int i)
Builds the pathname of the structure file meant to be the input for this task.
static String getFragmentsFileName(FragmenterParameters settings)
Builds the pathname of the structure file meant to hold fragments resulting from all tasks.
Logger logger
Logger for this task.
Object call()
Performs the whatever work has to be done by this task.
FragmenterTask(File inputFile, FragmenterParameters settings, int id)
Create a task by giving the input file (i.e., containing structures to work with) and the configurati...
static String getConfirmedFormulaFileName(FragmenterParameters settings, int i)
Builds the pathname of the structure file meant to hold structures that survive the comparison of str...
static String getResultsFileName(FragmenterParameters settings)
Builds the pathname of the structure file meant to hold results that are not necessarily fragments.
File inputFile
File containing the input for this task (i.e., a structure file)
static void checkElementalAnalysisAgainstFormula(File input, File output, Logger logger)
Processes all molecules analyzing the composition of the structure in the chemical representation as ...
static void filterStrucutresBySMARTS(File input, Set< String > smarts, File output, Logger logger)
Removes from the structures anyone that matches any of the given SMARTS queries.
static void manageFragmentCollection(File input, FragmenterParameters settings, File output, Logger logger)
Management of fragments: includes application of fragment filters, rejection rules,...
static boolean fragmentation(File input, FragmenterParameters settings, File output, Logger logger)
Performs fragmentation according to the given cutting rules.
String getWorkDirectory()
Gets the pathname to the working directory.
Logger getLogger()
Get the name of the program specific logger.
Parameters controlling execution of the fragmenter.
boolean doFiltering
Flag requesting to do post-fragmentation processing of fragments, i.e., application of all filtration...
boolean doFragmentation
Fag requesting the fragmentation of the structures.
A task that can throw exceptions.
Definition: Task.java:30
int id
A user-assigned id for this task.
Definition: Task.java:68
boolean completed
Flag about completion.
Definition: Task.java:41
Utilities for tasks.
Definition: TaskUtils.java:31
static synchronized int getUniqueTaskIndex()
Unique counter for tasks.
Definition: TaskUtils.java:41