Trait tskit::NodeListGenerator[][src]

pub trait NodeListGenerator: TableAccess {
    fn samples_as_vector(&self) -> Vec<NodeId> { ... }
fn create_node_id_vector(
        &self,
        f: impl FnMut(&NodeTableRow) -> bool
    ) -> Vec<NodeId> { ... } }
Expand description

Interface for returning lists of node ids from types implementing TableAccess.

Provided methods

Obtain a vector containing the indexes (“ids”) of all nodes for which crate::TSK_NODE_IS_SAMPLE is true.

The provided implementation dispatches to crate::NodeTable::samples_as_vector.

Obtain a vector containing the indexes (“ids”) of all nodes satisfying a certain criterion.

The provided implementation dispatches to crate::NodeTable::create_node_id_vector.

Parameters
  • f: a function. The function is passed the current table collection and each crate::node_table::NodeTableRow. If f returns true, the index of that row is included in the return value.
Examples

Get all nodes with time > 0.0:

use tskit::tsk_id_t;
use tskit::TableAccess;
use tskit::NodeListGenerator;

let mut tables = tskit::TableCollection::new(100.).unwrap();
tables
    .add_node(tskit::TSK_NODE_IS_SAMPLE, 0.0, tskit::PopulationId::NULL,
    tskit::IndividualId::NULL)
    .unwrap();
tables
    .add_node(tskit::TSK_NODE_IS_SAMPLE, 1.0, tskit::PopulationId::NULL,
    tskit::IndividualId::NULL)
    .unwrap();
let samples = tables.create_node_id_vector(
    |row: &tskit::NodeTableRow| row.time > 0.,
);
assert_eq!(samples[0], 1);

// Get all nodes that have a mutation:

fn node_has_mutation(
    // dyn trait here means this
    // will work with TreeSequence, too.
    tables_type: &dyn tskit::TableAccess,
    row: &tskit::NodeTableRow,
) -> bool {
    for mrow in tables_type.mutations_iter() {
        if mrow.node == row.id {
            return true;
        }
    }
    false
}

// Get all nodes that have a mutation:

tables.add_mutation(0, 0, tskit::MutationId::NULL, 0.0, None).unwrap();
let samples_with_mut = tables.create_node_id_vector(
    |row: &tskit::NodeTableRow| node_has_mutation(&tables, row));
assert_eq!(samples_with_mut[0], 0);

Implementors