RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
GeneralFileReader.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#ifndef GENERAL_FILE_READER_H
11#define GENERAL_FILE_READER_H
13#include <RDStreams/streams.h>
14
15#include <boost/algorithm/string.hpp>
16#include <iostream>
17#include <memory>
18#include <string>
19#include <vector>
20
21#include "MolSupplier.h"
24
25namespace RDKit {
26namespace GeneralMolSupplier {
28 bool takeOwnership = true;
29 bool sanitize = true;
30 bool removeHs = true;
31 bool strictParsing = true;
32
33 std::string delimiter = "\t";
34 int smilesColumn = 0;
35 int nameColumn = 1;
36 bool titleLine = true;
37
38 std::string nameRecord = "";
39 int confId2D = -1;
40 int confId3D = 0;
41
42 unsigned int numWriterThreads = 0;
43};
44//! current supported file formats
45const std::vector<std::string> supportedFileFormats{
46 "sdf", "mae", "maegz", "sdfgz", "smi", "csv", "txt", "tsv", "tdt"};
47//! current supported compression formats
48const std::vector<std::string> supportedCompressionFormats{"gz"};
49
50//! given file path determines the file and compression format
51//! returns true on success, otherwise false
52//! Note: Error handeling is done in the getSupplier method
53
54void determineFormat(const std::string path, std::string& fileFormat,
55 std::string& compressionFormat) {
56 //! filename without compression format
57 std::string basename;
58 //! Special case maegz.
59 //! NOTE: also supporting case-insensitive filesystems
60 if (boost::algorithm::iends_with(path, ".maegz")) {
61 fileFormat = "mae";
62 compressionFormat = "gz";
63 return;
64 } else if (boost::algorithm::iends_with(path, ".sdfgz")) {
65 fileFormat = "sdf";
66 compressionFormat = "gz";
67 return;
68 } else if (boost::algorithm::iends_with(path, ".gz")) {
69 compressionFormat = "gz";
70 basename = path.substr(0, path.size() - 3);
71 } else if (boost::algorithm::iends_with(path, ".zst") ||
72 boost::algorithm::iends_with(path, ".bz2") ||
73 boost::algorithm::iends_with(path, ".7z")) {
74 throw BadFileException(
75 "Unsupported compression extension (.zst, .bz2, .7z) given path: " +
76 path);
77 } else {
78 basename = path;
80 }
81 for (auto const& suffix : supportedFileFormats) {
82 if (boost::algorithm::iends_with(basename, "." + suffix)) {
84 return;
85 }
86 }
87 throw BadFileException(
88 "Unsupported structure or compression extension given path: " + path);
89}
90
91//! returns a new MolSupplier object based on the file name instantiated
92//! with the relevant options provided in the SupplierOptions struct
93/*!
94 <b>Note:</b>
95 - the caller owns the memory and therefore the pointer must be deleted
96*/
97
98std::unique_ptr<MolSupplier> getSupplier(const std::string& path,
99 const struct SupplierOptions& opt) {
100 std::string fileFormat = "";
101 std::string compressionFormat = "";
102 //! get the file and compression format form the path
104
105 std::istream* strm;
106 if (compressionFormat.empty()) {
107 strm = new std::ifstream(path.c_str(), std::ios::in | std::ios::binary);
108 } else {
109#ifdef RDK_USE_BOOST_IOSTREAMS
110 strm = new gzstream(path);
111#else
112 throw BadFileException(
113 "compressed files are only supported if the RDKit is built with boost::iostreams support");
114#endif
115 }
116
117 //! Dispatch to the appropriate supplier
118 if (fileFormat == "sdf") {
119#ifdef RDK_BUILD_THREADSAFE_SSS
120 if (opt.numWriterThreads > 0) {
122 strm, true, opt.sanitize, opt.removeHs, opt.strictParsing,
123 opt.numWriterThreads);
124 std::unique_ptr<MolSupplier> p(sdsup);
125 return p;
126 }
127#endif
129 strm, true, opt.sanitize, opt.removeHs, opt.strictParsing);
130 std::unique_ptr<MolSupplier> p(sdsup);
131 return p;
132 }
133
134 else if (fileFormat == "smi" || fileFormat == "csv" || fileFormat == "txt" ||
135 fileFormat == "tsv") {
136#ifdef RDK_BUILD_THREADSAFE_SSS
137 if (opt.numWriterThreads > 0) {
140 strm, true, opt.delimiter, opt.smilesColumn, opt.nameColumn,
141 opt.titleLine, opt.sanitize, opt.numWriterThreads);
142 std::unique_ptr<MolSupplier> p(smsup);
143 return p;
144 }
145#endif
147 new SmilesMolSupplier(strm, true, opt.delimiter, opt.smilesColumn,
148 opt.nameColumn, opt.titleLine, opt.sanitize);
149 std::unique_ptr<MolSupplier> p(smsup);
150 return p;
151 }
152#ifdef RDK_BUILD_MAEPARSER_SUPPORT
153 else if (fileFormat == "mae") {
155 new MaeMolSupplier(strm, true, opt.sanitize, opt.removeHs);
156 std::unique_ptr<MolSupplier> p(maesup);
157 return p;
158 }
159#endif
160 else if (fileFormat == "tdt") {
162 strm, true, opt.nameRecord, opt.confId2D, opt.confId3D, opt.sanitize);
163 std::unique_ptr<MolSupplier> p(tdtsup);
164 return p;
165 }
166 throw BadFileException("Unsupported file format: " + fileFormat);
167}
168
169} // namespace GeneralMolSupplier
170} // namespace RDKit
171#endif
used by various file parsing classes to indicate a bad file
lazy file parser for Smiles tables
lazy file parser for TDT files
const std::vector< std::string > supportedCompressionFormats
current supported compression formats
const std::vector< std::string > supportedFileFormats
current supported file formats
void determineFormat(const std::string path, std::string &fileFormat, std::string &compressionFormat)
std::unique_ptr< MolSupplier > getSupplier(const std::string &path, const struct SupplierOptions &opt)
Std stuff.
bool rdvalue_is(const RDValue_cast_t)