Skip to content

Commit

Permalink
Match tree extraction to tskit's 0-based indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
bodkan committed May 11, 2023
1 parent 087cbc2 commit 554e13e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
16 changes: 8 additions & 8 deletions R/tree-sequences.R
Original file line number Diff line number Diff line change
Expand Up @@ -1430,9 +1430,9 @@ ts_descendants <- function(ts, x, verbose = FALSE, complete = TRUE) {
#'
#' @param ts Tree sequence object of the class \code{slendr_ts}
#' @param i Position of the tree in the tree sequence. If \code{mode = "index"},
#' an i-th tree will be returned (in one-based indexing), if \code{mode =
#' "position"}, a tree covering an i-th base of the simulated genome will be
#' returned.
#' an i-th tree will be returned (in zero-based indexing as in tskit), if
#' \code{mode = "position"}, a tree covering the i-th base of the simulated genome will be
#' returned (again, in tskit's indexing).
#' @param mode How should the \code{i} argument be interpreted? Either "index"
#' as an i-th tree in the sequence of genealogies, or "position" along the
#' simulated genome.
Expand All @@ -1453,19 +1453,19 @@ ts_descendants <- function(ts, x, verbose = FALSE, complete = TRUE) {
#' # load the tree-sequence object from disk
#' ts <- ts_load(slendr_ts, model, simplify = TRUE)
#'
#' # extract the first tree in the tree sequence
#' tree <- ts_tree(ts, i = 1)
#' # extract the zero-th tree in the tree sequence
#' tree <- ts_tree(ts, i = 0)
#'
#' # extract the tree at a position 100000bp in the tree sequence
#' # extract the tree at a position in the tree sequence
#' tree <- ts_tree(ts, i = 100000, mode = "position")
#' @export
ts_tree <- function(ts, i, mode = c("index", "position"), ...) {
check_ts_class(ts)
mode <- match.arg(mode)
if (mode == "index")
tree <- ts$at_index(index = i - 1, ...)
tree <- ts$at_index(index = i, ...)
else
tree <- ts$at(position = i - 1, ...)
tree <- ts$at(position = i, ...)
attr(tree, "tree_sequence") <- ts
tree
}
Expand Down
6 changes: 3 additions & 3 deletions man/ts_phylo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/ts_tree.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vignettes/vignette-09-paper.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ ggsave("/Users/mp/Documents/postdoc/slendr-paper/preprint_models_v0.5/ex3.pdf",
```{r, ex4, time_it = TRUE, eval = eval_chunk, fig.keep='none'}
ts_small <- ts_simplify(ts, simplify_to = c("EUR_578", "YAM_75", "ANA_163", "EHG_208"))
tree <- ts_phylo(ts_small, i = 20 / scaling)
# tskit uses zero-based indexing
tree <- ts_phylo(ts_small, i = 19 / scaling)
nodes <- ts_nodes(tree)
edges <- ts_edges(tree)
Expand Down

0 comments on commit 554e13e

Please sign in to comment.