Skip to content

Commit

Permalink
filter_taxa now has better error messages for invalid inputs
Browse files Browse the repository at this point in the history
resolves #117
  • Loading branch information
zachary-foster committed Jan 12, 2018
1 parent c96be12 commit 63a3ec7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Description: Provides taxonomic classes for
with data. Methods provided are "taxonomically aware", in
that they know about ordering of ranks, and methods that
filter based on taxonomy also filter associated data.
Version: 0.2.0.9103
Version: 0.2.0.9104
Authors@R: c(
person("Scott", "Chamberlain", role = c("aut", "cre"), email = "myrmecocystus+r@gmail.com"),
person("Zachary", "Foster", role = "aut", email = "zacharyfoster1989@gmail.com")
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Current

### Bug fixes

* `filter_taxa` now has better error messages for invalid inputs ([issue #117](https://github.com/ropensci/taxa/issues/117)).
* Fix a bug that caused an error in `filter_taxa` when no taxa pass filter ([issue #116](https://github.com/ropensci/taxa/issues/116)).

taxa 0.2.0
Expand Down
43 changes: 35 additions & 8 deletions R/taxonomy--class.R
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ Taxonomy <- R6::R6Class(
reassign_obs <- parse_possibly_named_logical(
reassign_obs,
self$data,
default = formals(self$filter_taxa)$reassign_obs
default <- formals(self$filter_taxa)$reassign_obs
)
process_one <- function(data_index) {

Expand Down Expand Up @@ -996,7 +996,6 @@ Taxonomy <- R6::R6Class(
apply(self$edge_list, 1, paste0, collapse = "->")
},

# Remove taxa not in "el_indexes"
remove_taxa = function(el_indexes) {
# Remove taxa objects
self$taxa <- self$taxa[self$taxon_ids()[el_indexes]]
Expand All @@ -1005,10 +1004,8 @@ Taxonomy <- R6::R6Class(
self$edge_list <- self$edge_list[el_indexes, , drop = FALSE]

# Replace and edges going to removed taxa with NA
to_replace <- ! self$edge_list$from %in% self$taxon_ids()
if (sum(to_replace) > 0) {
self$edge_list[to_replace, "from"] <- as.character(NA)
}
self$edge_list[! self$edge_list$from %in% self$taxon_ids(), "from"] <-
as.character(NA)
},

# Takes one ore more NSE expressions and resolves them to indexes of edgelist rows
Expand All @@ -1022,15 +1019,45 @@ Taxonomy <- R6::R6Class(
return(self$taxon_indexes())
}

# Check that index input is valid
is_num <- vapply(selection, is.numeric, logical(1))
unused <- lapply(selection[is_num],
function(x) {
invalid_indexes <- x[x < 1 | x > length(self$taxon_ids())]
if (length(invalid_indexes) > 0) {
stop(call. = FALSE,
paste0("The following taxon indexes are invalid:\n",
limited_print(invalid_indexes, type = "silent")))
}
})

# Convert taxon_ids to indexes
is_char <- vapply(selection, is.character, logical(1))
selection[is_char] <- lapply(selection[is_char],
function(x) match(x, self$taxon_ids()))
function(x) {
result <- match(x, self$taxon_ids())
invalid_ids <- x[is.na(result)]
if (length(invalid_ids) > 0) {
stop(call. = FALSE,
paste0("The following taxon IDs do not exist:\n",
limited_print(invalid_ids, type = "silent")))
}
return(result)
})

# Convert logical to indexes
is_tf <- vapply(selection, is.logical, logical(1))
selection[is_tf] <- lapply(selection[is_tf],
function(x) which(x))
function(x) {
if (length(x) != length(self$taxon_ids())) {
stop(call. = FALSE,
paste0("TRUE/FALSE vector (length = ",
length(x),
") must be the same length as the number of taxa (",
length(self$taxon_ids()), ")"))
}
which(x)
})

# Combine index lists.
intersect_with_dups <- function(a, b) {
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test--taxonomy.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Testing `taxonomy` class

library(taxa)
library(testthat)
context("taxonomy")


Expand Down Expand Up @@ -268,6 +269,14 @@ test_that("Filtering taxa", {
expected_names <- expected_names[! expected_names %in% c("Solanum", "lycopersicum", "tuberosum")]
expect_true(all(expected_names == result$taxon_names()))

# Errors for invalid indexes
expect_error(filter_taxa(x, 100), "The following taxon indexes are invalid:")

# Errors for invalid IDs
expect_error(filter_taxa(x, "zzz"), "The following taxon IDs do not exist:")

# Errors for invalid logical
expect_error(filter_taxa(x, TRUE), "must be the same length as the number of taxa")
})


Expand Down

0 comments on commit 63a3ec7

Please sign in to comment.