galah 0.5.0

Microbial genome dereplicator
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
use std;
use std::io::BufReader;
use std::io::Write;

use crate::sorted_pair_genome_distance_cache::SortedPairGenomeDistanceCache;
use crate::ClusterDistanceFinder;
use crate::PreclusterDistanceFinder;

use bird_tool_utils::command::finish_command_safely;
use tempfile;

pub struct SkaniPreclusterer {
    pub threshold: f32,
    pub min_aligned_threshold: f32,
    pub small_genomes: bool,
    pub threads: u16,
    pub low_memory: bool,
}

impl PreclusterDistanceFinder for SkaniPreclusterer {
    fn distances(&self, genome_fasta_paths: &[&str]) -> SortedPairGenomeDistanceCache {
        if self.low_memory {
            precluster_skani_lowmem(
                genome_fasta_paths,
                self.threshold,
                self.min_aligned_threshold,
                self.small_genomes,
                self.threads,
            )
        } else {
            precluster_skani(
                genome_fasta_paths,
                self.threshold,
                self.min_aligned_threshold,
                self.small_genomes,
                self.threads,
            )
        }
    }

    fn distances_contigs(
        &self,
        genome_fasta_paths: &[&str],
        contig_names: &[&str],
    ) -> SortedPairGenomeDistanceCache {
        precluster_skani_contigs(
            genome_fasta_paths,
            self.threshold,
            self.min_aligned_threshold,
            self.small_genomes,
            self.threads,
            contig_names,
        )
    }

    fn distances_with_references(
        &self,
        genome_fasta_paths: &[&str],
        reference_genomes: &[&str],
    ) -> SortedPairGenomeDistanceCache {
        precluster_skani_with_references(
            genome_fasta_paths,
            reference_genomes,
            self.threshold,
            self.min_aligned_threshold,
            self.small_genomes,
            self.threads,
        )
    }

    fn method_name(&self) -> &str {
        "skani"
    }
}

/// Copy a FASTA file to a new tempfile, replacing any tab characters in header
/// lines with spaces. Returns the tempfile (kept alive by the caller).
/// skani uses TSV output, so tabs in sequence headers corrupt its output format.
/// Uses needletail to handle both plain and gzip-compressed FASTA files transparently.
fn sanitize_fasta_headers(fasta_path: &str) -> tempfile::TempPath {
    let mut tf = tempfile::Builder::new()
        .prefix("galah-sanitized-fasta")
        .suffix(".fna")
        .tempfile()
        .expect("Failed to create temporary file for sanitized fasta");

    let mut reader = needletail::parse_fastx_file(fasta_path)
        .unwrap_or_else(|e| panic!("Failed to open fasta file {}: {}", fasta_path, e));

    while let Some(record) = reader.next() {
        let record =
            record.unwrap_or_else(|e| panic!("Failed to parse record in {}: {}", fasta_path, e));
        let header = std::str::from_utf8(record.id())
            .unwrap_or_else(|e| panic!("Non-UTF8 header in {}: {}", fasta_path, e))
            .replace('\t', " ");
        writeln!(tf, ">{}", header).expect("Failed to write header to sanitized fasta tempfile");
        tf.write_all(&record.seq())
            .expect("Failed to write sequence to sanitized fasta tempfile");
        writeln!(tf).expect("Failed to write newline to sanitized fasta tempfile");
    }

    // Close the file handle (but keep the file on disk) to avoid exhausting
    // the OS file descriptor limit when sanitizing tens of thousands of genomes.
    tf.flush()
        .expect("Failed to flush sanitized fasta tempfile");
    tf.into_temp_path()
}

