RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
MultithreadedSmilesMolSupplier.h
Go to the documentation of this file.
1//
2// Copyright (C) 2020 Shrey Aryan
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#ifdef RDK_BUILD_THREADSAFE_SSS
11#ifndef MULTITHREADED_SMILES_MOL_SUPPLIER
12#define MULTITHREADED_SMILES_MOL_SUPPLIER
13
14#include <iosfwd>
15#include <memory>
16#include <string>
17
19namespace RDKit {
20namespace v2 {
21namespace FileParsers {
22//! This class is still a bit experimental and the public API may change
23//! in future releases.
24class RDKIT_FILEPARSERS_EXPORT MultithreadedSmilesMolSupplier final
25 : public MultithreadedMolSupplier {
26 public:
27 explicit MultithreadedSmilesMolSupplier(
28 const std::string &fileName, const Parameters &params = Parameters(),
29 const SmilesMolSupplierParams &parseParams = SmilesMolSupplierParams());
30
31 explicit MultithreadedSmilesMolSupplier(
32 std::istream *inStream, bool takeOwnership = true,
33 const Parameters &params = Parameters(),
34 const SmilesMolSupplierParams &parseParams = SmilesMolSupplierParams());
35
36 MultithreadedSmilesMolSupplier();
37
38 ~MultithreadedSmilesMolSupplier() final { close(); };
39
40 bool getEOFHitOnRead() const final { return false; }
41
42 //! reads next record and returns whether or not EOF was hit
43 bool extractNextRecord(std::string &record, unsigned int &lineNum,
44 unsigned int &index) final;
45
46 //! parses the record and returns the resulting molecule
47 std::unique_ptr<RWMol> processMoleculeRecord(const std::string &record,
48 unsigned int lineNum) final;
49
50 private:
51 void initFromSettings(
52 bool takeOwnership, const Parameters &params,
53 const SmilesMolSupplierParams &parseParams = SmilesMolSupplierParams());
54
55 //! reads and processes the title line
56 void processTitleLine();
57
58 STR_VECT d_props; //!< vector of property names
59 SmilesMolSupplierParams d_parseParams;
60};
61} // namespace FileParsers
62} // namespace v2
63
64inline namespace v1 {
65class RDKIT_FILEPARSERS_EXPORT MultithreadedSmilesMolSupplier
66 : public MolSupplier {
67 //! this is an abstract base class to concurrently supply molecules one at a
68 //! time
69 public:
70 using ContainedType = v2::FileParsers::MultithreadedSmilesMolSupplier;
71 MultithreadedSmilesMolSupplier() {}
72 explicit MultithreadedSmilesMolSupplier(
73 const std::string &fileName, const std::string &delimiter = " \t",
74 int smilesColumn = 0, int nameColumn = 1, bool titleLine = true,
75 bool sanitize = true, unsigned int numWriterThreads = 1,
76 size_t sizeInputQueue = 5, size_t sizeOutputQueue = 5) {
77 v2::FileParsers::MultithreadedSmilesMolSupplier::Parameters params;
78 params.numWriterThreads = numWriterThreads;
79 params.sizeInputQueue = sizeInputQueue;
80 params.sizeOutputQueue = sizeOutputQueue;
81 v2::FileParsers::SmilesMolSupplierParams parseParams;
82 parseParams.delimiter = delimiter;
83 parseParams.smilesColumn = smilesColumn;
84 parseParams.nameColumn = nameColumn;
85 parseParams.titleLine = titleLine;
86 parseParams.parseParameters.sanitize = sanitize;
87
88 dp_supplier.reset(new v2::FileParsers::MultithreadedSmilesMolSupplier(
89 fileName, params, parseParams));
90 }
91
92 explicit MultithreadedSmilesMolSupplier(
93 std::istream *inStream, bool takeOwnership = true,
94 const std::string &delimiter = " \t", int smilesColumn = 0,
95 int nameColumn = 1, bool titleLine = true, bool sanitize = true,
96 unsigned int numWriterThreads = 1, size_t sizeInputQueue = 5,
97 size_t sizeOutputQueue = 5) {
98 v2::FileParsers::MultithreadedSmilesMolSupplier::Parameters params;
99 params.numWriterThreads = numWriterThreads;
100 params.sizeInputQueue = sizeInputQueue;
101 params.sizeOutputQueue = sizeOutputQueue;
102 v2::FileParsers::SmilesMolSupplierParams parseParams;
103 parseParams.delimiter = delimiter;
104 parseParams.smilesColumn = smilesColumn;
105 parseParams.nameColumn = nameColumn;
106 parseParams.titleLine = titleLine;
107 parseParams.parseParameters.sanitize = sanitize;
108
109 dp_supplier.reset(new v2::FileParsers::MultithreadedSmilesMolSupplier(
110 inStream, takeOwnership, params, parseParams));
111 }
112
113 bool getEOFHitOnRead() const {
114 if (dp_supplier) {
115 return static_cast<ContainedType *>(dp_supplier.get())->getEOFHitOnRead();
116 }
117 return false;
118 }
119
120 //! returns the record id of the last extracted item
121 //! Note: d_LastRecordId = 0, initially therefore the value 0 is returned
122 //! if and only if the function is called before extracting the first
123 //! record
124 unsigned int getLastRecordId() const {
125 PRECONDITION(dp_supplier, "no supplier");
126 return static_cast<ContainedType *>(dp_supplier.get())->getLastRecordId();
127 }
128
129 //! returns the text block for the last extracted item
130 std::string getLastItemText() const {
131 PRECONDITION(dp_supplier, "no supplier");
132 return static_cast<ContainedType *>(dp_supplier.get())->getLastItemText();
133 }
134};
135} // namespace v1
136
137} // namespace RDKit
138#endif
139#endif
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
#define RDKIT_FILEPARSERS_EXPORT
Definition export.h:221
Std stuff.
std::vector< std::string > STR_VECT
Definition Dict.h:29