seqwish 0.1.3

A variation graph inducer - build pangenome graphs from pairwise alignments
Documentation
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
//! # seqwish - A variation graph inducer
//!
//! Seqwish builds variation graphs from pairwise sequence alignments.
//! It transforms a collection of sequences and their all-to-all alignments
//! into a graph representation that captures the variation between the sequences.
//!
//! ## Overview
//!
//! The algorithm proceeds in several stages:
//!
//! 1. **Sequence Indexing** - Load and index input sequences
//! 2. **Alignment Processing** - Parse and index PAF alignments
//! 3. **Transitive Closure** - Compute equivalence classes of aligned positions
//! 4. **Node Compaction** - Merge non-bifurcating regions into single nodes
//! 5. **Link Derivation** - Extract edges between nodes
//! 6. **GFA Emission** - Output the variation graph in GFA format
//!
//! ## Example
//!
//! ```rust,no_run
//! use seqwish::seqindex::SeqIndex;
//! use std::sync::{Arc, Mutex};
//!
//! // Build a sequence index
//! let mut seqidx = SeqIndex::new();
//! seqidx.build_index("sequences.fa").unwrap();
//! ```
//!
//! ## Command-line Usage
//!
//! ```bash
//! seqwish -s sequences.fa -p alignments.paf -g output.gfa
//! ```
//!
//! ## Features
//!
//! - Memory-safe parallel processing
//! - Disk-backed data structures for scalability
//! - Produces GFA v1.0 format output
//! - Compatible with standard pangenome tools

use std::ffi::{c_char, CStr, CString};
use std::ptr;
use std::sync::Arc;

use bitvec::prelude::*;

pub mod alignments;
pub mod cigar;
pub mod compact;
pub mod dna;
pub mod dset64;
pub mod dset64_asm;
pub mod dset64_unsafe;
pub mod gfa;
pub mod intervaltree;
pub mod links;
pub mod mmap;
pub mod paf;
pub mod pos;
pub mod seqindex;
pub mod sxs;
pub mod tempfile;
pub mod time;
pub mod transclosure;
pub mod utils;
pub mod version;

/// Returns the version string of the Rust component
#[no_mangle]
pub extern "C" fn seqwish_rust_version() -> *const c_char {
    b"0.1.0-rust\0".as_ptr() as *const c_char
}

/// Simple test function to verify FFI is working
#[no_mangle]
pub extern "C" fn seqwish_rust_add(a: i32, b: i32) -> i32 {
    a + b
}

// FFI wrappers for tempfile module

/// Create a temporary file. Returns a C string that must be freed with temp_file_free_string.
/// Returns NULL on error.
#[no_mangle]
pub extern "C" fn temp_file_create(base: *const c_char, suffix: *const c_char) -> *mut c_char {
    if base.is_null() || suffix.is_null() {
        return ptr::null_mut();
    }

    let base_str = unsafe {
        match CStr::from_ptr(base).to_str() {
            Ok(s) => s,
            Err(_) => return ptr::null_mut(),
        }
    };

    let suffix_str = unsafe {
        match CStr::from_ptr(suffix).to_str() {
            Ok(s) => s,
            Err(_) => return ptr::null_mut(),
        }
    };

    match tempfile::create(base_str, suffix_str) {
        Ok(path) => {
            let path_str = path.to_string_lossy().to_string();
            match CString::new(path_str) {
                Ok(c_string) => c_string.into_raw(),
                Err(_) => ptr::null_mut(),
            }
        }
        Err(_) => ptr::null_mut(),
    }
}

/// Remove a temporary file
#[no_mangle]
pub extern "C" fn temp_file_remove(filename: *const c_char) {
    if filename.is_null() {
        return;
    }

    let filename_str = unsafe {
        match CStr::from_ptr(filename).to_str() {
            Ok(s) => s,
            Err(_) => return,
        }
    };

    tempfile::remove(std::path::Path::new(filename_str));
}

