Skip to main content

rsomics_composition/
error.rs

1#[derive(Debug, thiserror::Error, PartialEq)]
2#[non_exhaustive]
3pub enum CompositionError {
4    #[error("matrix must contain at least one sample and one component")]
5    EmptyMatrix,
6    #[error(
7        "matrix has {values} values but its {samples} by {components} shape requires {expected}"
8    )]
9    Shape {
10        samples: usize,
11        components: usize,
12        values: usize,
13        expected: usize,
14    },
15    #[error("{kind} identity cannot be empty")]
16    EmptyIdentity { kind: &'static str },
17    #[error("duplicate {kind} identity {identity:?}")]
18    DuplicateIdentity {
19        kind: &'static str,
20        identity: String,
21    },
22    #[error("row {row:?} has {found} values; expected {expected}")]
23    RaggedRow {
24        row: String,
25        found: usize,
26        expected: usize,
27    },
28    #[error("invalid number {value:?} at sample {sample:?}, component {component:?}")]
29    InvalidNumber {
30        sample: String,
31        component: String,
32        value: String,
33    },
34    #[error("value {value} at sample {sample:?}, component {component:?} is not finite")]
35    NonFinite {
36        sample: String,
37        component: String,
38        value: f64,
39    },
40    #[error("value {value} at sample {sample:?}, component {component:?} must be {requirement}")]
41    InvalidComponent {
42        sample: String,
43        component: String,
44        value: f64,
45        requirement: &'static str,
46    },
47    #[error("sample {0:?} has no positive observed components")]
48    EmptyComposition(String),
49    #[error("component identities differ: {details}")]
50    ComponentMismatch { details: String },
51    #[error("sample identities differ: {details}")]
52    SampleMismatch { details: String },
53    #[error("reference component {0:?} does not exist")]
54    UnknownReference(String),
55    #[error("reference index {index} is outside a composition of width {width}")]
56    ReferenceOutOfRange { index: usize, width: usize },
57    #[error("operation requires at least {required} components, got {found}")]
58    TooFewComponents { found: usize, required: usize },
59    #[error("scalar must be finite, got {0}")]
60    NonFiniteScalar(f64),
61    #[error(
62        "inverse transform coordinate is not finite at sample {sample:?}, component {component:?}"
63    )]
64    NonFiniteCoordinate { sample: String, component: String },
65    #[error(
66        "basis must have {expected_rows} rows and {expected_columns} columns, got {rows} by {columns}"
67    )]
68    BasisShape {
69        rows: usize,
70        columns: usize,
71        expected_rows: usize,
72        expected_columns: usize,
73    },
74    #[error("SBP row {row} contains {value}; only -1, 0, and 1 are valid")]
75    InvalidPartitionValue { row: usize, value: i8 },
76    #[error("SBP row {0} must contain at least one -1 and one 1")]
77    EmptyPartitionSide(usize),
78    #[error("basis is not orthonormal: row {left} dot row {right} is {value}")]
79    NonOrthonormalBasis {
80        left: usize,
81        right: usize,
82        value: f64,
83    },
84    #[error(
85        "tree node {node} has {children} children; a balance tree must be strictly bifurcating"
86    )]
87    NonBifurcatingTree { node: usize, children: usize },
88    #[error("tree tip identities differ from basis components: {0}")]
89    TreeTipMismatch(String),
90    #[error("parameter {name} must be finite and {requirement}, got {value}")]
91    InvalidParameter {
92        name: &'static str,
93        requirement: &'static str,
94        value: f64,
95    },
96    #[error("ddof {ddof} leaves no degrees of freedom for {observations} observations")]
97    NoDegreesOfFreedom { ddof: usize, observations: usize },
98    #[error("{purpose} would contain {rows} by {columns} cells, exceeding the limit of {maximum}")]
99    DenseMatrixTooLarge {
100        purpose: &'static str,
101        rows: usize,
102        columns: usize,
103        maximum: usize,
104    },
105    #[error("could not allocate {cells} cells for {purpose}")]
106    AllocationFailed { purpose: &'static str, cells: usize },
107    #[error("component {0:?} does not exist")]
108    UnknownComponent(String),
109    #[error("metadata column {0:?} does not exist")]
110    UnknownMetadataColumn(String),
111    #[error("metadata sample {0:?} is duplicated")]
112    DuplicateMetadataSample(String),
113    #[error("metadata has no group for sample {0:?}")]
114    MissingMetadataSample(String),
115    #[error("metadata contains unused sample {0:?}")]
116    UnusedMetadataSample(String),
117    #[error("metadata group cannot be empty for sample {0:?}")]
118    EmptyGroup(String),
119    #[error("ANCOM requires at least two groups with within-group replication")]
120    InvalidGrouping,
121    #[error("statistical calculation failed: {0}")]
122    Statistics(String),
123    #[error("delimited input has no header")]
124    MissingHeader,
125    #[error("first header field must identify samples and remaining fields must name components")]
126    InvalidHeader,
127    #[error("delimited input error: {0}")]
128    Delimited(String),
129    #[error("I/O error: {0}")]
130    Io(String),
131}
132
133pub type Result<T> = std::result::Result<T, CompositionError>;