Skip to content

Commit 352d7e5

Browse files
committed
add add_row() functions for population migration provenance table
1 parent e33fc2d commit 352d7e5

8 files changed

Lines changed: 715 additions & 0 deletions

File tree

RcppTskit/NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ and releases adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html
4040
- Added `rtsk_mutation_table_add_row()` and
4141
`TableCollection$mutation_table_add_row()` to append mutation rows from
4242
\code{R}, mirroring `tsk_mutation_table_add_row()`.
43+
- Added `rtsk_population_table_add_row()` and
44+
`TableCollection$population_table_add_row()` to append population rows from
45+
\code{R}, mirroring `tsk_population_table_add_row()`.
46+
- Added `rtsk_migration_table_add_row()` and
47+
`TableCollection$migration_table_add_row()` to append migration rows from
48+
\code{R}, mirroring `tsk_migration_table_add_row()`.
49+
- Added `rtsk_provenance_table_add_row()` and
50+
`TableCollection$provenance_table_add_row()` to append provenance rows from
51+
\code{R}, mirroring `tsk_provenance_table_add_row()`.
4352
- Added `rtsk_node_table_get_row()` and `TableCollection$node_table_get_row()`
4453
to retrieve node-table rows by 0-based row index.
4554
- Added `rtsk_table_collection_sort()` and `TableCollection$sort()` to sort

RcppTskit/R/Class-TableCollection.R

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,31 @@ TableCollection <- R6Class(
138138
rtsk_table_collection_get_num_provenances(self$xptr)
139139
},
140140

141+
#' @description Add a row to the provenances table.
142+
#' @param timestamp character string timestamp for the new provenance.
143+
#' @param record character string record for the new provenance.
144+
#' @details See the \code{tskit Python} equivalent at
145+
#' \url{https://tskit.dev/tskit/docs/stable/python-api.html#tskit.ProvenanceTable.add_row}.
146+
#' @return An integer row index and hence ID (0-based) of the newly added provenance.
147+
#' @examples
148+
#' ts_file <- system.file("examples/test.trees", package = "RcppTskit")
149+
#' tc <- tc_load(ts_file)
150+
#' (n_before <- tc$num_provenances())
151+
#' new_id <- tc$provenance_table_add_row(
152+
#' timestamp = "2025-01-01T00:00:00Z",
153+
#' record = "{\"software\":\"RcppTskit\"}"
154+
#' )
155+
#' (n_after <- tc$num_provenances())
156+
provenance_table_add_row = function(timestamp, record) {
157+
validate_character_scalar_arg(timestamp, "timestamp")
158+
validate_character_scalar_arg(record, "record")
159+
rtsk_provenance_table_add_row(
160+
tc = self$xptr,
161+
timestamp = as.character(timestamp),
162+
record = as.character(record)
163+
)
164+
},
165+
141166
#' @description Get the number of populations in a table collection.
142167
#' @return A signed 64 bit integer \code{bit64::integer64}.
143168
#' @examples
@@ -148,6 +173,28 @@ TableCollection <- R6Class(
148173
rtsk_table_collection_get_num_populations(self$xptr)
149174
},
150175

176+
#' @description Add a row to the populations table.
177+
#' @param metadata for the new population; accepts \code{NULL},
178+
#' a raw vector, or a character of length 1.
179+
#' @details See the \code{tskit Python} equivalent at
180+
#' \url{https://tskit.dev/tskit/docs/stable/python-api.html#tskit.PopulationTable.add_row}.
181+
#' @return An integer row index and hence ID (0-based) of the newly added population.
182+
#' @examples
183+
#' ts_file <- system.file("examples/test.trees", package = "RcppTskit")
184+
#' tc <- tc_load(ts_file)
185+
#' (n_before <- tc$num_populations())
186+
#' new_id <- tc$population_table_add_row()
187+
#' new_id <- tc$population_table_add_row(metadata = "abc")
188+
#' new_id <- tc$population_table_add_row(metadata = charToRaw("xyz"))
189+
#' (n_after <- tc$num_populations())
190+
population_table_add_row = function(metadata = NULL) {
191+
metadata_raw <- validate_metadata_arg(metadata)
192+
rtsk_population_table_add_row(
193+
tc = self$xptr,
194+
metadata = metadata_raw
195+
)
196+
},
197+
151198
#' @description Get the number of migrations in a table collection.
152199
#' @return A signed 64 bit integer \code{bit64::integer64}.
153200
#' @examples
@@ -158,6 +205,71 @@ TableCollection <- R6Class(
158205
rtsk_table_collection_get_num_migrations(self$xptr)
159206
},
160207