/// Set temp directory
#[no_mangle]
pub extern "C" fn temp_file_set_dir(dir: *const c_char) {
    if dir.is_null() {
        return;
    }

    let dir_str = unsafe {
        match CStr::from_ptr(dir).to_str() {
            Ok(s) => s,
            Err(_) => return,
        }
    };

    tempfile::set_dir(dir_str);
}

/// Get temp directory. Returns a C string that must be freed with temp_file_free_string.
#[no_mangle]
pub extern "C" fn temp_file_get_dir() -> *mut c_char {
    let dir = tempfile::get_dir();
    let dir_str = dir.to_string_lossy().to_string();

    match CString::new(dir_str) {
        Ok(c_string) => c_string.into_raw(),
        Err(_) => ptr::null_mut(),
    }
}

/// Set whether to keep temp files
#[no_mangle]
pub extern "C" fn temp_file_set_keep_temp(setting: bool) {
    tempfile::set_keep_temp(setting);
}

/// Free a string returned by temp_file functions
#[no_mangle]
pub extern "C" fn temp_file_free_string(s: *mut c_char) {
    if !s.is_null() {
        unsafe {
            let _ = CString::from_raw(s);
        }
    }
}

// FFI wrappers for pos module

/// Create a position from offset and orientation
#[no_mangle]
pub extern "C" fn pos_make_pos_t(offset: u64, is_rev: bool) -> u64 {
    pos::make_pos_t(offset, is_rev)
}

/// Extract offset from position
#[no_mangle]
pub extern "C" fn pos_offset(pos: u64) -> u64 {
    pos::offset(pos)
}

/// Check if position is reverse
#[no_mangle]
pub extern "C" fn pos_is_rev(pos: u64) -> bool {
    pos::is_rev(pos)
}

/// Increment position
#[no_mangle]
pub extern "C" fn pos_incr_pos(pos: *mut u64) {
    if !pos.is_null() {
        unsafe {
            pos::incr_pos(&mut *pos);
        }
    }
}

/// Increment position by N
#[no_mangle]
pub extern "C" fn pos_incr_pos_by(pos: *mut u64, by: usize) {
    if !pos.is_null() {
        unsafe {
            pos::incr_pos_by(&mut *pos, by);
        }
    }
}

/// Decrement position
#[no_mangle]
pub extern "C" fn pos_decr_pos(pos: *mut u64) {
    if !pos.is_null() {
        unsafe {
            pos::decr_pos(&mut *pos);
        }
    }
}

/// Decrement position by N
#[no_mangle]
pub extern "C" fn pos_decr_pos_by(pos: *mut u64, by: usize) {
    if !pos.is_null() {
        unsafe {
            pos::decr_pos_by(&mut *pos, by);
        }
    }
}

/// Reverse position orientation
#[no_mangle]
pub extern "C" fn pos_rev_pos_t(pos: u64) -> u64 {
    pos::rev_pos_t(pos)
}

/// Convert position to string (returns C string that must be freed)
#[no_mangle]
pub extern "C" fn pos_to_string_c(pos: u64) -> *mut c_char {
    let s = pos::pos_to_string(pos);
    match CString::new(s) {
        Ok(c_string) => c_string.into_raw(),
        Err(_) => ptr::null_mut(),
    }
}

// FFI wrappers for dna module

/// Get complement of a single DNA base
#[no_mangle]
pub extern "C" fn dna_complement(c: u8) -> u8 {
    dna::complement(c)
}

/// Reverse complement a DNA sequence (allocates new string that must be freed)
#[no_mangle]
pub extern "C" fn dna_reverse_complement(seq: *const c_char, len: usize, out: *mut c_char) {
    if seq.is_null() || out.is_null() {
        return;
    }

    unsafe {
        let slice = std::slice::from_raw_parts(seq as *const u8, len);
        let rc = dna::reverse_complement(slice);
        std::ptr::copy_nonoverlapping(rc.as_ptr(), out as *mut u8, len);
    }
}

/// Reverse complement a DNA sequence in place
#[no_mangle]
pub extern "C" fn dna_reverse_complement_in_place(seq: *mut c_char, len: usize) {
    if seq.is_null() {
        return;
    }

    unsafe {
        let slice = std::slice::from_raw_parts_mut(seq as *mut u8, len);
        dna::reverse_complement_in_place(slice);
    }
}

