-
Notifications
You must be signed in to change notification settings - Fork 78
/
revindex.rs
271 lines (232 loc) · 7.62 KB
/
revindex.rs
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
use std::slice;
use camino::Utf8PathBuf as PathBuf;
use crate::ffi::index::SourmashSearchResult;
use crate::ffi::minhash::SourmashKmerMinHash;
use crate::ffi::signature::SourmashSignature;
use crate::ffi::utils::{ForeignObject, SourmashStr};
use crate::index::revindex::mem_revindex::RevIndex;
use crate::index::Index;
use crate::prelude::*;
use crate::signature::{Signature, SigsTrait};
use crate::sketch::minhash::KmerMinHash;
use crate::sketch::Sketch;
pub struct SourmashRevIndex;
impl ForeignObject for SourmashRevIndex {
type RustObject = RevIndex;
}
// TODO: remove this when it is possible to pass Selection thru the FFI
fn from_template(template: &Sketch) -> Selection {
let (num, scaled) = match template {
Sketch::MinHash(mh) => (mh.num(), mh.scaled() as u32),
Sketch::LargeMinHash(mh) => (mh.num(), mh.scaled() as u32),
_ => unimplemented!(),
};
Selection::builder()
.ksize(template.ksize() as u32)
.num(num)
.scaled(scaled)
.build()
}
ffi_fn! {
unsafe fn revindex_new_with_paths(
search_sigs_ptr: *const *const SourmashStr,
insigs: usize,
template_ptr: *const SourmashKmerMinHash,
threshold: usize,
queries_ptr: *const *const SourmashKmerMinHash,
inqueries: usize,
keep_sigs: bool,
) -> Result<*mut SourmashRevIndex> {
let search_sigs: Vec<PathBuf> = {
assert!(!search_sigs_ptr.is_null());
slice::from_raw_parts(search_sigs_ptr, insigs)
.iter()
.map(|path| {
let mut new_path = PathBuf::new();
new_path.push(SourmashStr::as_rust(*path).as_str());
new_path
})
.collect()
};
let template = {
assert!(!template_ptr.is_null());
//TODO: avoid clone here
Sketch::MinHash(SourmashKmerMinHash::as_rust(template_ptr).clone())
};
let queries_vec: Vec<KmerMinHash>;
let queries: Option<&[KmerMinHash]> = if queries_ptr.is_null() {
None
} else {
queries_vec = slice::from_raw_parts(queries_ptr, inqueries)
.iter()
.map(|mh_ptr|
// TODO: avoid this clone
SourmashKmerMinHash::as_rust(*mh_ptr).clone())
.collect();
Some(queries_vec.as_ref())
};
let selection = from_template(&template);
let revindex = RevIndex::new(
search_sigs.as_ref(),
&selection,
threshold,
queries,
keep_sigs,
)?;
Ok(SourmashRevIndex::from_rust(revindex))
}
}
ffi_fn! {
unsafe fn revindex_new_with_sigs(
search_sigs_ptr: *const *const SourmashSignature,
insigs: usize,
template_ptr: *const SourmashKmerMinHash,
threshold: usize,
queries_ptr: *const *const SourmashKmerMinHash,
inqueries: usize,
) -> Result<*mut SourmashRevIndex> {
let search_sigs: Vec<Signature> = {
assert!(!search_sigs_ptr.is_null());
slice::from_raw_parts(search_sigs_ptr, insigs)
.iter()
.map(|sig| SourmashSignature::as_rust(*sig))
.cloned()
.collect()
};
let template = {
assert!(!template_ptr.is_null());
//TODO: avoid clone here
Sketch::MinHash(SourmashKmerMinHash::as_rust(template_ptr).clone())
};
let queries_vec: Vec<KmerMinHash>;
let queries: Option<&[KmerMinHash]> = if queries_ptr.is_null() {
None
} else {
queries_vec = slice::from_raw_parts(queries_ptr, inqueries)
.iter()
.map(|mh_ptr|
// TODO: avoid this clone
SourmashKmerMinHash::as_rust(*mh_ptr).clone())
.collect();
Some(queries_vec.as_ref())
};
let selection = from_template(&template);
let revindex = RevIndex::new_with_sigs(search_sigs, &selection, threshold, queries)?;
Ok(SourmashRevIndex::from_rust(revindex))
}
}
#[no_mangle]
pub unsafe extern "C" fn revindex_free(ptr: *mut SourmashRevIndex) {
SourmashRevIndex::drop(ptr);
}
ffi_fn! {
unsafe fn revindex_search(
ptr: *const SourmashRevIndex,
sig_ptr: *const SourmashSignature,
threshold: f64,
do_containment: bool,
_ignore_abundance: bool,
size: *mut usize,
) -> Result<*const *const SourmashSearchResult> {
let revindex = SourmashRevIndex::as_rust(ptr);
let sig = SourmashSignature::as_rust(sig_ptr);
if sig.signatures.is_empty() {
*size = 0;
return Ok(std::ptr::null::<*const SourmashSearchResult>());
}
let mh = if let Sketch::MinHash(mh) = &sig.signatures[0] {
mh
} else {
// TODO: what if it is not a mh?
unimplemented!()
};
let results: Vec<(f64, Signature, String)> = revindex
.find_signatures(mh, threshold, do_containment, true)?
.into_iter()
.collect();
// FIXME: use the ForeignObject trait, maybe define new method there...
let ptr_sigs: Vec<*const SourmashSearchResult> = results
.into_iter()
.map(|x| Box::into_raw(Box::new(x)) as *const SourmashSearchResult)
.collect();
let b = ptr_sigs.into_boxed_slice();
*size = b.len();
Ok(Box::into_raw(b) as *const *const SourmashSearchResult)
}
}
ffi_fn! {
unsafe fn revindex_gather(
ptr: *const SourmashRevIndex,
sig_ptr: *const SourmashSignature,
threshold: f64,
_do_containment: bool,
_ignore_abundance: bool,
size: *mut usize,
) -> Result<*const *const SourmashSearchResult> {
let revindex = SourmashRevIndex::as_rust(ptr);
let sig = SourmashSignature::as_rust(sig_ptr);
if sig.signatures.is_empty() {
*size = 0;
return Ok(std::ptr::null::<*const SourmashSearchResult>());
}
let mh = if let Sketch::MinHash(mh) = &sig.signatures[0] {
mh
} else {
// TODO: what if it is not a mh?
unimplemented!()
};
// TODO: proper threshold calculation
let threshold: usize = (threshold * (mh.size() as f64)) as _;
let counter = revindex.counter_for_query(mh);
dbg!(&counter);
let results: Vec<(f64, Signature, String)> = revindex
.gather(counter, threshold, mh)
.unwrap() // TODO: proper error handling
.into_iter()
.map(|r| {
let filename = r.filename().to_owned();
let sig = r.get_match();
(r.f_match(), sig, filename)
})
.collect();
// FIXME: use the ForeignObject trait, maybe define new method there...
let ptr_sigs: Vec<*const SourmashSearchResult> = results
.into_iter()
.map(|x| Box::into_raw(Box::new(x)) as *const SourmashSearchResult)
.collect();
let b = ptr_sigs.into_boxed_slice();
*size = b.len();
Ok(Box::into_raw(b) as *const *const SourmashSearchResult)
}
}
#[no_mangle]
pub unsafe extern "C" fn revindex_scaled(ptr: *const SourmashRevIndex) -> u64 {
let revindex = SourmashRevIndex::as_rust(ptr);
if let Sketch::MinHash(mh) = revindex.template() {
mh.scaled()
} else {
unimplemented!()
}
}
#[no_mangle]
pub unsafe extern "C" fn revindex_len(ptr: *const SourmashRevIndex) -> u64 {
let revindex = SourmashRevIndex::as_rust(ptr);
revindex.len() as u64
}
ffi_fn! {
unsafe fn revindex_signatures(
ptr: *const SourmashRevIndex,
size: *mut usize,
) -> Result<*mut *mut SourmashSignature> {
let revindex = SourmashRevIndex::as_rust(ptr);
let sigs = revindex.signatures();
// FIXME: use the ForeignObject trait, maybe define new method there...
let ptr_sigs: Vec<*mut SourmashSignature> = sigs
.into_iter()
.map(|x| Box::into_raw(Box::new(x)) as *mut SourmashSignature)
.collect();
let b = ptr_sigs.into_boxed_slice();
*size = b.len();
Ok(Box::into_raw(b) as *mut *mut SourmashSignature)
}
}