208+
#' @description Add a row to the migrations table.
209+
#' @param left numeric scalar left coordinate for the new migration.
210+
#' @param right numeric scalar right coordinate for the new migration.
211+
#' @param node integer scalar node ID (0-based).
212+
#' @param source integer scalar source population ID (0-based).
213+
#' @param dest integer scalar destination population ID (0-based).
214+
#' @param time numeric scalar time value for the new migration.
215+
#' @param metadata for the new migration; accepts \code{NULL},
216+
#' a raw vector, or a character of length 1.
217+
#' @details See the \code{tskit Python} equivalent at
218+
#' \url{https://tskit.dev/tskit/docs/stable/python-api.html#tskit.MigrationTable.add_row}.
219+
#' @return An integer row index and hence ID (0-based) of the newly added migration.
220+
#' @examples
221+
#' ts_file <- system.file("examples/test.trees", package = "RcppTskit")
222+
#' tc <- tc_load(ts_file)
223+
#' (n_before <- tc$num_migrations())
224+
#' new_id <- tc$migration_table_add_row(
225+
#' left = 0,
226+
#' right = 1,
227+
#' node = 0L,
228+
#' source = 0L,
229+
#' dest = 0L,
230+
#' time = 1.0
231+
#' )
232+
#' new_id <- tc$migration_table_add_row(
233+
#' left = 1,
234+
#' right = 2,
235+
#' node = 1L,
236+
#' source = 0L,
237+
#' dest = 0L,
238+
#' time = 2.0,
239+
#' metadata = "abc"
240+
#' )
241+
#' (n_after <- tc$num_migrations())
242+
migration_table_add_row = function(
243+
left,
244+
right,
245+
node,
246+
source,
247+
dest,
248+
time,
249+
metadata = NULL
250+
) {
251+
validate_numeric_scalar_arg(left, "left")
252+
validate_numeric_scalar_arg(right, "right")
253+
if (as.numeric(left) >= as.numeric(right)) {
254+
stop("left must be strictly less than right!")
255+
}
256+
validate_row_index(node, "node")
257+
validate_row_index(source, "source")
258+
validate_row_index(dest, "dest")
259+
validate_numeric_scalar_arg(time, "time")
260+
metadata_raw <- validate_metadata_arg(metadata)
261+
rtsk_migration_table_add_row(
262+
tc = self$xptr,
263+
left = as.numeric(left),
264+
right = as.numeric(right),
265+
node = as.integer(node),
266+
source = as.integer(source),
267+
dest = as.integer(dest),
268+
time = as.numeric(time),
269+
metadata = metadata_raw
270+
)
271+
},
272+
161273
#' @description Get the number of individuals in a table collection.
162274
#' @return A signed 64 bit integer \code{bit64::integer64}.
163275
#' @examples

RcppTskit/R/RcppExports.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ rtsk_mutation_table_add_row <- function(tc, site, node, parent, time, derived_st
259259
.Call(`_RcppTskit_rtsk_mutation_table_add_row`, tc, site, node, parent, time, derived_state, metadata)
260260
}
261261

262+
rtsk_population_table_add_row <- function(tc, metadata = NULL) {
263+
.Call(`_RcppTskit_rtsk_population_table_add_row`, tc, metadata)
264+
}
265+
266+
rtsk_migration_table_add_row <- function(tc, left, right, node, source, dest, time, metadata = NULL) {
267+
.Call(`_RcppTskit_rtsk_migration_table_add_row`, tc, left, right, node, source, dest, time, metadata)
268+
}
269+
270+
rtsk_provenance_table_add_row <- function(tc, timestamp, record) {
271+
.Call(`_RcppTskit_rtsk_provenance_table_add_row`, tc, timestamp, record)
272+
}
273+
262274
test_tsk_bug_assert_c <- function() {
263275
invisible(.Call(`_RcppTskit_test_tsk_bug_assert_c`))
264276
}

RcppTskit/inst/include/RcppTskit_public.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,12 @@ int rtsk_mutation_table_add_row(
8686
SEXP tc, int site, int node, int parent, double time,
8787
const std::string &derived_state,
8888
Rcpp::Nullable<Rcpp::RawVector> metadata = R_NilValue);
89+
int rtsk_population_table_add_row(
90+
SEXP tc, Rcpp::Nullable<Rcpp::RawVector> metadata = R_NilValue);
91+
int rtsk_migration_table_add_row(
92+
SEXP tc, double left, double right, int node, int source, int dest,
93+
double time, Rcpp::Nullable<Rcpp::RawVector> metadata = R_NilValue);
94+
int rtsk_provenance_table_add_row(SEXP tc, const std::string &timestamp,
95+
const std::string &record);
8996

9097
#endif

0 commit comments

Comments
 (0)