forked from iqbal-lab-org/cobs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.cpp
454 lines (389 loc) · 16.7 KB
/
module.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*******************************************************************************
* python/module.cpp
*
* Copyright (c) 2019 Timo Bingmann
*
* All rights reserved. Published under the MIT License in the LICENSE file.
******************************************************************************/
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <cobs/construction/classic_index.hpp>
#include <cobs/construction/compact_index.hpp>
#include <cobs/file/classic_index_header.hpp>
#include <cobs/file/compact_index_header.hpp>
#include <cobs/query/classic_search.hpp>
#include <cobs/util/fs.hpp>
#include <cobs/util/calc_signature_size.hpp>
#include <cobs/settings.hpp>
#include <tlx/string.hpp>
/******************************************************************************/
uint64_t calc_signature_size(
uint64_t num_elements, double num_hashes,
double false_positive_rate)
{
return cobs::calc_signature_size(num_elements, num_hashes, false_positive_rate);
}
void classic_construct(
const std::string& input, const std::string& out_file,
const cobs::ClassicIndexParameters& index_params,
std::string file_type, std::string tmp_path)
{
// read file list
cobs::DocumentList filelist(input, cobs::StringToFileType(file_type));
cobs::classic_construct(filelist, out_file, tmp_path, index_params);
}
void classic_construct_list(
const cobs::DocumentList& list, const std::string& out_file,
const cobs::ClassicIndexParameters& index_params,
std::string tmp_path)
{
cobs::classic_construct(list, out_file, tmp_path, index_params);
}
void classic_construct_from_documents(
const cobs::DocumentList& list, const std::string& out_dir,
const cobs::ClassicIndexParameters& index_params)
{
cobs::classic_construct_from_documents(list, out_dir, index_params);
}
/******************************************************************************/
void compact_construct(
const std::string& input, const std::string& out_file,
const cobs::CompactIndexParameters& index_params,
std::string file_type, std::string tmp_path)
{
// read file list
cobs::DocumentList filelist(input, cobs::StringToFileType(file_type));
cobs::compact_construct(filelist, out_file, tmp_path, index_params);
}
void compact_construct_list(
const cobs::DocumentList& list, const std::string& out_file,
const cobs::CompactIndexParameters& index_params,
std::string tmp_path)
{
cobs::compact_construct(list, out_file, tmp_path, index_params);
}
/******************************************************************************/
int add(int i, int j) {
return i + j;
}
namespace py = pybind11;
PYBIND11_MODULE(cobs_index, m) {
m.doc() = R"pbdoc(
COBS Python API Reference
-------------------------
.. currentmodule:: cobs_index
.. rubric:: Classes and Types
.. autosummary::
:toctree: _generated
FileType
DocumentEntry
DocumentList
ClassicIndexParameters
CompactIndexParameters
Search
.. rubric:: Methods
.. autosummary::
:toctree: _generated
calc_signature_size
classic_construct
classic_construct_list
compact_construct
compact_construct_list
disable_cache
)pbdoc";
m.def("disable_cache",
[](bool disable) {
cobs::gopt_disable_cache = disable;
},
"disable FastA/FastQ cache files globally",
py::arg("disable") = true);
/**************************************************************************/
// DocumentList
using cobs::FileType;
py::enum_<FileType>(
m, "FileType", py::arithmetic(), "Enum of indexable file types")
.value("Any", FileType::Any, "Accept any supported file types in dir scan")
.value("Text", FileType::Text,
"Text file: read as sequential symbol stream")
.value("Cortex", FileType::Cortex, "McCortex file")
.value("KMerBuffer", FileType::KMerBuffer,
"Explicit list of k-mers for testing")
.value("Fasta", FileType::Fasta,
"FastA file, parsed as one document containing subsequences")
.value("Fastq", FileType::Fastq,
"FastQ file, parsed as one document containing subsequences")
.value("FastaMulti", FileType::FastaMulti,
"FastA multi-file, parsed as multiple documents")
.value("FastqMulti", FileType::FastqMulti,
"FastQ multi-file, parsed as multiple documents")
.export_values();
using cobs::DocumentEntry;
py::class_<DocumentEntry>(
m, "DocumentEntry",
"DocumentEntry object specifying a document to be indexed.")
.def_readwrite("path", &DocumentEntry::path_,
"file system path to document")
.def_readwrite("type", &DocumentEntry::type_,
"type of document")
.def_readwrite("name", &DocumentEntry::name_,
"name of the document")
.def_readwrite("size", &DocumentEntry::size_,
"size of the document in bytes")
.def_readwrite("subdoc_index", &DocumentEntry::subdoc_index_,
"subdocument index (for Multifile FASTA, FASTQ, etc)")
.def_readwrite("term_size", &DocumentEntry::term_size_,
"fixed term (term) size or zero")
.def_readwrite("term_count", &DocumentEntry::term_count_,
"number of terms if fixed size")
.def("num_terms",
[](DocumentEntry& e, const size_t k) {
return e.num_terms(k);
},
"number of terms",
py::arg("k"));
using cobs::DocumentList;
py::class_<DocumentList>(
m, "DocumentList",
"List of DocumentEntry objects for indexing")
.def(py::init<>(),
"default constructor, constructs an empty list.")
.def(py::init<std::string, FileType>(),
"construct and add path recursively.",
py::arg("root"),
py::arg("filter") = FileType::Any)
.def("size", &DocumentList::size,
"return number of DocumentEntry in list")
.def("add",
[](DocumentList& l, const std::string& path) {
return l.add(path);
},
"identify and add new file to DocumentList",
py::arg("path"))
.def("add_recursive",
[](DocumentList& l, const std::string& path, FileType filter) {
return l.add_recursive(path, filter);
},
"identify and add new file to DocumentList",
py::arg("path"),
py::arg("filter") = FileType::Any)
.def("sort_by_path", &DocumentList::sort_by_path,
"sort entries by path")
.def("sort_by_size", &DocumentList::sort_by_size,
"sort entries by size")
// Bare bones interface
.def("__len__", &DocumentList::size)
.def("__getitem__",
[](const DocumentList& l, size_t i) { return l[i]; })
.def("__iter__",
[](const DocumentList& l) {
return py::make_iterator(l.list().begin(), l.list().end());
},
// essential: keep object alive while iterator exists
py::keep_alive<0, 1>());
/**************************************************************************/
// ClassicIndexParameters
using cobs::ClassicIndexParameters;
py::class_<ClassicIndexParameters>(
m, "ClassicIndexParameters",
"Parameter object for classic_construct()")
.def(py::init<>(),
"constructor, fills the object with default parameters.")
.def_readwrite(
"term_size", &ClassicIndexParameters::term_size,
"length of terms / k-mers, default 31")
.def_readwrite(
"canonicalize", &ClassicIndexParameters::canonicalize,
"canonicalization flag for base pairs, default true")
.def_readwrite(
"num_hashes", &ClassicIndexParameters::num_hashes,
"number of hash functions, provided by user, default 1")
.def_readwrite(
"false_positive_rate", &ClassicIndexParameters::false_positive_rate,
"false positive rate, provided by user, default 0.3")
.def_readwrite(
"signature_size", &ClassicIndexParameters::signature_size,
"signature size, either provided by user or calculated "
"from false_positive_rate if zero, default 0")
.def_readwrite(
"mem_bytes", &ClassicIndexParameters::mem_bytes,
"memory to use bytes to create index, default 80% of RAM")
.def_readwrite(
"num_threads", &ClassicIndexParameters::num_threads,
"number of threads to use, default all cores")
.def_readwrite(
"log_prefix", &ClassicIndexParameters::log_prefix,
"log prefix (used by compact index construction), default empty")
.def_readwrite(
"clobber", &ClassicIndexParameters::clobber,
"clobber erase output directory if it exists, default false")
.def_readwrite(
"continue", &ClassicIndexParameters::continue_,
"continue in existing output directory, default false")
.def_readwrite(
"keep_temporary", &ClassicIndexParameters::keep_temporary,
"keep temporary files during construction, default false");
/**************************************************************************/
// calc_signature_size()
m.def(
"calc_signature_size", &calc_signature_size, R"pbdoc(
Calculate the number of cells in a Bloom filter with k hash functions into which num_elements are inserted such that it has expected given fpr.
:param uint64_t num_elements:
:param double num_hashes:
:param double false_positive_rate:
)pbdoc",
py::arg("num_elements"),
py::arg("num_hashes"),
py::arg("false_positive_rate"));
/**************************************************************************/
// classic_construct()
m.def(
"classic_construct", &classic_construct, R"pbdoc(
Construct a COBS Classic Index from a path of input files.
:param str input: path to the input directory or file
:param str out_file: path to the output ``.cobs_classic`` index file
:param ClassicIndexParameters index_params: instance of classic index parameter object
:param str file_type: filter input documents by file type (any, text, cortex, fasta, etc), default: any
:param str tmp_path: directory for intermediate index files, default: ``out_file`` + ``.tmp``
)pbdoc",
py::arg("input"),
py::arg("out_file"),
py::arg("index_params") = ClassicIndexParameters(),
py::arg("file_type") = "any",
py::arg("tmp_path") = "");
m.def(
"classic_construct_list", &classic_construct_list, R"pbdoc(
Construct a COBS Classic Index from a pre-populated DocumentList object.
:param DocumentList input: DocumentList object of documents to index
:param str out_file: path to the output ``.cobs_classic`` index file
:param ClassicIndexParameters index_params: instance of classic index parameter object
:param str tmp_path: directory for intermediate index files, default: ``out_file`` + ``.tmp``
)pbdoc",
py::arg("list"),
py::arg("out_file"),
py::arg("index_params") = ClassicIndexParameters(),
py::arg("tmp_path") = "");
m.def(
"classic_construct_from_documents", &classic_construct_from_documents, R"pbdoc(
Construct a COBS Classic Index from a pre-populated DocumentList object.
:param DocumentList input: DocumentList object of documents to index
:param str out_dir: path to the output directory
:param ClassicIndexParameters index_params: instance of classic index parameter object
)pbdoc",
py::arg("list"),
py::arg("out_dir"),
py::arg("index_params") = ClassicIndexParameters());
/**************************************************************************/
// CompactIndexParameters
using cobs::CompactIndexParameters;
py::class_<CompactIndexParameters>(
m, "CompactIndexParameters",
"Parameter object for compact_construct()")
.def(py::init<>(),
"constructor, fills the object with default parameters.")
.def_readwrite(
"term_size", &CompactIndexParameters::term_size,
"length of terms / k-mers, default 31")
.def_readwrite(
"canonicalize", &CompactIndexParameters::canonicalize,
"canonicalization flag for base pairs, default true")
.def_readwrite(
"num_hashes", &CompactIndexParameters::num_hashes,
"number of hash functions, provided by user, default 1")
.def_readwrite(
"false_positive_rate", &CompactIndexParameters::false_positive_rate,
"false positive rate, provided by user, default 0.3")
.def_readwrite(
"page_size", &CompactIndexParameters::page_size,
"page size (number of documents grouped into a classic index with "
"identical parameters), either provided by user or calculated "
"from number of documents if zero, default 0")
.def_readwrite(
"mem_bytes", &CompactIndexParameters::mem_bytes,
"memory to use bytes to create index, default 80% of RAM")
.def_readwrite(
"num_threads", &CompactIndexParameters::num_threads,
"number of threads to use, default all cores")
.def_readwrite(
"clobber", &CompactIndexParameters::clobber,
"clobber erase output directory if it exists, default false")
.def_readwrite(
"continue", &CompactIndexParameters::continue_,
"continue in existing output directory, default false")
.def_readwrite(
"keep_temporary", &CompactIndexParameters::keep_temporary,
"keep temporary files during construction, default false");
/**************************************************************************/
// compact_construct()
m.def(
"compact_construct", &compact_construct, R"pbdoc(
Construct a COBS Compact Index from a path of input files.
:param str input: path to the input directory or file
:param str out_file: path to the output ``.cobs_compact`` index file
:param CompactIndexParameters index_params: instance of compact index parameter object
:param str file_type: filter input documents by file type (any, text, cortex, fasta, etc), default: any
:param str tmp_path: directory for intermediate index files, default: ``out_file`` + ``.tmp``
)pbdoc",
py::arg("input"),
py::arg("out_file"),
py::arg("index_params") = CompactIndexParameters(),
py::arg("file_type") = "any",
py::arg("tmp_path") = "");
m.def(
"compact_construct_list", &compact_construct_list, R"pbdoc(
Construct a COBS Compact Index from a pre-populated DocumentList object.
:param DocumentList input: DocumentList object of documents to index
:param str out_file: path to the output ``.cobs_compact`` index file
:param CompactIndexParameters index_params: instance of compact index parameter object
:param str tmp_path: directory for intermediate index files, default: ``out_file`` + ``.tmp``
)pbdoc",
py::arg("list"),
py::arg("out_file"),
py::arg("index_params") = CompactIndexParameters(),
py::arg("tmp_path") = "");
/**************************************************************************/
// SearchResult
using cobs::SearchResult;
py::class_<SearchResult>(
m, "SearchResult",
"Return objects for Search")
.def(py::init<>(),
"constructor, fills the object with default parameters.")
.def_readwrite(
"doc_name", &SearchResult::doc_name,
"document name string")
.def_readwrite(
"score", &SearchResult::score,
"score of document");
/**************************************************************************/
// Search (renamed from cobs::ClassicSearch)
using Search = cobs::ClassicSearch;
py::class_<Search>(
m, "Search",
"Search object to run queries on COBS indices")
.def(py::init<std::string, bool>(),
py::arg("path"),
py::arg("load_complete_index") = false,
"constructor, loads the given classic or compact index file.")
.def(
"search",
[](Search& s,
const std::string& query, double threshold, size_t num_results, uint16_t step)
{
// lambda to allocate and return vector
std::vector<cobs::SearchResult> result;
s.search(query, result, threshold, num_results, step);
return result;
},
"search index for query returning vector of matches",
py::arg("query"),
py::arg("threshold") = 0.0,
py::arg("num_results") = 0,
py::arg("step") = 1);
/**************************************************************************/
#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
m.attr("__version__") = "dev";
#endif
}
/******************************************************************************/