Skip to content

Commit

Permalink
fix bracken import
Browse files Browse the repository at this point in the history
  • Loading branch information
cdiener committed Nov 1, 2023
1 parent 3b7ae9f commit f7f22ef
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions R/convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ hitdb_cleaner <- function(i, df, match, cutoff = 101) {

x <- sub("\\|.+;", ";", x)
if (all(!is.na(match[i, ]))) {
if (as.numeric(match[i, 4]) > cutoff)
if (as.numeric(match[i, 4]) > cutoff) {
x <- sub(match[i, 2], match[i, 3], x)
else x <- sub(match[i, 2], "unclassified", x)
} else {
x <- sub(match[i, 2], "unclassified", x)
}
}

# dada2 nows about empty fields so we do not need placeholders
Expand All @@ -28,15 +30,17 @@ hitdb_cleaner <- function(i, df, match, cutoff = 101) {
#' @param out Filename for the compressed output file.
#' @return Nothing.
#' @examples
#' NULL
#' NULL
#'
#' @export
mothur_to_dada <- function(seq_file, taxa_file, out = "taxonomy.fa.gz") {
taxa_df <- read.table(taxa_file, header = FALSE)
matches <- str_match(taxa_df[, 2], ANN_RE)

tax <- vapply(1:nrow(taxa_df), hitdb_cleaner, "", df = taxa_df,
match = matches)
tax <- vapply(1:nrow(taxa_df), hitdb_cleaner, "",
df = taxa_df,
match = matches
)
names(tax) <- taxa_df[, 1]

seqs <- readFasta(seq_file)
Expand All @@ -53,22 +57,22 @@ mothur_to_dada <- function(seq_file, taxa_file, out = "taxonomy.fa.gz") {
#' in the BRACKEN counts.
#' @return A phyloseq object for the data.
#' @examples
#' NULL
#' NULL
#'
#' @export
#' @importFrom data.table setkeyv
bracken_to_phyloseq <- function(
bracken,
metadata = NULL,
id_col = "sample_id") {

bracken,
metadata = NULL,
id_col = "sample_id") {
n_ranks <- which(names(bracken) == "reads") - 1
lowest_rank <- names(bracken)[n_ranks]
table <- dcast(
bracken,
reformulate(lowest_rank, response = "sample"),
value.var = "reads",
fill = 0
fill = 0,
fun.aggregate = sum
)
samps <- table[, sample]
table <- as.matrix(table[, !"sample"])
Expand Down Expand Up @@ -108,15 +112,14 @@ bracken_to_phyloseq <- function(
#' in the read counts.
#' @return A phyloseq object for the data.
#' @examples
#' NULL
#' NULL
#'
#' @export
reads_to_phyloseq <- function(
counts,
feature_col,
metadata = NULL,
id_col = "sample_id") {

table <- dcast(
counts,
reformulate(feature_col, response = "sample_id"),
Expand Down Expand Up @@ -146,5 +149,4 @@ reads_to_phyloseq <- function(
sample_data(metadata)
)
return(ps)

}
}

0 comments on commit f7f22ef

Please sign in to comment.