Skip to content

Commit

Permalink
Merge pull request #421 from EDiLD/master
Browse files Browse the repository at this point in the history
few tests
  • Loading branch information
eduardszoecs committed May 2, 2015
2 parents 7ae99c1 + 98e402a commit b3bb8ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
12 changes: 6 additions & 6 deletions R/tax_agg.R
Expand Up @@ -65,18 +65,18 @@

tax_agg <- function(x, rank, db = 'ncbi', verbose=FALSE, ...)
{
if(is.matrix(x))
{
if(is.null(colnames(x))) stop("The community data matrix must have named columns")
x <- data.frame(x, check.names=FALSE)
if (is.matrix(x)) {
if (is.null(colnames(x)))
stop("The community data matrix must have named columns")
x <- data.frame(x, check.names = FALSE)
}
# bring to long format
x$rownames <- rownames(x)
df_m <- melt(x, id = 'rownames')

# aggregate to family level (by querying NCBI for taxonomic classification)
uniq_tax <- as.character(unique(df_m$variable))
agg <- tax_name(uniq_tax, get = rank, db = db, verbose=verbose, ...)
agg <- tax_name(uniq_tax, get = rank, db = db, verbose = verbose, ...)
lookup <- data.frame(variable = uniq_tax, agg = agg[ , 3], stringsAsFactors = FALSE)

# merge lookup with orig.
Expand All @@ -87,7 +87,7 @@ tax_agg <- function(x, rank, db = 'ncbi', verbose=FALSE, ...)

# bring back to long format and aggregate
df_l <- dcast(df_merged, rownames ~ agg,
value.var='value', fun.aggregate = sum)
value.var = 'value', fun.aggregate = sum)

rownames(df_l) <- df_l$rownames
df_l$rownames <- NULL
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-rankagg.R
@@ -0,0 +1,19 @@
# tests for rankagg fxn in taxize
context("rankagg")
data(dune.taxon, package = 'vegan')
dat <- dune.taxon
set.seed(1234)
dat$abundance <- round(rlnorm(n = nrow(dat), meanlog = 5, sdlog = 2), 0)
out <- rankagg(data = dat, datacol = "abundance", rank = "Genus")


test_that("rankagg throws error", {
expect_error(rankagg(datacol = "abundance", rank = "Genus"))
expect_error(rankagg(data = dat, datacol = "abundance"))
})


test_that("Correct result", {
expect_is(out, "data.frame")
expect_equal(out[1, 2], 13)
})
4 changes: 4 additions & 0 deletions tests/testthat/test-tax_agg.R
Expand Up @@ -8,6 +8,9 @@ species <- c("Bellis perennis", "Empetrum nigrum", "Juncus bufonius", "Juncus ar
"Aira praecox")
colnames(take) <- species
out_ncbi <- tax_agg(take, rank = 'family', db = 'ncbi', verbose = FALSE)
take2 <- take
colnames(take2) <- NULL



test_that("tax_agg returns the correct class", {
Expand All @@ -21,6 +24,7 @@ test_that("tax_agg returns the correct value", {
expect_that(nrow(out_ncbi$x), equals(nrow(take)))
expect_that(nrow(out_ncbi$by), equals(length(unique(colnames(take)))))
expect_that(out_ncbi$n_pre, equals(length(unique(colnames(take)))))
expect_error(tax_agg(as.matrix(take2), rank = 'family', db = 'ncbi', verbose = FALSE))
})


Expand Down

0 comments on commit b3bb8ae

Please sign in to comment.