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
use crate::bindings as ll_bindings;
use crate::metadata;
use crate::SiteId;
use crate::TskitError;
use crate::{tsk_id_t, tsk_size_t};
pub struct SiteTableRow {
pub id: SiteId,
pub position: f64,
pub ancestral_state: Option<Vec<u8>>,
pub metadata: Option<Vec<u8>>,
}
impl PartialEq for SiteTableRow {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& crate::util::f64_partial_cmp_equal(&self.position, &other.position)
&& self.ancestral_state == other.ancestral_state
&& self.metadata == other.metadata
}
}
fn make_site_table_row(table: &SiteTable, pos: tsk_id_t) -> Option<SiteTableRow> {
if pos < table.num_rows() as tsk_id_t {
let rv = SiteTableRow {
id: pos.into(),
position: table.position(pos).unwrap(),
ancestral_state: table.ancestral_state(pos).unwrap(),
metadata: table_row_decode_metadata!(table, pos),
};
Some(rv)
} else {
None
}
}
pub type SiteTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a SiteTable<'a>>;
pub type SiteTableIterator<'a> = crate::table_iterator::TableIterator<SiteTable<'a>>;
impl<'a> Iterator for SiteTableRefIterator<'a> {
type Item = SiteTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_site_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl<'a> Iterator for SiteTableIterator<'a> {
type Item = SiteTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_site_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
pub struct SiteTable<'a> {
table_: &'a ll_bindings::tsk_site_table_t,
}
impl<'a> SiteTable<'a> {
pub(crate) fn new_from_table(sites: &'a ll_bindings::tsk_site_table_t) -> Self {
SiteTable { table_: sites }
}
pub fn num_rows(&'a self) -> tsk_size_t {
self.table_.num_rows
}
pub fn position<S: Into<SiteId> + Copy>(&'a self, row: S) -> Result<f64, TskitError> {
unsafe_tsk_column_access!(row.into().0, 0, self.num_rows(), self.table_.position)
}
pub fn ancestral_state<S: Into<SiteId>>(
&'a self,
row: S,
) -> Result<Option<Vec<u8>>, TskitError> {
crate::metadata::char_column_to_vector(
self.table_.ancestral_state,
self.table_.ancestral_state_offset,
row.into().0,
self.table_.num_rows,
self.table_.ancestral_state_length,
)
}
pub fn metadata<T: metadata::MetadataRoundtrip>(
&'a self,
row: SiteId,
) -> Result<Option<T>, TskitError> {
let buffer = metadata_to_vector!(self, row.0)?;
decode_metadata_row!(T, buffer)
}
pub fn iter(&self) -> SiteTableRefIterator {
crate::table_iterator::make_table_iterator::<&SiteTable<'a>>(self)
}
pub fn row<S: Into<SiteId> + Copy>(&self, r: S) -> Result<SiteTableRow, TskitError> {
table_row_access!(r.into().0, self, make_site_table_row)
}
}