Skip to content

Commit

Permalink
Add astarpa2::astarpa2_{simple,full} to C api and example
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarGrootKoerkamp committed Mar 25, 2024
1 parent acf99d9 commit f4fb1c8
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ More complex usage examples can be found in [[file:pa-bin/examples/][pa-bin/exam

** C API
The ~astarpa-c~ [[file:astarpa-c/astarpa.h][crate]] contains simple C-bindings for the
~astarpa::{astarpa,astarpa_gcsh}~ functions and an [[file:astarpa-c/example.c][example]] with [[file:astarpa-c/makefile][makefile]]. More should not be needed for
~astarpa::{astarpa,astarpa_gcsh}~ and ~astarpa2::astarpa2_{simple,full}~ functions and an [[file:astarpa-c/example.c][example]] with [[file:astarpa-c/makefile][makefile]]. More should not be needed for
simple usage. To run the resulting binary, make sure to ~export
LD_LIBRARY_PATH=/path/to/astarpa/target/release~.

Expand Down
1 change: 1 addition & 0 deletions astarpa-c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ crate-type = ["lib", "cdylib", "staticlib"]
astarpa.workspace = true
pa-types.workspace = true
pa-heuristic.workspace = true
astarpa2.workspace = true
24 changes: 24 additions & 0 deletions astarpa-c/astarpa.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@
extern "C" {
#endif // __cplusplus

/**
* Align sequences `a` and `b` of length `a_len` and `b_len` using A*PA2-simple.
*
* The returned cigar must be freed using `astarpa_free_cigar`.
*/
uint64_t astarpa2_simple(const uint8_t *a,
uintptr_t a_len,
const uint8_t *b,
uintptr_t b_len,
uint8_t **cigar_ptr,
uintptr_t *cigar_len);

/**
* Align sequences `a` and `b` of length `a_len` and `b_len` using A*PA2-full.
*
* The returned cigar must be freed using `astarpa_free_cigar`.
*/
uint64_t astarpa2_full(const uint8_t *a,
uintptr_t a_len,
const uint8_t *b,
uintptr_t b_len,
uint8_t **cigar_ptr,
uintptr_t *cigar_len);

/**
* Globally align sequences `a` and `b` of length `a_len` and `b_len`.
* This uses A*PA with GCSH with DT, inexact matches (r=2), seed length k=15, and pruning by start of matches.
Expand Down
19 changes: 17 additions & 2 deletions astarpa-c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@ int main() {
const char* b = "AACTCGTT";
size_t len;
uint8_t* cigar;
uint64_t cost =
astarpa((const uint8_t*)a, strlen(a), (const uint8_t*)b, strlen(b), &cigar, &len);
uint64_t cost;

cost = astarpa((const uint8_t*)a, strlen(a), (const uint8_t*)b, strlen(b), &cigar, &len);
assert(cost == 2);
astarpa_free_cigar(cigar);

cost = astarpa_gcsh((const uint8_t*)a, strlen(a), (const uint8_t*)b, strlen(b), 1, 15, false,
&cigar, &len);
assert(cost == 2);
astarpa_free_cigar(cigar);

cost =
astarpa2_simple((const uint8_t*)a, strlen(a), (const uint8_t*)b, strlen(b), &cigar, &len);
assert(cost == 2);
astarpa_free_cigar(cigar);

cost = astarpa2_full((const uint8_t*)a, strlen(a), (const uint8_t*)b, strlen(b), &cigar, &len);
assert(cost == 2);
printf("Cost: %lu\n", cost);
printf("Cigar len: %lu\n", len);
Expand Down
44 changes: 44 additions & 0 deletions astarpa-c/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
use pa_heuristic::Prune;
use std::ffi::CString;

/// Align sequences `a` and `b` of length `a_len` and `b_len` using A*PA2-simple.
///
/// The returned cigar must be freed using `astarpa_free_cigar`.
#[no_mangle]
pub unsafe extern "C" fn astarpa2_simple(
a: *const u8,
a_len: usize,
b: *const u8,
b_len: usize,
// output parameters
cigar_ptr: *mut *mut u8,
cigar_len: *mut usize,
) -> u64 {
let a = std::slice::from_raw_parts(a, a_len);
let b = std::slice::from_raw_parts(b, b_len);
let (cost, cigar) = astarpa2::astarpa2_simple(a, b);
let cigar_string = cigar.to_string();
*cigar_len = cigar_string.len();
*cigar_ptr = CString::new(cigar_string).unwrap().into_raw() as *mut u8;
cost as _
}

/// Align sequences `a` and `b` of length `a_len` and `b_len` using A*PA2-full.
///
/// The returned cigar must be freed using `astarpa_free_cigar`.
#[no_mangle]
pub unsafe extern "C" fn astarpa2_full(
a: *const u8,
a_len: usize,
b: *const u8,
b_len: usize,
// output parameters
cigar_ptr: *mut *mut u8,
cigar_len: *mut usize,
) -> u64 {
let a = std::slice::from_raw_parts(a, a_len);
let b = std::slice::from_raw_parts(b, b_len);
let (cost, cigar) = astarpa2::astarpa2_full(a, b);
let cigar_string = cigar.to_string();
*cigar_len = cigar_string.len();
*cigar_ptr = CString::new(cigar_string).unwrap().into_raw() as *mut u8;
cost as _
}

/// Globally align sequences `a` and `b` of length `a_len` and `b_len`.
/// This uses A*PA with GCSH with DT, inexact matches (r=2), seed length k=15, and pruning by start of matches.
///
Expand Down

0 comments on commit f4fb1c8

Please sign in to comment.