// FFI wrappers for cigar module

/// Opaque handle to CIGAR vector
pub struct CigarHandle {
    cigar: Vec<cigar::CigarOp>,
}

/// Opaque handle to SeqIndex
pub struct SeqIndexHandle {
    seqidx: Arc<seqindex::SeqIndex>,
}

/// Opaque handle to IITree (for node/path iitrees that use RwLock)
pub struct IITreeHandle {
    iitree: Arc<std::sync::RwLock<crate::intervaltree::AdaptiveTree<u64, pos::PosT>>>,
}

/// Opaque handle to Alignment IITree (uses Mutex for writing)
pub struct AlnIITreeHandle {
    iitree: Arc<std::sync::Mutex<crate::intervaltree::AdaptiveTree<u64, pos::PosT>>>,
}

/// Parse CIGAR string and return handle to CIGAR vector
/// Returns NULL on error. Must be freed with cigar_free.
#[no_mangle]
pub extern "C" fn cigar_from_string(s: *const c_char) -> *mut CigarHandle {
    if s.is_null() {
        return ptr::null_mut();
    }

    let s_str = unsafe {
        match CStr::from_ptr(s).to_str() {
            Ok(s) => s,
            Err(_) => return ptr::null_mut(),
        }
    };

    let cigar = cigar::cigar_from_string(s_str);
    Box::into_raw(Box::new(CigarHandle { cigar }))
}

/// Convert CIGAR vector to string
/// Returns C string that must be freed with temp_file_free_string
#[no_mangle]
pub extern "C" fn cigar_to_string(handle: *const CigarHandle) -> *mut c_char {
    if handle.is_null() {
        return ptr::null_mut();
    }

    let cigar_handle = unsafe { &*handle };
    let s = cigar::cigar_to_string(&cigar_handle.cigar);

    match CString::new(s) {
        Ok(c_string) => c_string.into_raw(),
        Err(_) => ptr::null_mut(),
    }
}

/// Get number of operations in CIGAR
#[no_mangle]
pub extern "C" fn cigar_length(handle: *const CigarHandle) -> usize {
    if handle.is_null() {
        return 0;
    }
    let cigar_handle = unsafe { &*handle };
    cigar_handle.cigar.len()
}

/// Get operation at index
/// Returns false if index out of bounds
#[no_mangle]
pub extern "C" fn cigar_get_op(
    handle: *const CigarHandle,
    index: usize,
    len_out: *mut u64,
    op_out: *mut u8,
) -> bool {
    if handle.is_null() || len_out.is_null() || op_out.is_null() {
        return false;
    }

    let cigar_handle = unsafe { &*handle };
    if index >= cigar_handle.cigar.len() {
        return false;
    }

    unsafe {
        *len_out = cigar_handle.cigar[index].len;
        *op_out = cigar_handle.cigar[index].op;
    }
    true
}

/// Free CIGAR handle
#[no_mangle]
pub extern "C" fn cigar_free(handle: *mut CigarHandle) {
    if !handle.is_null() {
        unsafe {
            let _ = Box::from_raw(handle);
        }
    }
}

// FFI wrappers for mmap module

/// Open a file and memory-map it
/// Returns the file size on success, 0 on error
/// The buffer pointer and file descriptor are written to the provided pointers
#[no_mangle]
pub extern "C" fn mmap_open_rust(
    filename: *const c_char,
    buf_out: *mut *mut c_char,
    fd_out: *mut i32,
) -> usize {
    if filename.is_null() || buf_out.is_null() || fd_out.is_null() {
        return 0;
    }

    let filename_str = unsafe {
        match CStr::from_ptr(filename).to_str() {
            Ok(s) => s,
            Err(_) => return 0,
        }
    };

    match mmap::mmap_open(filename_str) {
        Ok(handle) => {
            unsafe {
                *buf_out = handle.ptr;
                *fd_out = handle.fd;
            }
            let size = handle.size;
            // Prevent Drop from running - we're transferring ownership to C++
            std::mem::forget(handle);
            size
        }
        Err(_) => 0,
    }
}

