taxutils 1.1.7

Rust utilities for NCBI taxonomy data and FASTA files
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
use std::collections::{BTreeSet, HashMap, HashSet};

use anyhow::{Result, bail};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};

use crate::resources::{TaxutilsOptions, load_taxutils};

pub type TaxonId = i64;
pub(crate) const RANK_CODES: [&str; 10] = ["U", "R", "D", "K", "P", "C", "O", "F", "G", "S"];

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaxonNode {
    pub taxon: TaxonId,
    pub parent: Option<TaxonId>,
    /// Original, lower-case NCBI rank.
    pub rank: String,
    /// Corrected rank code, including subranks (`F2`, `S3`, ...).
    pub rank_code: String,
    /// Canonical base rank code.
    pub rank_base: char,
    /// Canonical rank order index.
    pub rank_idx: u8,
    /// Corrected canonical rank name.
    pub new_rank: String,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TopologyStat {
    NTaxa,
    NLeaves,
    MaxDepth,
    MeanDepth,
    TopologyScale,
    MaxChildren,
    BranchingTaxaFraction,
    TopChildFraction,
}

impl std::str::FromStr for TopologyStat {
    type Err = anyhow::Error;
    fn from_str(value: &str) -> Result<Self> {
        match value {
            "n_taxa" => Ok(Self::NTaxa),
            "n_leaves" => Ok(Self::NLeaves),
            "max_depth" => Ok(Self::MaxDepth),
            "mean_depth" => Ok(Self::MeanDepth),
            "topology_scale" => Ok(Self::TopologyScale),
            "max_children" => Ok(Self::MaxChildren),
            "branching_taxa_fraction" => Ok(Self::BranchingTaxaFraction),
            "top_child_fraction" => Ok(Self::TopChildFraction),
            _ => bail!(
                "stat must be one of: n_taxa, n_leaves, max_depth, mean_depth, \
                 topology_scale, max_children, branching_taxa_fraction, top_child_fraction"
            ),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TopologyProfile {
    pub taxon: TaxonId,
    pub name: String,
    pub rank_code: Option<String>,
    pub anchor_taxon: TaxonId,
    pub anchor_name: String,
    pub anchor_rank_code: Option<String>,
    pub n_taxa: usize,
    pub n_leaves: usize,
    pub max_depth: usize,
    pub mean_depth: f64,
    pub topology_scale: usize,
    pub max_children: usize,
    pub branching_taxa_fraction: f64,
    pub top_child_fraction: f64,
}

#[derive(Debug)]
pub struct TaxonomicUtils {
    pub names: HashMap<TaxonId, String>,
    pub nodes: Vec<TaxonNode>,
    pub target_taxa: Vec<TaxonId>,
    pub a2t: HashMap<String, TaxonId>,
    pub parent: HashMap<TaxonId, Option<TaxonId>>,
    pub(crate) canonical: bool,
    pub(crate) low_memory: bool,
    pub(crate) wgs: bool,
    pub(crate) save_folder: std::path::PathBuf,
    pub(crate) threads: Option<usize>,
    children: HashMap<TaxonId, Vec<TaxonId>>,
    node_index: HashMap<TaxonId, usize>,
    depth: HashMap<TaxonId, usize>,
    descendant_index: HashMap<TaxonId, (usize, usize)>,
}

pub(crate) struct AccessionLookupOptions {
    pub(crate) canonical: bool,
    pub(crate) low_memory: bool,
    pub(crate) wgs: bool,
    pub(crate) save_folder: std::path::PathBuf,
    pub(crate) threads: Option<usize>,
}

impl TaxonomicUtils {
    pub fn new(options: TaxutilsOptions) -> Result<Self> {
        load_taxutils(options)
    }

    pub(crate) fn from_parts(
        names: HashMap<TaxonId, String>,
        nodes: Vec<TaxonNode>,
        target_taxa: Vec<TaxonId>,
        a2t: HashMap<String, TaxonId>,
        lookup_options: AccessionLookupOptions,
    ) -> Self {
        let mut parent = nodes
            .iter()
            .map(|n| (n.taxon, n.parent))
            .collect::<HashMap<_, _>>();
        parent.insert(1, None);
        let mut children: HashMap<TaxonId, Vec<TaxonId>> = HashMap::new();
        for node in &nodes {
            if node.taxon != 1
                && let Some(parent) = parent.get(&node.taxon).copied().flatten()
            {
                children.entry(parent).or_default().push(node.taxon);
            }
        }
        let node_index = nodes
            .iter()
            .enumerate()
            .map(|(i, node)| (node.taxon, i))
            .collect();
        let (depth, descendant_index) = build_indexes(&parent, &children);
        Self {
            names,
            nodes,
            target_taxa,
            a2t,
            parent,
            canonical: lookup_options.canonical,
            low_memory: lookup_options.low_memory,
            wgs: lookup_options.wgs,
            save_folder: lookup_options.save_folder,
            threads: lookup_options.threads,
            children,
            node_index,
            depth,
            descendant_index,
        }
    }

    pub fn parse_accession(&self, text: &str, version: bool) -> String {
        crate::accession::parse_accession(text, version)
    }

    pub fn parse_accessions<S: AsRef<str> + Sync>(
        &self,
        values: &[S],
        version: bool,
    ) -> Vec<String> {
        crate::accession::parse_accessions(values, version)
    }

    pub fn get_rank_order(&self) -> Vec<&'static str> {
        RANK_CODES.to_vec()
    }

    pub(crate) fn node(&self, taxon: TaxonId) -> Option<&TaxonNode> {
        self.node_index.get(&taxon).map(|index| &self.nodes[*index])
    }

    pub fn get_branch(&self, taxon: TaxonId) -> Vec<TaxonId> {
        let mut branch = Vec::new();
        let mut current = Some(taxon);
        let mut seen = HashSet::new();
        while let Some(node) = current {
            if !seen.insert(node) {
                break;
            }
            branch.push(node);
            current = self.parent.get(&node).copied().flatten();
        }
        branch.reverse();
        branch
    }

    pub fn get_subtree(&self, taxon: TaxonId) -> Vec<TaxonId> {
        let mut result = Vec::new();
        let mut stack = vec![taxon];
        while let Some(node) = stack.pop() {
            result.push(node);
            if let Some(children) = self.children.get(&node) {
                stack.extend(children.iter().rev().copied());
            }
        }
        result
    }

    pub fn get_ancestor(&self, taxon: TaxonId, anchor_rank: &str) -> Result<TaxonId> {
        let code = rank_to_code(anchor_rank)?;
        for ancestor in self.get_branch(taxon).into_iter().rev() {
            if self.node(ancestor).is_some_and(|n| n.rank_base == code) {
                return Ok(ancestor);
            }
        }
        Ok(taxon)
    }

    pub fn get_ancestors(&self, taxa: &[TaxonId], anchor_rank: &str) -> Result<Vec<TaxonId>> {
        taxa.par_iter()
            .map(|taxon| self.get_ancestor(*taxon, anchor_rank))
            .collect()
    }

    /// Compute topology profiles concurrently while preserving input order.
    pub fn topologies(
        &self,
        taxa: &[TaxonId],
        anchor_rank: Option<&str>,
    ) -> Result<Vec<TopologyProfile>> {
        taxa.par_iter()
            .map(|taxon| self.topology(*taxon, anchor_rank))
            .collect()
    }

    /// Batch form of [`Self::is_leaf`], evaluated in parallel in input order.
    pub fn are_leaves(&self, taxa: &[TaxonId]) -> Vec<bool> {
        taxa.par_iter().map(|taxon| self.is_leaf(*taxon)).collect()
    }

    pub fn is_leaf(&self, taxon: TaxonId) -> bool {
        !self.children.contains_key(&taxon)
    }

    pub fn is_child(&self, taxon_a: TaxonId, taxon_b: TaxonId) -> bool {
        self.parent.get(&taxon_a).copied().flatten() == Some(taxon_b)
    }

    /// Pairwise batch form of [`Self::is_child`], evaluated in parallel.
    pub fn are_children(&self, taxon_a: &[TaxonId], taxon_b: &[TaxonId]) -> Result<Vec<bool>> {
        if taxon_a.len() != taxon_b.len() {
            bail!("taxon_a and taxon_b must have the same length");
        }
        Ok(taxon_a
            .par_iter()
            .zip(taxon_b.par_iter())
            .map(|(a, b)| self.is_child(*a, *b))
            .collect())
    }

    /// Strict descendant check; a taxon is not its own descendant.
    pub fn is_descendent(&self, taxon_a: TaxonId, taxon_b: TaxonId) -> bool {
        if taxon_a == taxon_b {
            return false;
        }
        match (
            self.descendant_index.get(&taxon_a),
            self.descendant_index.get(&taxon_b),
        ) {
            (Some((a, _)), Some((b_start, b_end))) => b_start <= a && a <= b_end,
            _ => false,
        }
    }

    /// Pairwise batch form of [`Self::is_descendent`], evaluated in parallel.
    pub fn are_descendents(&self, taxon_a: &[TaxonId], taxon_b: &[TaxonId]) -> Result<Vec<bool>> {
        if taxon_a.len() != taxon_b.len() {
            bail!("taxon_a and taxon_b must have the same length");
        }
        Ok(taxon_a
            .par_iter()
            .zip(taxon_b.par_iter())
            .map(|(a, b)| self.is_descendent(*a, *b))
            .collect())
    }

    pub fn get_lca(&self, mut a: TaxonId, mut b: TaxonId) -> TaxonId {
        if a == b {
            return a;
        }
        let mut depth_a = self.depth.get(&a).copied().unwrap_or(0);
        let mut depth_b = self.depth.get(&b).copied().unwrap_or(0);
        while depth_a > depth_b {
            let Some(parent) = self.parent.get(&a).copied().flatten() else {
                return 1;
            };
            a = parent;
            depth_a -= 1;
        }
        while depth_b > depth_a {
            let Some(parent) = self.parent.get(&b).copied().flatten() else {
                return 1;
            };
            b = parent;
            depth_b -= 1;
        }
        while a != b {
            let Some(parent_a) = self.parent.get(&a).copied().flatten() else {
                return 1;
            };
            let Some(parent_b) = self.parent.get(&b).copied().flatten() else {
                return 1;
            };
            a = parent_a;
            b = parent_b;
        }
        a
    }

    pub fn get_distance(&self, a: TaxonId, b: TaxonId) -> usize {
        let lca = self.get_lca(a, b);
        self.depth.get(&a).copied().unwrap_or(0) + self.depth.get(&b).copied().unwrap_or(0)
            - 2 * self.depth.get(&lca).copied().unwrap_or(0)
    }

    pub fn higher_than_rank(&self, taxa: &[TaxonId], rank: &str) -> Result<Vec<bool>> {
        let threshold = rank_index(rank_to_code(rank)?);
        Ok(taxa
            .par_iter()
            .map(|taxon| self.node(*taxon).map_or(threshold, |n| n.rank_idx) < threshold)
            .collect())
    }

    pub fn sort_taxa<I>(&self, taxa: I) -> Vec<TaxonId>
    where
        I: IntoIterator<Item = TaxonId>,
    {
        taxonomic_order(
            taxa.into_iter().collect(),
            &self.parent,
            &self
                .nodes
                .iter()
                .map(|n| (n.taxon, n.rank_code.clone()))
                .collect(),
            &self.names,
        )
    }

    pub fn format_tree<I>(
        &self,
        taxa: I,
        include_ancestors: bool,
        root: TaxonId,
        indent: &str,
    ) -> Vec<(TaxonId, String)>
    where
        I: IntoIterator<Item = TaxonId>,
    {
        let taxa: HashSet<_> = taxa.into_iter().collect();
        let mut visible = if include_ancestors {
            HashSet::new()
        } else {
            taxa.clone()
        };
        if include_ancestors {
            for taxon in taxa {
                let mut current = Some(taxon);
                let mut seen = HashSet::new();
                while let Some(node) = current {
                    if !seen.insert(node) {
                        break;
                    }
                    visible.insert(node);
                    if node == root {
                        break;
                    }
                    current = self.parent.get(&node).copied().flatten();
                }
            }
        }
        let order = self.sort_taxa(visible.iter().copied());
        let mut rows = Vec::with_capacity(order.len());
        for taxon in order {
            let depth = self
                .get_branch(taxon)
                .into_iter()
                .filter(|ancestor| visible.contains(ancestor))
                .count()
                .saturating_sub(1);
            rows.push((
                taxon,
                format!(
                    "{}{}",
                    indent.repeat(depth),
                    self.names
                        .get(&taxon)
                        .cloned()
                        .unwrap_or_else(|| taxon.to_string())
                ),
            ));
        }
        rows
    }

    pub fn topology(&self, taxon: TaxonId, anchor_rank: Option<&str>) -> Result<TopologyProfile> {
        let anchor = match anchor_rank {
            Some(rank) => self.get_ancestor(taxon, rank)?,
            None => taxon,
        };
        let subtree = self.get_subtree(anchor);
        let subtree_set: HashSet<_> = subtree.iter().copied().collect();
        let anchor_depth = self.depth.get(&anchor).copied().unwrap_or(0);
        let mut relative_depths = subtree
            .iter()
            .map(|node| {
                self.depth
                    .get(node)
                    .copied()
                    .unwrap_or(0)
                    .saturating_sub(anchor_depth)
            })
            .collect::<Vec<_>>();
        let child_counts = subtree
            .iter()
            .map(|node| {
                self.children.get(node).map_or(0, |values| {
                    values.iter().filter(|v| subtree_set.contains(v)).count()
                })
            })
            .collect::<Vec<_>>();
        let n_taxa = subtree.len();
        let n_leaves = child_counts.iter().filter(|count| **count == 0).count();
        let max_depth = relative_depths.iter().copied().max().unwrap_or(0);
        let mean_depth = if n_taxa == 0 {
            0.0
        } else {
            relative_depths.iter().sum::<usize>() as f64 / n_taxa as f64
        };
        relative_depths.retain(|depth| *depth > 0);
        relative_depths.sort_unstable();
        let topology_scale = relative_depths
            .get((0.95 * relative_depths.len().saturating_sub(1) as f64) as usize)
            .copied()
            .unwrap_or(0)
            .max(1);
        let max_children = child_counts.iter().copied().max().unwrap_or(0);
        let branching_taxa_fraction = if n_taxa == 0 {
            0.0
        } else {
            child_counts.iter().filter(|count| **count > 0).count() as f64 / n_taxa as f64
        };
        let mut subtree_sizes = HashMap::new();
        for node in subtree.iter().rev() {
            let size = 1 + self.children.get(node).map_or(0, |children| {
                children
                    .iter()
                    .filter(|child| subtree_set.contains(child))
                    .map(|child| subtree_sizes.get(child).copied().unwrap_or(0))
                    .sum::<usize>()
            });
            subtree_sizes.insert(*node, size);
        }
        let immediate_sizes = self
            .children
            .get(&anchor)
            .into_iter()
            .flatten()
            .filter(|child| subtree_set.contains(child))
            .map(|child| subtree_sizes.get(child).copied().unwrap_or(0))
            .collect::<Vec<_>>();
        let total_child_size: usize = immediate_sizes.iter().sum();
        let top_child_fraction = if total_child_size == 0 {
            1.0
        } else {
            immediate_sizes.iter().copied().max().unwrap_or(0) as f64 / total_child_size as f64
        };
        Ok(TopologyProfile {
            taxon,
            name: self
                .names
                .get(&taxon)
                .cloned()
                .unwrap_or_else(|| taxon.to_string()),
            rank_code: self.node(taxon).map(|n| n.rank_code.clone()),
            anchor_taxon: anchor,
            anchor_name: self
                .names
                .get(&anchor)
                .cloned()
                .unwrap_or_else(|| anchor.to_string()),
            anchor_rank_code: self.node(anchor).map(|n| n.rank_code.clone()),
            n_taxa,
            n_leaves,
            max_depth,
            mean_depth,
            topology_scale,
            max_children,
            branching_taxa_fraction,
            top_child_fraction,
        })
    }

    pub fn topology_stat(
        &self,
        taxon: TaxonId,
        anchor_rank: Option<&str>,
        stat: TopologyStat,
    ) -> Result<f64> {
        let p = self.topology(taxon, anchor_rank)?;
        Ok(match stat {
            TopologyStat::NTaxa => p.n_taxa as f64,
            TopologyStat::NLeaves => p.n_leaves as f64,
            TopologyStat::MaxDepth => p.max_depth as f64,
            TopologyStat::MeanDepth => p.mean_depth,
            TopologyStat::TopologyScale => p.topology_scale as f64,
            TopologyStat::MaxChildren => p.max_children as f64,
            TopologyStat::BranchingTaxaFraction => p.branching_taxa_fraction,
            TopologyStat::TopChildFraction => p.top_child_fraction,
        })
    }

    /// Batch form of [`Self::topology_stat`], evaluated in parallel in input order.
    pub fn topology_stats(
        &self,
        taxa: &[TaxonId],
        anchor_rank: Option<&str>,
        stat: TopologyStat,
    ) -> Result<Vec<f64>> {
        taxa.par_iter()
            .map(|taxon| self.topology_stat(*taxon, anchor_rank, stat))
            .collect()
    }
}

fn build_indexes(
    parent: &HashMap<TaxonId, Option<TaxonId>>,
    children: &HashMap<TaxonId, Vec<TaxonId>>,
) -> (HashMap<TaxonId, usize>, HashMap<TaxonId, (usize, usize)>) {
    let mut depth = HashMap::new();
    let mut intervals = HashMap::new();
    let mut visited = HashSet::new();
    let mut tick: usize = 0;
    let mut roots = parent
        .iter()
        .filter_map(|(node, p)| {
            if p.is_none()
                || *p == Some(*node)
                || p.is_some_and(|value| !parent.contains_key(&value))
            {
                Some(*node)
            } else {
                None
            }
        })
        .collect::<Vec<_>>();
    roots.sort_unstable();
    roots.extend(parent.keys().copied());
    for root in roots {
        if visited.contains(&root) {
            continue;
        }
        let mut stack = vec![(root, 0, false)];
        while let Some((node, node_depth, exiting)) = stack.pop() {
            if exiting {
                if let Some((start, _)) = intervals.get(&node).copied() {
                    intervals.insert(node, (start, tick.saturating_sub(1)));
                }
                continue;
            }
            if !visited.insert(node) {
                continue;
            }
            intervals.insert(node, (tick, tick));
            depth.insert(node, node_depth);
            tick += 1;
            stack.push((node, node_depth, true));
            if let Some(values) = children.get(&node) {
                for child in values.iter().rev() {
                    stack.push((*child, node_depth + 1, false));
                }
            }
        }
    }
    (depth, intervals)
}

pub(crate) fn rank_to_code(rank: &str) -> Result<char> {
    let rank = rank.trim().to_ascii_uppercase();
    match rank.as_str() {
        "U" | "UNCLASSIFIED" => Ok('U'),
        "R" | "ROOT" => Ok('R'),
        "D" | "DOMAIN" | "SUPERKINGDOM" | "REALM" => Ok('D'),
        "K" | "KINGDOM" => Ok('K'),
        "P" | "PHYLUM" => Ok('P'),
        "C" | "CLASS" | "CLADE" => Ok('C'),
        "O" | "ORDER" => Ok('O'),
        "F" | "FAMILY" | "SUBFAMILY" => Ok('F'),
        "G" | "GENUS" => Ok('G'),
        "S" | "SPECIES" => Ok('S'),
        _ => bail!("rank must be one of: {}", RANK_CODES.join(", ")),
    }
}

pub(crate) fn rank_index(code: char) -> u8 {
    RANK_CODES
        .iter()
        .position(|candidate| candidate.starts_with(code))
        .unwrap_or(0) as u8
}

pub(crate) fn canonical_name(code: char) -> &'static str {
    match code {
        'U' => "unclassified",
        'R' => "root",
        'D' => "domain",
        'K' => "kingdom",
        'P' => "phylum",
        'C' => "class",
        'O' => "order",
        'F' => "family",
        'G' => "genus",
        'S' => "species",
        _ => "unclassified",
    }
}

pub(crate) fn major_rank_code(rank: &str) -> Option<char> {
    match rank {
        "root" | "acellular root" | "cellular root" => Some('R'),
        "domain" | "superkingdom" | "realm" => Some('D'),
        "kingdom" => Some('K'),
        "phylum" => Some('P'),
        "class" => Some('C'),
        "order" => Some('O'),
        "family" => Some('F'),
        "genus" => Some('G'),
        "species" => Some('S'),
        _ => None,
    }
}

pub(crate) fn assign_rank_codes(
    parent: &HashMap<TaxonId, Option<TaxonId>>,
    ranks: &HashMap<TaxonId, String>,
) -> HashMap<TaxonId, String> {
    fn one(
        taxon: TaxonId,
        parent: &HashMap<TaxonId, Option<TaxonId>>,
        ranks: &HashMap<TaxonId, String>,
        cache: &mut HashMap<TaxonId, String>,
        visiting: &mut HashSet<TaxonId>,
    ) -> String {
        if let Some(value) = cache.get(&taxon) {
            return value.clone();
        }
        if !visiting.insert(taxon) {
            cache.insert(taxon, "R".to_owned());
            return "R".to_owned();
        }
        let code = if taxon == 0 {
            "U".to_owned()
        } else if taxon == 1 {
            "R".to_owned()
        } else {
            let raw = ranks.get(&taxon).and_then(|rank| major_rank_code(rank));
            match parent.get(&taxon).copied().flatten() {
                None => raw.unwrap_or('R').to_string(),
                Some(p) if p == taxon || !parent.contains_key(&p) => raw.unwrap_or('R').to_string(),
                Some(p) => {
                    let parent_code = one(p, parent, ranks, cache, visiting);
                    let parent_base = parent_code.chars().next().unwrap_or('R');
                    if raw.is_some_and(|r| rank_index(r) > rank_index(parent_base)) {
                        raw.unwrap().to_string()
                    } else {
                        let depth = parent_code
                            .get(1..)
                            .and_then(|s| s.parse::<u32>().ok())
                            .unwrap_or(1);
                        format!("{}{}", parent_base, depth + 1)
                    }
                }
            }
        };
        visiting.remove(&taxon);
        cache.insert(taxon, code.clone());
        code
    }
    let mut cache = HashMap::new();
    for taxon in parent.keys() {
        one(*taxon, parent, ranks, &mut cache, &mut HashSet::new());
    }
    cache
}

pub(crate) fn taxonomic_order(
    present: HashSet<TaxonId>,
    parent: &HashMap<TaxonId, Option<TaxonId>>,
    ranks: &HashMap<TaxonId, String>,
    names: &HashMap<TaxonId, String>,
) -> Vec<TaxonId> {
    let mut ancestors = HashSet::new();
    let mut stack = present.iter().copied().collect::<Vec<_>>();
    while let Some(taxon) = stack.pop() {
        if let Some(p) = parent.get(&taxon).copied().flatten()
            && ancestors.insert(p)
        {
            stack.push(p);
        }
    }
    let nodes = present.union(&ancestors).copied().collect::<HashSet<_>>();
    let mut children: HashMap<TaxonId, Vec<TaxonId>> = HashMap::new();
    for taxon in &nodes {
        if let Some(p) = parent
            .get(taxon)
            .copied()
            .flatten()
            .filter(|p| nodes.contains(p))
        {
            children.entry(p).or_default().push(*taxon);
        }
    }
    let key = |taxon: &TaxonId| {
        (
            ranks.get(taxon).cloned().unwrap_or_default(),
            names.get(taxon).cloned().unwrap_or_default(),
            *taxon,
        )
    };
    for values in children.values_mut() {
        values.sort_by_key(&key);
    }
    let special = [0, 1, 9606, 2, 10239];
    let mut roots = nodes
        .iter()
        .filter(|taxon| {
            parent
                .get(taxon)
                .copied()
                .flatten()
                .is_none_or(|p| !nodes.contains(&p))
        })
        .copied()
        .collect::<Vec<_>>();
    roots.sort_by_key(|taxon| {
        (
            special
                .iter()
                .position(|v| v == taxon)
                .unwrap_or(usize::MAX),
            key(taxon),
        )
    });
    let mut order = Vec::new();
    let mut seen = HashSet::new();
    for root in roots {
        let mut dfs = vec![(root, false)];
        while let Some((node, expanded)) = dfs.pop() {
            if expanded {
                continue;
            }
            if !seen.insert(node) {
                continue;
            }
            if present.contains(&node) {
                order.push(node);
            }
            if let Some(values) = children.get(&node) {
                dfs.extend(values.iter().rev().map(|child| (*child, false)));
            }
        }
    }
    for taxon in BTreeSet::from_iter(present) {
        if !seen.contains(&taxon) {
            order.push(taxon);
        }
    }
    order
}

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

    pub(crate) fn fixture() -> TaxonomicUtils {
        let raw = [
            (1, None, "root", "root"),
            (2, Some(1), "superkingdom", "Bacteria"),
            (10, Some(2), "family", "Family"),
            (11, Some(10), "genus", "Genus"),
            (12, Some(11), "species", "Species A"),
            (13, Some(11), "species", "Species B"),
        ];
        let parent = raw.iter().map(|(t, p, _, _)| (*t, *p)).collect();
        let ranks = raw
            .iter()
            .map(|(t, _, r, _)| (*t, (*r).to_owned()))
            .collect();
        let codes = assign_rank_codes(&parent, &ranks);
        let nodes = raw
            .iter()
            .map(|(taxon, parent, rank, _)| {
                let rank_code = codes[taxon].clone();
                let rank_base = rank_code.chars().next().unwrap();
                TaxonNode {
                    taxon: *taxon,
                    parent: *parent,
                    rank: (*rank).to_owned(),
                    rank_code,
                    rank_base,
                    rank_idx: rank_index(rank_base),
                    new_rank: canonical_name(rank_base).to_owned(),
                }
            })
            .collect();
        TaxonomicUtils::from_parts(
            raw.iter()
                .map(|(t, _, _, n)| (*t, (*n).to_owned()))
                .collect(),
            nodes,
            vec![],
            HashMap::new(),
            AccessionLookupOptions {
                canonical: true,
                low_memory: true,
                wgs: false,
                save_folder: ".".into(),
                threads: None,
            },
        )
    }

    #[test]
    fn tree_queries_match_reference() {
        let tu = fixture();
        assert_eq!(tu.get_branch(12), vec![1, 2, 10, 11, 12]);
        assert_eq!(tu.get_subtree(11), vec![11, 12, 13]);
        assert!(tu.is_child(12, 11));
        assert!(tu.is_descendent(12, 10));
        assert!(!tu.is_descendent(10, 10));
        assert_eq!(tu.are_leaves(&[11, 12, 13]), vec![false, true, true]);
        assert_eq!(
            tu.are_children(&[12, 13], &[11, 11]).unwrap(),
            vec![true, true]
        );
        assert_eq!(
            tu.are_descendents(&[12, 10], &[10, 10]).unwrap(),
            vec![true, false]
        );
        assert!(tu.are_children(&[12], &[11, 10]).is_err());
        assert_eq!(tu.get_lca(12, 13), 11);
        assert_eq!(tu.get_distance(12, 13), 2);
        assert_eq!(tu.get_ancestor(12, "F").unwrap(), 10);
    }

    #[test]
    fn topology_matches_reference_formulae() {
        let p = fixture().topology(11, None).unwrap();
        assert_eq!(p.n_taxa, 3);
        assert_eq!(p.n_leaves, 2);
        assert_eq!(p.max_depth, 1);
        assert_eq!(p.mean_depth, 2.0 / 3.0);
        assert_eq!(p.top_child_fraction, 0.5);
    }
}