Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions RcppTskit/inst/examples/create_test.trees.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 :(
Expand All @@ -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")

# -----------------------------------------------------------------------------
35 changes: 31 additions & 4 deletions RcppTskit/inst/examples/create_test.trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}')
Expand Down Expand Up @@ -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)
Expand All @@ -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")

# -----------------------------------------------------------------------------
Binary file added RcppTskit/inst/examples/test_with_ref_seq.trees
Binary file not shown.
74 changes: 61 additions & 13 deletions RcppTskit/tests/testthat/test_load_summary_and_dump.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
Loading