/// Close a memory-mapped file
#[no_mangle]
pub extern "C" fn mmap_close_rust(buf: *mut c_char, fd: i32, size: usize) {
    if buf.is_null() {
        return;
    }

    let mut handle = mmap::MmapHandle { ptr: buf, fd, size };

    mmap::mmap_close(&mut handle);
}

// FFI wrappers for utils module

/// Check if a file exists
#[no_mangle]
pub extern "C" fn file_exists(filename: *const c_char) -> bool {
    if filename.is_null() {
        return false;
    }

    let filename_str = unsafe {
        match CStr::from_ptr(filename).to_str() {
            Ok(s) => s,
            Err(_) => return false,
        }
    };

    utils::file_exists(filename_str)
}

/// Parse a number with optional suffix (k, m, g)
#[no_mangle]
pub extern "C" fn handy_parameter(value: *const c_char, default_value: f64) -> f64 {
    if value.is_null() {
        return default_value;
    }

    let value_str = unsafe {
        match CStr::from_ptr(value).to_str() {
            Ok(s) => s,
            Err(_) => return default_value,
        }
    };

    utils::handy_parameter(value_str, default_value)
}

// FFI wrappers for time module

/// Get milliseconds since Unix epoch
#[no_mangle]
pub extern "C" fn time_since_epoch_ms() -> u64 {
    time::time_since_epoch_ms()
}

// FFI wrappers for paf module

/// Parse PAF spec string, calling callback for each (filename, weight) pair
/// Callback signature: void callback(void* user_data, const char* filename, uint64_t weight)
#[no_mangle]
pub extern "C" fn parse_paf_spec(
    spec: *const c_char,
    user_data: *mut std::ffi::c_void,
    callback: Option<extern "C" fn(*mut std::ffi::c_void, *const c_char, u64)>,
) {
    if spec.is_null() || callback.is_none() {
        return;
    }

    let spec_str = unsafe {
        match CStr::from_ptr(spec).to_str() {
            Ok(s) => s,
            Err(_) => return,
        }
    };

    let callback_fn = callback.unwrap();
    for (filename, weight) in paf::parse_paf_spec(spec_str) {
        if let Ok(c_filename) = CString::new(filename) {
            callback_fn(user_data, c_filename.as_ptr(), weight);
        }
    }
}

// FFI wrappers for alignments module

/// Hash function for match parameters
#[no_mangle]
pub extern "C" fn match_hash(q: u64, t: u64, l: u64) -> u64 {
    alignments::match_hash(q, t, l)
}

/// Determine if a match should be kept based on sparsification factor
#[no_mangle]
pub extern "C" fn keep_sparse(q: u64, t: u64, l: u64, f: f32) -> bool {
    alignments::keep_sparse(q, t, l, f)
}

// FFI wrappers for paf module

/// Opaque handle to a parsed PAF row
pub struct PafRowHandle {
    row: paf::PafRow,
}

// FFI wrappers for sxs module

/// Opaque handle to a parsed SXS alignment
pub struct SxsHandle {
    aln: sxs::SxsAlignment,
}

/// Parse a PAF row from a C string line
/// Returns NULL if parsing fails
#[no_mangle]
pub extern "C" fn paf_row_parse(line: *const c_char) -> *mut PafRowHandle {
    if line.is_null() {
        return ptr::null_mut();
    }

    let line_str = unsafe {
        match CStr::from_ptr(line).to_str() {
            Ok(s) => s,
            Err(_) => return ptr::null_mut(),
        }
    };

    match paf::PafRow::from_line(line_str) {
        Some(row) => Box::into_raw(Box::new(PafRowHandle { row })),
        None => ptr::null_mut(),
    }
}

/// Free a PAF row handle
#[no_mangle]
pub extern "C" fn paf_row_free(handle: *mut PafRowHandle) {
    if !handle.is_null() {
        unsafe {
            let _ = Box::from_raw(handle);
        }
    }
}

