Struct tskit::TableCollection

source ·
pub struct TableCollection { /* private fields */ }
Expand description

A table collection.

This is a thin wrapper around the C type tsk_table_collection_t.

See also

Examples


let mut tables = tskit::TableCollection::new(100.).unwrap();
assert_eq!(tables.sequence_length(), 100.);

// Adding edges:

let rv = tables.add_edge(0., 53., 1, 11).unwrap();

// Add node:

let rv = tables.add_node(0, 3.2, tskit::PopulationId::NULL, tskit::IndividualId::NULL).unwrap();

// Get immutable reference to edge table
let edges = tables.edges();
assert_eq!(edges.num_rows(), 1);

// Get immutable reference to node table
let nodes = tables.nodes();
assert_eq!(nodes.num_rows(), 1);

Implementations

Create a new table collection with a sequence length.

Examples
let tables = tskit::TableCollection::new(55.0).unwrap();

Negative sequence lengths are errors:

let tables = tskit::TableCollection::new(-55.0).unwrap();

Load a table collection from a file.

Examples

The function is generic over references to str:

let tables = tskit::TableCollection::new_from_file("trees.file").unwrap();

let filename = String::from("trees.file");
// Pass filename by reference
let tables = tskit::TableCollection::new_from_file(&filename).unwrap();

// Move filename
let tables = tskit::TableCollection::new_from_file(filename).unwrap();

// Boxed String are an unlikely use case, but can be made to work:
let filename = Box::new(String::from("trees.file"));
let tables = tskit::TableCollection::new_from_file(&*filename.as_ref()).unwrap();
Panics

This function allocates a CString to pass the file name to the C API. A panic will occur if the system runs out of memory.

Length of the sequence/“genome”.

Examples
let tables = tskit::TableCollection::new(100.).unwrap();
assert_eq!(tables.sequence_length(), 100.0);

Add a row to the edge table

Examples

// left, right, parent, child
match tables.add_edge(0., 53., 1, 11) {
    // This is the first edge, so its id will be
    // zero (0).
    Ok(edge_id) => assert_eq!(edge_id, 0),
    Err(e) => panic!("{:?}", e),
}

You may also use Position and NodeId as inputs.

let left = tskit::Position::from(0.0);
let right = tskit::Position::from(53.0);
let parent = tskit::NodeId::from(1);
let child = tskit::NodeId::from(11);
match tables.add_edge(left, right, parent, child) {
    Ok(edge_id) => assert_eq!(edge_id, 0),
    Err(e) => panic!("{:?}", e),
}

Adding invalid data is allowed at this point:

assert!(tables.add_edge(0., 53.,
                        tskit::NodeId::NULL,
                        tskit::NodeId::NULL).is_ok());

See TableCollection::check_integrity for how to catch these data model violations.

Add a row with optional metadata to the edge table

Examples

See metadata for more details about required trait implementations. Those details have been omitted from this example.

let metadata = EdgeMetadata{x: 1};
assert!(tables.add_edge_with_metadata(0., 53., 1, 11, &metadata).is_ok());

Add a row to the individual table

Examples
No flags, location, nor parents
tables.add_individual(0, None, None).unwrap();
No flags, a 3d location, no parents
tables.add_individual(0, &[-0.5, 0.3, 10.0], None).unwrap();
No flags, no location, two parents
tables.add_individual(0, None, &[1, 11]);

Add a row with metadata to the individual table

Examples

See metadata for more details about required trait implementations. Those details have been omitted from this example.

 
let metadata = IndividualMetadata{x: 1};
assert!(tables.add_individual_with_metadata(0, None, None,
                                            &metadata).is_ok());

Add a row to the migration table

Warnings

Migration tables are not currently supported by tree sequence simplification.

Examples
assert!(tables.add_migration((0.5, 100.0),
                             3,
                             (0, 1),
                             53.5).is_ok());

Add a row with optional metadata to the migration table

Examples

See metadata for more details about required trait implementations. Those details have been omitted from this example.