fn precluster_skani(
    genome_fasta_paths: &[&str],
    threshold: f32,
    min_aligned_threshold: f32,
    small_genomes: bool,
    threads: u16,
) -> SortedPairGenomeDistanceCache {
    if threshold < 85.0 {
        panic!(
            "Error: skani produces inaccurate results with ANI less than 85%. Provided: {}",
            threshold
        );
    }

    // Sanitize FASTA headers to remove tabs, which corrupt skani's TSV output
    let sanitized: Vec<tempfile::TempPath> = genome_fasta_paths
        .iter()
        .map(|p| sanitize_fasta_headers(p))
        .collect();

    // Create a tempfile to list all the sanitized fasta file paths
    let mut tf = tempfile::Builder::new()
        .prefix("galah-input-genomes")
        .suffix(".txt")
        .tempfile()
        .expect("Failed to open temporary file to run skani");

    for sf in &sanitized {
        writeln!(tf, "{}", sf.to_str().unwrap())
            .expect("Failed to write sanitized genome fasta paths to tempfile for skani");
    }

    // --sparse only outputs non-zero entries in an edge-list output
    // Ref_file Query_file ANI Align_fraction_ref Align_fraction_query Ref_name Query_name
    info!("Running skani to get distances ..");
    let mut cmd = std::process::Command::new("skani");
    cmd.arg("triangle")
        .arg("-t")
        .arg(format!("{threads}"))
        .arg("--sparse")
        .arg("--min-af")
        .arg(format!("{}", min_aligned_threshold * 100.0));

    if small_genomes {
        cmd.arg("--small-genomes");
    }

    cmd.arg("-l")
        .arg(tf.path().to_str().unwrap())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped());
    debug!("Running skani command: {:?}", &cmd);

    // Parse the distances
    let mut process = cmd
        .spawn()
        .unwrap_or_else(|_| panic!("Failed to spawn {}", "skani"));
    let stdout = process.stdout.as_mut().unwrap();
    let stdout_reader = BufReader::new(stdout);

    let mut rdr = csv::ReaderBuilder::new()
        .delimiter(b'\t')
        .has_headers(true)
        .from_reader(stdout_reader);

    let mut distances = SortedPairGenomeDistanceCache::new();

    // Order is likely not conserved, so need to keep track.
    // Map sanitized tempfile paths back to original genome_fasta_paths indices.
    for record_res in rdr.records() {
        match record_res {
            Ok(record) => {
                debug!("Found skani record {:?}", record);

                // Match sanitized path back to original index
                let genome_id1 = sanitized
                    .iter()
                    .position(|sf| sf.to_str().unwrap() == &record[0])
                    .unwrap_or_else(|| {
                        panic!(
                            "Failed to find sanitized genome path in sanitized list: {}",
                            &record[0]
                        )
                    });
                let genome_id2 = sanitized
                    .iter()
                    .position(|sf| sf.to_str().unwrap() == &record[1])
                    .unwrap_or_else(|| {
                        panic!(
                            "Failed to find sanitized genome path in sanitized list: {}",
                            &record[1]
                        )
                    });

                let ani: f32 = record[2].parse().unwrap_or_else(|_| {
                    panic!("Failed to convert skani ANI to float value: {}", &record[2])
                });
                trace!("Found ANI {}", ani);
                if ani >= threshold {
                    trace!("Accepting ANI since it passed threshold");
                    distances.insert((genome_id1, genome_id2), Some(ani))
                }
            }
            Err(e) => {
                error!("Error parsing skani output: {}", e);
                std::process::exit(1);
            }
        }
    }
    finish_command_safely(process, "skani")
        .wait()
        .expect("Unexpected wait failure outside bird_tool_utils for skani");
    debug!("Found skani distances: {:#?}", distances);
    info!("Finished skani triangle.");

    distances
}

