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
#[cfg(feature = "c_ffi")]
pub mod c_ffi {
use crate::{GtfSortError, SortAnnotationsJobResult};
use std::ffi::{c_char, c_ulong, c_void, CStr, CString};
#[repr(C)]
pub struct GtfSortErrorFFI {
pub code: i32,
pub message: *const c_char,
}
pub const GTFSORT_ERROR_INVALID_INPUT: i32 = 1;
pub const GTFSORT_ERROR_INVALID_OUTPUT: i32 = 2;
pub const GTFSORT_ERROR_PARSE_ERROR: i32 = 3;
pub const GTFSORT_ERROR_INVALID_THREADS: i32 = 4;
pub const GTFSORT_ERROR_IO_ERROR: i32 = 5;
pub const GTFSORT_ERROR_INVALID_PARAMETER: i32 = -1;
macro_rules! cstr {
($s:expr) => {
CString::new($s).unwrap().into_raw()
};
}
macro_rules! cstr_free {
($s:expr) => {
if !$s.is_null() {
drop(CString::from_raw($s as *mut _));
}
};
}
impl From<GtfSortError> for GtfSortErrorFFI {
fn from(e: GtfSortError) -> Self {
match e {
GtfSortError::InvalidInput(s) => Self {
code: GTFSORT_ERROR_INVALID_INPUT,
message: cstr!(s),
},
GtfSortError::InvalidOutput(s) => Self {
code: GTFSORT_ERROR_INVALID_OUTPUT,
message: cstr!(s),
},
GtfSortError::ParseError(s) => Self {
code: GTFSORT_ERROR_PARSE_ERROR,
message: cstr!(s),
},
GtfSortError::InvalidThreads(s) => Self {
code: GTFSORT_ERROR_INVALID_THREADS,
message: cstr!(s),
},
GtfSortError::IoError(s, e) => Self {
code: GTFSORT_ERROR_IO_ERROR,
message: cstr!(format!("{}: {}", s, e)),
},
GtfSortError::InvalidParameter(s) => Self {
code: GTFSORT_ERROR_INVALID_PARAMETER,
message: cstr!(s),
},
}
}
}
#[repr(C)]
pub struct SortAnnotationsJobResultFFI {
pub input: *const c_char,
pub output: *const c_char,
pub threads: usize,
pub input_mmaped: bool,
pub output_mmaped: bool,
pub parsing_secs: f64,
pub indexing_secs: f64,
pub writing_secs: f64,
pub start_mem_mb: f64,
pub end_mem_mb: f64,
}
impl From<SortAnnotationsJobResult<'_>> for SortAnnotationsJobResultFFI {
fn from(r: SortAnnotationsJobResult) -> Self {
Self {
input: cstr!(r.input),
output: cstr!(r.output),
threads: r.threads,
input_mmaped: r.input_mmaped,
output_mmaped: r.output_mmaped,
parsing_secs: r.parsing_secs,
indexing_secs: r.indexing_secs,
writing_secs: r.writing_secs,
start_mem_mb: r.start_mem_mb.unwrap_or(f64::NAN),
end_mem_mb: r.end_mem_mb.unwrap_or(f64::NAN),
}
}
}
#[repr(C)]
pub enum SortAnnotationsRet {
Ok(*mut SortAnnotationsJobResultFFI),
Err(*mut GtfSortErrorFFI),
}
impl SortAnnotationsRet {
pub fn clear(&mut self) {
unsafe {
match self {
SortAnnotationsRet::Ok(p) => {
if !p.is_null() {
let p = Box::from_raw(*p);
cstr_free!(p.input);
cstr_free!(p.output);
}
}
SortAnnotationsRet::Err(p) => {
if !p.is_null() {
let p = Box::from_raw(*p);
cstr_free!(p.message);
}
}
}
}
}
pub fn is_filled(&self) -> bool {
match self {
SortAnnotationsRet::Ok(p) => !p.is_null(),
SortAnnotationsRet::Err(p) => !p.is_null(),
}
}
}
pub const GTFSORT_PARSE_MODE_GTF: u8 = 1;
pub const GTFSORT_PARSE_MODE_GFF: u8 = 2;
pub const GTFSORT_PARSE_MODE_GFF3: u8 = 2;
/// Initializes the logger with the given log level.
/// The log level must be one of the following: trace, debug, info, warn, error.
///
/// # Safety
/// level must be a valid C string.
#[no_mangle]
pub unsafe extern "C" fn gtfsort_init_logger(level: *const c_char) {
let level = unsafe { CStr::from_ptr(level).to_str().unwrap_or("info") };
match level.to_ascii_lowercase().as_str() {
"trace" => simple_logger::init_with_level(log::Level::Trace).unwrap(),
"debug" => simple_logger::init_with_level(log::Level::Debug).unwrap(),
"info" => simple_logger::init_with_level(log::Level::Info).unwrap(),
"warn" => simple_logger::init_with_level(log::Level::Warn).unwrap(),
"error" => simple_logger::init_with_level(log::Level::Error).unwrap(),
_ => simple_logger::init_with_level(log::Level::Info).unwrap(),
}
}
/// Allocates a new [SortAnnotationsRet] on the Rust heap.
///
/// # Safety
/// The caller is responsible for freeing the allocated memory using [gtfsort_free_sort_annotations_ret].
/// Do not free the memory using any other method.
#[no_mangle]
pub unsafe extern "C" fn gtfsort_new_sort_annotations_ret() -> *mut SortAnnotationsRet {
Box::into_raw(Box::new(SortAnnotationsRet::Ok(std::ptr::null_mut())))
}
/// Frees the [SortAnnotationsRet].
///
/// # Safety
/// ret must be a valid pointer to a [SortAnnotationsRet] that is allocated by [gtfsort_new_sort_annotations_ret].
#[no_mangle]
pub unsafe extern "C" fn gtfsort_free_sort_annotations_ret(ret: *mut SortAnnotationsRet) {
let mut b = Box::from_raw(ret);
b.clear();
}
/// Sorts the annotations in the given GTF or GFF3 file and writes the result to the output file.
///
/// `result_ptr` is a pointer to a [SortAnnotationsRet] that will be set to the result of the operation.
/// if you don't need the result, you can pass a null pointer.
///
/// The return value is true if the operation was successful, false otherwise.
///
/// # Safety
/// input and output must be valid C strings that point to valid file paths.
#[no_mangle]
pub unsafe extern "C" fn gtfsort_sort_annotations(
input: *const std::os::raw::c_char,
output: *const std::os::raw::c_char,
threads: usize,
result_ptr: *mut SortAnnotationsRet,
) -> bool {
let input = std::path::PathBuf::from(unsafe { CStr::from_ptr(input).to_str().unwrap() });
let output = std::path::PathBuf::from(unsafe { CStr::from_ptr(output).to_str().unwrap() });
let result = crate::sort_annotations(&input, &output, threads);
let ok = result.is_ok();
if !result_ptr.is_null() {
unsafe {
result_ptr.as_mut().unwrap().clear();
*result_ptr = match result {
Ok(r) => SortAnnotationsRet::Ok(Box::into_raw(Box::new(r.into()))),
Err(e) => SortAnnotationsRet::Err(Box::into_raw(Box::new(e.into()))),
};
}
}
ok
}
/// Sorts the annotations in the given GTF or GFF3 string and writes the result chunk by chunk to the output callback.
///
/// The mode must be one of the following:
/// - [GTFSORT_PARSE_MODE_GTF]
/// - [GTFSORT_PARSE_MODE_GFF3]
/// - [GTFSORT_PARSE_MODE_GFF]
///
/// output is a callback function that will be called with the following arguments:
/// - caller_data: a pointer to the caller data
/// - output: a pointer to the output bytes
/// - len: the length of the output bytes
///
/// The callback function should return a null pointer in case of success, or an error message in case of failure.
///
/// caller_data is a pointer to the caller data that will be passed to the output callback.
///
/// result_ptr is a pointer to a SortAnnotationsRet that will be set to the result of the operation.
/// if you don't need the result, you can pass a null pointer.
///
/// the return value is true if the operation was successful, false otherwise.
///
/// # Safety
///
/// input must be a valid C string.
///
/// The caller is responsible for freeing the error message in output callback.
///
#[no_mangle]
pub unsafe extern "C" fn gtfsort_sort_annotations_gtf_str(
mode: u8,
input: *const c_char,
output: extern "C" fn(*mut c_void, *const c_char, c_ulong) -> *const c_char,
threads: usize,
caller_data: *mut c_void,
result_ptr: *mut SortAnnotationsRet,
) -> bool {
let input = unsafe { CStr::from_ptr(input).to_str().unwrap() };
let mut output = |str: &[u8]| {
let ret = output(
caller_data,
unsafe { CStr::from_bytes_with_nul_unchecked(str).as_ptr() },
str.len() as c_ulong,
);
match ret.is_null() {
true => Ok(str.len()),
false => Err(std::io::Error::other(unsafe {
CStr::from_ptr(ret).to_str().unwrap()
})),
}
};
let result = match mode {
GTFSORT_PARSE_MODE_GTF => {
crate::sort_annotations_string::<b' ', _>(input, &mut output, threads)
}
GTFSORT_PARSE_MODE_GFF3 => {
crate::sort_annotations_string::<b'=', _>(input, &mut output, threads)
}
_ => {
if !result_ptr.is_null() {
unsafe {
*result_ptr = SortAnnotationsRet::Err(Box::into_raw(Box::new(
GtfSortError::InvalidParameter("invalid parse mode").into(),
)));
}
}
return false;
}
};
let ok = result.is_ok();
if !result_ptr.is_null() {
unsafe {
result_ptr.as_mut().unwrap().clear();
*result_ptr = match result {
Ok(r) => SortAnnotationsRet::Ok(Box::into_raw(Box::new(r.into()))),
Err(e) => SortAnnotationsRet::Err(Box::into_raw(Box::new(e.into()))),
};
}
}
ok
}
}