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