/
signature.rs
341 lines (277 loc) · 9.16 KB
/
signature.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
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
use std::convert::TryInto;
use std::ffi::CStr;
use std::io;
use std::os::raw::c_char;
use std::slice;
use crate::signature::Signature;
use crate::sketch::minhash::HashFunctions;
use crate::sketch::Sketch;
use crate::ffi::cmd::compute::SourmashComputeParameters;
use crate::ffi::minhash::SourmashKmerMinHash;
use crate::ffi::utils::{ForeignObject, SourmashStr};
pub struct SourmashSignature;
impl ForeignObject for SourmashSignature {
type RustObject = Signature;
}
// Signature methods
#[no_mangle]
pub unsafe extern "C" fn signature_new() -> *mut SourmashSignature {
SourmashSignature::from_rust(Signature::default())
}
#[no_mangle]
pub unsafe extern "C" fn signature_from_params(
ptr: *const SourmashComputeParameters,
) -> *mut SourmashSignature {
let params = SourmashComputeParameters::as_rust(ptr);
SourmashSignature::from_rust(Signature::from_params(params))
}
#[no_mangle]
pub unsafe extern "C" fn signature_free(ptr: *mut SourmashSignature) {
SourmashSignature::drop(ptr);
}
#[no_mangle]
pub unsafe extern "C" fn signature_len(ptr: *const SourmashSignature) -> usize {
let sig = SourmashSignature::as_rust(ptr);
sig.size()
}
ffi_fn! {
unsafe fn signature_add_sequence(ptr: *mut SourmashSignature, sequence: *const c_char, force: bool) ->
Result<()> {
let sig = SourmashSignature::as_rust_mut(ptr);
// FIXME replace with buffer + len
let c_str = {
assert!(!sequence.is_null());
CStr::from_ptr(sequence)
};
sig.add_sequence(c_str.to_bytes(), force)
}
}
ffi_fn! {
unsafe fn signature_add_protein(ptr: *mut SourmashSignature, sequence: *const c_char) ->
Result<()> {
let sig = SourmashSignature::as_rust_mut(ptr);
// FIXME replace with buffer + len
let c_str = {
assert!(!sequence.is_null());
CStr::from_ptr(sequence)
};
sig.add_protein(c_str.to_bytes())
}
}
ffi_fn! {
unsafe fn signature_set_name(ptr: *mut SourmashSignature, name: *const c_char) ->
Result<()> {
let sig = SourmashSignature::as_rust_mut(ptr);
// FIXME replace with buffer + len
let c_str = {
assert!(!name.is_null());
CStr::from_ptr(name)
};
if let Ok(name) = c_str.to_str() {
sig.set_name(name)
}
Ok(())
}
}
ffi_fn! {
unsafe fn signature_set_filename(ptr: *mut SourmashSignature, name: *const c_char) ->
Result<()> {
let sig = SourmashSignature::as_rust_mut(ptr);
// FIXME replace with buffer + len
let c_str = {
assert!(!name.is_null());
CStr::from_ptr(name)
};
if let Ok(name) = c_str.to_str() {
sig.set_filename(name)
}
Ok(())
}
}
ffi_fn! {
unsafe fn signature_push_mh(ptr: *mut SourmashSignature, other: *const SourmashKmerMinHash) ->
Result<()> {
let sig = SourmashSignature::as_rust_mut(ptr);
let mh = SourmashKmerMinHash::as_rust(other);
sig.push(Sketch::MinHash(mh.clone()));
Ok(())
}
}
ffi_fn! {
unsafe fn signature_set_mh(ptr: *mut SourmashSignature, other: *const SourmashKmerMinHash) ->
Result<()> {
let sig = SourmashSignature::as_rust_mut(ptr);
let mh = SourmashKmerMinHash::as_rust(other);
sig.reset_sketches();
sig.push(Sketch::MinHash(mh.clone()));
Ok(())
}
}
ffi_fn! {
unsafe fn signature_get_name(ptr: *const SourmashSignature) -> Result<SourmashStr> {
let sig = SourmashSignature::as_rust(ptr);
if let Some(ref name) = sig.name {
Ok(name.clone().into())
} else {
Ok("".into())
}
}
}
ffi_fn! {
unsafe fn signature_get_filename(ptr: *const SourmashSignature) -> Result<SourmashStr> {
let sig = SourmashSignature::as_rust(ptr);
Ok(sig.filename().into())
}
}
ffi_fn! {
unsafe fn signature_get_license(ptr: *const SourmashSignature) -> Result<SourmashStr> {
let sig = SourmashSignature::as_rust(ptr);
Ok(sig.license().into())
}
}
ffi_fn! {
unsafe fn signature_first_mh(ptr: *const SourmashSignature) -> Result<*mut SourmashKmerMinHash> {
let sig = SourmashSignature::as_rust(ptr);
if let Some(item) = sig.signatures.get(0) {
if let Sketch::MinHash(mh) = item {
Ok(SourmashKmerMinHash::from_rust(mh.clone()))
} else {
unimplemented!()
}
} else {
// TODO: need to select the correct one
unimplemented!()
}
}
}
ffi_fn! {
unsafe fn signature_eq(ptr: *const SourmashSignature, other: *const SourmashSignature) -> Result<bool> {
let sig = SourmashSignature::as_rust(ptr);
let other_sig = SourmashSignature::as_rust(other);
Ok(sig == other_sig)
}
}
ffi_fn! {
unsafe fn signature_save_json(ptr: *const SourmashSignature) -> Result<SourmashStr> {
let sig = SourmashSignature::as_rust(ptr);
let st = serde_json::to_string(sig)?;
Ok(SourmashStr::from_string(st))
}
}
ffi_fn! {
unsafe fn signature_get_mhs(ptr: *const SourmashSignature, size: *mut usize) -> Result<*mut *mut SourmashKmerMinHash> {
let sig = SourmashSignature::as_rust(ptr);
let output = sig.sketches();
// FIXME: how to fit this into the ForeignObject trait?
let ptr_sigs: Vec<*mut Signature> = output.into_iter().map(|x| {
Box::into_raw(Box::new(x)) as *mut Signature
}).collect();
let b = ptr_sigs.into_boxed_slice();
*size = b.len();
Ok(Box::into_raw(b) as *mut *mut SourmashKmerMinHash)
}
}
ffi_fn! {
unsafe fn signatures_save_buffer(ptr: *const *const SourmashSignature, size: usize, compression: u8, osize: *mut usize) -> Result<*const u8> {
// FIXME: review this for ForeignObject
let sigs = {
assert!(!ptr.is_null());
slice::from_raw_parts(ptr, size)
};
let rsigs: Vec<&Signature> = sigs.iter().map(|x| SourmashSignature::as_rust(*x)).collect();
let mut buffer = vec![];
{
let mut writer = if compression > 0 {
let level = match compression {
1 => niffler::compression::Level::One,
2 => niffler::compression::Level::Two,
3 => niffler::compression::Level::Three,
4 => niffler::compression::Level::Four,
5 => niffler::compression::Level::Five,
6 => niffler::compression::Level::Six,
7 => niffler::compression::Level::Seven,
8 => niffler::compression::Level::Eight,
_ => niffler::compression::Level::Nine,
};
niffler::get_writer(Box::new(&mut buffer),
niffler::compression::Format::Gzip,
level)?
} else {
Box::new(&mut buffer)
};
serde_json::to_writer(&mut writer, &rsigs)?;
}
let b = buffer.into_boxed_slice();
*osize = b.len();
Ok(Box::into_raw(b) as *const u8)
}
}
ffi_fn! {
unsafe fn signatures_load_path(ptr: *const c_char,
_ignore_md5sum: bool,
ksize: usize,
select_moltype: *const c_char,
size: *mut usize) -> Result<*mut *mut SourmashSignature> {
// FIXME use buffer + len instead of cstr
let buf = {
assert!(!ptr.is_null());
CStr::from_ptr(ptr)
};
// FIXME take select_moltype as enum
let moltype: Option<HashFunctions> = if select_moltype.is_null() {
None
} else {
let mol = CStr::from_ptr(select_moltype).to_str()?;
Some(mol.try_into()?)
};
// TODO: implement ignore_md5sum
let k = match ksize {
0 => None,
x => Some(x)
};
let (mut input, _) = niffler::from_path(buf.to_str()?)?;
let filtered_sigs = Signature::load_signatures(&mut input, k, moltype, None)?;
// FIXME: use the ForeignObject trait, maybe define new method there...
let ptr_sigs: Vec<*mut SourmashSignature> = filtered_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)
}
}
ffi_fn! {
unsafe fn signatures_load_buffer(ptr: *const c_char,
insize: usize,
_ignore_md5sum: bool,
ksize: usize,
select_moltype: *const c_char,
size: *mut usize) -> Result<*mut *mut SourmashSignature> {
// FIXME use buffer + len instead of cstr
let buf = {
assert!(!ptr.is_null());
slice::from_raw_parts(ptr as *mut u8, insize)
};
// FIXME take select_moltype as enum
let moltype: Option<HashFunctions> = if select_moltype.is_null() {
None
} else {
let mol = CStr::from_ptr(select_moltype).to_str()?;
Some(mol.try_into()?)
};
let k = match ksize {
0 => None,
x => Some(x)
};
// TODO: implement ignore_md5sum
let mut reader = io::BufReader::new(buf);
let filtered_sigs = Signature::load_signatures(&mut reader, k, moltype, None)?;
// FIXME: use the ForeignObject trait, maybe define new method there...
let ptr_sigs: Vec<*mut SourmashSignature> = filtered_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)
}
}