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
.
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> {
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
sourceimpl TableCollection
impl TableCollection
sourcepub fn new<P: Into<Position>>(sequence_length: P) -> Result<Self, TskitError>
pub fn new<P: Into<Position>>(sequence_length: P) -> Result<Self, TskitError>
Create a new table collection with a sequence length.
sourcepub fn new_from_file(filename: &str) -> Result<Self, TskitError>
pub fn new_from_file(filename: &str) -> Result<Self, TskitError>
Load a table collection from a file.
sourcepub fn sequence_length(&self) -> Position
pub fn sequence_length(&self) -> Position
Length of the sequence/“genome”.
sourcepub fn add_edge<L: Into<Position>, R: Into<Position>, P: Into<NodeId>, C: Into<NodeId>>(
&mut self,
left: L,
right: R,
parent: P,
child: C
) -> Result<EdgeId, TskitError>
pub fn add_edge<L: Into<Position>, R: Into<Position>, P: Into<NodeId>, C: Into<NodeId>>(
&mut self,
left: L,
right: R,
parent: P,
child: C
) -> Result<EdgeId, TskitError>
Add a row to the edge table
sourcepub fn add_edge_with_metadata<L: Into<Position>, R: Into<Position>, P: Into<NodeId>, C: Into<NodeId>, M: EdgeMetadata>(
&mut self,
left: L,
right: R,
parent: P,
child: C,
metadata: &M
) -> Result<EdgeId, TskitError>
pub fn add_edge_with_metadata<L: Into<Position>, R: Into<Position>, P: Into<NodeId>, C: Into<NodeId>, M: EdgeMetadata>(
&mut self,
left: L,
right: R,
parent: P,
child: C,
metadata: &M
) -> Result<EdgeId, TskitError>
Add a row with optional metadata to the edge table
sourcepub fn add_individual<L: Into<Location>, I: Into<IndividualId>>(
&mut self,
flags: tsk_flags_t,
location: &[L],
parents: &[I]
) -> Result<IndividualId, TskitError>
pub fn add_individual<L: Into<Location>, I: Into<IndividualId>>(
&mut self,
flags: tsk_flags_t,
location: &[L],
parents: &[I]
) -> Result<IndividualId, TskitError>
Add a row to the individual table
sourcepub fn add_individual_with_metadata<L: Into<Location>, I: Into<IndividualId>, M: IndividualMetadata>(
&mut self,
flags: tsk_flags_t,
location: &[L],
parents: &[I],
metadata: &M
) -> Result<IndividualId, TskitError>
pub fn add_individual_with_metadata<L: Into<Location>, I: Into<IndividualId>, M: IndividualMetadata>(
&mut self,
flags: tsk_flags_t,
location: &[L],
parents: &[I],
metadata: &M
) -> Result<IndividualId, TskitError>
Add a row with metadata to the individual table
sourcepub fn add_migration<L: Into<Position>, R: Into<Position>, N: Into<NodeId>, SOURCE: Into<PopulationId>, DEST: Into<PopulationId>, T: Into<Time>>(
&mut self,
span: (L, R),
node: N,
source_dest: (SOURCE, DEST),
time: T
) -> Result<MigrationId, TskitError>
pub fn add_migration<L: Into<Position>, R: Into<Position>, N: Into<NodeId>, SOURCE: Into<PopulationId>, DEST: Into<PopulationId>, T: Into<Time>>(
&mut self,
span: (L, R),
node: N,
source_dest: (SOURCE, DEST),
time: T
) -> Result<MigrationId, TskitError>
Add a row to the migration table
Warnings
Migration tables are not currently supported by tree sequence simplification.
sourcepub fn add_migration_with_metadata<L: Into<Position>, R: Into<Position>, N: Into<NodeId>, SOURCE: Into<PopulationId>, DEST: Into<PopulationId>, MD: MigrationMetadata, T: Into<Time>>(
&mut self,
span: (L, R),
node: N,
source_dest: (SOURCE, DEST),
time: T,
metadata: &MD
) -> Result<MigrationId, TskitError>
pub fn add_migration_with_metadata<L: Into<Position>, R: Into<Position>, N: Into<NodeId>, SOURCE: Into<PopulationId>, DEST: Into<PopulationId>, MD: MigrationMetadata, T: Into<Time>>(
&mut self,
span: (L, R),
node: N,
source_dest: (SOURCE, DEST),
time: T,
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.
sourcepub fn add_node<T: Into<Time>, POP: Into<PopulationId>, I: Into<IndividualId>>(
&mut self,
flags: tsk_flags_t,
time: T,
population: POP,
individual: I
) -> Result<NodeId, TskitError>
pub fn add_node<T: Into<Time>, POP: Into<PopulationId>, I: Into<IndividualId>>(
&mut self,
flags: tsk_flags_t,
time: T,
population: POP,
individual: I
) -> Result<NodeId, TskitError>
Add a row to the node table
sourcepub fn add_node_with_metadata<T: Into<Time>, POP: Into<PopulationId>, I: Into<IndividualId>, M: NodeMetadata>(
&mut self,
flags: tsk_flags_t,
time: T,
population: POP,
individual: I,
metadata: &M
) -> Result<NodeId, TskitError>
pub fn add_node_with_metadata<T: Into<Time>, POP: Into<PopulationId>, I: Into<IndividualId>, M: NodeMetadata>(
&mut self,
flags: tsk_flags_t,
time: T,
population: POP,
individual: I,
metadata: &M
) -> Result<NodeId, TskitError>
Add a row with optional metadata to the node table
sourcepub fn add_site<P: Into<Position>>(
&mut self,
position: P,
ancestral_state: Option<&[u8]>
) -> Result<SiteId, TskitError>
pub fn add_site<P: Into<Position>>(
&mut self,
position: P,
ancestral_state: Option<&[u8]>
) -> Result<SiteId, TskitError>
Add a row to the site table
sourcepub fn add_site_with_metadata<P: Into<Position>, M: SiteMetadata>(
&mut self,
position: P,
ancestral_state: Option<&[u8]>,
metadata: &M
) -> Result<SiteId, TskitError>
pub fn add_site_with_metadata<P: Into<Position>, M: SiteMetadata>(
&mut self,
position: P,
ancestral_state: Option<&[u8]>,
metadata: &M
) -> Result<SiteId, TskitError>
Add a row with optional metadata to the site table
sourcepub fn add_mutation<S: Into<SiteId>, N: Into<NodeId>, M: Into<MutationId>, T: Into<Time>>(
&mut self,
site: S,
node: N,
parent: M,
time: T,
derived_state: Option<&[u8]>
) -> Result<MutationId, TskitError>
pub fn add_mutation<S: Into<SiteId>, N: Into<NodeId>, M: Into<MutationId>, T: Into<Time>>(
&mut self,
site: S,
node: N,
parent: M,
time: T,
derived_state: Option<&[u8]>
) -> Result<MutationId, TskitError>
Add a row to the mutation table.
sourcepub fn add_mutation_with_metadata<S: Into<SiteId>, N: Into<NodeId>, M: Into<MutationId>, MD: MutationMetadata, T: Into<Time>>(
&mut self,
site: S,
node: N,
parent: M,
time: T,
derived_state: Option<&[u8]>,
metadata: &MD
) -> Result<MutationId, TskitError>
pub fn add_mutation_with_metadata<S: Into<SiteId>, N: Into<NodeId>, M: Into<MutationId>, MD: MutationMetadata, T: Into<Time>>(
&mut self,
site: S,
node: N,
parent: M,
time: T,
derived_state: Option<&[u8]>,
metadata: &MD
) -> Result<MutationId, TskitError>
Add a row with optional metadata to the mutation table.
sourcepub fn add_population(&mut self) -> Result<PopulationId, TskitError>
pub fn add_population(&mut self) -> Result<PopulationId, TskitError>
Add a row to the population_table
sourcepub 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
sourcepub fn build_index(&mut self) -> TskReturnValue
pub fn build_index(&mut self) -> TskReturnValue
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.
sourcepub fn is_indexed(&self) -> bool
pub fn is_indexed(&self) -> bool
Return true
if tables are indexed.
sourcepub fn edge_insertion_order(&self) -> Option<&[EdgeId]>
pub fn edge_insertion_order(&self) -> Option<&[EdgeId]>
If self.is_indexed()
is true
, return a non-owning
slice containing the edge insertion order.
Otherwise, return None
.
sourcepub fn edge_removal_order(&self) -> Option<&[EdgeId]>
pub fn edge_removal_order(&self) -> Option<&[EdgeId]>
If self.is_indexed()
is true
, return a non-owning
slice containing the edge removal order.
Otherwise, return None
.
sourcepub fn sort(
&mut self,
start: &Bookmark,
options: TableSortOptions
) -> TskReturnValue
pub fn sort(
&mut self,
start: &Bookmark,
options: TableSortOptions
) -> TskReturnValue
Sort the tables.
The bookmark
can
be used to affect where sorting starts from for each table.
Note
As of 0.7.0
, this function does not sort the individual table!
See
topological_sort_individuals
.
sourcepub fn full_sort(&mut self, options: TableSortOptions) -> TskReturnValue
pub fn full_sort(&mut self, options: TableSortOptions) -> TskReturnValue
Fully sort all tables.
Implemented via a call to sort
.
Note
As of 0.7.0
, this function does not sort the individual table!
See
topological_sort_individuals
.
sourcepub fn topological_sort_individuals(
&mut self,
options: IndividualTableSortOptions
) -> TskReturnValue
pub fn topological_sort_individuals(
&mut self,
options: IndividualTableSortOptions
) -> TskReturnValue
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::<f64, i32>(0,&[],&[1]).unwrap();
assert_eq!(i0, 0);
let i1 = tables.add_individual::<f64, i32>(0,&[],&[]).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.
sourcepub fn dump(
&self,
filename: &str,
options: TableOutputOptions
) -> TskReturnValue
pub fn dump(
&self,
filename: &str,
options: TableOutputOptions
) -> TskReturnValue
Dump the table collection to file.
sourcepub fn clear(&mut self, options: TableClearOptions) -> TskReturnValue
pub fn clear(&mut self, options: TableClearOptions) -> TskReturnValue
Clear the contents of all tables. Does not release memory. Memory will be released when the object goes out of scope.
sourcepub fn equals(
&self,
other: &TableCollection,
options: TableEqualityOptions
) -> bool
pub fn equals(
&self,
other: &TableCollection,
options: TableEqualityOptions
) -> bool
Return true
if self
contains the same
data as other
, and false
otherwise.
sourcepub fn deepcopy(&self) -> Result<TableCollection, TskitError>
pub fn deepcopy(&self) -> Result<TableCollection, TskitError>
Return a “deep” copy of the tables.
sourcepub fn tree_sequence(
self,
flags: TreeSequenceFlags
) -> Result<TreeSequence, TskitError>
pub fn tree_sequence(
self,
flags: TreeSequenceFlags
) -> Result<TreeSequence, TskitError>
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.
sourcepub fn simplify<N: Into<NodeId>>(
&mut self,
samples: &[N],
options: SimplificationOptions,
idmap: bool
) -> Result<Option<Vec<NodeId>>, TskitError>
pub fn simplify<N: Into<NodeId>>(
&mut self,
samples: &[N],
options: SimplificationOptions,
idmap: bool
) -> Result<Option<Vec<NodeId>>, TskitError>
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.
sourcepub fn check_integrity(&self, flags: TableIntegrityCheckFlags) -> TskReturnValue
pub fn check_integrity(&self, flags: TableIntegrityCheckFlags) -> TskReturnValue
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();
let mut tables = tskit::TableCollection::new(10.0).unwrap();
// Left position is < 0.0
tables.add_edge(-1., 10.0, 0, 0);
tables.check_integrity(tskit::TableIntegrityCheckFlags::default()).unwrap();
let mut tables = tskit::TableCollection::new(10.0).unwrap();
// Edges cannot have null node ids
tables.add_edge(0., 10.0, tskit::NodeId::NULL, 0);
tables.check_integrity(tskit::TableIntegrityCheckFlags::default()).unwrap();
sourcepub fn add_provenance(
&mut self,
record: &str
) -> Result<ProvenanceId, TskitError>
pub fn add_provenance(
&mut self,
record: &str
) -> Result<ProvenanceId, TskitError>
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
use tskit::TableAccess;
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(×tamp).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);
Trait Implementations
sourceimpl Drop for TableCollection
impl Drop for TableCollection
sourceimpl NodeListGenerator for TableCollection
impl NodeListGenerator for TableCollection
sourcefn samples_as_vector(&self) -> Vec<NodeId>
fn samples_as_vector(&self) -> Vec<NodeId>
Obtain a vector containing the indexes (“ids”)
of all nodes for which crate::TSK_NODE_IS_SAMPLE
is true
. Read more
sourcefn create_node_id_vector(
&self,
f: impl FnMut(&NodeTableRow) -> bool
) -> Vec<NodeId>
fn create_node_id_vector(
&self,
f: impl FnMut(&NodeTableRow) -> bool
) -> Vec<NodeId>
Obtain a vector containing the indexes (“ids”) of all nodes satisfying a certain criterion. Read more
sourceimpl TableAccess for TableCollection
impl TableAccess for TableCollection
sourcefn individuals(&self) -> IndividualTable<'_>
fn individuals(&self) -> IndividualTable<'_>
Get reference to the IndividualTable
.
sourcefn migrations(&self) -> MigrationTable<'_>
fn migrations(&self) -> MigrationTable<'_>
Get reference to the MigrationTable
.
sourcefn mutations(&self) -> MutationTable<'_>
fn mutations(&self) -> MutationTable<'_>
Get reference to the MutationTable
.
sourcefn populations(&self) -> PopulationTable<'_>
fn populations(&self) -> PopulationTable<'_>
Get reference to the PopulationTable
.
sourcefn provenances(&self) -> ProvenanceTable<'_>
fn provenances(&self) -> ProvenanceTable<'_>
Get reference to the ProvenanceTable
sourcefn edges_iter(&self) -> Box<dyn Iterator<Item = EdgeTableRow>>
fn edges_iter(&self) -> Box<dyn Iterator<Item = EdgeTableRow>>
Return an iterator over the edges.
sourcefn nodes_iter(&self) -> Box<dyn Iterator<Item = NodeTableRow>>
fn nodes_iter(&self) -> Box<dyn Iterator<Item = NodeTableRow>>
Return an iterator over the nodes.
sourcefn mutations_iter(&self) -> Box<dyn Iterator<Item = MutationTableRow>>
fn mutations_iter(&self) -> Box<dyn Iterator<Item = MutationTableRow>>
Return an iterator over the mutations.
sourcefn sites_iter(&self) -> Box<dyn Iterator<Item = SiteTableRow>>
fn sites_iter(&self) -> Box<dyn Iterator<Item = SiteTableRow>>
Return an iterator over the sites.
sourcefn populations_iter(&self) -> Box<dyn Iterator<Item = PopulationTableRow>>
fn populations_iter(&self) -> Box<dyn Iterator<Item = PopulationTableRow>>
Return an iterator over the populations.
sourcefn migrations_iter(&self) -> Box<dyn Iterator<Item = MigrationTableRow>>
fn migrations_iter(&self) -> Box<dyn Iterator<Item = MigrationTableRow>>
Return an iterator over the migration events.
sourcefn individuals_iter(&self) -> Box<dyn Iterator<Item = IndividualTableRow>>
fn individuals_iter(&self) -> Box<dyn Iterator<Item = IndividualTableRow>>
Return an iterator over the individuals.
sourcefn provenances_iter(&self) -> Box<dyn Iterator<Item = ProvenanceTableRow>>
fn provenances_iter(&self) -> Box<dyn Iterator<Item = ProvenanceTableRow>>
Return an iterator over provenances
sourceimpl TskitTypeAccess<tsk_table_collection_t> for TableCollection
impl TskitTypeAccess<tsk_table_collection_t> for TableCollection
sourcefn as_ptr(&self) -> *const tsk_table_collection_t
fn as_ptr(&self) -> *const tsk_table_collection_t
Return const pointer
sourcefn as_mut_ptr(&mut self) -> *mut tsk_table_collection_t
fn as_mut_ptr(&mut self) -> *mut tsk_table_collection_t
Return mutable pointer
Auto Trait Implementations
impl RefUnwindSafe for TableCollection
impl !Send for TableCollection
impl !Sync for TableCollection
impl Unpin for TableCollection
impl UnwindSafe for TableCollection
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more