Skip to content

Commit db0b90b

Browse files
committed
make TableCollection() aligned with Python version
1 parent fef7582 commit db0b90b

8 files changed

Lines changed: 77 additions & 43 deletions

File tree

RcppTskit/NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ and releases adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html
4343
- Added `rtsk_node_table_get_row()` and `TableCollection$node_table_get_row()`
4444
to retrieve node-table rows by 0-based row index.
4545
- Added `rtsk_table_collection_sort()` and `TableCollection$sort()` to sort
46-
table collections with 0-based `edge_start` semantics.
46+
table collections with 0-based `edge_start`, `site_start`, and
47+
`mutation_start` semantics.
4748
- Added low-level variant iterators
4849
(`rtsk_variant_iterator_init()`/`rtsk_variant_iterator_next()`) and a
4950
user-facing `TreeSequence$variants()` method to iterate over decoded

RcppTskit/R/Class-TableCollection.R

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,39 +103,28 @@ TableCollection <- R6Class(
103103
TreeSequence$new(xptr = ts_xptr)
104104
},
105105

106-
# TODO: add site_start and mutation_start
107106
#' @description Sort this table collection in place.
108107
#' @param edge_start integer scalar edge-table start row index (0-based).
109-
#' # TODO: remove this argument since Python code does not have it
110-
#' @param no_check_integrity logical; when \code{TRUE}, pass
111-
#' \code{TSK_NO_CHECK_INTEGRITY} to \code{tskit C}.
108+
#' @param site_start integer scalar site-table start row index (0-based).
109+
#' @param mutation_start integer scalar mutation-table start row index
110+
#' (0-based).
112111
#' @details See the \code{tskit Python} equivalent at
113112
#' \url{https://tskit.dev/tskit/docs/latest/python-api.html#tskit.TableCollection.sort}.
114113
#' @return No return value; called for side effects.
115114
#' @examples
116115
#' ts_file <- system.file("examples/test.trees", package = "RcppTskit")
117116
#' tc <- tc_load(ts_file)
118117
#' tc$sort()
119-
sort = function(edge_start = 0L, no_check_integrity = FALSE) {
118+
sort = function(edge_start = 0L, site_start = 0L, mutation_start = 0L) {
120119
validate_row_index(edge_start, "edge_start")
121-
# TODO: remove this argument since Python code does not have it
122-
if (
123-
!is.logical(no_check_integrity) ||
124-
length(no_check_integrity) != 1L ||
125-
is.na(no_check_integrity)
126-
) {
127-
stop("no_check_integrity must be TRUE/FALSE!")
128-
}
129-
options <- if (isTRUE(no_check_integrity)) {
130-
as.integer(rtsk_const_tsk_no_check_integrity())
131-
} else {
132-
0L
133-
}
120+
validate_row_index(site_start, "site_start")
121+
validate_row_index(mutation_start, "mutation_start")
134122
rtsk_table_collection_sort(
135123
tc = self$xptr,
136124
edge_start = as.integer(edge_start),
137-
# TODO: remove this argument since Python code does not have it
138-
options = options
125+
site_start = as.integer(site_start),
126+
mutation_start = as.integer(mutation_start),
127+
options = 0L
139128
)
140129
},
141130

RcppTskit/R/RcppExports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ rtsk_table_collection_drop_index <- function(tc, options = 0L) {
223223
invisible(.Call(`_RcppTskit_rtsk_table_collection_drop_index`, tc, options))
224224
}
225225

226-
rtsk_table_collection_sort <- function(tc, edge_start = 0L, options = 0L) {
227-
invisible(.Call(`_RcppTskit_rtsk_table_collection_sort`, tc, edge_start, options))
226+
rtsk_table_collection_sort <- function(tc, edge_start = 0L, site_start = 0L, mutation_start = 0L, options = 0L) {
227+
invisible(.Call(`_RcppTskit_rtsk_table_collection_sort`, tc, edge_start, site_start, mutation_start, options))
228228
}
229229

230230
rtsk_table_collection_summary <- function(tc) {

RcppTskit/inst/include/RcppTskit_public.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ Rcpp::String rtsk_table_collection_get_file_uuid(SEXP tc);
6363
bool rtsk_table_collection_has_index(SEXP tc, int options = 0);
6464
void rtsk_table_collection_build_index(SEXP tc, int options = 0);
6565
void rtsk_table_collection_drop_index(SEXP tc, int options = 0);
66-
void rtsk_table_collection_sort(SEXP tc, int edge_start = 0, int options = 0);
66+
void rtsk_table_collection_sort(SEXP tc, int edge_start = 0, int site_start = 0,
67+
int mutation_start = 0, int options = 0);
6768
Rcpp::List rtsk_table_collection_summary(SEXP tc);
6869
Rcpp::List rtsk_table_collection_metadata_length(SEXP tc);
6970
int rtsk_individual_table_add_row(

RcppTskit/man/TableCollection.Rd

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RcppTskit/src/RcppExports.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,14 +594,16 @@ BEGIN_RCPP
594594
END_RCPP
595595
}
596596
// rtsk_table_collection_sort
597-
void rtsk_table_collection_sort(SEXP tc, int edge_start, int options);
598-
RcppExport SEXP _RcppTskit_rtsk_table_collection_sort(SEXP tcSEXP, SEXP edge_startSEXP, SEXP optionsSEXP) {
597+
void rtsk_table_collection_sort(SEXP tc, int edge_start, int site_start, int mutation_start, int options);
598+
RcppExport SEXP _RcppTskit_rtsk_table_collection_sort(SEXP tcSEXP, SEXP edge_startSEXP, SEXP site_startSEXP, SEXP mutation_startSEXP, SEXP optionsSEXP) {
599599
BEGIN_RCPP
600600
Rcpp::RNGScope rcpp_rngScope_gen;
601601
Rcpp::traits::input_parameter< SEXP >::type tc(tcSEXP);
602602
Rcpp::traits::input_parameter< int >::type edge_start(edge_startSEXP);
603+
Rcpp::traits::input_parameter< int >::type site_start(site_startSEXP);
604+
Rcpp::traits::input_parameter< int >::type mutation_start(mutation_startSEXP);
603605
Rcpp::traits::input_parameter< int >::type options(optionsSEXP);
604-
rtsk_table_collection_sort(tc, edge_start, options);
606+
rtsk_table_collection_sort(tc, edge_start, site_start, mutation_start, options);
605607
return R_NilValue;
606608
END_RCPP
607609
}
@@ -899,7 +901,7 @@ static const R_CallMethodDef CallEntries[] = {
899901
{"_RcppTskit_rtsk_table_collection_has_index", (DL_FUNC) &_RcppTskit_rtsk_table_collection_has_index, 2},
900902
{"_RcppTskit_rtsk_table_collection_build_index", (DL_FUNC) &_RcppTskit_rtsk_table_collection_build_index, 2},
901903
{"_RcppTskit_rtsk_table_collection_drop_index", (DL_FUNC) &_RcppTskit_rtsk_table_collection_drop_index, 2},
902-
{"_RcppTskit_rtsk_table_collection_sort", (DL_FUNC) &_RcppTskit_rtsk_table_collection_sort, 3},
904+
{"_RcppTskit_rtsk_table_collection_sort", (DL_FUNC) &_RcppTskit_rtsk_table_collection_sort, 5},
903905
{"_RcppTskit_rtsk_table_collection_summary", (DL_FUNC) &_RcppTskit_rtsk_table_collection_summary, 1},
904906
{"_RcppTskit_rtsk_table_collection_metadata_length", (DL_FUNC) &_RcppTskit_rtsk_table_collection_metadata_length, 1},
905907
{"_RcppTskit_rtsk_individual_table_add_row", (DL_FUNC) &_RcppTskit_rtsk_individual_table_add_row, 5},

RcppTskit/src/RcppTskit.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,14 +1365,16 @@ void rtsk_table_collection_drop_index(SEXP tc, int options = 0) {
13651365
// # nocov end
13661366
}
13671367

1368-
// TODO: model this after
1369-
// https://github.com/tskit-dev/tskit/blob/2f26dc6f0d033cdf7d6fb1adfbab96468dde8831/python/_tskitmodule.c#L4526
13701368
// PUBLIC, wrapper for tsk_table_collection_sort
13711369
// @title Sort a table collection
13721370
// @param tc an external pointer to table collection as a
13731371
// \code{tsk_table_collection_t} object.
13741372
// @param edge_start integer scalar edge-table start row index (0-based) used in
13751373
// sorting bookmark.
1374+
// @param site_start integer scalar site-table start row index (0-based) used in
1375+
// sorting bookmark.
1376+
// @param mutation_start integer scalar mutation-table start row index (0-based)
1377+
// used in sorting bookmark.
13761378
// @param options passed to \code{tskit C}; this wrapper supports
13771379
// \code{TSK_NO_CHECK_INTEGRITY}.
13781380
// @details This function calls
@@ -1383,19 +1385,36 @@ void rtsk_table_collection_drop_index(SEXP tc, int options = 0) {
13831385
// tc_xptr <- RcppTskit:::rtsk_table_collection_load(ts_file)
13841386
// RcppTskit:::rtsk_table_collection_sort(tc_xptr)
13851387
// [[Rcpp::export]]
1386-
void rtsk_table_collection_sort(SEXP tc, int edge_start = 0, int options = 0) {
1388+
void rtsk_table_collection_sort(SEXP tc, int edge_start = 0, int site_start = 0,
1389+
int mutation_start = 0, int options = 0) {
13871390
if (Rcpp::IntegerVector::is_na(edge_start)) {
13881391
Rcpp::stop(
13891392
"edge_start must not be NA_integer_ in rtsk_table_collection_sort");
13901393
}
13911394
if (edge_start < 0) {
13921395
Rcpp::stop("edge_start must be >= 0 in rtsk_table_collection_sort");
13931396
}
1397+
if (Rcpp::IntegerVector::is_na(site_start)) {
1398+
Rcpp::stop(
1399+
"site_start must not be NA_integer_ in rtsk_table_collection_sort");
1400+
}
1401+
if (site_start < 0) {
1402+
Rcpp::stop("site_start must be >= 0 in rtsk_table_collection_sort");
1403+
}
1404+
if (Rcpp::IntegerVector::is_na(mutation_start)) {
1405+
Rcpp::stop(
1406+
"mutation_start must not be NA_integer_ in rtsk_table_collection_sort");
1407+
}
1408+
if (mutation_start < 0) {
1409+
Rcpp::stop("mutation_start must be >= 0 in rtsk_table_collection_sort");
1410+
}
13941411
const tsk_flags_t flags = validate_supported_options(
13951412
options, kTableSortSupportedFlags, "rtsk_table_collection_sort");
13961413
rtsk_table_collection_t tc_xptr(tc);
13971414
tsk_bookmark_t start = {0};
13981415
start.edges = static_cast<tsk_size_t>(edge_start);
1416+
start.sites = static_cast<tsk_size_t>(site_start);
1417+
start.mutations = static_cast<tsk_size_t>(mutation_start);
13991418
int ret = tsk_table_collection_sort(tc_xptr, &start, flags);
14001419
if (ret != 0) {
14011420
Rcpp::stop(tsk_strerror(ret));

RcppTskit/tests/testthat/test_TableCollection.R

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,28 @@ test_that("table_collection_sort wrapper validates inputs and sorts in place", {
290290
rtsk_table_collection_sort(tc_xptr, edge_start = -1L),
291291
regexp = "edge_start must be >= 0 in rtsk_table_collection_sort"
292292
)
293+
expect_error(
294+
rtsk_table_collection_sort(tc_xptr, site_start = NA_integer_),
295+
regexp = "site_start must not be NA_integer_ in rtsk_table_collection_sort"
296+
)
297+
expect_error(
298+
rtsk_table_collection_sort(tc_xptr, site_start = -1L),
299+
regexp = "site_start must be >= 0 in rtsk_table_collection_sort"
300+
)
301+
expect_error(
302+
rtsk_table_collection_sort(tc_xptr, mutation_start = NA_integer_),
303+
regexp = "mutation_start must not be NA_integer_ in rtsk_table_collection_sort"
304+
)
305+
expect_error(
306+
rtsk_table_collection_sort(tc_xptr, mutation_start = -1L),
307+
regexp = "mutation_start must be >= 0 in rtsk_table_collection_sort"
308+
)
293309
expect_error(
294310
rtsk_table_collection_sort(tc_xptr, options = bitwShiftL(1L, 4)),
295311
regexp = "only supports options"
296312
)
297313
expect_no_error(rtsk_table_collection_sort(tc_xptr))
298-
expect_no_error(rtsk_table_collection_sort(
299-
tc_xptr,
300-
options = as.integer(rtsk_const_tsk_no_check_integrity())
301-
))
314+
expect_no_error(rtsk_table_collection_sort(tc_xptr, 0L, 0L, 0L))
302315

303316
expect_error(
304317
tc$sort(edge_start = NA_integer_),
@@ -309,11 +322,19 @@ test_that("table_collection_sort wrapper validates inputs and sorts in place", {
309322
regexp = "edge_start must be a non-NA zero or positive integer scalar!"
310323
)
311324
expect_error(
312-
tc$sort(no_check_integrity = NA),
313-
regexp = "no_check_integrity must be TRUE/FALSE!"
325+
tc$sort(site_start = NA_integer_),
326+
regexp = "site_start must be a non-NA zero or positive integer scalar!"
327+
)
328+
expect_error(
329+
tc$sort(mutation_start = NA_integer_),
330+
regexp = "mutation_start must be a non-NA zero or positive integer scalar!"
314331
)
315332
expect_no_error(tc$sort())
316-
expect_no_error(tc$sort(no_check_integrity = TRUE))
333+
expect_no_error(tc$sort(
334+
edge_start = 0L,
335+
site_start = 0L,
336+
mutation_start = 0L
337+
))
317338
})
318339

319340
test_that("individual_table_add_row wrapper expands the table collection and handles inputs", {

0 commit comments

Comments
 (0)