From 250e5b7ee6fd1b82a282e358b5cb78c7b0c2eb11 Mon Sep 17 00:00:00 2001 From: edisz Date: Fri, 1 May 2015 20:57:36 +0200 Subject: [PATCH] more tests for tax_agg --- R/tax_agg.R | 12 ++++++------ tests/testthat/test-tax_agg.R | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/R/tax_agg.R b/R/tax_agg.R index 9b16092f..44bf2d30 100644 --- a/R/tax_agg.R +++ b/R/tax_agg.R @@ -64,10 +64,10 @@ 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) @@ -75,7 +75,7 @@ tax_agg <- function(x, rank, db = 'ncbi', verbose=FALSE, ...) # 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. @@ -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 diff --git a/tests/testthat/test-tax_agg.R b/tests/testthat/test-tax_agg.R index 4869058a..8c3e78a5 100644 --- a/tests/testthat/test-tax_agg.R +++ b/tests/testthat/test-tax_agg.R @@ -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", { @@ -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)) })