Skip to content

Commit b70650b

Browse files
committed
Added test case and unit tests for options
1 parent adfb1a4 commit b70650b

4 files changed

Lines changed: 127 additions & 17 deletions

File tree

RcppTskit/inst/examples/create_test.trees.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ builtins <- import_builtins()
2020
msprime <- import("msprime")
2121
tskit <- import("tskit")
2222

23+
# -----------------------------------------------------------------------------
24+
2325
# Generate a tree sequence for testing
2426
ts <- msprime$sim_ancestry(
2527
samples = 80,
@@ -85,6 +87,8 @@ length(ts$tables$individuals$metadata) # 0
8587
ts$dump("inst/examples/test.trees")
8688
# ts <- tskit$load("inst/examples/test.trees")
8789

90+
# -----------------------------------------------------------------------------
91+
8892
# Create a second tree sequence with metadata in some tables
8993
# basic_schema <- tskit$MetadataSchema("{'codec': 'json'}")
9094
# Can't get this to work via reticulate :(
@@ -104,3 +108,34 @@ ts$metadata_schema # {"codec":"json"}
104108
ts$tables$individuals$metadata # R vector
105109
builtins$type(ts$tables$individuals$metadata) # numpy.ndarray
106110
length(ts$tables$individuals$metadata) # 21
111+
112+
# -----------------------------------------------------------------------------
113+
114+
# Another example with a reference sequence
115+
116+
ts <- msprime$sim_ancestry(
117+
samples = 3,
118+
ploidy = 2,
119+
sequence_length = 10,
120+
random_seed = 2
121+
)
122+
ts <- msprime$sim_mutations(ts, rate = 0.1, random_seed = 2)
123+
ts$has_reference_sequence() # FALSE
124+
ts$reference_sequence # NULL
125+
126+
tables <- ts$dump_tables()
127+
tables$reference_sequence$data <- "ATCGAATTCG"
128+
ts <- tables$tree_sequence()
129+
ts$has_reference_sequence() # TRUE
130+
ts$reference_sequence
131+
# ReferenceSequence({'metadata_schema': '', 'metadata': b'', 'data': 'ATCGAATTCG', 'url': ''})
132+
133+
ali <- ts$alignments()
134+
iterate(ali, function(i) cat(i, "\n"))
135+
# iterate(ali, function(i) print(i)) # produces no output
136+
ali_vec <- iterate(ts$alignments())
137+
print(ali_vec)
138+
139+
ts$dump("RcppTskit/inst/examples/test_with_ref_seq.trees")
140+
141+
# -----------------------------------------------------------------------------

RcppTskit/inst/examples/create_test.trees.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import tskit
33
import os
44

5+
# -----------------------------------------------------------------------------
6+
57
# Generate a tree sequence for testing
68
ts = msprime.sim_ancestry(
79
samples=80, sequence_length=1e4, recombination_rate=1e-4, random_seed=42
@@ -55,11 +57,13 @@
5557
ts.tables.individuals.metadata.shape # (0,)
5658

5759
os.getcwd()
58-
os.chdir("RcppTskit")
59-
ts.dump("inst/examples/test.trees")
60-
# ts = tskit.load("test.trees")
60+
ts.dump("RcppTskit/inst/examples/test.trees")
61+
# ts = tskit.load("RcppTskit/inst/examples/test.trees")
62+
63+
# -----------------------------------------------------------------------------
6164

6265
# Create a second tree sequence with metadata in some tables
66+
# ts = tskit.load("RcppTskit/inst/examples/test.trees")
6367
ts2_tables = ts.dump_tables()
6468
len(ts2_tables.metadata)
6569
ts2_tables.metadata = tskit.pack_bytes('{"seed": 42, "note": "ts2"}')
@@ -107,7 +111,7 @@
107111
len(ts.tables.individuals.metadata) # 21
108112
ts.tables.individuals.metadata.shape # (21,)
109113

110-
ts.dump("inst/examples/test2.trees")
114+
ts.dump("RcppTskit/inst/examples/test2.trees")
111115

112116
tables = ts.dump_tables()
113117
tables.metadata_schema = tskit.MetadataSchema(None)
@@ -116,3 +120,26 @@
116120
ts = tables.tree_sequence()
117121
ts.metadata
118122
len(ts.metadata) # 23
123+
124+
# -----------------------------------------------------------------------------
125+
126+
# Another example with a reference sequence
127+
128+
ts = msprime.sim_ancestry(samples=3, ploidy=2, sequence_length=10, random_seed=2)
129+
ts = msprime.sim_mutations(ts, rate=0.1, random_seed=2)
130+
ts.has_reference_sequence()
131+
ts.reference_sequence
132+
133+
tables = ts.dump_tables()
134+
tables.reference_sequence.data = "ATCGAATTCG"
135+
ts = tables.tree_sequence()
136+
ts.has_reference_sequence()
137+
ts.reference_sequence
138+
139+
ali = ts.alignments()
140+
for i in ali:
141+
print(i)
142+
143+
ts.dump("RcppTskit/inst/examples/test_with_ref_seq.trees")
144+
145+
# -----------------------------------------------------------------------------
8.49 KB
Binary file not shown.

RcppTskit/tests/testthat/test_load_summary_and_dump.R

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,57 @@ test_that("ts/tc_load(), ts/tc_summary*(), and ts/tc_dump(x) work", {
77
expect_error(ts_load("nonexistent_ts"))
88
ts_file <- system.file("examples/test.trees", package = "RcppTskit")
99

10+
expect_error(
11+
ts_ptr_load(ts_file, options = bitwShiftL(1L, 30)),
12+
regexp = "ts_ptr_load only supports load options"
13+
# TSK_LOAD_SKIP_TABLES (1 << 0) and TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1)
14+
)
15+
16+
# TSK_LOAD_SKIP_TABLES (1 << 0)
1017
expect_error(
1118
ts_load(ts_file, skip_tables = "y"),
1219
regexp = "skip_tables must be TRUE/FALSE!"
1320
)
14-
expect_no_error(tc_load(ts_file, skip_tables = TRUE))
21+
expect_no_error(ts_ptr_load(ts_file, options = bitwShiftL(1L, 0L)))
22+
expect_no_error(ts_load(ts_file, skip_tables = TRUE))
23+
check_empty_tables_ptr <- function(ts) {
24+
# jarl-ignore implicit_assignment: it's just a test
25+
tmp <- capture.output(p <- ts_ptr_print(ts))
26+
expect_true(all(p$tables$number == 0))
27+
}
1528
check_empty_tables <- function(ts) {
1629
# jarl-ignore implicit_assignment: it's just a test
1730
tmp <- capture.output(p <- ts$print())
1831
expect_true(all(p$tables$number == 0))
1932
}
33+
ts_ptr <- ts_ptr_load(ts_file, options = bitwShiftL(1L, 0L))
34+
check_empty_tables_ptr(ts_ptr)
2035
ts <- ts_load(ts_file, skip_tables = TRUE)
2136
check_empty_tables(ts)
22-
ts <- TableCollection$new(file = ts_file, skip_tables = TRUE)
37+
ts <- TreeSequence$new(file = ts_file, skip_tables = TRUE)
2338
check_empty_tables(ts)
2439

40+
# TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1)
2541
expect_error(
2642
ts_load(ts_file, skip_reference_sequence = 1L),
2743
regexp = "skip_reference_sequence must be TRUE/FALSE!"
2844
)
45+
expect_no_error(ts_ptr_load(ts_file, options = bitwShiftL(1L, 1L)))
2946
expect_no_error(ts_load(ts_file, skip_reference_sequence = TRUE))
3047

31-
expect_error(
32-
ts_ptr_load(ts_file, options = bitwShiftL(1L, 30)),
33-
regexp = "ts_ptr_load only supports load options"
34-
# TSK_LOAD_SKIP_TABLES (1 << 0) and TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1)
48+
ts_with_ref_seq_file <- system.file(
49+
"examples/test_with_ref_seq.trees",
50+
package = "RcppTskit"
3551
)
36-
52+
expect_no_error(ts_ptr_load(ts_with_ref_seq_file))
53+
expect_no_error(ts_ptr_load(
54+
ts_with_ref_seq_file,
55+
options = bitwShiftL(1L, 1L)
56+
))
57+
expect_no_error(ts_load(ts_with_ref_seq_file))
58+
expect_no_error(ts_load(ts_with_ref_seq_file, skip_reference_sequence = TRUE))
59+
60+
# For tests below
3761
ts_ptr <- ts_ptr_load(ts_file)
3862
ts <- ts_load(ts_file)
3963

@@ -45,33 +69,57 @@ test_that("ts/tc_load(), ts/tc_summary*(), and ts/tc_dump(x) work", {
4569
expect_error(tc_load("nonexistent_ts"))
4670
ts_file <- system.file("examples/test.trees", package = "RcppTskit")
4771

72+
expect_error(
73+
tc_ptr_load(ts_file, options = bitwShiftL(1L, 30)),
74+
regexp = "tc_ptr_load only supports load options"
75+
# TSK_LOAD_SKIP_TABLES (1 << 0) and TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1)
76+
)
77+
78+
# TSK_LOAD_SKIP_TABLES (1 << 0)
4879
expect_error(
4980
tc_load(ts_file, skip_tables = "y"),
5081
regexp = "skip_tables must be TRUE/FALSE!"
5182
)
83+
expect_no_error(tc_ptr_load(ts_file, options = bitwShiftL(1L, 0L)))
5284
expect_no_error(tc_load(ts_file, skip_tables = TRUE))
85+
check_empty_tables_ptr <- function(tc) {
86+
# jarl-ignore implicit_assignment: it's just a test
87+
tmp <- capture.output(p <- tc_ptr_print(tc))
88+
expect_true(all(p$tables$number == 0))
89+
}
5390
check_empty_tables <- function(tc) {
54-
# jarl-ignore implicit_assignment: it's just a test
91+
# jarl-ignore implicit_assignment: it's just a test
5592
tmp <- capture.output(p <- tc$print())
5693
expect_true(all(p$tables$number == 0))
5794
}
95+
tc_ptr <- tc_ptr_load(ts_file, options = bitwShiftL(1L, 0L))
96+
check_empty_tables_ptr(tc_ptr)
5897
tc <- tc_load(ts_file, skip_tables = TRUE)
5998
check_empty_tables(tc)
6099
tc <- TableCollection$new(file = ts_file, skip_tables = TRUE)
61100
check_empty_tables(tc)
62101

102+
# TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1)
63103
expect_error(
64104
tc_load(ts_file, skip_reference_sequence = 1L),
65105
regexp = "skip_reference_sequence must be TRUE/FALSE!"
66106
)
107+
expect_no_error(tc_ptr_load(ts_file, options = bitwShiftL(1L, 1L)))
67108
expect_no_error(tc_load(ts_file, skip_reference_sequence = TRUE))
68109

69-
expect_error(
70-
tc_ptr_load(ts_file, options = bitwShiftL(1L, 30)),
71-
regexp = "tc_ptr_load only supports load options"
72-
# TSK_LOAD_SKIP_TABLES (1 << 0) and TSK_LOAD_SKIP_REFERENCE_SEQUENCE (1 << 1)
110+
ts_with_ref_seq_file <- system.file(
111+
"examples/test_with_ref_seq.trees",
112+
package = "RcppTskit"
73113
)
74-
114+
expect_no_error(tc_ptr_load(ts_with_ref_seq_file))
115+
expect_no_error(tc_ptr_load(
116+
ts_with_ref_seq_file,
117+
options = bitwShiftL(1L, 1L)
118+
))
119+
expect_no_error(tc_load(ts_with_ref_seq_file))
120+
expect_no_error(tc_load(ts_with_ref_seq_file, skip_reference_sequence = TRUE))
121+
122+
# For tests below
75123
tc_ptr <- tc_ptr_load(ts_file)
76124
tc <- tc_load(ts_file)
77125

0 commit comments

Comments
 (0)