Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
TuomasBorman committed Jan 17, 2025
1 parent 8a94ef8 commit 5f155d3
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions tests/testthat/test-4IO.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("Importing biom files yield SummarizedExperiment objects", {
expect_s4_class(me2, "SummarizedExperiment")
expect_equal(dim(me), dim(me2))
expect_equal(rowData(me), rowData(me2))

biom_object <- biomformat::read_biom(
system.file("extdata", "Aggregated_humanization2.biom",
package="mia")
Expand All @@ -29,10 +29,10 @@ test_that("Importing biom files yield SummarizedExperiment objects", {
expect_false(
sapply(tolower(colnames(rowData(tse))),
function(x) x %in% TAXONOMY_RANKS) %>% all())
# Testing the artifact.rm, since the original artifact in the biom file
# Testing the artifact.rm, since the original artifact in the biom file
# is '\"'
expect_false(apply(rowData(tse), 2, grepl, pattern="^\"") %>% all())

# Testing prefixes removed
tse <- convertFromBIOM(biom_object,
prefix.rm=TRUE,
Expand All @@ -42,7 +42,7 @@ test_that("Importing biom files yield SummarizedExperiment objects", {
expect_false(rowData(tse) %>%
apply(2,grepl,pattern="sk__|([dkpcofgs]+)__") %>%
all())

# Testing parsing taxonomy ranks from prefixes
tse <- convertFromBIOM(biom_object,
prefix.rm=FALSE,
Expand All @@ -52,8 +52,8 @@ test_that("Importing biom files yield SummarizedExperiment objects", {
expect_true(
sapply(tolower(colnames(rowData(tse))),
function(x) x %in% TAXONOMY_RANKS) %>% all())
# Testing the artifact.rm, the original artifact in the biom file

# Testing the artifact.rm, the original artifact in the biom file
# is '\"', as a test we rather try remove a non existing pattern.
tse <- convertFromBIOM(biom_object,
prefix.rm=FALSE,
Expand All @@ -62,23 +62,23 @@ test_that("Importing biom files yield SummarizedExperiment objects", {
pattern = "\\*|\\?")
# with wrong pattern artifact not cleaned
expect_true(apply(rowData(tse), 2, grepl, pattern="\"") %>% any())
# Testing the artifact.rm, with the value 'auto' to automatically
# Testing the artifact.rm, with the value 'auto' to automatically
# detect the artifact and remove it (in our case the artifact is '\"').
tse <- convertFromBIOM(biom_object,
prefix.rm=FALSE,
rank.from.prefix=FALSE,
artifact.rm = TRUE)
# Checking if 'auto' has detected and cleaned the artifact
expect_false(apply(rowData(tse), 2, grepl, pattern="\"") %>% any())
# Testing the artifact.rm, with the value NULL to not detect or clean
# Testing the artifact.rm, with the value NULL to not detect or clean
# anything.
tse <- convertFromBIOM(biom_object,
prefix.rm=FALSE,
rank.from.prefix=FALSE,
artifact.rm = FALSE)
# Checking if the '\"' artifact still exists.
expect_true(apply(rowData(tse), 2, grepl, pattern="\"") %>% any())

# General final test
tse <- convertFromBIOM(biom_object,
prefix.rm=TRUE,
Expand All @@ -94,7 +94,7 @@ test_that("Importing biom files yield SummarizedExperiment objects", {
expect_true(
sapply(tolower(colnames(rowData(tse))),
function(x) x %in% TAXONOMY_RANKS) %>% all())

# General final test with another biom file
biom_object <- biomformat::read_biom(
system.file("extdata", "rich_dense_otu_table.biom",
Expand All @@ -112,7 +112,7 @@ test_that("Importing biom files yield SummarizedExperiment objects", {
expect_true(
sapply(tolower(colnames(rowData(tse))),
function(x) x %in% TAXONOMY_RANKS) %>% all())

# Check that convertToBIOM works
# Get errors if input is incorrect
expect_error( convertToBIOM() )
Expand Down Expand Up @@ -151,7 +151,7 @@ test_that("Importing biom files yield SummarizedExperiment objects", {
# colData has one empty column (only NA values)
test <- as.data.frame( biomformat::sample_metadata(biom) )
expect_true( all(is.na(test)) && colnames(test) == "V1" )

})

test_that("Importing phyloseq objects yield TreeSummarizedExperiment objects", {
Expand Down Expand Up @@ -186,7 +186,7 @@ test_that("Importing dada2 objects yield TreeSummarizedExperiment objects", {
})

test_that("Importing Mothur files yield SummarizedExperiment objects", {

counts <- system.file("extdata", "mothur_example.shared", package = "mia")
taxa <- system.file("extdata", "mothur_example.cons.taxonomy", package = "mia")
taxa2 <- system.file("extdata", "mothur_example.taxonomy", package = "mia")
Expand All @@ -207,25 +207,25 @@ test_that("Importing Mothur files yield SummarizedExperiment objects", {
se2 <- importMothur(assay.file = counts, row.file = taxa2, col.file = meta)
expect_s4_class(se, "SummarizedExperiment")
expect_s4_class(se2, "SummarizedExperiment")

# Checks dimensions, rownames, and colnames of assay
expect_equal(nrow(assays(se)$counts), 100)
expect_equal(rownames(assays(se)$counts)[1:10],
c("Otu001", "Otu002", "Otu003", "Otu004", "Otu005",
"Otu006", "Otu007", "Otu008", "Otu009", "Otu010"))
expect_equal(ncol(assays(se)$counts), 100)
expect_equal(colnames(assays(se)$counts)[1:10],
c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5",
c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5",
"Sample6", "Sample7", "Sample8", "Sample9", "Sample10"))
expect_equal(nrow(assays(se2)$counts), 100)
expect_equal(rownames(assays(se2)$counts)[1:10],
c("Otu001", "Otu002", "Otu003", "Otu004", "Otu005",
"Otu006", "Otu007", "Otu008", "Otu009", "Otu010"))
expect_equal(ncol(assays(se2)$counts), 100)
expect_equal(colnames(assays(se)$counts)[1:10],
c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5",
c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5",
"Sample6", "Sample7", "Sample8", "Sample9", "Sample10"))

# Checks that rowData has right dimensions, rownames, and colnames
expect_equal(nrow(rowData(se)), 100)
expect_equal(rownames(rowData(se))[1:10],
Expand All @@ -239,18 +239,18 @@ test_that("Importing Mothur files yield SummarizedExperiment objects", {
"Otu006", "Otu007", "Otu008", "Otu009", "Otu010"))
expect_equal(colnames(rowData(se2)),
c("OTU", "Kingdom", "Phylum", "Order", "Class", "Family", "Genus"))

expect_equal(nrow(colData(se)), 100)
expect_equal(rownames(colData(se))[1:10],
c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5",
c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5",
"Sample6", "Sample7", "Sample8", "Sample9", "Sample10"))

# Checks colData's dimensions and names of columns and rows
expect_equal(colnames(colData(se)),
c("group", "sex", "age", "drug", "label", "numOtus", "Group"))
expect_equal(nrow(colData(se2)), 100)
expect_equal(rownames(colData(se2))[1:10],
c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5",
c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5",
"Sample6", "Sample7", "Sample8", "Sample9", "Sample10"))
expect_equal(colnames(colData(se2)),
c("group", "sex", "age", "drug", "label", "numOtus", "Group"))
Expand Down Expand Up @@ -416,27 +416,27 @@ test_that("Confidence of taxa is numberic", {
test_that("dimnames of feature table is identicle with meta data", {
skip_if_not(require("biomformat", quietly = TRUE))
feature_tab <- importQZA(assay.file)

sample_meta <- .read_q2sample_meta(col.file)
taxa_meta <- importQZA(row.file)
data_list <- .set_feature_tab_dimnames(
feature_tab,
sample_meta,
feature_tab,
sample_meta,
taxa_meta
)
expect_identical(colnames(data_list[[1]]), colnames(feature_tab))
expect_identical(rownames(data_list[[1]]), rownames(feature_tab))
expect_identical(rownames(data_list[[1]]), rownames(data_list[[2]]))
expect_identical(colnames(data_list[[1]]), rownames(data_list[[3]]))
expect_identical(colnames(data_list[[1]]), colnames(data_list[[1]]))

# sample_meta or feature meta is NULL
sample_meta2 <- S4Vectors::make_zero_col_DFrame(ncol(feature_tab))
rownames(sample_meta2) <- colnames(feature_tab)
taxa_meta2 <- S4Vectors::make_zero_col_DFrame(nrow(feature_tab))
rownames(taxa_meta2) <- rownames(feature_tab)
expect_silent(.set_feature_tab_dimnames(feature_tab, sample_meta2, taxa_meta))

# sample meta or feature meta without any information, only contains sample/feature
# ID in its rownames
feature_tab3 <- S4Vectors::DataFrame(
Expand All @@ -448,8 +448,8 @@ test_that("dimnames of feature table is identicle with meta data", {
sample_meta3 <- S4Vectors::DataFrame(row.names = paste0("sample", 3:1))
feature_meta3 <- S4Vectors::DataFrame(row.names = paste0("feature", c(2, 3, 1)))
data_list <- .set_feature_tab_dimnames(
feature_tab3,
sample_meta3,
feature_tab3,
sample_meta3,
feature_meta3
)
expect_identical(rownames(data_list[[1]]), rownames(feature_tab3))
Expand Down Expand Up @@ -485,32 +485,32 @@ test_that("convertToPhyloseq", {
# Test that referenceSeq is in refseq. Expect error, because there should not be
# reference sequences.
expect_error(phyloseq::refseq(phy))

# Test with agglomeration that that pruning is done internally
test1 <- agglomerateByRank(tse, rank = "Phylum", update.tree = FALSE)
test2 <- agglomerateByRank(tse, rank = "Phylum", update.tree = TRUE)
test1_phy <- expect_warning(convertToPhyloseq(test1))
test2_phy <- convertToPhyloseq(test2) |> expect_warning()
expect_equal(length(phyloseq::phy_tree(test1_phy)$node),
test2_phy <- convertToPhyloseq(test2)

expect_equal(length(phyloseq::phy_tree(test1_phy)$node),
length(ape::keep.tip(rowTree(test1), rowLinks(test1)$nodeLab)$node))
expect_equal(phyloseq::phy_tree(test1_phy)$tip.label, rownames(test2))
# The tip labels do not match because of renaming
expect_identical(phyloseq::phy_tree(test2_phy)$edge, rowTree(test2)$edge)

# Check that everything works also with agglomerated data
for (level in colnames(rowData(tse)) ){
temp <- agglomerateByRank(tse, rank = level)
expect_no_warning(convertToPhyloseq(temp))
}

tse2 <- tse
# Concerts data frame to factors
rowData(tse2) <- DataFrame(lapply(rowData(tse2), as.factor))
phy <- convertToPhyloseq(tse)
phy2 <- convertToPhyloseq(tse2)
expect_equal(phyloseq::tax_table(phy2), phyloseq::tax_table(phy))

# TSE object
data(esophagus, package="mia")
tse <- esophagus
Expand All @@ -522,15 +522,15 @@ test_that("convertToPhyloseq", {

# Test that rowTree is in phy_tree
expect_identical(phyloseq::phy_tree(phy), rowTree(tse))

# Test that merging objects lead to correct phyloseq
tse <- mergeSEs(GlobalPatterns, esophagus, assay.type="counts", missing.values = 0)
pseq <- convertToPhyloseq(tse, assay.type="counts")

# Include rownames from both trees
tse_compare <- tse[ c(rownames(GlobalPatterns), rownames(esophagus)), ]
pseq_compare <- convertToPhyloseq(tse_compare, assay.type="counts")

expect_equal(phyloseq::otu_table(pseq), phyloseq::otu_table(pseq_compare))
})

Expand Down

0 comments on commit 5f155d3

Please sign in to comment.