Skip to content

Commit 6bc5511

Browse files
committed
functions for getting sample/node lists out of table collections
1 parent a2dfcfe commit 6bc5511

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/table_collection.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,85 @@ impl TableCollection {
415415
handle_tsk_return_value!(rv)
416416
}
417417

418+
/// Obtain a vector containing the indexes ("ids")
419+
/// of all nodes for which [`crate::TSK_NODE_IS_SAMPLE`]
420+
/// is `true`.
421+
pub fn samples_as_vector(&self) -> Vec<tsk_id_t> {
422+
let mut samples: Vec<tsk_id_t> = vec![];
423+
for (i, row) in self.nodes_iter(false).enumerate() {
424+
if row.flags & crate::TSK_NODE_IS_SAMPLE > 0 {
425+
samples.push(i as tsk_id_t);
426+
}
427+
}
428+
samples
429+
}
430+
431+
/// Obtain a vector containing the indexes ("ids") of all nodes
432+
/// satisfying a certain criterion.
433+
///
434+
/// # Parameters
435+
///
436+
/// * `f`: a function. The function is passed the current table
437+
/// collection and each [`crate::node_table::NodeTableRow`].
438+
/// If `f` returns `true`, the index of that row is included
439+
/// in the return value.
440+
///
441+
/// # Examples
442+
///
443+
/// Get all nodes with time > 0.0:
444+
///
445+
/// ```
446+
/// use tskit::TSK_NULL;
447+
/// use tskit::tsk_id_t;
448+
/// use tskit::TableAccess;
449+
///
450+
/// let mut tables = tskit::TableCollection::new(100.).unwrap();
451+
/// tables
452+
/// .add_node(tskit::TSK_NODE_IS_SAMPLE, 0.0, TSK_NULL, TSK_NULL)
453+
/// .unwrap();
454+
/// tables
455+
/// .add_node(tskit::TSK_NODE_IS_SAMPLE, 1.0, TSK_NULL, TSK_NULL)
456+
/// .unwrap();
457+
/// let samples = tables.create_node_id_vector(
458+
/// |row: &tskit::NodeTableRow, _: tsk_id_t| row.time > 0.,
459+
/// );
460+
/// assert_eq!(samples[0], 1);
461+
///
462+
/// // Get all nodes that have a mutation:
463+
///
464+
/// fn node_has_mutation(
465+
/// tables: &tskit::TableCollection,
466+
/// row: &tskit::NodeTableRow,
467+
/// i: tsk_id_t,
468+
/// ) -> bool {
469+
/// for mrow in tables.mutations_iter(false) {
470+
/// if mrow.node == i {
471+
/// return true;
472+
/// }
473+
/// }
474+
/// false
475+
/// }
476+
///
477+
/// // Get all nodes that have a mutation:
478+
///
479+
/// tables.add_mutation(0, 0, TSK_NULL, 0.0, None).unwrap();
480+
/// let samples_with_mut = tables.create_node_id_vector(
481+
/// |row: &tskit::NodeTableRow, i: tsk_id_t| node_has_mutation(&tables, row, i));
482+
/// assert_eq!(samples_with_mut[0], 0);
483+
/// ```
484+
pub fn create_node_id_vector(
485+
&self,
486+
mut f: impl FnMut(&crate::NodeTableRow, tsk_id_t) -> bool,
487+
) -> Vec<tsk_id_t> {
488+
let mut samples: Vec<tsk_id_t> = vec![];
489+
for (i, row) in self.nodes_iter(true).enumerate() {
490+
if f(&row, i as tsk_id_t) {
491+
samples.push(i as tsk_id_t);
492+
}
493+
}
494+
samples
495+
}
496+
418497
/// Sort the tables.
419498
/// The [``bookmark``](crate::types::Bookmark) can
420499
/// be used to affect where sorting starts from for each table.

0 commit comments

Comments
 (0)