From 11b5e7a68a85768c53dd1b68e1e22956b4cd70ad Mon Sep 17 00:00:00 2001 From: Gene233 Date: Tue, 12 Nov 2024 17:35:01 +1100 Subject: [PATCH] update top_markers function, allow batch correction for glm estimation. --- DESCRIPTION | 2 +- R/top_markers.R | 15 +++++++++++++-- man/smartid_Package.Rd | 9 +++++++++ man/top_markers_glm.Rd | 3 +++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 87af15e..fbeabc5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,7 @@ biocViews: Software, GeneExpression, Transcriptomics License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Collate: 'AllClasses.R' 'tf_idf_iae_wrappers.R' diff --git a/R/top_markers.R b/R/top_markers.R index 0a506ca..cc4cb1f 100644 --- a/R/top_markers.R +++ b/R/top_markers.R @@ -106,6 +106,7 @@ top_markers_abs <- function(data, label, n = 10, #' @param data matrix, features in row and samples in column #' @param n integer, number of returned top genes for each group #' @param family family for glm, details in [stats::glm()] +#' @param batch a vector of batch labels, default NULL #' @param scale logical, if to scale data by row #' @param use.mgm logical, if to scale data using [scale_mgm()] #' @param softmax logical, if to apply softmax transformation on output @@ -119,6 +120,7 @@ top_markers_abs <- function(data, label, n = 10, #' top_markers_glm(data, label = rep(c("A", "B"), 5)) top_markers_glm <- function(data, label, n = 10, family = gaussian(), # score are continuous non-negative, can use gamma or inverse.gaussian, if continuous and unbounded use gaussian, if discrete use poisson, if binary or proportions between [0,1] or binary freq counts use binomial + batch = NULL, scale = TRUE, use.mgm = TRUE, pooled.sd = FALSE, # log = TRUE, @@ -144,8 +146,17 @@ top_markers_glm <- function(data, label, n = 10, # data <- log(data + 1e-8) # } - ## estimate betas based on given group - betas <- apply(data, 1, \(s) glm(s ~ 0 + label, family = family)$coef) + ## estimate betas based on given group and/or batch label + if(is.null(batch)) { + ## model with group label only + betas <- apply(data, 1, \(s) glm(s ~ 0 + label, family = family)$coef) + } else { + ## model with both group and batch label + betas <- apply(data, 1, \(s) glm(s ~ 0 + label + batch, family = family)$coef) + ## only extract betas of group label + betas <- betas[grep("^label", rownames(betas)), , drop = FALSE] + } + rownames(betas) <- gsub("label", "", rownames(betas)) # ## compute logFC (1 vs all mean) for each group diff --git a/man/smartid_Package.Rd b/man/smartid_Package.Rd index 402a787..07bbd1c 100644 --- a/man/smartid_Package.Rd +++ b/man/smartid_Package.Rd @@ -2,6 +2,7 @@ % Please edit documentation in R/smartid-package.R \docType{package} \name{smartid_Package} +\alias{smartid-package} \alias{smartid_Package} \alias{smartid} \title{Scoring and Marker Selection method based on modified TF-IDF} @@ -14,6 +15,14 @@ especially for rare population. The package is developed for generating specific lists of signature genes based on TF-IDF modified methods. It can also be used as a new gene-set scoring method or data transformation method. Multiple visualization functions are implemented in this package. +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://davislaboratory.github.io/smartid} + \item Report bugs at \url{https://github.com/DavisLaboratory/smartid/issues} +} + } \author{ Jinjin Chen \email{chen.j@wehi.edu.au} diff --git a/man/top_markers_glm.Rd b/man/top_markers_glm.Rd index 43fc9d9..fe9dde8 100644 --- a/man/top_markers_glm.Rd +++ b/man/top_markers_glm.Rd @@ -9,6 +9,7 @@ top_markers_glm( label, n = 10, family = gaussian(), + batch = NULL, scale = TRUE, use.mgm = TRUE, pooled.sd = FALSE, @@ -25,6 +26,8 @@ top_markers_glm( \item{family}{family for glm, details in \code{\link[stats:glm]{stats::glm()}}} +\item{batch}{a vector of batch labels, default NULL} + \item{scale}{logical, if to scale data by row} \item{use.mgm}{logical, if to scale data using \code{\link[=scale_mgm]{scale_mgm()}}}