From b70650be3b8f0ed2c11f5c72be268bdeedacede8 Mon Sep 17 00:00:00 2001 From: Gregor Gorjanc Date: Wed, 11 Feb 2026 07:48:06 +0000 Subject: [PATCH] Added test case and unit tests for options --- RcppTskit/inst/examples/create_test.trees.R | 35 +++++++++ RcppTskit/inst/examples/create_test.trees.py | 35 ++++++++- .../inst/examples/test_with_ref_seq.trees | Bin 0 -> 8692 bytes .../testthat/test_load_summary_and_dump.R | 74 +++++++++++++++--- 4 files changed, 127 insertions(+), 17 deletions(-) create mode 100644 RcppTskit/inst/examples/test_with_ref_seq.trees diff --git a/RcppTskit/inst/examples/create_test.trees.R b/RcppTskit/inst/examples/create_test.trees.R index 02cbfa2..213da2d 100644 --- a/RcppTskit/inst/examples/create_test.trees.R +++ b/RcppTskit/inst/examples/create_test.trees.R @@ -20,6 +20,8 @@ builtins <- import_builtins() msprime <- import("msprime") tskit <- import("tskit") +# ----------------------------------------------------------------------------- + # Generate a tree sequence for testing ts <- msprime$sim_ancestry( samples = 80, @@ -85,6 +87,8 @@ length(ts$tables$individuals$metadata) # 0 ts$dump("inst/examples/test.trees") # ts <- tskit$load("inst/examples/test.trees") +# ----------------------------------------------------------------------------- + # Create a second tree sequence with metadata in some tables # basic_schema <- tskit$MetadataSchema("{'codec': 'json'}") # Can't get this to work via reticulate :( @@ -104,3 +108,34 @@ ts$metadata_schema # {"codec":"json"} ts$tables$individuals$metadata # R vector builtins$type(ts$tables$individuals$metadata) # numpy.ndarray length(ts$tables$individuals$metadata) # 21 + +# ----------------------------------------------------------------------------- + +# Another example with a reference sequence + +ts <- msprime$sim_ancestry( + samples = 3, + ploidy = 2, + sequence_length = 10, + random_seed = 2 +) +ts <- msprime$sim_mutations(ts, rate = 0.1, random_seed = 2) +ts$has_reference_sequence() # FALSE +ts$reference_sequence # NULL + +tables <- ts$dump_tables() +tables$reference_sequence$data <- "ATCGAATTCG" +ts <- tables$tree_sequence() +ts$has_reference_sequence() # TRUE +ts$reference_sequence +# ReferenceSequence({'metadata_schema': '', 'metadata': b'', 'data': 'ATCGAATTCG', 'url': ''}) + +ali <- ts$alignments() +iterate(ali, function(i) cat(i, "\n")) +# iterate(ali, function(i) print(i)) # produces no output +ali_vec <- iterate(ts$alignments()) +print(ali_vec) + +ts$dump("RcppTskit/inst/examples/test_with_ref_seq.trees") + +# ----------------------------------------------------------------------------- diff --git a/RcppTskit/inst/examples/create_test.trees.py b/RcppTskit/inst/examples/create_test.trees.py index 5f94cdf..4c00823 100644 --- a/RcppTskit/inst/examples/create_test.trees.py +++ b/RcppTskit/inst/examples/create_test.trees.py @@ -2,6 +2,8 @@ import tskit import os +# ----------------------------------------------------------------------------- + # Generate a tree sequence for testing ts = msprime.sim_ancestry( samples=80, sequence_length=1e4, recombination_rate=1e-4, random_seed=42 @@ -55,11 +57,13 @@ ts.tables.individuals.metadata.shape # (0,) os.getcwd() -os.chdir("RcppTskit") -ts.dump("inst/examples/test.trees") -# ts = tskit.load("test.trees") +ts.dump("RcppTskit/inst/examples/test.trees") +# ts = tskit.load("RcppTskit/inst/examples/test.trees") + +# ----------------------------------------------------------------------------- # Create a second tree sequence with metadata in some tables +# ts = tskit.load("RcppTskit/inst/examples/test.trees") ts2_tables = ts.dump_tables() len(ts2_tables.metadata) ts2_tables.metadata = tskit.pack_bytes('{"seed": 42, "note": "ts2"}') @@ -107,7 +111,7 @@ len(ts.tables.individuals.metadata) # 21 ts.tables.individuals.metadata.shape # (21,) -ts.dump("inst/examples/test2.trees") +ts.dump("RcppTskit/inst/examples/test2.trees") tables = ts.dump_tables() tables.metadata_schema = tskit.MetadataSchema(None) @@ -116,3 +120,26 @@ ts = tables.tree_sequence() ts.metadata len(ts.metadata) # 23 + +# ----------------------------------------------------------------------------- + +# Another example with a reference sequence + +ts = msprime.sim_ancestry(samples=3, ploidy=2, sequence_length=10, random_seed=2) +ts = msprime.sim_mutations(ts, rate=0.1, random_seed=2) +ts.has_reference_sequence() +ts.reference_sequence + +tables = ts.dump_tables() +tables.reference_sequence.data = "ATCGAATTCG" +ts = tables.tree_sequence() +ts.has_reference_sequence() +ts.reference_sequence + +ali = ts.alignments() +for i in ali: + print(i) + +ts.dump("RcppTskit/inst/examples/test_with_ref_seq.trees") + +# ----------------------------------------------------------------------------- diff --git a/RcppTskit/inst/examples/test_with_ref_seq.trees b/RcppTskit/inst/examples/test_with_ref_seq.trees new file mode 100644 index 0000000000000000000000000000000000000000..3a94227295a40693072263b536d14f2eba5df463 GIT binary patch literal 8692 zcmeHLU5p$@5#9hX!NKH02*d`V@#r2dW z%wBxC99W2lfQ*2=V38LDN)(9)kW4@%JcNj!NQlThVDS#7xq;md4+| zG3o52@ucP*B={BKS^sxA@yHuLza{*;l40RP@1@R^=4z6^KzMSS;(0xj%&`9p3jSzL z{~W(v*C!s{NsbuL@tEYFvxI+(3Q)$p z{(Xf10Lfp@$)EAZ2)~E$Gw(|})7H=YL&8&2SRnjc6~5|_Np7ZBf@}C!fpW^>QPCVz&mlgcXO-Uf9`RDkbAw0kLON8gKvTf;**Z&=5{ezr% z&Y!ai|8EgKbu^*F{C`S#uHP8X_0$A-f&HIX@aJ;+=k@sh z@XY^eDm=LUI7;}vlNL_%9KYQJHYC3!t?uaitwqU2_5!tEBqOs*Z)ZceVY z4-%g9^$GGn?fzl|4-ua4({qHMWrYc?==Mcevez89n{-{#i;|uvBCBgf*7lJZ^m?IC zRLsy`6CP;}EGc|akWPOs>IKrXqLOcUg1YNMhECu+epj3X_vpcN{7}dUX3ao$h1@2S z!VA_dH+g` z8=ljbG2^h*6=9Sa;!b3Sj2kn>9!92E`C=!2&=naI@djnaVK9_-Rv~gc;SHmdqIA|p z*9>7|(loP4K}=?3hG!zPA3T< zMNh!Vw1pXp&knIu!s9(=n%8g%5pB>IjiGeezUd0TAFbi;gr#^-^o~;VSRzWk*>~zc z7;DtUI3pHq{3kyv2UeC>mJ^GU_~r43T=9!MIo8Pc4fsG4x7g!^7q&GJ@#Dt+FMFLeD1Y#?MLSx_{pxv|J;7y8_n~_pZa6_v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s&6*{*G0f<>q3w)~L_d7Dabn7`^Je(Wp1)ZKJ$cX;dnW)ym?( E0D@9MT>t<8 literal 0 HcmV?d00001 diff --git a/RcppTskit/tests/testthat/test_load_summary_and_dump.R b/RcppTskit/tests/testthat/test_load_summary_and_dump.R index 40bac75..6881186 100644 --- a/RcppTskit/tests/testthat/test_load_summary_and_dump.R +++ b/RcppTskit/tests/testthat/test_load_summary_and_dump.R @@ -7,33 +7,57 @@ test_that("ts/tc_load(), ts/tc_summary*(), and ts/tc_dump(x) work", { expect_error(ts_load("nonexistent_ts")) ts_file <- system.file("examples/test.trees", package = "RcppTskit") + expect_error( + ts_ptr_load(ts_file, options = bitwShiftL(1L, 30)), + regexp = "ts_ptr_load only supports load options" + # TSK_LOAD_SKIP_TABLES (1 << 0) and TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1) + ) + + # TSK_LOAD_SKIP_TABLES (1 << 0) expect_error( ts_load(ts_file, skip_tables = "y"), regexp = "skip_tables must be TRUE/FALSE!" ) - expect_no_error(tc_load(ts_file, skip_tables = TRUE)) + expect_no_error(ts_ptr_load(ts_file, options = bitwShiftL(1L, 0L))) + expect_no_error(ts_load(ts_file, skip_tables = TRUE)) + check_empty_tables_ptr <- function(ts) { + # jarl-ignore implicit_assignment: it's just a test + tmp <- capture.output(p <- ts_ptr_print(ts)) + expect_true(all(p$tables$number == 0)) + } check_empty_tables <- function(ts) { # jarl-ignore implicit_assignment: it's just a test tmp <- capture.output(p <- ts$print()) expect_true(all(p$tables$number == 0)) } + ts_ptr <- ts_ptr_load(ts_file, options = bitwShiftL(1L, 0L)) + check_empty_tables_ptr(ts_ptr) ts <- ts_load(ts_file, skip_tables = TRUE) check_empty_tables(ts) - ts <- TableCollection$new(file = ts_file, skip_tables = TRUE) + ts <- TreeSequence$new(file = ts_file, skip_tables = TRUE) check_empty_tables(ts) + # TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1) expect_error( ts_load(ts_file, skip_reference_sequence = 1L), regexp = "skip_reference_sequence must be TRUE/FALSE!" ) + expect_no_error(ts_ptr_load(ts_file, options = bitwShiftL(1L, 1L))) expect_no_error(ts_load(ts_file, skip_reference_sequence = TRUE)) - expect_error( - ts_ptr_load(ts_file, options = bitwShiftL(1L, 30)), - regexp = "ts_ptr_load only supports load options" - # TSK_LOAD_SKIP_TABLES (1 << 0) and TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1) + ts_with_ref_seq_file <- system.file( + "examples/test_with_ref_seq.trees", + package = "RcppTskit" ) - + expect_no_error(ts_ptr_load(ts_with_ref_seq_file)) + expect_no_error(ts_ptr_load( + ts_with_ref_seq_file, + options = bitwShiftL(1L, 1L) + )) + expect_no_error(ts_load(ts_with_ref_seq_file)) + expect_no_error(ts_load(ts_with_ref_seq_file, skip_reference_sequence = TRUE)) + + # For tests below ts_ptr <- ts_ptr_load(ts_file) ts <- ts_load(ts_file) @@ -45,33 +69,57 @@ test_that("ts/tc_load(), ts/tc_summary*(), and ts/tc_dump(x) work", { expect_error(tc_load("nonexistent_ts")) ts_file <- system.file("examples/test.trees", package = "RcppTskit") + expect_error( + tc_ptr_load(ts_file, options = bitwShiftL(1L, 30)), + regexp = "tc_ptr_load only supports load options" + # TSK_LOAD_SKIP_TABLES (1 << 0) and TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1) + ) + + # TSK_LOAD_SKIP_TABLES (1 << 0) expect_error( tc_load(ts_file, skip_tables = "y"), regexp = "skip_tables must be TRUE/FALSE!" ) + expect_no_error(tc_ptr_load(ts_file, options = bitwShiftL(1L, 0L))) expect_no_error(tc_load(ts_file, skip_tables = TRUE)) + check_empty_tables_ptr <- function(tc) { + # jarl-ignore implicit_assignment: it's just a test + tmp <- capture.output(p <- tc_ptr_print(tc)) + expect_true(all(p$tables$number == 0)) + } check_empty_tables <- function(tc) { - # jarl-ignore implicit_assignment: it's just a test + # jarl-ignore implicit_assignment: it's just a test tmp <- capture.output(p <- tc$print()) expect_true(all(p$tables$number == 0)) } + tc_ptr <- tc_ptr_load(ts_file, options = bitwShiftL(1L, 0L)) + check_empty_tables_ptr(tc_ptr) tc <- tc_load(ts_file, skip_tables = TRUE) check_empty_tables(tc) tc <- TableCollection$new(file = ts_file, skip_tables = TRUE) check_empty_tables(tc) + # TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1) expect_error( tc_load(ts_file, skip_reference_sequence = 1L), regexp = "skip_reference_sequence must be TRUE/FALSE!" ) + expect_no_error(tc_ptr_load(ts_file, options = bitwShiftL(1L, 1L))) expect_no_error(tc_load(ts_file, skip_reference_sequence = TRUE)) - expect_error( - tc_ptr_load(ts_file, options = bitwShiftL(1L, 30)), - regexp = "tc_ptr_load only supports load options" - # TSK_LOAD_SKIP_TABLES (1 << 0) and TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1) + ts_with_ref_seq_file <- system.file( + "examples/test_with_ref_seq.trees", + package = "RcppTskit" ) - + expect_no_error(tc_ptr_load(ts_with_ref_seq_file)) + expect_no_error(tc_ptr_load( + ts_with_ref_seq_file, + options = bitwShiftL(1L, 1L) + )) + expect_no_error(tc_load(ts_with_ref_seq_file)) + expect_no_error(tc_load(ts_with_ref_seq_file, skip_reference_sequence = TRUE)) + + # For tests below tc_ptr <- tc_ptr_load(ts_file) tc <- tc_load(ts_file)