Skip to content

Commit

Permalink
fix #166
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperSkytte committed May 17, 2024
1 parent a03d778 commit 3dde4d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: ampvis2
Type: Package
Title: Tools for visualising amplicon data
Description: ampvis2 is a small set of tools that allows effortless visualisation of amplicon data.
Version: 2.8.8
Version: 2.8.9
Authors@R: c(
person(
c("Kasper", "Skytte"), "Andersen",
Expand Down
29 changes: 22 additions & 7 deletions R/amp_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@ parseTaxonomy <- function(x) {
#'
#' @return A data frame
#' @keywords internal
findOTUcol <- function(x) {
findOTUcol <- function(
x,
OTUcolname = c("OTU", "ASV", "#OTU ID")
) {
DF <- as.data.frame(x)
otucol <- tolower(colnames(DF)) %in% c("otu", "asv", "#otu id")
otucol <- tolower(colnames(DF)) %in% tolower(OTUcolname)
if (sum(otucol) > 1L) {
stop(
paste(
"More than one column in",
deparse(substitute(x)),
"is named OTU/ASV, don't know which one to use."
"contains OTUs/ASVs. I don't know which one to use."
),
call. = FALSE
)
Expand All @@ -85,9 +88,16 @@ findOTUcol <- function(x) {
} else if (!any(otucol)) {
warning(
paste0(
"Could not find a column named OTU/ASV in ",
"Could not find a column named ",
if(length(OTUcolname) > 1L) "one of \"",
paste(OTUcolname, collapse = ", "),
"\" in ",
deparse(substitute(x)),
", using rownames as OTU ID's"
". Using row names as OTU ID's",
if(identical(rownames(DF), as.character(1:nrow(DF))))
", however they seem to be just numbers and are likely made by R"
else
"."
),
call. = FALSE
)
Expand Down Expand Up @@ -376,6 +386,8 @@ amp_load <- function(otutable,
tree = NULL,
pruneSingletons = FALSE,
removeAbsentOTUs = TRUE,
otutable_OTUcolname = c("OTU", "ASV", "#OTU ID"),
taxonomy_OTUcolname = c("OTU", "ASV", "#OTU ID"),
...) {
### Check whether otutable is a phyloseq object
if(inherits(otutable, "phyloseq")) {
Expand Down Expand Up @@ -451,13 +463,16 @@ amp_load <- function(otutable,

### import and check otutable (with or without taxonomy)
otutable <- import(otutable, ...)
otutable <- findOTUcol(otutable)
otutable <- findOTUcol(otutable, OTUcolname = otutable_OTUcolname)

### extract read abundances from otutable (same if taxonomy present or not)
taxcols <- tolower(colnames(otutable)) %in% c("domain", tolower(tax_levels))
abund <- otutable[, !taxcols, drop = FALSE]
abund[is.na(abund)] <- 0L

if(!all(sapply(abund, is.numeric)))
stop("Abundance table contains non-numeric columns", call. = FALSE)

# warn if any empty sample(s)
if (any(colSums(abund) == 0L)) {
warning("One or more sample(s) have 0 reads", call. = FALSE)
Expand All @@ -468,7 +483,7 @@ amp_load <- function(otutable,
tax <- parseTaxonomy(otutable)
} else if (!is.null(taxonomy)) {
taxonomy <- import(taxonomy)
taxonomy <- findOTUcol(taxonomy)
taxonomy <- findOTUcol(taxonomy, OTUcolname = taxonomy_OTUcolname)
tax <- parseTaxonomy(taxonomy)

# check if provided otutable and taxonomy match, message with missing/excess OTUs
Expand Down

0 comments on commit 3dde4d1

Please sign in to comment.