gbz-base 0.4.0

Pangenome file formats based on SQLite
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
//! GAF file sorting using multiway external memory merge sort.
//!
//! This module provides functionality to sort GAF (Graph Alignment Format) files
//! using an external memory merge sort algorithm, similar to GNU sort.

use std::cmp::Ordering;
use std::collections::{BinaryHeap, VecDeque};
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::thread::JoinHandle;
use std::time::Instant;

use gbz::{support, Orientation};

use simple_sds::serialize;

use crate::formats;
use crate::utils;

#[cfg(test)]
mod tests;

//-----------------------------------------------------------------------------

// Types of keys that can be derived from GAF records.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyType {
    // Sort by (minimum handle, maximum handle) in the path.
    // Handles encode both node ID and orientation.
    NodeInterval,
    // Sort by hash of the value for random shuffling.
    Hash,
}

// A GAF record with a sorting key.
#[derive(Clone, Debug)]
struct GAFRecord {
    // Integer key for sorting.
    key: u64,
    // Original GAF line.
    value: Vec<u8>,
}

impl GAFRecord {
    // Missing key value. Records without a key are sorted to the end.
    const MISSING_KEY: u64 = u64::MAX;

    // 0-based field number for the path in a GAF line.
    const PATH_FIELD: usize = 5;

    // Creates a new record from a line and sets the key.
    fn new(value: Vec<u8>, key_type: KeyType) -> Self {
        let mut record = Self {
            key: Self::MISSING_KEY,
            value,
        };
        record.set_key(key_type);
        record
    }

    //Sets the key based on the key type.
    fn set_key(&mut self, key_type: KeyType) {
        self.key = match key_type {
            KeyType::NodeInterval => self.extract_node_interval_key(),
            KeyType::Hash => self.extract_hash_key(),
        };
    }

    // Extracts (min_handle, max_handle) from the path field.
    fn extract_node_interval_key(&self) -> u64 {
        let path = match self.get_field(Self::PATH_FIELD) {
            Some(p) => p,
            None => return Self::MISSING_KEY,
        };

        let mut min_handle: u32 = u32::MAX;
        let mut max_handle: u32 = 0;

        // Parse the path field: e.g., ">1>2>3" or "<5>6<7"
        let mut i = 0;
        while i < path.len() {
            // Parse orientation character
            let orientation = if path[i] == b'>' {
                i += 1;
                Orientation::Forward
            } else if path[i] == b'<' {
                i += 1;
                Orientation::Reverse
            } else {
                return Self::MISSING_KEY;
            };

            // Parse node id
            let start = i;
            while i < path.len() && path[i].is_ascii_digit() {
                i += 1;
            }

            if start < i {
                if let Ok(id_str) = std::str::from_utf8(&path[start..i]) {
                    if let Ok(id) = id_str.parse::<usize>() {
                        let handle = support::encode_node(id, orientation) as u32;
                        min_handle = min_handle.min(handle);
                        max_handle = max_handle.max(handle);
                    } else {
                        return Self::MISSING_KEY;
                    }
                } else {
                    return Self::MISSING_KEY;
                }
            }
        }

        if min_handle == u32::MAX {
            Self::MISSING_KEY
        } else {
            ((min_handle as u64) << 32) | (max_handle as u64)
        }
    }

    // Extracts a hash of the value for random shuffling.
    fn extract_hash_key(&self) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        self.value.hash(&mut hasher);
        hasher.finish()
    }

    // Gets the nth field (0-indexed) from the GAF line.
    fn get_field(&self, field_index: usize) -> Option<&[u8]> {
        self.value.split(|&b| b == b'\t').nth(field_index)
    }

    // Serializes the record to a writer.
    fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
        writer.write_all(&self.key.to_le_bytes())?;
        let len = self.value.len() as u64;
        writer.write_all(&len.to_le_bytes())?;
        writer.write_all(&self.value)?;
        Ok(())
    }

    // Writes a GAF line to the output writer.
    fn write_gaf_line<W: Write>(&self, writer: &mut W) -> io::Result<()> {
        writer.write_all(&self.value)?; // The line already includes the newline character.
        Ok(())
    }

    // Deserializes a record from a reader.
    fn deserialize<R: Read>(reader: &mut R) -> io::Result<Self> {
        let mut key_bytes = [0u8; 8];
        reader.read_exact(&mut key_bytes)?;
        let key = u64::from_le_bytes(key_bytes);

        let mut len_bytes = [0u8; 8];
        reader.read_exact(&mut len_bytes)?;
        let len = u64::from_le_bytes(len_bytes) as usize;

        let mut value = vec![0u8; len];
        reader.read_exact(&mut value)?;

        Ok(Self { key, value })
    }

    // Flips the key to reverse the order (for priority queue).
    fn flip_key(&mut self) {
        self.key = u64::MAX - self.key;
    }
}