let metadata = MigrationMetadata{x: 1};
assert!(tables.add_migration_with_metadata((0.5, 100.0),
                                           3,
                                           (0, 1),
                                           53.5,
                                           &metadata).is_ok());
Warnings

Migration tables are not currently supported by tree sequence simplification.

Add a row to the node table

Add a row with optional metadata to the node table

Examples

See metadata for more details about required trait implementations. Those details have been omitted from this example.

let metadata = NodeMetadata{x: 1};
assert!(tables.add_node_with_metadata(0, 0.0, -1, -1, &metadata).is_ok());

Add a row to the site table

Add a row with optional metadata to the site table

Examples

See metadata for more details about required trait implementations. Those details have been omitted from this example.

let metadata = SiteMetadata{x: 1};
assert!(tables.add_site_with_metadata(tskit::Position::from(111.0),
                                      Some(&[111]),
                                      &metadata).is_ok());

Add a row to the mutation table.

Add a row with optional metadata to the mutation table.

Examples

See metadata for more details about required trait implementations. Those details have been omitted from this example.

let metadata = MutationMetadata{x: 1};
assert!(tables.add_mutation_with_metadata(0, 0, 0, 100.0, None,
                                          &metadata).is_ok());

Add a row to the population_table

Examples
tables.add_population().unwrap();

Add a row with optional metadata to the population_table

Examples

See metadata for more details about required trait implementations. Those details have been omitted from this example.

let metadata = PopulationMetadata{x: 1};
assert!(tables.add_population_with_metadata(&metadata).is_ok());

Build the “input” and “output” indexes for the edge table.

Note

The C API call behind this takes a flags argument that is currently unused. A future release may break API here if the C library is updated to use flags.

Return true if tables are indexed.

If self.is_indexed() is true, return a non-owning slice containing the edge insertion order. Otherwise, return None.

If self.is_indexed() is true, return a non-owning slice containing the edge removal order. Otherwise, return None.

Sort the tables.
The bookmark can be used to affect where sorting starts from for each table.

Details

See full_sort for more details about which tables are sorted.

Fully sort all tables. Implemented via a call to sort.

Details

This function only sorts the tables that have a strict sortedness requirement according to the tskit data model.

These tables are:

  • edges
  • mutations
  • sites

For some use cases it is desirable to have the individual table sorted so that parents appear before offspring. See topological_sort_individuals.

Sorts the individual table in place, so that parents come before children, and the parent column is remapped as required. Node references to individuals are also updated.

This function is needed because neither sort nor full_sort sorts the individual table!

Examples
// Parent comes AFTER the child
let mut tables = tskit::TableCollection::new(1.0).unwrap();
let i0 = tables.add_individual(0, None, &[1]).unwrap();
assert_eq!(i0, 0);
let i1 = tables.add_individual(0, None, None).unwrap();
assert_eq!(i1, 1);
let n0 = tables.add_node(0, 0.0, -1, i1).unwrap();
assert_eq!(n0, 0);
let n1 = tables.add_node(0, 1.0, -1, i0).unwrap();
assert_eq!(n1, 1);

// Testing for valid individual order will Err:
match tables.check_integrity(tskit::TableIntegrityCheckFlags::CHECK_INDIVIDUAL_ORDERING) {
    Ok(_) => panic!("expected Err"),
    Err(_) => (),
};

// The standard sort doesn't fix the Err...:
tables.full_sort(tskit::TableSortOptions::default()).unwrap();
match tables.check_integrity(tskit::TableIntegrityCheckFlags::CHECK_INDIVIDUAL_ORDERING) {
    Ok(_) => panic!("expected Err"),
    Err(_) => (),
};

// ... so we need to intentionally sort the individuals.
let _ = tables.topological_sort_individuals(tskit::IndividualTableSortOptions::default()).unwrap();
tables.check_integrity(tskit::TableIntegrityCheckFlags::CHECK_INDIVIDUAL_ORDERING).unwrap();
Errors

Will return an error code if the underlying C function returns an error.

Dump the table collection to file.

Panics

This function allocates a CString to pass the file name to the C API. A panic will occur if the system runs out of memory.

