Skip to content

Commit

Permalink
more tests for tax_agg
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardszoecs committed May 1, 2015
1 parent 2389436 commit 250e5b7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 6 additions & 6 deletions R/tax_agg.R
Expand Up @@ -64,18 +64,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 @@ -86,7 +86,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
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 250e5b7

Please sign in to comment.