// Field accessors for PAF row
#[no_mangle]
pub extern "C" fn paf_row_query_sequence_name(handle: *const PafRowHandle) -> *mut c_char {
    if handle.is_null() {
        return ptr::null_mut();
    }
    let row = unsafe { &(*handle).row };
    match CString::new(row.query_sequence_name.clone()) {
        Ok(s) => s.into_raw(),
        Err(_) => ptr::null_mut(),
    }
}

#[no_mangle]
pub extern "C" fn paf_row_target_sequence_name(handle: *const PafRowHandle) -> *mut c_char {
    if handle.is_null() {
        return ptr::null_mut();
    }
    let row = unsafe { &(*handle).row };
    match CString::new(row.target_sequence_name.clone()) {
        Ok(s) => s.into_raw(),
        Err(_) => ptr::null_mut(),
    }
}

#[no_mangle]
pub extern "C" fn paf_row_query_sequence_length(handle: *const PafRowHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).row.query_sequence_length }
}

#[no_mangle]
pub extern "C" fn paf_row_query_start(handle: *const PafRowHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).row.query_start }
}

#[no_mangle]
pub extern "C" fn paf_row_query_end(handle: *const PafRowHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).row.query_end }
}

#[no_mangle]
pub extern "C" fn paf_row_query_target_same_strand(handle: *const PafRowHandle) -> bool {
    if handle.is_null() {
        return false;
    }
    unsafe { (*handle).row.query_target_same_strand }
}

#[no_mangle]
pub extern "C" fn paf_row_target_sequence_length(handle: *const PafRowHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).row.target_sequence_length }
}

#[no_mangle]
pub extern "C" fn paf_row_target_start(handle: *const PafRowHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).row.target_start }
}

#[no_mangle]
pub extern "C" fn paf_row_target_end(handle: *const PafRowHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).row.target_end }
}

#[no_mangle]
pub extern "C" fn paf_row_num_matches(handle: *const PafRowHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).row.num_matches }
}

#[no_mangle]
pub extern "C" fn paf_row_alignment_block_length(handle: *const PafRowHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).row.alignment_block_length }
}

#[no_mangle]
pub extern "C" fn paf_row_mapping_quality(handle: *const PafRowHandle) -> u16 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).row.mapping_quality }
}

#[no_mangle]
pub extern "C" fn paf_row_cigar(handle: *const PafRowHandle) -> *mut CigarHandle {
    if handle.is_null() {
        return ptr::null_mut();
    }
    let row = unsafe { &(*handle).row };
    Box::into_raw(Box::new(CigarHandle {
        cigar: row.cigar.clone(),
    }))
}

/// Create a new empty SXS alignment
#[no_mangle]
pub extern "C" fn sxs_new() -> *mut SxsHandle {
    Box::into_raw(Box::new(SxsHandle {
        aln: sxs::SxsAlignment::new(),
    }))
}

/// Parse SXS alignment from array of C strings (lines)
/// Returns NULL if parsing fails
#[no_mangle]
pub extern "C" fn sxs_parse_lines(lines: *const *const c_char, num_lines: usize) -> *mut SxsHandle {
    if lines.is_null() {
        return ptr::null_mut();
    }

    let mut line_vec = Vec::new();
    for i in 0..num_lines {
        unsafe {
            let line_ptr = *lines.add(i);
            if line_ptr.is_null() {
                continue;
            }
            match CStr::from_ptr(line_ptr).to_str() {
                Ok(s) => line_vec.push(s),
                Err(_) => return ptr::null_mut(),
            }
        }
    }

    match sxs::SxsAlignment::from_lines(&line_vec) {
        Some(aln) => Box::into_raw(Box::new(SxsHandle { aln })),
        None => ptr::null_mut(),
    }
}

/// Free an SXS handle
#[no_mangle]
pub extern "C" fn sxs_free(handle: *mut SxsHandle) {
    if !handle.is_null() {
        unsafe {
            let _ = Box::from_raw(handle);
        }
    }
}

