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
use std::ptr::NonNull;
use crate::bindings as ll_bindings;
use crate::metadata;
use crate::tsk_id_t;
use crate::Position;
use crate::SiteId;
use crate::SizeType;
use crate::TskitError;
use ll_bindings::{tsk_site_table_free, tsk_site_table_init};
#[derive(Debug)]
pub struct SiteTableRow {
pub id: SiteId,
pub position: Position,
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::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> {
let table_ref = table.as_ref();
let ancestral_state = table.ancestral_state(pos).map(|s| s.to_vec());
Some(SiteTableRow {
id: pos.into(),
position: table.position(pos)?,
ancestral_state,
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
})
}
pub(crate) type SiteTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a SiteTable>;
pub(crate) type SiteTableIterator = crate::table_iterator::TableIterator<SiteTable>;
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 Iterator for SiteTableIterator {
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 {
table_: NonNull<ll_bindings::tsk_site_table_t>,
}
impl SiteTable {
pub(crate) fn new_from_table(
sites: *mut ll_bindings::tsk_site_table_t,
) -> Result<Self, TskitError> {
let n = NonNull::new(sites).ok_or_else(|| {
TskitError::LibraryError("null pointer to tsk_site_table_t".to_string())
})?;
Ok(SiteTable { table_: n })
}
pub(crate) fn as_ref(&self) -> &ll_bindings::tsk_site_table_t {
unsafe { self.table_.as_ref() }
}
pub fn num_rows(&self) -> SizeType {
self.as_ref().num_rows.into()
}
pub fn position<S: Into<SiteId> + Copy>(&self, row: S) -> Option<Position> {
unsafe_tsk_column_access!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
position,
Position
)
}
pub fn ancestral_state<S: Into<SiteId>>(&self, row: S) -> Option<&[u8]> {
crate::metadata::char_column_to_slice(
self,
self.as_ref().ancestral_state,
self.as_ref().ancestral_state_offset,
row.into().0,
self.as_ref().num_rows,
self.as_ref().ancestral_state_length,
)
}
pub fn metadata<T: metadata::SiteMetadata>(
&self,
row: SiteId,
) -> Option<Result<T, TskitError>> {
let table_ref = self.as_ref();
let buffer = metadata_to_vector!(self, table_ref, row.0)?;
Some(decode_metadata_row!(T, buffer).map_err(TskitError::from))
}
pub fn iter(&self) -> impl Iterator<Item = SiteTableRow> + '_ {
crate::table_iterator::make_table_iterator::<&SiteTable>(self)
}
pub fn row<S: Into<SiteId> + Copy>(&self, r: S) -> Option<SiteTableRow> {
let ri = r.into().0;
table_row_access!(ri, self, make_site_table_row)
}
}
build_owned_table_type!(
=> OwnedSiteTable,
SiteTable,
tsk_site_table_t,
tsk_site_table_init,
tsk_site_table_free,
ll_bindings::tsk_site_table_clear
);
impl OwnedSiteTable {
site_table_add_row!(=> add_row, self, *self.table);
site_table_add_row_with_metadata!(=> add_row_with_metadata, self, *self.table);
}