/// Create preclusters using skani in low-memory mode by sketching all genomes
/// then searching all genomes against the sketch database.
fn precluster_skani_lowmem(
    genome_fasta_paths: &[&str],
    threshold: f32,
    min_aligned_threshold: f32,
    small_genomes: bool,
    threads: u16,
) -> SortedPairGenomeDistanceCache {
    if threshold < 85.0 {
        panic!(
            "Error: skani produces inaccurate results with ANI less than 85%. Provided: {}",
            threshold
        );
    }

    if small_genomes {
        panic!("Error: skani does not support small genomes with low-memory preclustering");
    }

    // Sanitize FASTA headers to remove tabs, which corrupt skani's TSV output
    let sanitized: Vec<tempfile::TempPath> = genome_fasta_paths
        .iter()
        .map(|p| sanitize_fasta_headers(p))
        .collect();

    // Create a tempfile to list all the sanitized fasta file paths
    let mut tf = tempfile::Builder::new()
        .prefix("galah-input-genomes")
        .suffix(".txt")
        .tempfile()
        .expect("Failed to open temporary file to run skani");

    for sf in &sanitized {
        writeln!(tf, "{}", sf.to_str().unwrap())
            .expect("Failed to write sanitized genome fasta paths to tempfile for skani");
    }

    // Create a tempdir to store all genome sketches
    let db_dir =
        tempfile::TempDir::new().expect("Failed to create temporary directory for skani sketches");

    info!("Running skani to sketch genomes for low-memory mode ..");
    let mut cmd_sketch = std::process::Command::new("skani");
    cmd_sketch
        .arg("sketch")
        .arg("-t")
        .arg(format!("{threads}"))
        .arg("-l")
        .arg(tf.path().to_str().unwrap())
        .arg("-o")
        .arg(db_dir.path().join("galah-skani").to_str().unwrap())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null());
    debug!("Running skani command: {:?}", &cmd_sketch);

    let mut process_sketch = cmd_sketch
        .spawn()
        .unwrap_or_else(|_| panic!("Failed to spawn {}", "skani"));

    // Wait for sketching to complete and check the result
    process_sketch
        .wait()
        .expect("Failed to wait for skani sketch");

    info!("Running skani search to get distances ..");
    let mut cmd = std::process::Command::new("skani");
    cmd.arg("search")
        .arg("-t")
        .arg(format!("{threads}"))
        .arg("--min-af")
        .arg(format!("{}", min_aligned_threshold * 100.0))
        .arg("--ql")
        .arg(tf.path().to_str().unwrap())
        .arg("-d")
        .arg(db_dir.path().join("galah-skani").to_str().unwrap())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::null());
    debug!("Running skani command: {:?}", &cmd);

    // Parse the distances
    // Ref_file Query_file ANI Align_fraction_ref Align_fraction_query Ref_name Query_name
    let mut process = cmd
        .spawn()
        .unwrap_or_else(|_| panic!("Failed to spawn {}", "skani"));
    let stdout = process.stdout.as_mut().unwrap();
    let stdout_reader = BufReader::new(stdout);

    let mut rdr = csv::ReaderBuilder::new()
        .delimiter(b'\t')
        .has_headers(true)
        .from_reader(stdout_reader);

    let mut distances = SortedPairGenomeDistanceCache::new();

    // Order is likely not conserved, so need to keep track
    // Map sanitized tempfile paths back to original genome_fasta_paths indices.
    for record_res in rdr.records() {
        match record_res {
            Ok(record) => {
                debug!("Found skani record {:?}", record);

                if record[0] == record[1] {
                    // Ignore self matches
                    continue;
                }

                // Match sanitized path back to original index
                let genome_id1 = sanitized
                    .iter()
                    .position(|sf| sf.to_str().unwrap() == &record[0])
                    .unwrap_or_else(|| {
                        panic!(
                            "Failed to find sanitized genome path in sanitized list: {}",
                            &record[0]
                        )
                    });
                let genome_id2 = sanitized
                    .iter()
                    .position(|sf| sf.to_str().unwrap() == &record[1])
                    .unwrap_or_else(|| {
                        panic!(
                            "Failed to find sanitized genome path in sanitized list: {}",
                            &record[1]
                        )
                    });

                let ani: f32 = record[2].parse().unwrap_or_else(|_| {
                    panic!("Failed to convert skani ANI to float value: {}", &record[2])
                });
                trace!("Found ANI {}", ani);
                if ani >= threshold {
                    trace!("Accepting ANI since it passed threshold");
                    distances.insert((genome_id1, genome_id2), Some(ani))
                }
            }
            Err(e) => {
                error!("Error parsing skani output: {}", e);
                std::process::exit(1);
            }
        }
    }

    finish_command_safely(process, "skani")
        .wait()
        .expect("Unexpected wait failure outside bird_tool_utils for skani");
    debug!("Found skani distances (low-memory): {:#?}", distances);
    info!("Finished skani low-memory search.");

    distances
}

