Skip to content

Commit

Permalink
Merge pull request #3 from danforthcenter/da_edits
Browse files Browse the repository at this point in the history
update for combined field differential abundance stuff
  • Loading branch information
joshqsumner authored Sep 5, 2024
2 parents 68ef4b8 + 6bc493c commit eb59c10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions R/da.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' @importFrom pscl zeroinfl
#' @importFrom MASS glm.nb
#'
#' @return A data frame of model results.
#' @return A data frame of model results and Log2 fold changes.
#'
#' @examples
#'
Expand All @@ -22,14 +22,16 @@ da <- function(df, col, predictors, zi_cutoff = 0.1) {
if (any(missing(df), missing(col), missing(predictors))) {
stop("df, col, and predictors must be specified")
}
log2_values <- log2(df[[col]] + 0.0001)
vals <- split(log2_values, df[, c(predictors)])
df[[col]] <- round(df[[col]]) # in case of calibrated pseudo counts
if (mean(df[[col]] == 0) > zi_cutoff) {
mod_type <- "zinb"
} else {
mod_type <- "nb"
}

if (mod_type == "zinb") {
m <- pscl::zeroinfl(as.formula(paste0(col, "~", paste(predictors, collapse = "+"))),
m <- pscl::zeroinfl(as.formula(paste0(col, "~", paste(predictors, collapse = "*"))),
data = df,
dist = "negbin", link = "logit"
)
Expand All @@ -42,15 +44,24 @@ da <- function(df, col, predictors, zi_cutoff = 0.1) {
m_df <- rbind(dc, dz)
m_df$model <- "zinb"
rownames(m_df) <- NULL
m_df
log2FC <- unlist(lapply(seq_along(vals), function(i) {
mean(vals[[i]]) - mean(vals[[1]])
}))
log2_mu <- unlist(lapply(vals, mean))
m_df$log2_mu <- c(log2_mu, NA, log2_mu)
m_df$log2FC <- c(log2FC, NA, log2FC)
} else {
m2 <- MASS::glm.nb(as.formula(paste0(col, "~", paste(predictors, collapse = "+"))), data = df)
m2 <- MASS::glm.nb(as.formula(paste0(col, "~", paste(predictors, collapse = "*"))), data = df)
m_df <- as.data.frame(summary(m2)$coefficients)
m_df$comp <- "count"
m_df$model <- "zinb"
m_df$model <- "nb"
m_df$coef <- rownames(m_df)
rownames(m_df) <- NULL
m_df
m_df$log2_mu <- unlist(lapply(vals, mean))
m_df$log2FC <- unlist(lapply(seq_along(vals), function(i) {
mean(vals[[i]]) - mean(vals[[1]])
}))
}
m_df$col <- col
return(m_df)
}
2 changes: 1 addition & 1 deletion man/da.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eb59c10

Please sign in to comment.