Skip to main content

k2tools_lib/report/
row.rs

1use serde::{Deserialize, Serialize};
2
3use super::rank::TaxonomicRank;
4
5/// A single row from a kraken2 report file.
6///
7/// Each row represents one taxon and includes classification counts, the taxonomic rank,
8/// and the taxon name. The `depth` field is derived from the leading whitespace indentation
9/// in the original report and encodes the position in the taxonomy tree.
10///
11/// For extended reports (with minimizer data), `minimizer_count` and
12/// `distinct_minimizer_count` are `Some`; for standard reports they are `None`.
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14pub struct ReportRow {
15    percentage: f64,
16    clade_count: u64,
17    direct_count: u64,
18    minimizer_count: Option<u64>,
19    distinct_minimizer_count: Option<u64>,
20    taxonomic_rank: TaxonomicRank,
21    taxon_id: u64,
22    name: String,
23    depth: usize,
24}
25
26impl ReportRow {
27    /// Creates a new `ReportRow`. Not public — rows are created only during report parsing.
28    #[allow(clippy::too_many_arguments)]
29    pub(super) fn new(
30        percentage: f64,
31        clade_count: u64,
32        direct_count: u64,
33        minimizer_count: Option<u64>,
34        distinct_minimizer_count: Option<u64>,
35        taxonomic_rank: TaxonomicRank,
36        taxon_id: u64,
37        name: String,
38        depth: usize,
39    ) -> Self {
40        Self {
41            percentage,
42            clade_count,
43            direct_count,
44            minimizer_count,
45            distinct_minimizer_count,
46            taxonomic_rank,
47            taxon_id,
48            name,
49            depth,
50        }
51    }
52
53    /// Percentage of fragments rooted at this taxon's clade.
54    #[must_use]
55    pub fn percentage(&self) -> f64 {
56        self.percentage
57    }
58
59    /// Number of fragments in the clade rooted at this taxon.
60    #[must_use]
61    pub fn clade_count(&self) -> u64 {
62        self.clade_count
63    }
64
65    /// Number of fragments assigned directly to this taxon.
66    #[must_use]
67    pub fn direct_count(&self) -> u64 {
68        self.direct_count
69    }
70
71    /// Number of minimizers in the clade (extended reports only).
72    #[must_use]
73    pub fn minimizer_count(&self) -> Option<u64> {
74        self.minimizer_count
75    }
76
77    /// Number of distinct minimizers in the clade (extended reports only).
78    #[must_use]
79    pub fn distinct_minimizer_count(&self) -> Option<u64> {
80        self.distinct_minimizer_count
81    }
82
83    /// The taxonomic rank of this taxon.
84    #[must_use]
85    pub fn taxonomic_rank(&self) -> TaxonomicRank {
86        self.taxonomic_rank
87    }
88
89    /// NCBI taxonomy ID (0 for unclassified).
90    #[must_use]
91    pub fn taxon_id(&self) -> u64 {
92        self.taxon_id
93    }
94
95    /// Scientific name of the taxon (trimmed, no leading indentation).
96    #[must_use]
97    pub fn name(&self) -> &str {
98        &self.name
99    }
100
101    /// Depth in the taxonomy tree, derived from leading indentation (spaces / 2).
102    #[must_use]
103    pub fn depth(&self) -> usize {
104        self.depth
105    }
106}