Struct tskit::TableCollection [−][src]
pub struct TableCollection { /* fields omitted */ }
Expand description
A table collection.
This is a thin wrapper around the C type tsk_table_collection_t
.
Current limitations
Examples
use tskit::TableAccess;
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);
Metadata round trips and table iteration
use tskit;
use tskit::TableAccess;
use tskit::metadata::MetadataRoundtrip;
// Define a type for metadata
struct F {
x: i32,
}
// Implement our metadata trait for type F.
// NOTE: this is hard because we are only using the
// rust standard library here. See the examples/
// directory of the repository for examples using
// other, more convenient, crates.
impl tskit::metadata::MetadataRoundtrip for F {
fn encode(&self) -> Result<Vec<u8>, tskit::metadata::MetadataError> {
let mut rv = vec![];
rv.extend(self.x.to_le_bytes().iter().copied());
Ok(rv)
}
fn decode(md: &[u8]) -> Result<Self, tskit::metadata::MetadataError> {
use std::convert::TryInto;
let (x_int_bytes, rest) = md.split_at(std::mem::size_of::<i32>());
Ok(Self {
x: i32::from_le_bytes(x_int_bytes.try_into().unwrap()),
})
}
}
impl tskit::metadata::MutationMetadata for F {}
// Crate a table and add a mutation with metadata
let mut tables = tskit::TableCollection::new(100.).unwrap();
// The metadata takes a reference in the event that it could
// be data store in some container somewhere, and you don't want
// it moved.
tables.add_mutation_with_metadata(0, 0, 0, 0., None, &F{x: -33}).unwrap();
// Iterate over each row in the table.
// The "true" means to include (a copy of) the
// encoded metadata, if any exist.
for row in tables.mutations().iter() {
// Decode the metadata if any exists.
if !row.metadata.is_none() {
let md = F::decode(&row.metadata.unwrap()).unwrap();
assert_eq!(md.x, -33);
}
}
Future road map
- Support all table types. Currently, we only support those needed for current goals in ongoing projects.
- Strengthen some of the error handling.
Implementations
Create a new table collection with a sequence length.
Load a table collection from a file.
Length of the sequence/“genome”.
Add a row to the edge table
pub fn add_edge_with_metadata<P: Into<NodeId>, C: Into<NodeId>, M: EdgeMetadata>(
&mut self,
left: f64,
right: f64,
parent: P,
child: C,
metadata: &M
) -> Result<EdgeId, TskitError>
pub fn add_edge_with_metadata<P: Into<NodeId>, C: Into<NodeId>, M: EdgeMetadata>(
&mut self,
left: f64,
right: f64,
parent: P,
child: C,
metadata: &M
) -> Result<EdgeId, TskitError>
Add a row with optional metadata to the edge table
pub fn add_individual<I: Into<IndividualId>>(
&mut self,
flags: tsk_flags_t,
location: &[f64],
parents: &[I]
) -> Result<IndividualId, TskitError>
pub fn add_individual<I: Into<IndividualId>>(
&mut self,
flags: tsk_flags_t,
location: &[f64],
parents: &[I]
) -> Result<IndividualId, TskitError>
Add a row to the individual table
pub fn add_individual_with_metadata<I: Into<IndividualId>, M: IndividualMetadata>(
&mut self,
flags: tsk_flags_t,
location: &[f64],
parents: &[I],
metadata: &M
) -> Result<IndividualId, TskitError>
pub fn add_individual_with_metadata<I: Into<IndividualId>, M: IndividualMetadata>(
&mut self,
flags: tsk_flags_t,
location: &[f64],
parents: &[I],
metadata: &M
) -> Result<IndividualId, TskitError>
Add a row with metadata to the individual table
pub fn add_migration<N: Into<NodeId>, SOURCE: Into<PopulationId>, DEST: Into<PopulationId>>(
&mut self,
span: (f64, f64),
node: N,
source_dest: (SOURCE, DEST),
time: f64
) -> Result<MigrationId, TskitError>
pub fn add_migration<N: Into<NodeId>, SOURCE: Into<PopulationId>, DEST: Into<PopulationId>>(
&mut self,
span: (f64, f64),
node: N,
source_dest: (SOURCE, DEST),
time: f64
) -> Result<MigrationId, TskitError>
Add a row to the migration table
Warnings
Migration tables are not currently supported by tree sequence simplification.
pub fn add_migration_with_metadata<N: Into<NodeId>, SOURCE: Into<PopulationId>, DEST: Into<PopulationId>, MD: MigrationMetadata>(
&mut self,
span: (f64, f64),
node: N,
source_dest: (SOURCE, DEST),
time: f64,
metadata: &MD
) -> Result<MigrationId, TskitError>
pub fn add_migration_with_metadata<N: Into<NodeId>, SOURCE: Into<PopulationId>, DEST: Into<PopulationId>, MD: MigrationMetadata>(
&mut self,
span: (f64, f64),
node: N,
source_dest: (SOURCE, DEST),
time: f64,
metadata: &MD
) -> Result<MigrationId, TskitError>
Add a row with optional metadata to the migration table
Warnings
Migration tables are not currently supported by tree sequence simplification.
pub fn add_node<I: Into<IndividualId>, POP: Into<PopulationId>>(
&mut self,
flags: tsk_flags_t,
time: f64,
population: POP,
individual: I
) -> Result<NodeId, TskitError>
pub fn add_node<I: Into<IndividualId>, POP: Into<PopulationId>>(
&mut self,
flags: tsk_flags_t,
time: f64,
population: POP,
individual: I
) -> Result<NodeId, TskitError>
Add a row to the node table
pub fn add_node_with_metadata<I: Into<IndividualId>, POP: Into<PopulationId>, M: NodeMetadata>(
&mut self,
flags: tsk_flags_t,
time: f64,
population: POP,
individual: I,
metadata: &M
) -> Result<NodeId, TskitError>
pub fn add_node_with_metadata<I: Into<IndividualId>, POP: Into<PopulationId>, M: NodeMetadata>(
&mut self,
flags: tsk_flags_t,
time: f64,
population: POP,
individual: I,
metadata: &M
) -> Result<NodeId, TskitError>
Add a row with optional metadata to the node table
Add a row to the site table
pub fn add_site_with_metadata<M: SiteMetadata>(
&mut self,
position: f64,
ancestral_state: Option<&[u8]>,
metadata: &M
) -> Result<SiteId, TskitError>
pub fn add_site_with_metadata<M: SiteMetadata>(
&mut self,
position: f64,
ancestral_state: Option<&[u8]>,
metadata: &M
) -> Result<SiteId, TskitError>
Add a row with optional metadata to the site table
pub fn add_mutation<N: Into<NodeId>, M: Into<MutationId>, S: Into<SiteId>>(
&mut self,
site: S,
node: N,
parent: M,
time: f64,
derived_state: Option<&[u8]>
) -> Result<MutationId, TskitError>
pub fn add_mutation<N: Into<NodeId>, M: Into<MutationId>, S: Into<SiteId>>(
&mut self,
site: S,
node: N,
parent: M,
time: f64,
derived_state: Option<&[u8]>
) -> Result<MutationId, TskitError>
Add a row to the mutation table.
pub fn add_mutation_with_metadata<N: Into<NodeId>, M: Into<MutationId>, S: Into<SiteId>, MD: MutationMetadata>(
&mut self,
site: S,
node: N,
parent: M,
time: f64,
derived_state: Option<&[u8]>,
metadata: &MD
) -> Result<MutationId, TskitError>
pub fn add_mutation_with_metadata<N: Into<NodeId>, M: Into<MutationId>, S: Into<SiteId>, MD: MutationMetadata>(
&mut self,
site: S,
node: N,
parent: M,
time: f64,
derived_state: Option<&[u8]>,
metadata: &MD
) -> Result<MutationId, TskitError>
Add a row with optional metadata to the mutation table.
Add a row to the population_table
pub fn add_population_with_metadata<M: PopulationMetadata>(
&mut self,
metadata: &M
) -> Result<PopulationId, TskitError>
pub fn add_population_with_metadata<M: PopulationMetadata>(
&mut self,
metadata: &M
) -> Result<PopulationId, TskitError>
Add a row with optional metadata to the population_table
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.
Fully sort all functions.
Implemented via a call to sort
.
Dump the table collection to file.
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
: ASimplificationOptions
bit field controlling the behavior of simplification.idmap
: iftrue
, 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 orNodeId::NULL
if the input node is not part of the simplified history.
Trait Implementations
Obtain a vector containing the indexes (“ids”)
of all nodes for which crate::TSK_NODE_IS_SAMPLE
is true
. Read more
Obtain a vector containing the indexes (“ids”) of all nodes satisfying a certain criterion. Read more
Add provenance record with a time stamp. Read more
Return an immutable reference to the table, type ProvenanceTable
Return an iterator over the rows of the ProvenanceTable
.
See ProvenanceTable::iter
for details. Read more
Get reference to the IndividualTable
.
Get reference to the MigrationTable
.
Get reference to the MutationTable
.
Get reference to the PopulationTable
.
Return an iterator over the edges.
See EdgeTable::iter
for details. Read more
Return an iterator over the nodes.
See NodeTable::iter
for details. Read more
Return an iterator over the mutations.
See MutationTable::iter
for details. Read more
Return an iterator over the sites.
See SiteTable::iter
for details. Read more
Return an iterator over the populations.
See PopulationTable::iter
for details. Read more
Return an iterator over the migration events.
See MigrationTable::iter
for details. Read more
Return an iterator over the individuals.
See IndividualTable::iter
for details. Read more
Return const pointer
Return mutable pointer