Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve glmmseq #306

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: tidybulk
Title: Brings transcriptomics to the tidyverse
Version: 1.15.3
Version: 1.15.4
Authors@R: c(person("Stefano", "Mangiola", email = "[email protected]",
role = c("aut", "cre")),
person("Maria", "Doyle", email = "[email protected]",
Expand Down
5 changes: 5 additions & 0 deletions R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,16 @@ get_differential_transcript_abundance_glmmSeq <- function(.data,
# Make sure the order matches the counts
dispersion = dispersion[rownames(counts)]

# Scaling
sizeFactors <- counts |> edgeR::calcNormFactors(method = scaling_method)


glmmSeq_object =
glmmSeq( .formula,
countdata = counts ,
metadata = metadata,
dispersion = dispersion,
sizeFactors = sizeFactors,
progress = TRUE,
method = method |> str_remove("(?i)^glmmSeq_"),
...
Expand Down
21 changes: 13 additions & 8 deletions R/functions_SE.R
Original file line number Diff line number Diff line change
Expand Up @@ -1100,13 +1100,13 @@ get_differential_transcript_abundance_glmmSeq_SE <- function(.data,
omit_contrast_in_colnames = FALSE
}

# Check if package is installed, otherwise install
if (find.package("edgeR", quiet = TRUE) %>% length %>% equals(0)) {
message("tidybulk says: Installing edgeR needed for differential transcript abundance analyses")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::install("edgeR", ask = FALSE)
}
# # Check if package is installed, otherwise install
# if (find.package("edgeR", quiet = TRUE) %>% length %>% equals(0)) {
# message("tidybulk says: Installing edgeR needed for differential transcript abundance analyses")
# if (!requireNamespace("BiocManager", quietly = TRUE))
# install.packages("BiocManager", repos = "https://cloud.r-project.org")
# BiocManager::install("edgeR", ask = FALSE)
# }

# Check if package is installed, otherwise install
if (find.package("glmmSeq", quiet = TRUE) %>% length %>% equals(0)) {
Expand Down Expand Up @@ -1136,7 +1136,7 @@ get_differential_transcript_abundance_glmmSeq_SE <- function(.data,
# Create design matrix for dispersion, removing random effects
design =
model.matrix(
object = .formula |> eliminate_random_effects(),
object = .formula |> lme4::nobars(),
data = metadata
)

Expand All @@ -1154,11 +1154,16 @@ get_differential_transcript_abundance_glmmSeq_SE <- function(.data,
# Make sure the order matches the counts
dispersion = dispersion[rownames(counts)]

# Scaling
sizeFactors <- counts |> edgeR::calcNormFactors(method = scaling_method)


glmmSeq_object =
glmmSeq( .formula,
countdata = counts ,
metadata = metadata |> as.data.frame(),
dispersion = dispersion,
sizeFactors = sizeFactors,
progress = TRUE,
method = method |> str_remove("(?i)^glmmSeq_" ),
...
Expand Down
29 changes: 0 additions & 29 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -1524,32 +1524,3 @@ feature__ = get_special_column_name_symbol(".feature")
sample__ = get_special_column_name_symbol(".sample")


#' Produced using ChatGPT - Eliminate Random Effects from a Formula
#'
#' This function takes a mixed-effects model formula and returns a modified
#' formula with all random effects removed, leaving only the fixed effects.
#'
#' @param formula An object of class \code{formula}, representing a mixed-effects model formula.
#' @return A formula object with random effects parts removed.
#' @examples
#' eliminate_random_effects(~ age_days * sex + (1 | file_id) + ethnicity_simplified + assay_simplified + .aggregated_cells + (1 + age_days * sex | tissue))
#' @noRd
#' @importFrom stats as.formula
eliminate_random_effects <- function(formula) {
# Convert the formula to a string
formula_str <- deparse(formula)

# Split the string by '+' while keeping the brackets content together
split_str <- unlist(strsplit(formula_str, "(?<=\\))\\s*\\+\\s*|\\+\\s*(?=\\()", perl = TRUE))

# Filter out the random effects parts
fixed_effects <- grep("\\|", split_str, value = TRUE, invert = TRUE)

# Combine the fixed effects parts back into a single string
modified_str <- paste(fixed_effects, collapse = " + ")

# Convert the modified string back to a formula
modified_formula <- as.formula(modified_str)

return(modified_formula)
}
Loading