impl PartialEq for GAFRecord {
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key
    }
}

impl Eq for GAFRecord {}

impl PartialOrd for GAFRecord {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for GAFRecord {
    fn cmp(&self, other: &Self) -> Ordering {
        self.key.cmp(&other.key)
    }
}

//-----------------------------------------------------------------------------

// A zstd-compressed temporary file for storing sorted GAF records.
//
// This object should be wrapped in [`Arc`] in multithreaded contexts.
struct TempFile {
    path: PathBuf,
    records: usize,
}

impl TempFile {
    // Creates a new temporary file.
    fn create() -> io::Result<Self> {
        let path = serialize::temp_file_name("gaf-sort");
        Ok(Self { path, records: 0 })
    }

    // Opens the file for writing.
    fn writer(&self) -> io::Result<BufWriter<zstd::Encoder<'static, File>>> {
        let file = File::create(&self.path)?;
        let encoder = zstd::Encoder::new(file, 3)?; // Compression level 3
        Ok(BufWriter::new(encoder))
    }

    // Opens the file for reading.
    fn reader(&self) -> io::Result<zstd::Decoder<'static, BufReader<std::fs::File>>> {
        let file = std::fs::File::open(&self.path)?;
        let decoder = zstd::Decoder::new(file)?;
        Ok(decoder)
    }
}

impl Drop for TempFile {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.path);
    }
}

// The result of the initial sort.
enum InitialSortResult {
    // Temporary files created during the initial sort.
    TempFiles(Vec<Arc<TempFile>>),
    // A single batch of GAF lines.
    SingleBatch(Vec<Vec<u8>>),
}

//-----------------------------------------------------------------------------

/// Parameters for GAF sorting.
#[derive(Clone, Debug)]
pub struct SortParameters {
    /// Key type used for sorting.
    pub key_type: KeyType,
    /// Number of records per file in the initial sort.
    pub records_per_file: usize,
    /// Number of files to merge at once.
    pub files_per_merge: usize,
    /// Buffer size for reading and writing records.
    pub buffer_size: usize,
    /// Number of worker threads for the initial sort and intermediate merges.
    pub threads: usize,
    /// Use stable sorting.
    pub stable: bool,
    /// Print progress information to stderr.
    pub progress: bool,
}

impl SortParameters {
    /// Default for `records_per_file`.
    pub const DEFAULT_RECORDS_PER_FILE: usize = 1_000_000;
    /// Default for `files_per_merge`.
    pub const DEFAULT_FILES_PER_MERGE: usize = 32;
    /// Default for `buffer_size`.
    pub const DEFAULT_BUFFER_SIZE: usize = 1000;

    /// Returns a new `SortParameters` with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Validates the parameters and returns an error message if they are invalid.
    pub fn validate(&self) -> Result<(), String> {
        if self.records_per_file == 0 {
            return Err(String::from("SortParameters: records_per_file must be greater than 0"));
        }
        if self.files_per_merge < 2 {
            return Err(String::from("SortParameters: files_per_merge must be at least 2"));
        }
        if self.buffer_size == 0 {
            return Err(String::from("SortParameters: buffer_size must be greater than 0"));
        }
        if self.threads == 0 {
            return Err(String::from("SortParameters: threads must be greater than 0"));
        }
        Ok(())
    }
}

impl Default for SortParameters {
    fn default() -> Self {
        Self {
            key_type: KeyType::NodeInterval,
            records_per_file: Self::DEFAULT_RECORDS_PER_FILE,
            files_per_merge: Self::DEFAULT_FILES_PER_MERGE,
            buffer_size: Self::DEFAULT_BUFFER_SIZE,
            threads: 1,
            stable: false,
            progress: false,
        }
    }
}

//-----------------------------------------------------------------------------

// TODO: This is still not as fast as the C++ implementation.
// For example, 1705 seconds vs. 1525 seconds for ~40x short reads.

