Debian Codegraph @

avogadro/qtplugins/openbabel/obprocess.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_QTPLUGINS_OBPROCESS_H
7 #define AVOGADRO_QTPLUGINS_OBPROCESS_H
8
9 #include <avogadro/core/array.h>
10
11 #include <QtCore/QMap>
12 #include <QtCore/QObject>
13 #include <QtCore/QStringList>
14
15 class QProcess;
16
17 namespace Avogadro {
18 namespace QtPlugins {
19
20 /**
21 * @brief The OBProcess class provides an interface to the `obabel` executable,
22 * which is run in a separate process.
23 *
24 * The `obabel` executable used by this class can be overridden by setting the
25 * AVO_OBABEL_EXECUTABLE environment variable.
26 */
27 class OBProcess : public QObject
28 {
29 Q_OBJECT
30 public:
31 explicit OBProcess(QObject* parent_ = nullptr);
32
33 /**
34 * @return The output of obabel -V.
35 */
36 QString version();
37
38 /**
39 * @name Process Management
40 * Methods, slots, and signals used to interact with the OpenBabel process.
41 * @{
42 */
43 public:
44 /**
45 * The `obabel` executable used by the process.
46 */
47 QString obabelExecutable() const { return m_obabelExecutable; }
48
49 /**
50 * @return True if the process is in use, false otherwise.
51 */
52 bool inUse() const { return m_processLocked; }
53
54 public slots:
55 /**
56 * Abort any currently running processes.
57 *
58 * This will cause aborted() to be emitted, but not any of the
59 * operation-specific "finished" signals.
60 */
61 void abort();
62
63 /**
64 * Called when an error in the process occurs.
65 */
66 void obError();
67
68 signals:
69 /**
70 * Emitted when the abort() method has been called.
71 */
72 void aborted();
73
74 // end Process Management doxygen group
75 /**@}*/
76
77 /**
78 * @name File Format Support
79 * Query the obabel executable for supported file formats.
80 * @{
81 */
82 public slots:
83 /**
84 * Request a list of all supported input formats from obabel.
85 *
86 * After calling this method, the queryReadFormatsFinished signal will be
87 * emitted. This method executes
88 *
89 * `obabel -L formats read`
90 *
91 * and parses the output into a map (keys are format descriptions, values are
92 * format extensions).
93 *
94 * If an error occurs, queryReadFormatsFinished will be emitted with an empty
95 * argument.
96 *
97 * @return True if the process started successfully, false otherwise.
98 */
99 bool queryReadFormats();
100
101 /**
102 * Request a list of all supported output formats from obabel.
103 *
104 * After calling this method, the queryWriteFormatsFinished signal will be
105 * emitted. This method executes
106 *
107 * `obabel -L formats write`
108 *
109 * and parses the output into a map (keys are format descriptions, values are
110 * format extensions).
111 *
112 * If an error occurs, queryWriteFormatsFinished will be emitted with an empty
113 * argument.
114 *
115 * @return True if the process started successfully, false otherwise.
116 */
117 bool queryWriteFormats();
118
119 signals:
120 /**
121 * Triggered when the process started by queryReadFormats() completes.
122 * @param readFormats The input file formats that OpenBabel understands. Keys
123 * are non-translated (english), human-readable descriptions of the formats,
124 * and the values are the corresponding file extensions.
125 *
126 * @note readFormats will usually contain more than one extensions per format,
127 * so accessing the values with QMap::values() (instead of QMap::value()) is
128 * required.
129 *
130 * If an error occurs, readFormats will be empty.
131 */
132 void queryReadFormatsFinished(QMultiMap<QString, QString> readFormats);
133
134 /**
135 * Triggered when the process started by queryWriteFormats() completes.
136 * @param writeFormats The file formats that OpenBabel can write. Keys
137 * are non-translated (english), human-readable descriptions of the formats,
138 * and the values are the corresponding file extensions.
139 *
140 * @note writeFormats will usually contain more than one extensions per
141 * format, so accessing the values with QMap::values() (instead of
142 * QMap::value()) is required.
143 *
144 * If an error occurs, writeFormats will be empty.
145 */
146 void queryWriteFormatsFinished(QMultiMap<QString, QString> writeFormats);
147
148 private slots:
149 void queryReadFormatsPrepare();
150 void queryWriteFormatsPrepare();
151
152 // end File Format Support doxygen group
153 /**@}*/
154
155 /**
156 * @name Format Operations
157 * Operations that manipulate molecular representations.
158 * @{
159 */
160 public slots:
161 /**
162 * Convert the text representation in @a input from @a inFormat to
163 * @a outFormat.
164 *
165 * @param input Text representation of molecule in @a inFormat format.
166 * @param inFormat File extension corresponding to input format
167 * (see `obabel -L formats`).
168 * @param outFormat File extension corresponding to output format.
169 * @param options Additional options passed to obabel.
170 *
171 * After calling this method, the convertFinished signal will be emitted to
172 * indicate return status along with the requested representation of the
173 * molecule.
174 *
175 * The conversion is performed as:
176 * `obabel -i<inFormat> -o<outFormat> <options> < input > output`
177 *
178 * @return True if the process started successfully, false otherwise.
179 */
180 bool convert(const QByteArray& input, const QString& inFormat,
181 const QString& outFormat,
182 const QStringList& options = QStringList());
183
184 /**
185 * Convert the file @a filename from @a inFormat to @a outFormat.
186 *
187 * @param filename File containing molecule representation in @a inFormat
188 * format.
189 * @param inFormat File extension corresponding to input format
190 * (see `obabel -L formats`).
191 * @param outFormat File extension corresponding to output format.
192 * @param options Additional options passed to obabel.
193 *
194 * After calling this method, the convertFinished signal will be emitted to
195 * indicate return status along with the requested representation of the
196 * molecule.
197 *
198 * The conversion is performed as:
199 * `obabel -i<inFormat> <filename> -o<outFormat> <options> > output`
200 *
201 * @return True if the process started successfully, false otherwise.
202 */
203 bool convert(const QString& filename, const QString& inFormat,
204 const QString& outFormat,
205 const QStringList& options = QStringList());
206
207 signals:
208 /**
209 * Emitted after a call to convert() finishes.
210 * @param output The molecule in CML format, or an empty QByteArray if an e
211 * error occurred.
212 */
213 void convertFinished(const QByteArray& output);
214
215 private slots:
216 void convertPrepareOutput();
217
218 // end Format Operations doxygen group
219 /**@}*/
220
221 /**
222 * @name Force Fields
223 * Methods, signals, and slots pertaining to force fields (e.g. geometry
224 * optimizations).
225 * @{
226 */
227
228 public slots:
229 /**
230 * Request a list of all supported force fields from obabel.
231 *
232 * After calling this method, the queryForceFieldsFinished signal will be
233 * emitted. This method executes
234 *
235 * `obabel -L forcefields`
236 *
237 * and parses the output.
238 *
239 * If an error occurs, queryForceFieldsFinished will be emitted with an empty
240 * argument.
241 *
242 * @return True if the process started successfully, false otherwise.
243 */
244 bool queryForceFields();
245
246 signals:
247 /**
248 * Triggered when the process started by queryForceFields() completes.
249 * @param forceFields The force fields supported by OpenBabel. Keys
250 * are unique identifiers for the force fields, and the values are
251 * non-translated (english), human-readable descriptions.
252 *
253 * If an error occurs, forceFields will be empty.
254 */
255 void queryForceFieldsFinished(const QMultiMap<QString, QString>& forceFields);
256
257 private slots:
258 void queryForceFieldsPrepare();
259
260 public slots:
261 /**
262 * Request that obabel optimize a molecular structure using its minimize
263 * operation.
264 * @param cml A Chemical Markup Language representation of the molecule.
265 * @param options Options for the optimization. See OBForceFieldDialog::prompt
266 * for an easy method way to get the options from the user.
267 *
268 * After calling this method, the optimizeGeometryStatusUpdate signal will be
269 * emitted periodically to indicate the optimization's progress. Once the
270 * optimization finishes, optimizeGeometryFinished will be emitted with the
271 * result of the optimization.
272 *
273 * The optimization is started with, e.g.
274 * `obabel -icml -ocml --minimize <options>`
275 *
276 * The standard output is recorded and returned by optimizeGeometryFinished.
277 * If @a options contains `--log`, the obabel process's standard error stream
278 * is monitored for the data used in the optimizeGeometryStatusUpdate progress
279 * updates.
280 *
281 * @return True if the process started successfully, false otherwise.
282 */
283 bool optimizeGeometry(const QByteArray& cml, const QStringList& options,
284 std::string format = "cml");
285 bool generateConformers(const QByteArray& cml, const QStringList& options,
286 std::string format = "cml");
287 signals:
288 /**
289 * Emitted with the standard output of the process when it finishes.
290 * If an error occurs, the argument will not be valid CML.
291 */
292 void optimizeGeometryFinished(const QByteArray& cml);
293 void generateConformersFinished(const QByteArray& cml);
294 /**
295 * Emitted every 10 steps of the optimization to indicate the current
296 * progress.
297 * @param step The current step of the minimization algorithm.
298 * @param maxSteps The maximum number of steps before the minimization is
299 * aborted.
300 * @param currentEnergy The energy of the molecule at the current step.
301 * @param lastEnergy The energy of the molecule at the previous minimization
302 * step.
303 */
304 void optimizeGeometryStatusUpdate(int step, int maxSteps,
305 double currentEnergy, double lastEnergy);
306
307 void conformerStatusUpdate(int step, int maxSteps, double currentEnergy,
308 double lastEnergy);
309 private slots:
310 void optimizeGeometryPrepare();
311 void optimizeGeometryReadLog();
312 void conformerPrepare();
313 void conformerReadLog();
314
315 // end Force Fields doxygen group
316 /**@}*/
317
318 /**
319 * @name Charge Models
320 * Methods, signals, and slots pertaining to partial charges
321 * @{
322 */
323
324 public slots:
325 /**
326 * Request a list of all supported charge models from obabel.
327 *
328 * After calling this method, the queryChargesFinished signal will be
329 * emitted. This method executes
330 *
331 * `obabel -L charges`
332 *
333 * and parses the output.
334 *
335 * If an error occurs, queryChargesFinished will be emitted with an empty
336 * argument.
337 *
338 * @return True if the process started successfully, false otherwise.
339 */
340 bool queryCharges();
341
342 signals:
343 /**
344 * Triggered when the process started by queryCharges() completes.
345 * @param charges The charge models supported by OpenBabel. Keys
346 * are unique identifiers for the charge models, and the values are
347 * non-translated (english), human-readable descriptions.
348 *
349 * If an error occurs, charges will be empty.
350 */
351 void queryChargesFinished(const QMultiMap<QString, QString>& charges);
352
353 private slots:
354 void queryChargesPrepare();
355
356 public slots:
357 /**
358 * Calculate the partial charges on the molecule @a input using @a type
359 *
360 * @param input String containing molecule representation in @a inFormat
361 * format.
362 * @param inFormat File extension corresponding to input format
363 * (see `obabel -L formats`).
364 * @param type The charge model to use (e.g., MMFF94, gasteiger, etc.)
365 *
366 * After calling this method, the chargesFinished signal will be emitted to
367 * indicate return status along with the charges as text.
368 *
369 * The process is performed as:
370 * `obabel -i<inFormat> -onul --partialcharge <type> --print < input >
371 * output`
372 *
373 * @return True if the process started successfully, false otherwise.
374 */
375 bool calculateCharges(const QByteArray& input,
376 const std::string& inFormat = "cml",
377 const std::string& type = "mmff94");
378
379 private slots:
380 void chargesPrepareOutput();
381
382 signals:
383 /**
384 * Emitted after a call to calculateCharges() finishes.
385 * @param charges the set of partial charges
386 */
387 void chargesFinished(const Core::Array<double>& charges);
388 // end Charge Models doxygen group
389 /**@}*/
390
391 private:
392 /**
393 * Internal method for launching the obabel executable.
394 * @param options List of options to pass to QProcess::start
395 * @param receiver A QObject subclass instance that has @a slot as a member.
396 * @param slot The slot to call when completed. Must have no arguments.
397 * @param obabelStdin Standard input for the obabel process (optional).
398 *
399 * Call this method like so:
400 @code
401 QStringList options;
402 <Populate options>
403 executeObabel(options, this, SLOT(mySlot()));
404 @endcode
405 *
406 * @a slot will be connected to QProcess::finished(int) and
407 * QProcess::error(QProcess::ProcessError) with @a receiver as receiver and
408 * @a m_process as sender. @a m_process is then started using
409 * m_obabelExecutable and options as arguments. If provided, the obabelStdin
410 * data will be written to the obabel stdin channel.
411 */
412 void executeObabel(const QStringList& options, QObject* receiver = nullptr,
413 const char* slot = nullptr,
414 const QByteArray& obabelStdin = QByteArray());
415
416 void resetState();
417
418 // Not thread safe -- just uses a bool.
419 bool tryLockProcess()
420 {
421 if (m_processLocked)
422 return false;
423 m_processLocked = true;
424 resetState();
425 return true;
426 }
427
428 void releaseProcess() { m_processLocked = false; }
429
430 bool m_processLocked;
431 bool m_aborted;
432 QProcess* m_process;
433 QString m_obabelExecutable;
434
435 // Optimize geometry ivars:
436 int m_optimizeGeometryMaxSteps;
437 unsigned m_maxConformers;
438 QString m_optimizeGeometryLog;
439 };
440
441 } // namespace QtPlugins
442 } // namespace Avogadro
443
444 #endif // AVOGADRO_QTPLUGINS_OBPROCESS_H