Struct tskit::TreeSequence
source · [−]pub struct TreeSequence { /* private fields */ }
Expand description
A tree sequence.
This is a thin wrapper around the C type tsk_treeseq_t
.
When created from a TableCollection
, the input tables are
moved into the TreeSequence
object.
Examples
let mut tables = tskit::TableCollection::new(1000.).unwrap();
tables.add_node(0, 1.0, tskit::PopulationId::NULL, tskit::IndividualId::NULL).unwrap();
tables.add_node(0, 0.0, tskit::PopulationId::NULL, tskit::IndividualId::NULL).unwrap();
tables.add_node(0, 0.0, tskit::PopulationId::NULL, tskit::IndividualId::NULL).unwrap();
tables.add_edge(0., 1000., 0, 1).unwrap();
tables.add_edge(0., 1000., 0, 2).unwrap();
// index
tables.build_index();
// tables gets moved into our treeseq variable:
let treeseq = tables.tree_sequence(tskit::TreeSequenceFlags::default()).unwrap();
Implementations
sourceimpl TreeSequence
impl TreeSequence
sourcepub fn new(
tables: TableCollection,
flags: TreeSequenceFlags
) -> Result<Self, TskitError>
pub fn new(
tables: TableCollection,
flags: TreeSequenceFlags
) -> Result<Self, TskitError>
Create a tree sequence from a TableCollection
.
In general, TableCollection::tree_sequence
may be preferred.
The table collection is moved/consumed.
Parameters
tables
, aTableCollection
Errors
TskitError
if the tables are not indexed.
Examples
let mut tables = tskit::TableCollection::new(1000.).unwrap();
tables.build_index();
let tree_sequence = tskit::TreeSequence::new(tables, tskit::TreeSequenceFlags::default()).unwrap();
The following may be preferred to the previous example, and more closely
mimics the Python tskit
interface:
let mut tables = tskit::TableCollection::new(1000.).unwrap();
tables.build_index();
let tree_sequence = tables.tree_sequence(tskit::TreeSequenceFlags::default()).unwrap();
The following raises an error because the tables are not indexed:
let mut tables = tskit::TableCollection::new(1000.).unwrap();
let tree_sequence = tskit::TreeSequence::new(tables,
tskit::TreeSequenceFlags::default()).unwrap();
sourcepub fn dump(
&self,
filename: &str,
options: TableOutputOptions
) -> TskReturnValue
pub fn dump(
&self,
filename: &str,
options: TableOutputOptions
) -> TskReturnValue
Dump the tree sequence to file.
Note
options
is currently not used. Set to default value. This behavior may change in a future release, which could breakAPI
.
sourcepub fn load(filename: &str) -> Result<Self, TskitError>
pub fn load(filename: &str) -> Result<Self, TskitError>
Load from a file.
sourcepub fn dump_tables(&self) -> Result<TableCollection, TskitError>
pub fn dump_tables(&self) -> Result<TableCollection, TskitError>
Obtain a copy of the TableCollection
.
The result is a “deep” copy of the tables.
Errors
TskitError
will be raised if the underlying C library returns an error code.
sourcepub fn tree_iterator(&self, flags: TreeFlags) -> Result<Tree, TskitError>
pub fn tree_iterator(&self, flags: TreeFlags) -> Result<Tree, TskitError>
Create an iterator over trees.
Parameters
flags
ATreeFlags
bit field.
Errors
Examples
// You must include streaming_iterator as a dependency
// and import this type.
use streaming_iterator::StreamingIterator;
// Import this to allow .next_back() for reverse
// iteration over trees.
use streaming_iterator::DoubleEndedStreamingIterator;
let mut tables = tskit::TableCollection::new(1000.).unwrap();
tables.build_index();
let tree_sequence = tables.tree_sequence(tskit::TreeSequenceFlags::default()).unwrap();
let mut tree_iterator = tree_sequence.tree_iterator(tskit::TreeFlags::default()).unwrap();
while let Some(tree) = tree_iterator.next() {
}
Warning
The following code results in an infinite loop. Be sure to note the difference from the previous example.
use streaming_iterator::StreamingIterator;
let mut tables = tskit::TableCollection::new(1000.).unwrap();
tables.build_index();
let tree_sequence = tables.tree_sequence(tskit::TreeSequenceFlags::default()).unwrap();
while let Some(tree) = tree_sequence.tree_iterator(tskit::TreeFlags::default()).unwrap().next() {
}
sourcepub fn samples_to_vec(&self) -> Vec<NodeId>
👎 Deprecated since 0.2.3: Please use TreeSequence::sample_nodes instead
pub fn samples_to_vec(&self) -> Vec<NodeId>
Please use TreeSequence::sample_nodes instead
Get the list of samples as a vector.
sourcepub fn sample_nodes(&self) -> &[NodeId]
pub fn sample_nodes(&self) -> &[NodeId]
Get the list of sample nodes as a slice.
sourcepub fn kc_distance(
&self,
other: &TreeSequence,
lambda: f64
) -> Result<f64, TskitError>
pub fn kc_distance(
&self,
other: &TreeSequence,
lambda: f64
) -> Result<f64, TskitError>
Calculate the average Kendall-Colijn (K-C
) distance between
pairs of trees whose intervals overlap.
Note
Parameters
lambda
specifies the relative weight of topology and branch length. SeeTree::kc_distance
for more details.
pub fn num_samples(&self) -> SizeType
sourcepub fn simplify(
&self,
samples: &[NodeId],
options: SimplificationOptions,
idmap: bool
) -> Result<(Self, Option<Vec<NodeId>>), TskitError>
pub fn simplify(
&self,
samples: &[NodeId],
options: SimplificationOptions,
idmap: bool
) -> Result<(Self, Option<Vec<NodeId>>), TskitError>
Simplify tables and return a new tree sequence.
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 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
use tskit::TableAccess;
let mut tables = tskit::TableCollection::new(1000.).unwrap();
let mut treeseq = tables.tree_sequence(tskit::TreeSequenceFlags::BUILD_INDEXES).unwrap();
treeseq.add_provenance(&String::from("All your provenance r belong 2 us.")).unwrap();
let prov_ref = treeseq.provenances();
let row_0 = prov_ref.row(0).unwrap();
assert_eq!(row_0.record, "All your provenance r belong 2 us.");
let record_0 = prov_ref.record(0).unwrap();
assert_eq!(record_0, row_0.record);
let timestamp = prov_ref.timestamp(0).unwrap();
assert_eq!(timestamp, row_0.timestamp);
use core::str::FromStr;
let dt_utc = humantime::Timestamp::from_str(×tamp).unwrap();
println!("utc = {}", dt_utc);
Trait Implementations
sourceimpl Drop for TreeSequence
impl Drop for TreeSequence
sourceimpl NodeListGenerator for TreeSequence
impl NodeListGenerator for TreeSequence
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 TreeSequence
impl TableAccess for TreeSequence
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_treeseq_t> for TreeSequence
impl TskitTypeAccess<tsk_treeseq_t> for TreeSequence
sourcefn as_ptr(&self) -> *const tsk_treeseq_t
fn as_ptr(&self) -> *const tsk_treeseq_t
Return const pointer
sourcefn as_mut_ptr(&mut self) -> *mut tsk_treeseq_t
fn as_mut_ptr(&mut self) -> *mut tsk_treeseq_t
Return mutable pointer
Auto Trait Implementations
impl RefUnwindSafe for TreeSequence
impl !Send for TreeSequence
impl !Sync for TreeSequence
impl Unpin for TreeSequence
impl UnwindSafe for TreeSequence
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