/// Sorts a GAF file using multiway external memory merge sort.
///
/// Returns the number of records sorted on success, or an error message on failure.
///
/// Sorting proceeds in three phases:
///
/// 1. Initial sort: Sort batches of records into compressed temporary files using multiple worker threads.
/// 2. Intermediate merges: Merge batches of temporary files into new temporary files using multiple worker threads.
/// 3. Final merge: Merge the remaining temporary files into the output file.
///
/// Temporary files are created and cleaned up automatically.
///
/// # Arguments
///
/// * `input_file`: Path to the input GAF file (possibly gzip-compressed). Use "-" for stdin.
/// * `output_file`: Path to the output GAF file. Use "-" for stdout.
/// * `params`: Sorting parameters.
///
/// # Errors
///
/// Returns an error if file I/O fails or if the GAF records are malformed.
///
/// # Examples
///
/// ```
/// use gbz_base::gaf_sort::{sort_gaf, SortParameters};
/// use gbz_base::utils;
/// use simple_sds::serialize;
/// use std::fs;
///
/// let input_file = utils::get_test_data("shuffled.gaf.gz");
/// let output_file = serialize::temp_file_name("sorted.gaf");
/// let params = SortParameters::default();
/// let result = sort_gaf(&input_file, &output_file, &params);
/// assert_eq!(result, Ok(12439));
/// let _ = fs::remove_file(output_file);
/// ```
pub fn sort_gaf<P: AsRef<Path>, Q: AsRef<Path>>(
    input_file: P,
    output_file: Q,
    params: &SortParameters,
) -> Result<usize, String> {
    params.validate()?;

    let start_time = Instant::now();
    if params.progress {
        eprintln!("Sorting GAF records with {} worker thread(s)", params.threads);
    }

    // Open input file and read header lines
    let mut reader = utils::open_file(input_file.as_ref())?;
    let header_lines = formats::read_gaf_header_lines(&mut reader)
        .map_err(|e| format!("Failed to read GAF header: {}", e))?;

    // Initial sort that may create temporary files or return a single batch of lines.
    let sort_result = initial_sort(reader, params)?;
    let mut temp_files = match sort_result {
        InitialSortResult::TempFiles(files) => files,
        InitialSortResult::SingleBatch(lines) => {
            let total_records = lines.len();
            sort_to_output(lines, &header_lines, output_file.as_ref(), params)?;
            if params.progress {
                let elapsed = start_time.elapsed().as_secs_f64();
                eprintln!("Sorted {} records in {:.2} seconds", total_records, elapsed);
            }
            return Ok(total_records);
        }
    };

    // Merge iteratively until the number of files is small enough to merge at once.
    let mut round = 0;
    while temp_files.len() > params.files_per_merge {
        temp_files = merge_round(temp_files, round, params)?;
        round += 1;
    }

    // Final merge to output
    if params.progress {
        eprintln!("Starting the final merge");
    }
    let total_records = merge_to_output(&temp_files, &header_lines, output_file.as_ref(), params)?;

    if params.progress {
        let elapsed = start_time.elapsed().as_secs_f64();
        eprintln!("Sorted {} records in {:.2} seconds", total_records, elapsed);
    }
    Ok(total_records)
}

//-----------------------------------------------------------------------------