fn precluster_skani_contigs(
    genome_fasta_paths: &[&str],
    threshold: f32,
    min_aligned_threshold: f32,
    small_genomes: bool,
    threads: u16,
    contig_names: &[&str],
) -> SortedPairGenomeDistanceCache {
    if threshold < 85.0 {
        panic!(
            "Error: skani produces inaccurate results with ANI less than 85%. Provided: {}",
            threshold
        );
    }

    // Sanitize FASTA headers to remove tabs, which corrupt skani's TSV output
    let sanitized: Vec<tempfile::TempPath> = genome_fasta_paths
        .iter()
        .map(|p| sanitize_fasta_headers(p))
        .collect();

    // Create a tempfile to list all the sanitized fasta file paths
    let mut tf = tempfile::Builder::new()
        .prefix("galah-input-genomes")
        .suffix(".txt")
        .tempfile()
        .expect("Failed to open temporary file to run skani");

    for sf in &sanitized {
        writeln!(tf, "{}", sf.to_str().unwrap())
            .expect("Failed to write sanitized genome fasta paths to tempfile for skani");
    }

    // --sparse only outputs non-zero entries in an edge-list output
    info!("Running skani to get distances ..");
    let mut cmd = std::process::Command::new("skani");
    cmd.arg("triangle")
        .arg("-i")
        .arg("-t")
        .arg(format!("{threads}"))
        .arg("--sparse")
        .arg("--min-af")
        .arg(format!("{}", min_aligned_threshold * 100.0));

    if small_genomes {
        cmd.arg("--small-genomes");
    }

    cmd.arg("-l")
        .arg(tf.path().to_str().unwrap())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped());
    debug!("Running skani command: {:?}", &cmd);

    // Parse the distances
    let mut process = cmd
        .spawn()
        .unwrap_or_else(|_| panic!("Failed to spawn {}", "skani"));
    let stdout = process.stdout.as_mut().unwrap();
    let stdout_reader = BufReader::new(stdout);

    let mut rdr = csv::ReaderBuilder::new()
        .delimiter(b'\t')
        .has_headers(true)
        .from_reader(stdout_reader);

    let mut distances = SortedPairGenomeDistanceCache::new();

    // Returning contig names from Ref_name and Query_name (columns 5 and 6).
    // These are sanitized (tabs replaced with spaces), matching what was written
    // into the sanitized FASTA headers.
    // Ref_file Query_file ANI Align_fraction_ref Align_fraction_query Ref_name Query_name
    for record_res in rdr.records() {
        match record_res {
            Ok(record) => {
                debug!("Found skani record {:?}", record);

                // Get index of contigs within contig_names.
                // Note: contig names with tabs will have had them replaced with
                // spaces in the sanitized FASTA, so we match against the
                // sanitized form here.
                let sanitized_ref = record[5].replace('\t', " ");
                let sanitized_query = record[6].replace('\t', " ");

                let contig_id1 = contig_names
                    .iter()
                    .position(|&x| x.replace('\t', " ") == sanitized_ref)
                    .unwrap_or_else(|| {
                        panic!("Failed to find contig name in contig_names: {}", &record[5])
                    });
                let contig_id2 = contig_names
                    .iter()
                    .position(|&x| x.replace('\t', " ") == sanitized_query)
                    .unwrap_or_else(|| {
                        panic!("Failed to find contig name in contig_names: {}", &record[6])
                    });

                let ani: f32 = record[2].parse().unwrap_or_else(|_| {
                    panic!("Failed to convert skani ANI to float value: {}", &record[2])
                });
                trace!("Found ANI {}", ani);
                if ani >= threshold {
                    trace!("Accepting ANI since it passed threshold");
                    distances.insert((contig_id1, contig_id2), Some(ani))
                }
            }
            Err(e) => {
                error!("Error parsing skani output: {}", e);
                std::process::exit(1);
            }
        }
    }
    finish_command_safely(process, "skani")
        .wait()
        .expect("Unexpected wait failure outside bird_tool_utils for skani");
    debug!("Found skani distances: {:#?}", distances);
    info!("Finished skani triangle.");

    distances
}

