Skip to content

Commit ba82ae9

Browse files
committed
Add TableCollection::check_integrity.
Closes #185
1 parent 562014b commit ba82ae9

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/flags.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,27 @@ bitflags! {
147147
const BUILD_INDEXES = ll_bindings::TSK_BUILD_INDEXES;
148148
}
149149
}
150+
151+
bitflags! {
152+
#[derive(Default)]
153+
pub struct TableIntegrityCheckFlags: tsk_flags_t {
154+
/// Default behavior is a set of basic checks
155+
const DEFAULT = 0;
156+
/// Check that edges are ordered
157+
const CHECK_EDGE_ORDERING =ll_bindings::TSK_CHECK_EDGE_ORDERING;
158+
/// Check that sites are ordered
159+
const CHECK_SITE_ORDERING =ll_bindings::TSK_CHECK_SITE_ORDERING;
160+
/// Check for duplicated sites
161+
const CHECK_SITE_DUPLICATES=ll_bindings::TSK_CHECK_SITE_DUPLICATES;
162+
/// Check that mutations are ordered
163+
const CHECK_MUTATION_ORDERING =ll_bindings::TSK_CHECK_MUTATION_ORDERING;
164+
/// Check that individuals are ordered
165+
const CHECK_INDIVIDUAL_ORDERING=ll_bindings::TSK_CHECK_INDIVIDUAL_ORDERING;
166+
/// Check that migrations are ordered
167+
const CHECK_MIGRATION_ORDERING= ll_bindings::TSK_CHECK_MIGRATION_ORDERING;
168+
/// Check that table indexes are valid
169+
const CHECK_INDEXES=ll_bindings::TSK_CHECK_INDEXES;
170+
/// Check tree integrity. Enables most or all of the preceding options.
171+
const CHECK_TREES=ll_bindings::TSK_CHECK_TREES;
172+
}
173+
}

src/table_collection.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::SiteTable;
1414
use crate::TableAccess;
1515
use crate::TableClearOptions;
1616
use crate::TableEqualityOptions;
17+
use crate::TableIntegrityCheckFlags;
1718
use crate::TableOutputOptions;
1819
use crate::TableSortOptions;
1920
use crate::TreeSequenceFlags;
@@ -686,6 +687,56 @@ impl TableCollection {
686687
}
687688
)
688689
}
690+
691+
/// Validate the contents of the table collection
692+
///
693+
/// # Parameters
694+
///
695+
/// `flags` is an instance of [`TableIntegrityCheckFlags`]
696+
///
697+
/// # Return value
698+
///
699+
/// `0` upon success, or an error code.
700+
/// However, if `flags` contains [`TableIntegrityCheckFlags::CHECK_TREES`],
701+
/// and no error is returned, then the return value is the number
702+
/// of trees.
703+
///
704+
/// # Note
705+
///
706+
/// Creating a [`crate::TreeSequence`] from a table collection will automatically
707+
/// run an integrity check.
708+
/// See [`TableCollection::tree_sequence`].
709+
///
710+
/// # Examples
711+
///
712+
/// There are many ways for a table colletion to be invalid.
713+
/// These examples are just the tip of the iceberg.
714+
///
715+
/// ```should_panic
716+
/// let mut tables = tskit::TableCollection::new(10.0).unwrap();
717+
/// // Right position is > sequence_length
718+
/// tables.add_edge(0.0, 11.0, 0, 0);
719+
/// tables.check_integrity(tskit::TableIntegrityCheckFlags::default()).unwrap();
720+
/// ```
721+
///
722+
/// ```should_panic
723+
/// let mut tables = tskit::TableCollection::new(10.0).unwrap();
724+
/// // Left position is < 0.0
725+
/// tables.add_edge(-1., 10.0, 0, 0);
726+
/// tables.check_integrity(tskit::TableIntegrityCheckFlags::default()).unwrap();
727+
/// ```
728+
///
729+
/// ```should_panic
730+
/// let mut tables = tskit::TableCollection::new(10.0).unwrap();
731+
/// // Edges cannot have null node ids
732+
/// tables.add_edge(0., 10.0, tskit::NodeId::NULL, 0);
733+
/// tables.check_integrity(tskit::TableIntegrityCheckFlags::default()).unwrap();
734+
/// ```
735+
pub fn check_integrity(&self, flags: TableIntegrityCheckFlags) -> TskReturnValue {
736+
let rv =
737+
unsafe { ll_bindings::tsk_table_collection_check_integrity(self.inner, flags.bits()) };
738+
handle_tsk_return_value!(rv)
739+
}
689740
}
690741

691742
impl TableAccess for TableCollection {

0 commit comments

Comments
 (0)