// Performs the initial sort of the input GAF file and returns either temporary files or a single batch of lines.
fn initial_sort(reader: Box<dyn BufRead>, params: &SortParameters) -> Result<InitialSortResult, String> {
    let mut reader = reader;
    if params.progress {
        eprintln!("Initial sort: {} records per file", params.records_per_file);
    }

    let mut workers: Vec<Option<JoinHandle<Result<Arc<TempFile>, String>>>> = Vec::with_capacity(params.threads);
    for _ in 0..params.threads {
        workers.push(None);
    }
    let mut outputs = Vec::new();
    let mut join = |worker: Option<JoinHandle<Result<Arc<TempFile>, String>>>, thread: usize| -> bool {
        if let Some(worker) = worker {
            match worker.join() {
                Ok(Ok(sorted)) => outputs.push(sorted),
                Ok(Err(e)) => {
                    eprintln!("Worker thread {} failed: {}", thread, e);
                    return false;
                },
                Err(_) => {
                    eprintln!("Worker thread {} panicked", thread);
                    return false;
                },
            };
        }
        true
    };

    let mut batch = 0;
    let mut total_records = 0;
    let mut ok = true;
    loop {
        let mut lines = Vec::new();
        for _ in 0..params.records_per_file {
            let mut line = Vec::new();
            match reader.read_until(b'\n', &mut line) {
                Ok(0) => break,
                Ok(_) => {
                    if !line.is_empty() && line[0] != b'@' {
                        lines.push(line.clone());
                    }
                }
                Err(e) => {
                    eprintln!("Failed to read input: {}", e);
                    ok = false;
                    break;
                },
            }
        }
        if !ok {
            break;
        }
        total_records += lines.len();
        if lines.is_empty() {
            break;
        }

        if batch == 0 {
            let buffer = reader.fill_buf().map_err(|e| format!("Failed to read input: {}", e))?;
            if buffer.is_empty() {
                // No more data after the first batch.
                return Ok(InitialSortResult::SingleBatch(lines));
            }
        }
        let thread = batch % params.threads;
        if workers[thread].is_some() {
            // Wait for the worker to finish before starting a new one.
            if !join(workers[thread].take(), thread) {
                ok = false;
                break;
            }
        }
        let key_type = params.key_type;
        let stable = params.stable;
        workers[thread] = Some(std::thread::spawn(move || sort_to_temp(lines, key_type, stable)));
        batch += 1;
    }

    for (thread, worker) in workers.into_iter().enumerate() {
        if !join(worker, thread) {
            ok = false;
        }
    }

    if !ok {
        return Err(String::from("Initial sort failed"));
    }
    if params.progress {
        eprintln!(
            "Initial sort finished with {} records in {} files",
            total_records,
            outputs.len()
        );
    }
    Ok(InitialSortResult::TempFiles(outputs))
}

//-----------------------------------------------------------------------------

// Performs one round of merges and returns a new set of temporary files.
fn merge_round(inputs: Vec<Arc<TempFile>>, round: usize, params: &SortParameters) -> Result<Vec<Arc<TempFile>>, String> {
    if params.progress {
        eprintln!("Round {}: {} files per batch", round, params.files_per_merge);
    }

    let mut workers: Vec<Option<JoinHandle<Result<Arc<TempFile>, String>>>> = Vec::with_capacity(params.threads);
    for _ in 0..params.threads {
        workers.push(None);
    }
    let mut outputs = Vec::new();
    let mut join = |worker: Option<JoinHandle<Result<Arc<TempFile>, String>>>, thread: usize| -> bool {
        if let Some(worker) = worker {
            match worker.join() {
                Ok(Ok(merged)) => outputs.push(merged),
                Ok(Err(e)) => {
                    eprintln!("Worker thread {} failed: {}", thread, e);
                    return false;
                },
                Err(_) => {
                    eprintln!("Worker thread {} panicked", thread);
                    return false;
                },
            };
        }
        true
    };

    let mut i = 0;
    let mut batch = 0;
    let mut ok = true;
    while i + 1 < inputs.len() {
        let end = (i + params.files_per_merge).min(inputs.len());
        let thread = batch % params.threads;
        if workers[thread].is_some() {
            // Wait for the worker to finish before starting a new one.
            if !join(workers[thread].take(), thread) {
                ok = false;
                break;
            }
        }
        let batch_files = inputs[i..end].to_vec();
        let buffer_size = params.buffer_size;
        workers[thread] = Some(std::thread::spawn(move || merge_files(batch_files, buffer_size)));
        i = end;
        batch += 1;
    }

    for (thread, worker) in workers.into_iter().enumerate() {
        if !join(worker, thread) {
            ok = false;
        }
    }
    if i + 1 == inputs.len() {
        // If we have one file left that wasn't included in a batch, we pass it to the next round.
        outputs.push(inputs[i].clone());
    }

    if !ok {
        let msg = format!("Merge round {} failed", round);
        return Err(msg);
    }
    if params.progress {
        eprintln!("Round {} finished with {} files", round, outputs.len());
    }
    Ok(outputs)
}

//-----------------------------------------------------------------------------

// Creates a writer for the output file, or stdout if the path is "-".
fn create_output_writer(output_file: &Path) -> Result<Box<dyn Write>, String> {
    if output_file == Path::new("-") {
        Ok(Box::new(BufWriter::new(io::stdout())))
    } else {
        let file = File::create(output_file)
            .map_err(|e| format!("Failed to create output file: {}", e))?;
        Ok(Box::new(BufWriter::new(file)))
    }
}

