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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
use std::ptr::NonNull;
use crate::bindings as ll_bindings;
use crate::metadata;
use crate::NodeFlags;
use crate::SizeType;
use crate::Time;
use crate::{tsk_id_t, TskitError};
use crate::{IndividualId, NodeId, PopulationId};
use ll_bindings::{tsk_node_table_free, tsk_node_table_init};
/// Row of a [`NodeTable`]
#[derive(Debug)]
pub struct NodeTableRow {
pub id: NodeId,
pub time: Time,
pub flags: NodeFlags,
pub population: PopulationId,
pub individual: IndividualId,
pub metadata: Option<Vec<u8>>,
}
impl PartialEq for NodeTableRow {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.flags == other.flags
&& self.population == other.population
&& self.individual == other.individual
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& self.metadata == other.metadata
}
}
fn make_node_table_row(table: &NodeTable, pos: tsk_id_t) -> Option<NodeTableRow> {
let table_ref = table.as_ref();
Some(NodeTableRow {
id: pos.into(),
time: table.time(pos)?,
flags: table.flags(pos)?,
population: table.population(pos)?,
individual: table.individual(pos)?,
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
})
}
pub(crate) type NodeTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a NodeTable>;
pub(crate) type NodeTableIterator = crate::table_iterator::TableIterator<NodeTable>;
impl<'a> Iterator for NodeTableRefIterator<'a> {
type Item = NodeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_node_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl Iterator for NodeTableIterator {
type Item = crate::node_table::NodeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_node_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
/// An immtable view of a node table.
///
/// These are not created directly but are accessed
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
pub struct NodeTable {
table_: NonNull<ll_bindings::tsk_node_table_t>,
}
impl NodeTable {
pub(crate) fn new_from_table(
nodes: *mut ll_bindings::tsk_node_table_t,
) -> Result<Self, TskitError> {
let n = NonNull::new(nodes).ok_or_else(|| {
TskitError::LibraryError("null pointer to tsk_node_table_t".to_string())
})?;
Ok(NodeTable { table_: n })
}
pub(crate) fn as_ref(&self) -> &ll_bindings::tsk_node_table_t {
// SAFETY: NonNull
unsafe { self.table_.as_ref() }
}
/// Return the number of rows
pub fn num_rows(&self) -> SizeType {
self.as_ref().num_rows.into()
}
/// Return the ``time`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(time)` if `row` is valid.
/// * `None` otherwise.
///
/// # Examples
///
/// ```
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(0, 10.0, -1, -1).unwrap();
/// if let Some(time) = tables.nodes().time(0) {
/// // then node id 0 is a valid row id
/// # assert_eq!(time, 10.0);
/// }
/// # else {
/// # panic!("expected 0 to be a valid row id")
/// # }
/// ```
pub fn time<N: Into<NodeId> + Copy>(&self, row: N) -> Option<Time> {
unsafe_tsk_column_access!(row.into().0, 0, self.num_rows(), self.as_ref(), time, Time)
}
/// Return the ``flags`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(flags)` if `row` is valid.
/// * `None` otherwise.
///
/// # Examples
///
/// ```
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
/// if let Some(flags) = tables.nodes().flags(0) {
/// // then node id 0 is a valid row id
/// # assert!(flags.is_sample());
/// }
/// # else {
/// # panic!("expected 0 to be a valid row id")
/// # }
/// ```
pub fn flags<N: Into<NodeId> + Copy>(&self, row: N) -> Option<NodeFlags> {
unsafe_tsk_column_access_and_map_into!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
flags
)
}
/// Mutable access to node flags.
///
/// # Examples
///
///
/// ```
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
/// let flags = tables.nodes_mut().flags_array_mut();
/// for flag in flags {
/// // Can do something...
/// # assert!(flag.is_sample());
/// }
/// ```
///
/// ```
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
/// for flag in tables.nodes_mut().flags_array_mut() {
/// # assert!(flag.is_sample());
/// }
/// ```
///
/// The returned slice is *mutable*, allowing one to do things like
/// clear the sample status of all nodes:
///
/// A copy of the flags can be obtained by collecting results into `Vec`:
///
/// ```
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
/// for flag in tables.nodes_mut().flags_array_mut() {
/// flag.remove(tskit::NodeFlags::IS_SAMPLE);
/// }
/// assert!(!tables.nodes_mut().flags_array_mut().iter().any(|f| f.is_sample()));
/// assert!(tables.nodes().samples_as_vector().is_empty());
/// ```
///
/// ```
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
/// let flags = tables.nodes_mut().flags_array_mut().to_vec();
/// # assert!(flags.iter().all(|f| f.is_sample()));
/// ```
///
/// ## Standalone tables
///
/// The ownership semantics differ when tables are not part of a
/// table collection:
///
/// ```
/// let mut nodes = tskit::OwnedNodeTable::default();
/// assert!(nodes.add_row(tskit::NodeFlags::IS_SAMPLE, 10., -1, -1).is_ok());
/// # assert_eq!(nodes.num_rows(), 1);
/// let flags = nodes.flags_array_mut();
/// # assert_eq!(flags.len(), 1);
/// assert!(flags.iter().all(|f| f.is_sample()));
///
/// // while we are at it, let's use our node
/// // table to populate a table collection.
/// #
/// let mut tables = tskit::TableCollection::new(10.0).unwrap();
/// tables.set_nodes(&nodes);
/// assert_eq!(tables.nodes().num_rows(), 1);
/// assert_eq!(tables.nodes_mut().flags_array_mut().iter().filter(|f| f.is_sample()).count(), 1);
/// ```
///
/// # Note
///
/// Internally, we rely on a conversion of u64 to usize.
/// This conversion is fallible on some platforms.
/// If the conversion fails, an empty slice is returned.
pub fn flags_array_mut(&mut self) -> &mut [NodeFlags] {
unsafe {
std::slice::from_raw_parts_mut(
self.as_ref().flags.cast::<NodeFlags>(),
usize::try_from(self.as_ref().num_rows).unwrap_or(0),
)
}
}
/// Mutable access to node times.
///
/// # Examples
///
/// For a [`crate::TableCollection`], accessing the table creates a temporary
/// that will be dropped, causing this code to not compile:
///
/// ```compile_fail
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
/// let time = tables.nodes().time_array_mut();
/// println!("{}", time.len()); // ERROR: the temporary node table is dropped by now
/// ```
///
/// Treating the returned slice as an iterable succeeds:
///
/// ```
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
/// for time in tables.nodes_mut().time_array_mut() {
/// *time = 55.0.into(); // change each node's time value
/// }
/// assert!(tables.nodes_mut().time_array_mut().iter().all(|t| t == &55.0));
/// ```
///
/// # Note
///
/// Internally, we rely on a conversion of u64 to usize.
/// This conversion is fallible on some platforms.
/// If the conversion fails, an empty slice is returned.
pub fn time_array_mut(&mut self) -> &mut [Time] {
unsafe {
std::slice::from_raw_parts_mut(
self.as_ref().time.cast::<Time>(),
usize::try_from(self.as_ref().num_rows).unwrap_or(0),
)
}
}
/// Return the ``population`` value from row ``row`` of the table.
///
/// # Examples
///
/// ```
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(0, 10.0, -1, -1).unwrap();
/// if let Some(pop) = tables.nodes().population(0) {
/// // then node id 0 is a valid row id
/// # assert!(pop.is_null());
/// }
/// # else {
/// # panic!("expected 0 to be a valid row id")
/// # }
/// ```
///
/// # Returns
///
/// * `Some(population)` if `row` is valid.
/// * `None` otherwise.
pub fn population<N: Into<NodeId> + Copy>(&self, row: N) -> Option<PopulationId> {
unsafe_tsk_column_access!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
population,
PopulationId
)
}
/// Return the ``population`` value from row ``row`` of the table.
///
/// # Examples
///
/// See [`NodeTable::population`] for examples.
///
/// # Returns
///
/// * `Some(population)` if `row` is valid.
/// * `None` otherwise.
pub fn deme<N: Into<NodeId> + Copy>(&self, row: N) -> Option<PopulationId> {
self.population(row)
}
/// Return the ``individual`` value from row ``row`` of the table.
///
/// # Examples
///
/// ```
/// # use tskit::prelude::*;
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
/// # tables.add_node(0, 10.0, -1, -1).unwrap();
/// if let Some(individual) = tables.nodes().individual(0) {
/// // then node id 0 is a valid row id
/// # assert!(individual.is_null());
/// }
/// # else {
/// # panic!("expected 0 to be a valid row id")
/// # }
/// ```
///
/// # Returns
///
/// * `Some(individual)` if `row` is valid.
/// * `None` otherwise.
pub fn individual<N: Into<NodeId> + Copy>(&self, row: N) -> Option<IndividualId> {
unsafe_tsk_column_access!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
individual,
IndividualId
)
}
/// Retrieve decoded metadata for a `row`.
///
/// # Returns
///
/// * `Some(Ok(T))` if `row` is valid and decoding succeeded.
/// * `Some(Err(_))` if `row` is not valid and decoding failed.
/// * `None` if `row` is not valid.
///
/// # Errors
///
/// * [`TskitError::MetadataError`] if decoding fails.
///
/// # Examples.
///
/// The big-picture semantics are the same for all table types.
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::NodeMetadata>(
&self,
row: NodeId,
) -> 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(|e| e.into()))
}
/// Return an iterator over rows of the table.
/// The value of the iterator is [`NodeTableRow`].
pub fn iter(&self) -> impl Iterator<Item = NodeTableRow> + '_ {
crate::table_iterator::make_table_iterator::<&NodeTable>(self)
}
/// Return row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row)` if `r` is valid
/// * `None` otherwise
pub fn row<N: Into<NodeId> + Copy>(&self, r: N) -> Option<NodeTableRow> {
let ri = r.into().0;
table_row_access!(ri, self, make_node_table_row)
}
/// Obtain a vector containing the indexes ("ids")
/// of all nodes for which [`crate::TSK_NODE_IS_SAMPLE`]
/// is `true`.
pub fn samples_as_vector(&self) -> Vec<NodeId> {
self.create_node_id_vector(|row| row.flags.contains(NodeFlags::IS_SAMPLE))
}
/// Obtain a vector containing the indexes ("ids") of all nodes
/// satisfying a certain criterion.
pub fn create_node_id_vector(&self, mut f: impl FnMut(&NodeTableRow) -> bool) -> Vec<NodeId> {
self.iter()
.filter(|row| f(row))
.map(|row| row.id)
.collect::<Vec<_>>()
}
}
build_owned_table_type!(
/// A standalone node table that owns its data.
///
/// # Examples
///
/// ```
/// use tskit::OwnedNodeTable;
///
/// let mut nodes = OwnedNodeTable::default();
/// let rowid = nodes.add_row(0, 1.1, -1, -1).unwrap();
/// assert_eq!(rowid, 0);
/// assert_eq!(nodes.num_rows(), 1);
/// ```
///
/// An example with metadata.
/// This requires the cargo feature `"derive"` for `tskit`.
///
/// ```
/// # #[cfg(any(feature="doc", feature="derive"))] {
/// use tskit::OwnedNodeTable;
///
/// #[derive(serde::Serialize,
/// serde::Deserialize,
/// tskit::metadata::NodeMetadata)]
/// #[serializer("serde_json")]
/// struct NodeMetadata {
/// value: i32,
/// }
///
/// let metadata = NodeMetadata{value: 42};
///
/// let mut nodes = OwnedNodeTable::default();
///
/// let rowid = nodes.add_row_with_metadata(0, 1., -1, -1, &metadata).unwrap();
/// assert_eq!(rowid, 0);
///
/// match nodes.metadata::<NodeMetadata>(rowid) {
/// // rowid is in range, decoding succeeded
/// Some(Ok(decoded)) => assert_eq!(decoded.value, 42),
/// // rowid is in range, decoding failed
/// Some(Err(e)) => panic!("error decoding metadata: {:?}", e),
/// None => panic!("row id out of range")
/// }
///
/// # }
/// ```
=> OwnedNodeTable,
NodeTable,
tsk_node_table_t,
tsk_node_table_init,
tsk_node_table_free,
ll_bindings::tsk_node_table_clear
);
impl OwnedNodeTable {
node_table_add_row!(=> add_row, self, (*self.table));
node_table_add_row_with_metadata!(=> add_row_with_metadata, self, (*self.table));
}
#[cfg(test)]
mod test_owned_node_table {
use super::*;
#[test]
fn test_add_row() {
let mut nodes = OwnedNodeTable::default();
let rowid = nodes.add_row(0, 1.1, -1, -1).unwrap();
assert_eq!(rowid, 0);
assert_eq!(nodes.num_rows(), 1);
}
}