/// Create preclusters based on reference genomes using skani
// Assumes that both genome sets are already independently dereplicated
fn precluster_skani_with_references(
    combined_genomes: &[&str],
    reference_genomes: &[&str],
    threshold: f32,
    min_aligned_threshold: f32,
    small_genomes: bool,
    threads: u16,
) -> SortedPairGenomeDistanceCache {
    if threshold < 85.0 {
        panic!(
            "Error: skani produces inaccurate results with ANI less than 85%. Provided: {}",
            threshold
        );
    }

    if small_genomes {
        panic!("Error: skani does not support small genomes with reference genome preclustering");
    }

    // Sanitize reference FASTA headers to remove tabs, which corrupt skani's TSV output
    let sanitized_refs: Vec<tempfile::TempPath> = reference_genomes
        .iter()
        .map(|p| sanitize_fasta_headers(p))
        .collect();

    // Create a tempfile to list all the reference file paths
    let mut tf_ref = tempfile::Builder::new()
        .prefix("galah-input-reference-genomes")
        .suffix(".txt")
        .tempfile()
        .expect("Failed to open temporary file to run skani");

    for sf in &sanitized_refs {
        writeln!(tf_ref, "{}", sf.to_str().unwrap())
            .expect("Failed to write sanitized reference genome fasta paths to tempfile for skani");
    }

    // Create a tempdir to store the reference genome sketches
    let ref_db = tempfile::TempDir::new()
        .expect("Failed to create temporary directory for skani reference genomes");

    info!("Running skani to sketch reference genomes ..");
    let mut cmd_sketch = std::process::Command::new("skani");
    cmd_sketch
        .arg("sketch")
        .arg("-t")
        .arg(format!("{threads}"))
        .arg("-l")
        .arg(tf_ref.path().to_str().unwrap())
        .arg("-o")
        .arg(ref_db.path().join("galah-skani").to_str().unwrap())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null());
    debug!("Running skani command: {:?}", &cmd_sketch);

    let mut process_sketch = cmd_sketch
        .spawn()
        .unwrap_or_else(|_| panic!("Failed to spawn {}", "skani"));

    // Wait for sketching to complete and check the result
    process_sketch
        .wait()
        .expect("Failed to wait for skani sketch");

    // Sanitize non-reference combined genome FASTA headers
    let sanitized_combined: Vec<(&&str, tempfile::TempPath)> = combined_genomes
        .iter()
        .filter(|fasta| !reference_genomes.contains(fasta))
        .map(|p| (p, sanitize_fasta_headers(p)))
        .collect();

    // Create a tempfile to list all the non-reference genome file paths
    let mut tf = tempfile::Builder::new()
        .prefix("galah-input-genomes")
        .suffix(".txt")
        .tempfile()
        .expect("Failed to open temporary file to run skani");

    for (_, sf) in &sanitized_combined {
        writeln!(tf, "{}", sf.to_str().unwrap())
            .expect("Failed to write sanitized genome fasta paths to tempfile for skani");
    }

    info!("Running skani search to get distances ..");
    let mut cmd = std::process::Command::new("skani");
    cmd.arg("search")
        .arg("-t")
        .arg(format!("{threads}"))
        .arg("--min-af")
        .arg(format!("{}", min_aligned_threshold * 100.0))
        .arg("--ql")
        .arg(tf.path().to_str().unwrap())
        .arg("-d")
        .arg(ref_db.path().join("galah-skani").to_str().unwrap())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::null());
    debug!("Running skani command: {:?}", &cmd);

    // Parse the distances
    // Ref_file Query_file ANI Align_fraction_ref Align_fraction_query Ref_name Query_name
    let mut process = cmd
        .spawn()
        .unwrap_or_else(|_| panic!("Failed to spawn {}", "skani"));
    let stdout = process.stdout.as_mut().unwrap();
    let stdout_reader = BufReader::new(stdout);

    let mut rdr = csv::ReaderBuilder::new()
        .delimiter(b'\t')
        .has_headers(true)
        .from_reader(stdout_reader);

    let mut distances = SortedPairGenomeDistanceCache::new();

    // Order is likely not conserved, so need to keep track.
    // Map sanitized paths back to indices in combined_genomes.
    for record_res in rdr.records() {
        match record_res {
            Ok(record) => {
                debug!("Found skani record {:?}", record);

                // record[0] is a reference (sanitized), record[1] is a query (sanitized)
                let genome_id1 = sanitized_refs
                    .iter()
                    .position(|sf| sf.to_str().unwrap() == &record[0])
                    .map(|i| {
                        combined_genomes
                            .iter()
                            .position(|&g| g == reference_genomes[i])
                            .unwrap_or_else(|| {
                                panic!(
                                    "Failed to find reference genome in combined_genomes: {}",
                                    reference_genomes[i]
                                )
                            })
                    })
                    .unwrap_or_else(|| {
                        panic!(
                            "Failed to find sanitized reference path in sanitized_refs: {}",
                            &record[0]
                        )
                    });

                let genome_id2 = sanitized_combined
                    .iter()
                    .position(|(_, sf)| sf.to_str().unwrap() == &record[1])
                    .map(|i| {
                        combined_genomes
                            .iter()
                            .position(|&g| g == *sanitized_combined[i].0)
                            .unwrap_or_else(|| {
                                panic!(
                                    "Failed to find genome in combined_genomes: {}",
                                    sanitized_combined[i].0
                                )
                            })
                    })
                    .unwrap_or_else(|| {
                        panic!(
                            "Failed to find sanitized genome path in sanitized_combined: {}",
                            &record[1]
                        )
                    });

                let ani: f32 = record[2].parse().unwrap_or_else(|_| {
                    panic!("Failed to convert skani ANI to float value: {}", &record[2])
                });
                trace!("Found ANI {}", ani);
                if ani >= threshold {
                    trace!("Accepting ANI since it passed threshold");
                    distances.insert((genome_id1, genome_id2), Some(ani))
                }
            }
            Err(e) => {
                error!("Error parsing skani output: {}", e);
                std::process::exit(1);
            }
        }
    }
    finish_command_safely(process, "skani")
        .wait()
        .expect("Unexpected wait failure outside bird_tool_utils for skani");
    debug!("Found skani distances: {:#?}", distances);
    info!("Finished skani dist to references.");

    distances
}