Clear the contents of all tables. Does not release memory. Memory will be released when the object goes out of scope.

Return true if self contains the same data as other, and false otherwise.

Return a “deep” copy of the tables.

Return a crate::TreeSequence based on the tables. This function will raise errors if tables are not sorted, not indexed, or invalid in any way.

Simplify tables in place.

Parameters
  • samples: a slice containing non-null node ids. The tables are simplified with respect to the ancestry of these nodes.
  • options: A SimplificationOptions bit field controlling the behavior of simplification.
  • idmap: if true, the return value contains a vector equal in length to the input node table. For each input node, this vector either contains the node’s new index or NodeId::NULL if the input node is not part of the simplified history.

Validate the contents of the table collection

Parameters

flags is an instance of TableIntegrityCheckFlags

Return value

0 upon success, or an error code. However, if flags contains TableIntegrityCheckFlags::CHECK_TREES, and no error is returned, then the return value is the number of trees.

Note

Creating a crate::TreeSequence from a table collection will automatically run an integrity check. See TableCollection::tree_sequence.

Examples

There are many ways for a table colletion to be invalid. These examples are just the tip of the iceberg.

let mut tables = tskit::TableCollection::new(10.0).unwrap();
// Right position is > sequence_length
tables.add_edge(0.0, 11.0, 0, 0);
tables.check_integrity(tskit::TableIntegrityCheckFlags::default()).unwrap();
// Left position is < 0.0
tables.add_edge(-1., 10.0, 0, 0);
tables.check_integrity(tskit::TableIntegrityCheckFlags::default()).unwrap();
// Edges cannot have null node ids
tables.add_edge(0., 10.0, tskit::NodeId::NULL, 0);
tables.check_integrity(tskit::TableIntegrityCheckFlags::default()).unwrap();

Add provenance record with a time stamp.

All implementation of this trait provided by tskit use an ISO 8601 format time stamp written using the RFC 3339 specification. This formatting approach has been the most straightforward method for supporting round trips to/from a crate::provenance::ProvenanceTable. The implementations used here use the humantime crate.

Parameters
  • record: the provenance record
Examples
 
let mut tables = tskit::TableCollection::new(1000.).unwrap();
tables.add_provenance(&String::from("Some provenance")).unwrap();

// Get reference to the table
let prov_ref = tables.provenances();

// Get the first row
let row_0 = prov_ref.row(0).unwrap();

assert_eq!(row_0.record, "Some provenance");

// Get the first record
let record_0 = prov_ref.record(0).unwrap();
assert_eq!(record_0, row_0.record);

// Get the first time stamp
let timestamp = prov_ref.timestamp(0).unwrap();
assert_eq!(timestamp, row_0.timestamp);

// You can get the `humantime::Timestamp` object back from the `String`:
use core::str::FromStr;
let timestamp_string = humantime::Timestamp::from_str(&timestamp).unwrap();

// Provenance transfers to the tree sequences
let treeseq = tables.tree_sequence(tskit::TreeSequenceFlags::BUILD_INDEXES).unwrap();
assert_eq!(treeseq.provenances().record(0).unwrap(), "Some provenance");
// We can still compare to row_0 because it is a copy of the row data:
assert_eq!(treeseq.provenances().record(0).unwrap(), row_0.record);

Set the edge table from an OwnedEdgeTable

Errors

Any errors from the C API propagate.

Example
let mut tables = tskit::TableCollection::new(1.0).unwrap();
let mut edges = tskit::OwnedEdgeTable::default();
edges.add_row(0., 1., 0, 12).unwrap();
tables.set_edges(&edges).unwrap();
assert_eq!(tables.edges().num_rows(), 1);
assert_eq!(tables.edges().child(0).unwrap(), 12);

Set the node table from an OwnedNodeTable

Errors

Any errors from the C API propagate.

Example
let mut tables = tskit::TableCollection::new(1.0).unwrap();
let mut nodes = tskit::OwnedNodeTable::default();
nodes.add_row(0, 10.0, -1, -1).unwrap();
tables.set_nodes(&nodes).unwrap();
assert_eq!(tables.nodes().num_rows(), 1);
assert_eq!(tables.nodes().time(0).unwrap(), 10.0);