// Sorts lines directly to output file (for single batch).
fn sort_to_output(
    lines: Vec<Vec<u8>>,
    header_lines: &[String],
    output_file: &Path,
    params: &SortParameters,
) -> Result<(), String> {
    let mut records: Vec<GAFRecord> = lines
        .into_iter()
        .map(|line| GAFRecord::new(line, params.key_type))
        .collect();

    if params.stable {
        records.sort();
    } else {
        records.sort_unstable();
    }

    let mut writer = create_output_writer(output_file)?;

    // Write header
    for line in header_lines {
        writeln!(writer, "{}", line)
            .map_err(|e| format!("Failed to write header: {}", e))?;
    }

    // Write sorted records
    for record in records {
        record.write_gaf_line(&mut writer)
            .map_err(|e| format!("Failed to write output: {}", e))?;
    }

    writer.flush()
        .map_err(|e| format!("Failed to flush output: {}", e))?;

    Ok(())
}

// Sorts lines to a temporary file.
fn sort_to_temp(lines: Vec<Vec<u8>>, key_type: KeyType, stable: bool) -> Result<Arc<TempFile>, String> {
    let mut records: Vec<GAFRecord> = lines
        .into_iter()
        .map(|line| GAFRecord::new(line, key_type))
        .collect();

    if stable {
        records.sort();
    } else {
        records.sort_unstable();
    }

    let mut temp = TempFile::create()
        .map_err(|e| format!("Failed to create temporary file: {}", e))?;
    temp.records = records.len();

    let mut writer = temp.writer()
        .map_err(|e| format!("Failed to open temporary file for writing: {}", e))?;

    for record in records {
        record.serialize(&mut writer)
            .map_err(|e| format!("Failed to write to temporary file: {}", e))?;
    }

    writer.into_inner()
        .map_err(|e| format!("Failed to finish compression: {}", e))?
        .finish()
        .map_err(|e| format!("Failed to finish compression: {}", e))?;

    Ok(Arc::new(temp))
}

fn flip_source_index(index: usize, total: usize) -> usize {
    total - 1 - index
}

// Merges multiple temporary files into a new temporary file.
fn merge_files(inputs: Vec<Arc<TempFile>>, buffer_size: usize) -> Result<Arc<TempFile>, String> {
    let mut output = TempFile::create()
        .map_err(|e| format!("Failed to create temporary file: {}", e))?;

    // Open all input files
    let mut readers: Vec<_> = inputs
        .iter()
        .map(|temp| temp.reader())
        .collect::<Result<_, _>>()
        .map_err(|e| format!("Failed to open temporary file for reading: {}", e))?;

    let mut buffers: Vec<VecDeque<GAFRecord>> = vec![VecDeque::new(); readers.len()];
    let mut remaining: Vec<usize> = inputs.iter().map(|t| t.records).collect();

    // Helper to read a buffer
    let read_buffer = |reader_idx: usize,
                       readers: &mut Vec<_>,
                       buffers: &mut Vec<VecDeque<GAFRecord>>,
                       remaining: &mut Vec<usize>|
     -> Result<(), String> {
        let count = remaining[reader_idx].min(buffer_size);
        if count > 0 {
            buffers[reader_idx].clear();
            for _ in 0..count {
                let mut record = GAFRecord::deserialize(&mut readers[reader_idx])
                    .map_err(|e| format!("Failed to read from temporary file: {}", e))?;
                record.flip_key(); // Flip for priority queue
                buffers[reader_idx].push_back(record);
            }
            remaining[reader_idx] -= count;
        }
        Ok(())
    };

    // Read initial buffers
    for i in 0..readers.len() {
        read_buffer(i, &mut readers, &mut buffers, &mut remaining)?;
    }

    // Priority queue for merge (max heap, but we flipped keys, and now we also flip the source index for stability)
    let mut heap = BinaryHeap::new();
    for (i, buffer) in buffers.iter_mut().enumerate() {
        if let Some(record) = buffer.pop_front() {
            heap.push((record, flip_source_index(i, inputs.len())));
        }
    }

    // Open output
    let mut writer = output.writer()
        .map_err(|e| format!("Failed to open output temporary file for writing: {}", e))?;
    let mut out_buffer = Vec::new();

    // Merge loop
    while let Some((mut record, mut source)) = heap.pop() {
        // Restore original key and source index.
        record.flip_key();
        source = flip_source_index(source, inputs.len());
        out_buffer.push(record);

        // Write buffer if full
        if out_buffer.len() >= buffer_size {
            for rec in out_buffer.drain(..) {
                rec.serialize(&mut writer)
                    .map_err(|e| format!("Failed to write to output: {}", e))?;
                output.records += 1;
            }
        }

        // Refill source buffer if empty
        if buffers[source].is_empty() && remaining[source] > 0 {
            read_buffer(source, &mut readers, &mut buffers, &mut remaining)?;
        }

        // Add next record from source
        if let Some(next) = buffers[source].pop_front() {
            heap.push((next, flip_source_index(source, inputs.len())));
        }
    }

    // Write remaining output buffer
    for rec in out_buffer {
        rec.serialize(&mut writer)
            .map_err(|e| format!("Failed to write to output: {}", e))?;
        output.records += 1;
    }

    writer.into_inner()
        .map_err(|e| format!("Failed to finish compression: {}", e))?
        .finish()
        .map_err(|e| format!("Failed to finish compression: {}", e))?;

    Ok(Arc::new(output))
}

