Expand description
A rust interface to tskit.
This crate provides a mapping of the tskit C API to rust.
The result is an interface similar to the tskit Python interface,
but with all operations implemented using compiled code.
§Features
§Interface to the C library
TableCollectionwrapstsk_table_collection_t.TreeSequencewrapstsk_treeseq_t.Treewrapstsk_tree_t.- Tree iteration occurs via traits from streaming_iterator.
- Errors returned from C map to
TskitError::ErrorCode. Their string messages can be obtained by printing the error type.
§Safety
- The types listed above handle all the memory management!
- All array accesses are range-checked.
- Object lifetimes are clear:
- Creating a tree sequence moves/consumes a table collection.
- Tree lifetimes are tied to that of the parent tree sequence.
- Table objects (
NodeTable, etc..) are only represented by non-owning, immutable types.
§Prelude
The prelude module contains definitions that are difficult/annoying to live without.
In particuar, this module exports various traits that make it so that client code does
not have to use them a la carte.
We recomment that client code import all symbols from this module:
use tskit::prelude::*;The various documentation examples manually use each trait both in order
to illustrate which traits are needed and to serve as doc tests.
§Optional features
Some features are optional, and are activated by requesting them in your Cargo.toml file.
-
provenance- Enables
provenance
- Enables
-
deriveenables the following derive macros:crate::metadata::MutationMetadatacrate::metadata::IndividualMetadatacrate::metadata::SiteMetadatacrate::metadata::EdgeMetadatacrate::metadata::NodeMetadatacrate::metadata::MigrationMetadatacrate::metadata::PopulationMetadata
To see these derive macros in action, take a look
here. -
unsafe_initenablescrate::TableCollection::new_from_raw
To add features to your Cargo.toml file:
[dependencies]
tskit = {version = "0.2.0", features=["feature0", "feature1"]}§Table rows and iterators over rows
§Background: what is going on at the C level
The C API represents a row as a struct containing
various fields.
For example, a “mutation” (row of a mutation table) is
represented by crate::bindings::tsk_mutation_t.
These low-level types contain pointers into ragged arrays such as metadata. These pointers do not point to new allocations. Rather, they point to subsets of the ragged arrays found in the parent objects (tables).
The API to populate the row types is to first allocate one
(either on the stack or on the heap) and then call a function.
For example, crate::bindings::tsk_mutation_table_get_row
will fill in the fields of a crate::bindings::tsk_mutation_t.
The challenge on the rust side is how to specify the lifetime relationship between a row object and its parent object.
§Differences between table collections and tree sequences
The row types mentioned above can be accessed from table collections and from tree sequences.
However, tree sequence initialization pre-computes site and mutation objects as well as the nodes associated with individuals. Therefore, we can obtain constant-time access to references to site and mutation objects from a tree sequence.
The situation for objects directly from table collections poses a challenge. We could re-use an instance of a row object for, say, a “mutation table row iterator” type, but doing so would result in incorrect data if the output values were stored. (Re-use would mean that the pointers to metadata, etc., would get re-written at each “turn” of the iterator.) Therefore, when accessing from tables, we return new instances of the low level types.
§The relevant rust types
For tables:
Nodeis returned byNodeTable::rowand is the iterator value ofNodeTable::iterandTableCollection::node_iter.Edgeis returned byEdgeTable::rowand is the iterator value ofEdgeTable::iterandTableCollection::edge_iter.Individualis returned byIndividualTable::rowand is the iterator value ofIndividualTable::iterandTableCollection::individual_iter.Siteis returned bySiteTable::rowand is the iterator value ofSiteTable::iterandTableCollection::site_iter.Mutationis returned byMutationTable::rowand is the iterator value ofMutationTable::iterandTableCollection::mutation_iter.Populationis returned byPopulationTable::rowand is the iterator value ofPopulationTable::iterandTableCollection::population_iter.Migrationis returned byMigrationTable::rowand is the iterator value ofMigrationTable::iterandTableCollection::migration_iter.Provenanceis returned byprovenance::ProvenanceTable::rowand is the iterator value ofprovenance::ProvenanceTable::iterandTableCollection::provenance_iter.
These types are thin wrappers around the C types and have the same sizeof and alignment.
For table collections and trees:
SiteRefandMutationRefreplaceSiteandMutation, respectively.SiteRefis the output ofTreeSequence::site_iterandTree::site_iter.MutationRefis the output ofSiteRef::mutation_iter.
These “Ref” types are thin wrappers around shared references to the underlying C types.
Further,
TreeSequence::individual_iteroutputsIndividualobjects whosenodesfield is populated (if the individual is associated w/any nodes).
§What is missing?
- A lot of wrappers to the C functions.
- Tree sequence statistics!
§Manual
A manual is here.
Re-exports§
pub use error::TskitError;
Modules§
- bindings
bindings - Low-level (“unsafe”) bindings to the C API.
- error
- Error handling
- metadata
- Support for table row metadata
- prelude
- Export commonly-use types and traits
- provenance
provenance - Optional module for table and tree sequence provenance tables.
- types
- “Other” tskit types live here.
Macros§
- handle_
metadata_ return - Convenience macro to handle implementing
crate::metadata::MetadataRoundtrip
Structs§
- Current
Tree Edge Differences - Edge
- A lifetime-bound Edge.
- Edge
Difference - An edge difference. Edge insertions and removals are differentiated by
marker types
InsertionandRemoval, respectively. - Edge
Differences Iterator - Manages iteration over trees to obtain edge differences.
- EdgeId
- An edge ID
- Edge
Insertions Iterator - Edge
Removals Iterator - Edge
Table - An edge table.
- Individual
- A lifetime-bound Individual.
- Individual
Flags - Individual flags
- Individual
Id - An individual ID
- Individual
Table - An individual table.
- Individual
Table Sort Options - Modify behavior of
crate::TableCollection::topological_sort_individuals. - Insertion
- Marker type for edge insertion.
- Location
- A newtype for the concept of location.
A
Locationmay represent a location or the output of arithmetic involving locations. - Migration
- A lifetime-bound Migration.
- Migration
Id - A migration ID
- Migration
Table - A migration table.
- Mutation
- A lifetime-bound Mutation.
- Mutation
Id - A mutation ID
- Mutation
Parents Flags - Mutation
Ref - Reference to a mutation stored in a tree sequence.
- Mutation
Table - An immutable view of site table.
- Node
- A lifetime-bound Node.
- Node
Defaults - Defaults for node table rows without metadata
- Node
Defaults With Metadata - Notes
- Node
Flags - NodeId
- A node ID
- Node
Table - A node table
- Population
- A lifetime-bound Population.
- Population
Id - A population ID
- Population
Table - A population table
- Position
- A newtype for the concept of “genomic position”.
A
Positioncan represent either a locus or a distance between loci. - Provenance
provenance - Provenance
Id provenance - A provenance ID
- Removal
- Marker type for edge removal.
- Simplification
Options - Control the behavior of table simplification.
- Site
- A lifetime-bound site.
- SiteId
- A site ID
- SiteRef
- Reference to a site stored in a tree sequence.
- Site
Table - A site table.
- Size
Type - Wraps
tsk_size_t - Table
Clear Options - Modify behavior of
crate::TableCollection::clear. - Table
Collection - A table collection.
- Table
Equality Options - Modify behavior of
crate::TableCollection::equals. - Table
Integrity Check Flags - Flags to affect the behavior of
TableCollection::check_integrity. - Table
Output Options - Modify behavior of
crate::TableCollection::dump. - Table
Sort Options - Modify behavior of
crate::TableCollection::sort. - Time
- A newtype for the concept of time.
A
Timevalue can represent either a point in time or the output of arithmetic involving time. - Tree
- A Tree.
- Tree
Flags - Specify the behavior of iterating over
crate::Treeobjects. Seecrate::TreeSequence::tree_iterator. - Tree
Sequence - A tree sequence.
- Tree
Sequence Flags - Modify behavior of
crate::TableCollection::tree_sequenceandcrate::TreeSequence::new.
Enums§
Traits§
- Double
Ended Streaming Iterator - A streaming iterator able to yield elements from both ends.
- Individual
Location - Abstraction of individual location.
- Individual
Parents - Abstraction of individual parents.
- Streaming
Iterator - An interface for dealing with streaming iterators.
- Table
Column - Interface of a non-ragged table column.
Functions§
- c_
api_ major_ version - C API major version
- c_
api_ minor_ version - C API minor version
- c_
api_ patch_ version - C API patch version
- c_
api_ version - The C API version in MAJOR.MINOR.PATCH format
- version
- Version of the rust crate.
Type Aliases§
- Edge
Insertion - Type alias for
EdgeDifference<Insertion> - Edge
Removal - Type alias for
EdgeDifference<Removal> - RawFlags
- Alias for tsk_flags_t
- TskReturn
Value - Handles return codes from low-level tskit functions.