Set the site table from an OwnedSiteTable

Errors

Any errors from the C API propagate.

Example
let mut tables = tskit::TableCollection::new(1.0).unwrap();
let mut sites = tskit::OwnedSiteTable::default();
sites.add_row(11.0, None).unwrap();
tables.set_sites(&sites).unwrap();
assert_eq!(tables.sites().num_rows(), 1);
assert_eq!(tables.sites().position(0).unwrap(), 11.0);

Set the mutation table from an OwnedMutationTable

Errors

Any errors from the C API propagate.

Example
let mut tables = tskit::TableCollection::new(1.0).unwrap();
let mut mutations = tskit::OwnedMutationTable::default();
mutations.add_row(14, 12, -1, 11.3, None).unwrap();
tables.set_mutations(&mutations).unwrap();
assert_eq!(tables.mutations().num_rows(), 1);
assert_eq!(tables.mutations().site(0).unwrap(), 14);

Set the individual table from an OwnedIndividualTable

Errors

Any errors from the C API propagate.

Example
let mut tables = tskit::TableCollection::new(1.0).unwrap();
let mut individuals = tskit::OwnedIndividualTable::default();
individuals.add_row(0, [0.1, 10.0], None).unwrap();
tables.set_individuals(&individuals).unwrap();
assert_eq!(tables.individuals().num_rows(), 1);
let expected = vec![tskit::Location::from(0.1), tskit::Location::from(10.0)];
assert_eq!(tables.individuals().location(0), Some(expected.as_slice()));

Set the migration table from an OwnedMigrationTable

Errors

Any errors from the C API propagate.

Example
let mut tables = tskit::TableCollection::new(1.0).unwrap();
let mut migrations = tskit::OwnedMigrationTable::default();
migrations.add_row((0.25, 0.37), 1, (0, 1), 111.0).unwrap();
tables.set_migrations(&migrations).unwrap();
assert_eq!(tables.migrations().num_rows(), 1);
assert_eq!(tables.migrations().time(0).unwrap(), 111.0);

Set the population table from an OwnedPopulationTable

Errors

Any errors from the C API propagate.

Example
let mut tables = tskit::TableCollection::new(1.0).unwrap();
let mut populations = tskit::OwnedPopulationTable::default();
populations.add_row().unwrap();
tables.set_populations(&populations).unwrap();
assert_eq!(tables.populations().num_rows(), 1);
Available on crate feature provenance only.

Set the provenance table from an OwnedProvenanceTable

Errors

Any errors from the C API propagate.

Example
let mut tables = tskit::TableCollection::new(1.0).unwrap();
let mut provenances = tskit::provenance::OwnedProvenanceTable::default();
provenances.add_row("I like pancakes").unwrap();
tables.set_provenances(&provenances).unwrap();
assert_eq!(tables.provenances().num_rows(), 1);
assert_eq!(tables.provenances().record(0).unwrap(), "I like pancakes");

Methods from Deref<Target = TableViews>

Get reference to the EdgeTable.

Get reference to the NodeTable.

Get mutable reference to the NodeTable.

Get reference to the SiteTable.

Get reference to the MutationTable.

Get reference to the IndividualTable.

Get reference to the PopulationTable.

Get reference to the MigrationTable.

Available on crate feature provenance only.

Get reference to the ProvenanceTable

Return an iterator over the edges.

Return an iterator over the nodes.

Return an iterator over the sites.

Return an iterator over the mutations.

Return an iterator over the individuals.

Return an iterator over the populations.

Return an iterator over the migrations.

Available on crate feature provenance only.

Return an iterator over provenances

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::bindings::tsk_id_t;

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 std::ops::Deref<Target=tskit::table_views::TableViews>,
    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);

Trait Implementations

The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Executes the destructor for this type. Read more
The type returned in the event of a conversion error.
Performs the conversion.
Return const pointer
Return mutable pointer

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Drops the content pointed by this pointer and frees it. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.