Skip to content

Commit c9f10d3

Browse files
committed
add TableCollection and TreeSequence simplification
1 parent c62ccd2 commit c9f10d3

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

src/flags.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::bindings as ll_bindings;
2+
use crate::ffi::TskitTypeAccess;
3+
use crate::TableCollection;
4+
use crate::TskitError;
5+
use crate::{tsk_flags_t, tsk_id_t, tsk_size_t, TSK_NULL};
6+
use bitflags::bitflags;
7+
8+
bitflags! {
9+
#[derive(Default)]
10+
pub struct SimplificationOptions: tsk_flags_t {
11+
/// Default behavior
12+
const NONE = 0;
13+
const FILTER_SITES = ll_bindings::TSK_FILTER_SITES;
14+
const FILTER_POPULATIONS = ll_bindings::TSK_FILTER_POPULATIONS;
15+
const FILTER_INDIVIDUALS = ll_bindings::TSK_FILTER_INDIVIDUALS;
16+
const REDUCE_TO_SITE_TOPOLOGY = ll_bindings::TSK_REDUCE_TO_SITE_TOPOLOGY;
17+
const KEEP_UNARY = ll_bindings::TSK_KEEP_UNARY;
18+
const KEEP_INPUT_ROOTS = ll_bindings::TSK_KEEP_INPUT_ROOTS;
19+
const KEEP_UNARY_IN_INDIVIDUALS = ll_bindings::TSK_KEEP_UNARY_IN_INDIVIDUALS;
20+
}
21+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod _macros; // Starts w/_ to be sorted at front by rustfmt!
1010
mod edge_table;
1111
pub mod error;
1212
pub mod ffi;
13+
mod flags;
1314
mod individual_table;
1415
pub mod metadata;
1516
mod migration_table;
@@ -43,6 +44,7 @@ pub const TSK_NULL: tsk_id_t = -1;
4344

4445
pub use edge_table::{EdgeTable, EdgeTableRow};
4546
pub use error::TskitError;
47+
pub use flags:: SimplificationOptions;
4648
pub use individual_table::{IndividualTable, IndividualTableRow};
4749
pub use migration_table::{MigrationTable, MigrationTableRow};
4850
pub use mutation_table::{MutationTable, MutationTableRow};

src/table_collection.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ use crate::MigrationTable;
99
use crate::MutationTable;
1010
use crate::NodeTable;
1111
use crate::PopulationTable;
12+
use crate::SimplificationOptions;
1213
use crate::SiteTable;
1314
use crate::TableAccess;
1415
use crate::TskReturnValue;
15-
use crate::{tsk_flags_t, tsk_id_t, tsk_size_t};
16+
use crate::{tsk_flags_t, tsk_id_t, tsk_size_t, TSK_NULL};
1617
use ll_bindings::tsk_table_collection_free;
1718

1819
/// A table collection.
@@ -487,6 +488,42 @@ impl TableCollection {
487488
pub fn tree_sequence(self) -> Result<crate::TreeSequence, TskitError> {
488489
crate::TreeSequence::new(self)
489490
}
491+
492+
pub fn simplify(
493+
&mut self,
494+
samples: &[tsk_id_t],
495+
options: SimplificationOptions,
496+
idmap: bool,
497+
) -> Result<Option<Vec<tsk_id_t>>, TskitError> {
498+
match idmap {
499+
true => {
500+
let mut output_node_map: Vec<tsk_id_t> =
501+
vec![TSK_NULL; self.nodes().num_rows() as usize];
502+
let rv = unsafe {
503+
ll_bindings::tsk_table_collection_simplify(
504+
self.as_mut_ptr(),
505+
samples.as_ptr(),
506+
samples.len() as tsk_size_t,
507+
options.bits(),
508+
output_node_map.as_mut_ptr(),
509+
)
510+
};
511+
handle_tsk_return_value!(rv, Some(output_node_map))
512+
}
513+
false => {
514+
let rv = unsafe {
515+
ll_bindings::tsk_table_collection_simplify(
516+
self.as_mut_ptr(),
517+
samples.as_ptr(),
518+
samples.len() as tsk_size_t,
519+
options.bits(),
520+
std::ptr::null_mut(),
521+
)
522+
};
523+
handle_tsk_return_value!(rv, None)
524+
}
525+
}
526+
}
490527
}
491528

492529
impl TableAccess for TableCollection {

src/trees.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::MigrationTable;
77
use crate::MutationTable;
88
use crate::NodeTable;
99
use crate::PopulationTable;
10+
use crate::SimplificationOptions;
1011
use crate::SiteTable;
1112
use crate::TableAccess;
1213
use crate::{tsk_flags_t, tsk_id_t, tsk_size_t, TableCollection, TSK_NULL};
@@ -831,6 +832,47 @@ impl TreeSequence {
831832
pub fn num_samples(&self) -> tsk_size_t {
832833
unsafe { ll_bindings::tsk_treeseq_get_num_samples(self.as_ptr()) }
833834
}
835+
836+
pub fn simplify(
837+
&mut self,
838+
samples: &[tsk_id_t],
839+
options: SimplificationOptions,
840+
idmap: bool,
841+
) -> Result<(Self, Option<Vec<tsk_id_t>>), TskitError> {
842+
let tables = TableCollection::new(unsafe { (*self.inner.tables).sequence_length })?;
843+
let mut ts = tables.tree_sequence()?;
844+
845+
match idmap {
846+
true => {
847+
let mut output_node_map: Vec<tsk_id_t> =
848+
vec![TSK_NULL; unsafe { (*self.inner.tables).nodes.num_rows } as usize];
849+
let rv = unsafe {
850+
ll_bindings::tsk_treeseq_simplify(
851+
self.as_mut_ptr(),
852+
samples.as_ptr(),
853+
samples.len() as tsk_size_t,
854+
options.bits(),
855+
ts.as_mut_ptr(),
856+
output_node_map.as_mut_ptr(),
857+
)
858+
};
859+
handle_tsk_return_value!(rv, (ts, Some(output_node_map)))
860+
}
861+
false => {
862+
let rv = unsafe {
863+
ll_bindings::tsk_treeseq_simplify(
864+
self.as_mut_ptr(),
865+
samples.as_ptr(),
866+
samples.len() as tsk_size_t,
867+
options.bits(),
868+
ts.as_mut_ptr(),
869+
std::ptr::null_mut(),
870+
)
871+
};
872+
handle_tsk_return_value!(rv, (ts, None))
873+
}
874+
}
875+
}
834876
}
835877

836878
impl TableAccess for TreeSequence {

0 commit comments

Comments
 (0)