// Field accessors for SXS
#[no_mangle]
pub extern "C" fn sxs_query_sequence_name(handle: *const SxsHandle) -> *mut c_char {
    if handle.is_null() {
        return ptr::null_mut();
    }
    let aln = unsafe { &(*handle).aln };
    match CString::new(aln.query_sequence_name.clone()) {
        Ok(s) => s.into_raw(),
        Err(_) => ptr::null_mut(),
    }
}

#[no_mangle]
pub extern "C" fn sxs_target_sequence_name(handle: *const SxsHandle) -> *mut c_char {
    if handle.is_null() {
        return ptr::null_mut();
    }
    let aln = unsafe { &(*handle).aln };
    match CString::new(aln.target_sequence_name.clone()) {
        Ok(s) => s.into_raw(),
        Err(_) => ptr::null_mut(),
    }
}

#[no_mangle]
pub extern "C" fn sxs_query_start(handle: *const SxsHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).aln.query_start }
}

#[no_mangle]
pub extern "C" fn sxs_query_end(handle: *const SxsHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).aln.query_end }
}

#[no_mangle]
pub extern "C" fn sxs_target_start(handle: *const SxsHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).aln.target_start }
}

#[no_mangle]
pub extern "C" fn sxs_target_end(handle: *const SxsHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).aln.target_end }
}

#[no_mangle]
pub extern "C" fn sxs_num_matches(handle: *const SxsHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).aln.num_matches }
}

#[no_mangle]
pub extern "C" fn sxs_mapping_quality(handle: *const SxsHandle) -> u16 {
    if handle.is_null() {
        return 0;
    }
    unsafe { (*handle).aln.mapping_quality }
}

#[no_mangle]
pub extern "C" fn sxs_cigar(handle: *const SxsHandle) -> *mut CigarHandle {
    if handle.is_null() {
        return ptr::null_mut();
    }
    let aln = unsafe { &(*handle).aln };
    Box::into_raw(Box::new(CigarHandle {
        cigar: aln.cigar.clone(),
    }))
}

#[no_mangle]
pub extern "C" fn sxs_is_good(handle: *const SxsHandle) -> bool {
    if handle.is_null() {
        return false;
    }
    unsafe { (*handle).aln.is_good() }
}

#[no_mangle]
pub extern "C" fn sxs_is_reverse(handle: *const SxsHandle) -> bool {
    if handle.is_null() {
        return false;
    }
    unsafe { (*handle).aln.is_reverse() }
}

// FFI wrappers for compact module

/// Compact nodes by marking boundaries in the graph
///
/// # Arguments
/// * `seqidx_handle` - Handle to the seqindex
/// * `graph_size` - Size of the graph sequence
/// * `node_iitree_handle` - Handle to the node iitree
/// * `path_iitree_handle` - Handle to the path iitree
/// * `seq_id_bv` - Pointer to bitvector array (will be modified)
/// * `seq_id_bv_size` - Size of the bitvector
/// * `num_threads` - Number of threads to use
///
/// # Returns
/// 0 on success, 1 on error
#[no_mangle]
pub extern "C" fn compact_compact_nodes(
    seqidx_handle: *const SeqIndexHandle,
    graph_size: usize,
    node_iitree_handle: *const IITreeHandle,
    path_iitree_handle: *const IITreeHandle,
    seq_id_bv: *mut u64,
    seq_id_bv_size: usize,
    num_threads: usize,
) -> i32 {
    if seqidx_handle.is_null()
        || node_iitree_handle.is_null()
        || path_iitree_handle.is_null()
        || seq_id_bv.is_null()
    {
        eprintln!("[compact] Error: null pointer passed to compact_compact_nodes");
        return 1;
    }

    unsafe {
        let seqidx = Arc::clone(&(*seqidx_handle).seqidx);
        let node_iitree = Arc::clone(&(*node_iitree_handle).iitree);
        let path_iitree = Arc::clone(&(*path_iitree_handle).iitree);

        // Create BitVec from raw pointer
        let bit_count = seq_id_bv_size * 64; // 64 bits per u64
        let slice = std::slice::from_raw_parts_mut(seq_id_bv, seq_id_bv_size);
        let mut bitvec = BitVec::from_slice(slice);
        bitvec.resize(bit_count, false);

        match compact::compact_nodes(
            seqidx,
            graph_size,
            node_iitree,
            path_iitree,
            &mut bitvec,
            num_threads,
        ) {
            Ok(()) => {
                // Copy bitvec back to raw pointer
                let bitvec_slice = bitvec.as_raw_slice();
                std::ptr::copy_nonoverlapping(
                    bitvec_slice.as_ptr(),
                    seq_id_bv,
                    seq_id_bv_size.min(bitvec_slice.len()),
                );
                0
            }
            Err(e) => {
                eprintln!("[compact] Error in compact_nodes: {}", e);
                1
            }
        }
    }
}