pub struct SkaniClusterer {
    pub threshold: f32,
    pub min_aligned_threshold: f32,
    pub small_genomes: bool,
}

impl ClusterDistanceFinder for SkaniClusterer {
    fn initialise(&self) {
        assert!(self.threshold > 1.0);
    }

    fn method_name(&self) -> &str {
        "skani"
    }

    fn get_ani_threshold(&self) -> f32 {
        self.threshold
    }

    fn calculate_ani(&self, fasta1: &str, fasta2: &str) -> Option<f32> {
        Some(calculate_skani(
            fasta1,
            fasta2,
            self.small_genomes,
            self.min_aligned_threshold,
        ))
    }
}

pub fn calculate_skani(
    fasta1: &str,
    fasta2: &str,
    small_genomes: bool,
    min_aligned_threshold: f32,
) -> f32 {
    // Sanitize FASTA headers to remove tabs, which corrupt skani's TSV output
    let sf1 = sanitize_fasta_headers(fasta1);
    let sf2 = sanitize_fasta_headers(fasta2);

    // --sparse only outputs non-zero entries in an edge-list output
    // Ref_file Query_file ANI Align_fraction_ref Align_fraction_query Ref_name Query_name
    let mut cmd = std::process::Command::new("skani");
    cmd.arg("dist")
        .arg("--min-af")
        .arg(format!("{}", min_aligned_threshold * 100.0));

    if small_genomes {
        cmd.arg("--small-genomes");
    }

    cmd.arg("-q")
        .arg(&sf1)
        .arg("-r")
        .arg(&sf2)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped());
    debug!("Running skani command: {:?}", &cmd);

    // Parse the distances
    let mut process = cmd
        .spawn()
        .unwrap_or_else(|_| panic!("Failed to spawn {}", "skani"));
    let stdout = process.stdout.as_mut().unwrap();
    let stdout_reader = BufReader::new(stdout);

    let mut rdr = csv::ReaderBuilder::new()
        .delimiter(b'\t')
        .has_headers(true)
        .from_reader(stdout_reader);

    let mut num_records = 0;
    let mut to_return = 0.0;

    for record_res in rdr.records() {
        match record_res {
            Ok(record) => {
                if num_records > 0 {
                    error!("Unexpectedly found >1 result from skani");
                    std::process::exit(1);
                }

                assert!(record.len() == 7);
                to_return = record[2].parse().unwrap_or_else(|_| {
                    panic!("Failed to convert skani ANI to float value: {}", &record[2])
                });
                num_records += 1;
            }
            Err(e) => {
                error!("Error parsing skani output: {}", e);
                std::process::exit(1);
            }
        }
    }

    debug!("skani of {} against {} was {:?}", fasta1, fasta2, to_return);
    finish_command_safely(process, "skani")
        .wait()
        .expect("Unexpected wait failure outside bird_tool_utils for skani");
    to_return
}

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

    fn init() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    #[test]
    #[should_panic(
        expected = "Error: skani produces inaccurate results with ANI less than 85%. Provided: 80"
    )]
    fn test_precluster_skani_with_low_ani() {
        init();
        precluster_skani(
            &[
                "tests/data/abisko4/73.20120800_S1X.13.fna",
                "tests/data/abisko4/73.20120600_S2D.19.fna",
                "tests/data/abisko4/73.20120700_S3X.12.fna",
                "tests/data/abisko4/73.20110800_S2D.13.fna",
            ],
            80.0,
            0.2,
            false,
            1,
        );
    }

    #[test]
    fn test_precluster_skani_with_valid_ani() {
        init();
        precluster_skani(
            &[
                "tests/data/abisko4/73.20120800_S1X.13.fna",
                "tests/data/abisko4/73.20120600_S2D.19.fna",
                "tests/data/abisko4/73.20120700_S3X.12.fna",
                "tests/data/abisko4/73.20110800_S2D.13.fna",
            ],
            95.0,
            0.2,
            false,
            1,
        );
    }

    #[test]
    fn test_precluster_skani_with_tab_in_headers() {
        init();
        // Genomes with tab characters in FASTA headers should not cause parse errors
        precluster_skani(
            &[
                "tests/data/abisko_tabs/73.20120800_S1D.21.fna",
                "tests/data/abisko_tabs/73.20110800_S2M.16.fna",
            ],
            95.0,
            0.2,
            false,
            1,
        );
    }
}