// Merges temporary files to the final output file.
//
// Returns the total number of records merged.
fn merge_to_output(
    inputs: &[Arc<TempFile>],
    header_lines: &[String],
    output_file: &Path,
    params: &SortParameters,
) -> Result<usize, String> {
    let mut writer = create_output_writer(output_file)?;

    // Write header
    for line in header_lines {
        writeln!(writer, "{}", line)
            .map_err(|e| format!("Failed to write header: {}", e))?;
    }

    // Open all input files
    let mut readers: Vec<_> = inputs
        .iter()
        .map(|temp| temp.reader())
        .collect::<Result<_, _>>()
        .map_err(|e| format!("Failed to open temporary file for reading: {}", e))?;
    let total_records: usize = inputs.iter().map(|t| t.records).sum();

    let mut buffers: Vec<VecDeque<GAFRecord>> = vec![VecDeque::new(); readers.len()];
    let mut remaining: Vec<usize> = inputs.iter().map(|t| t.records).collect();

    // Helper to read a buffer
    let read_buffer = |reader_idx: usize,
                       readers: &mut Vec<_>,
                       buffers: &mut Vec<VecDeque<GAFRecord>>,
                       remaining: &mut Vec<usize>|
     -> Result<(), String> {
        let count = remaining[reader_idx].min(params.buffer_size);
        if count > 0 {
            buffers[reader_idx].clear();
            for _ in 0..count {
                let mut record = GAFRecord::deserialize(&mut readers[reader_idx])
                    .map_err(|e| format!("Failed to read from temporary file: {}", e))?;
                record.flip_key();
                buffers[reader_idx].push_back(record);
            }
            remaining[reader_idx] -= count;
        }
        Ok(())
    };

    // Read initial buffers
    for i in 0..readers.len() {
        read_buffer(i, &mut readers, &mut buffers, &mut remaining)?;
    }

    // Priority queue for merge (max heap, but we flipped keys, and now we also flip the source index for stability)
    let mut heap = BinaryHeap::new();
    for (i, buffer) in buffers.iter_mut().enumerate() {
        if let Some(record) = buffer.pop_front() {
            heap.push((record, flip_source_index(i, inputs.len())));
        }
    }

    // Merge loop
    let mut out_buffer = Vec::new();
    while let Some((mut record, mut source)) = heap.pop() {
        record.flip_key();
        source = flip_source_index(source, inputs.len());
        out_buffer.push(record);

        // Write buffer if full
        if out_buffer.len() >= params.buffer_size {
            for rec in out_buffer.drain(..) {
                rec.write_gaf_line(&mut writer)
                    .map_err(|e| format!("Failed to write to output: {}", e))?;
            }
        }

        // Refill source buffer if empty
        if buffers[source].is_empty() && remaining[source] > 0 {
            read_buffer(source, &mut readers, &mut buffers, &mut remaining)?;
        }

        // Add next record from source
        if let Some(next) = buffers[source].pop_front() {
            heap.push((next, flip_source_index(source, inputs.len())));
        }
    }

    // Write remaining output buffer
    for rec in out_buffer {
        rec.write_gaf_line(&mut writer)
            .map_err(|e| format!("Failed to write to output: {}", e))?;
    }

    writer.flush()
        .map_err(|e| format!("Failed to flush output: {}", e))?;

    Ok(total_records)
}

//-----------------------------------------------------------------------------