// FFI wrappers for transclosure module

// TODO: Add helper functions to create handles from C++ objects
// For now, the C++ side needs to manage creation of SeqIndexHandle and IITreeHandle
// directly by wrapping the Rust objects in Arc<> and Arc<Mutex<>>

/// Compute transitive closures for variation graph construction
///
/// # Arguments
/// * `seqidx_handle` - Handle to the seqindex
/// * `aln_iitree_handle` - Handle to the alignment iitree
/// * `seq_v_file` - Path to output sequence file
/// * `node_iitree_handle` - Handle to the node iitree
/// * `path_iitree_handle` - Handle to the path iitree
/// * `repeat_max` - Maximum repeat count
/// * `min_repeat_dist` - Minimum repeat distance
/// * `transclose_batch_size` - Batch size for transitive closure
/// * `show_progress` - Whether to show progress messages
/// * `num_threads` - Number of threads to use
///
/// # Returns
/// The length of the graph sequence, or 0 on error
#[no_mangle]
pub extern "C" fn transclosure_compute(
    seqidx_handle: *const SeqIndexHandle,
    aln_iitree_handle: *const AlnIITreeHandle,
    seq_v_file: *const c_char,
    node_iitree_handle: *const IITreeHandle,
    path_iitree_handle: *const IITreeHandle,
    repeat_max: u64,
    min_repeat_dist: u64,
    transclose_batch_size: u64,
    show_progress: bool,
    num_threads: usize,
) -> usize {
    if seqidx_handle.is_null()
        || aln_iitree_handle.is_null()
        || seq_v_file.is_null()
        || node_iitree_handle.is_null()
        || path_iitree_handle.is_null()
    {
        eprintln!("[transclosure] Error: null pointer passed to transclosure_compute");
        return 0;
    }

    unsafe {
        let seqidx = Arc::clone(&(*seqidx_handle).seqidx);
        let aln_iitree_mutex = Arc::clone(&(*aln_iitree_handle).iitree);
        let node_iitree = Arc::clone(&(*node_iitree_handle).iitree);
        let path_iitree = Arc::clone(&(*path_iitree_handle).iitree);

        // Unwrap the Mutex - alignment tree is read-only during transclosure
        let aln_iitree = match Arc::try_unwrap(aln_iitree_mutex) {
            Ok(mutex) => Arc::new(mutex.into_inner().unwrap()),
            Err(_) => {
                eprintln!("[transclosure] Error: Cannot unwrap aln_iitree Arc (multiple references exist)");
                return 0;
            }
        };

        let seq_v_file_str = match CStr::from_ptr(seq_v_file).to_str() {
            Ok(s) => s,
            Err(e) => {
                eprintln!("[transclosure] Error converting seq_v_file path: {}", e);
                return 0;
            }
        };

        match transclosure::compute_transitive_closures(
            seqidx,
            aln_iitree,
            seq_v_file_str,
            node_iitree,
            path_iitree,
            repeat_max,
            min_repeat_dist,
            transclose_batch_size,
            show_progress,
            num_threads,
        ) {
            Ok(length) => length,
            Err(e) => {
                eprintln!("[transclosure] Error in compute_transitive_closures: {}", e);
                0
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add() {
        assert_eq!(seqwish_rust_add(2, 3), 5);
    }
}