sce/
iter.rs

1use crate::MatrixValueTrait;
2use crate::SingleCellExperiment;
3use sprs::num_matrixmarket::Displayable;
4
5impl<'a, T: MatrixValueTrait> IntoIterator for &'a SingleCellExperiment<T>
6where
7    for<'n> Displayable<&'n T>: std::fmt::Display,
8{
9    type Item = SingleCellExperimentRow<'a, T>;
10    type IntoIter = SingleCellExperimentIntoIterator<'a, T>;
11
12    fn into_iter(self) -> Self::IntoIter {
13        SingleCellExperimentIntoIterator {
14            sce: self,
15            row_id: 0,
16            row_it: Box::new(self.counts.outer_iterator()),
17        }
18    }
19}
20
21pub struct SingleCellExperimentRow<'a, T: MatrixValueTrait>
22where
23    for<'n> Displayable<&'n T>: std::fmt::Display,
24{
25    row_id: usize,
26    row_counts: sprs::CsVecBase<&'a [usize], &'a [T], T>,
27    sce: &'a SingleCellExperiment<T>,
28}
29
30impl<'a, T: MatrixValueTrait> SingleCellExperimentRow<'a, T>
31where
32    for<'n> Displayable<&'n T>: std::fmt::Display,
33{
34    pub fn name(&'a self) -> &'a String {
35        &self.sce.rows[self.row_id]
36    }
37
38    pub fn id(&'a self) -> usize {
39        self.row_id
40    }
41}
42
43impl<'a, T: MatrixValueTrait> IntoIterator for &'a SingleCellExperimentRow<'a, T>
44where
45    for<'n> Displayable<&'n T>: std::fmt::Display,
46{
47    type Item = SingleCellExperimentEntry<'a, T>;
48    type IntoIter = SingleCellExperimentIntoRow<'a, T>;
49
50    fn into_iter(self) -> Self::IntoIter {
51        SingleCellExperimentIntoRow {
52            sce: self.sce,
53            row_it: self.row_counts.iter(),
54        }
55    }
56}
57
58pub struct SingleCellExperimentEntry<'a, T: MatrixValueTrait>
59where
60    for<'n> Displayable<&'n T>: std::fmt::Display,
61{
62    sce: &'a SingleCellExperiment<T>,
63    count: &'a T,
64    col_id: usize,
65}
66
67impl<'a, T: MatrixValueTrait> SingleCellExperimentEntry<'a, T>
68where
69    for<'n> Displayable<&'n T>: std::fmt::Display,
70{
71    pub fn id(&self) -> usize {
72        self.col_id
73    }
74
75    pub fn name(&self) -> &'a String {
76        &self.sce.cols[self.col_id]
77    }
78
79    pub fn count(&self) -> &T {
80        self.count
81    }
82}
83
84pub struct SingleCellExperimentIntoRow<'a, T: MatrixValueTrait>
85where
86    for<'n> Displayable<&'n T>: std::fmt::Display,
87{
88    sce: &'a SingleCellExperiment<T>,
89    row_it: sprs::vec::VectorIterator<'a, T, usize>,
90}
91
92impl<'a, T: MatrixValueTrait> Iterator for SingleCellExperimentIntoRow<'a, T>
93where
94    for<'n> Displayable<&'n T>: std::fmt::Display,
95{
96    type Item = SingleCellExperimentEntry<'a, T>;
97
98    fn next(&mut self) -> Option<Self::Item> {
99        let nval = self.row_it.next();
100        match nval {
101            Some(x) => Some(SingleCellExperimentEntry {
102                sce: self.sce,
103                count: x.1,
104                col_id: x.0,
105            }),
106            None => None,
107        }
108    }
109}
110
111pub struct SingleCellExperimentIntoIterator<'a, T: MatrixValueTrait>
112where
113    for<'n> Displayable<&'n T>: std::fmt::Display,
114{
115    row_id: usize,
116    sce: &'a SingleCellExperiment<T>,
117    row_it: Box<dyn Iterator<Item = sprs::CsVecBase<&'a [usize], &'a [T], T>> + 'a>,
118}
119
120impl<'a, T: MatrixValueTrait> Iterator for SingleCellExperimentIntoIterator<'a, T>
121where
122    for<'n> Displayable<&'n T>: std::fmt::Display,
123{
124    type Item = SingleCellExperimentRow<'a, T>;
125
126    fn next(&mut self) -> Option<Self::Item> {
127        if self.row_id >= self.sce.rows() {
128            return None;
129        }
130
131        let row_id = self.row_id;
132        let row_counts = self.row_it.next().expect("can't get rowdata");
133
134        self.row_id += 1;
135        Some(SingleCellExperimentRow {
136            row_id,
137            row_counts,
138            sce: self.sce,
139        })
140    }
141}