Skip to content

Commit eaafdec

Browse files
committed
Add parameter sig_gene_threshold
Genes must be detected in at least `sig_gene_threshold` proportion of cells to be considered when calculating signature scores/backgrounds.
1 parent dbe93b5 commit eaafdec

10 files changed

Lines changed: 78 additions & 17 deletions

R/AnalysisFunctions.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,11 @@ computeProjectionGenes <- function(object,
211211
#' on creating Signature objects.
212212
#' @param min_signature_genes Signature that match less than this number of genes in the
213213
#' supplied expression matrix are removed.
214+
#' @param sig_gene_threshold Proportion of cells that a gene must be detected in (nonzero)
215+
#' to be used in signature score calculations.
214216
#' @return the VISION object, with the @sigData slot updated
215217
#' @export
216-
addSignatures <- function(object, signatures, min_signature_genes=5) {
218+
addSignatures <- function(object, signatures, min_signature_genes=5, sig_gene_threshold=.01) {
217219

218220
if (is.list(signatures)) {
219221
sigs <- lapply(signatures, function(sig){
@@ -237,7 +239,7 @@ addSignatures <- function(object, signatures, min_signature_genes=5) {
237239
Signature objects")
238240
}
239241

240-
sigs <- processSignatures(sigs, rownames(object@exprData), min_signature_genes)
242+
sigs <- processSignatures(sigs, object@exprData, min_signature_genes, sig_gene_threshold)
241243

242244
object@sigData <- c(object@sigData, sigs)
243245

R/methods-Signature.R

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,31 @@
44
#' Drops signatures with less than `minSignatureGenes` matching genes
55
#'
66
#' @param sigData list of signature objects
7-
#' @param expressionGenes list of gene identifiers in the expression matrix
7+
#' @param exprData gene expression matrix
88
#' @param minSignatureGenes minimum number of genes a signature must match
99
#' in the expression matrix in order to be retained
10+
#' @param sig_gene_threshold Proportion of cells that a gene must be detected in (nonzero)
11+
#' to be used in signature score calculations.
12+
#' @importFrom Matrix rowSums
1013
#' @return processedSigData list of signature objects
11-
processSignatures <- function(sigData, expressionGenes, minSignatureGenes){
14+
processSignatures <- function(sigData, exprData, minSignatureGenes, sig_gene_threshold){
15+
expressionGenes <- rownames(exprData)
16+
17+
18+
cell_threshold <- sig_gene_threshold * ncol(exprData)
19+
gene_detects <- rowSums(exprData > 0)
20+
valid_genes <- gene_detects >= cell_threshold
21+
22+
message(
23+
sprintf(
24+
"\nUsing %i/%i genes detected in %.2f%% of cells for signature analysis.",
25+
sum(valid_genes), nrow(exprData), sig_gene_threshold*100)
26+
)
27+
message(
28+
"See the `sig_gene_threshold` input to change this behavior.\n"
29+
)
30+
31+
expressionGenes <- rownames(exprData)[valid_genes]
1232
out <- lapply(sigData, function(sig){
1333
validGenes <- names(sig@sigDict) %in% expressionGenes
1434
sig@sigDict <- sig@sigDict[validGenes]

R/methods-Vision.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#' genes to use when computing projections.
2121
#' @param min_signature_genes Signature that match less than this number of genes in the
2222
#' supplied expression matrix are removed.
23+
#' @param sig_gene_threshold Proportion of cells that a gene must be detected in (nonzero)
24+
#' to be used in signature score calculations.
2325
#' @param threshold Threshold to apply when using the 'threshold' or 'fano' projection genes filter.
2426
#' If greater than 1, this specifies the number of cells in which a gene must be detected
2527
#' for it to be used when computing PCA. If less than 1, this instead specifies the proportion of cells needed
@@ -75,7 +77,9 @@ setMethod("Vision", signature(data = "matrixORSparse"),
7577
function(data, signatures=list(),
7678
proteinData=NULL,
7779
unnormalizedData = NULL, meta=NULL,
78-
projection_genes=c("fano"), min_signature_genes=5,
80+
projection_genes=c("fano"),
81+
min_signature_genes=5,
82+
sig_gene_threshold=.005,
7983
threshold=.05, perm_wPCA=FALSE,
8084
projection_methods = c("tSNE30"),
8185
sig_norm_method = c("znorm_columns", "none", "znorm_rows",
@@ -101,7 +105,16 @@ setMethod("Vision", signature(data = "matrixORSparse"),
101105
.Object@params$micropooling <- list()
102106

103107
rownames(data) <- toupper(rownames(data))
104-
data <- data[ !duplicated(rownames(data)), , drop = FALSE]
108+
toRemove <- rownames(data)[duplicated(rownames(data))]
109+
if (length(toRemove) > 0){
110+
message(sprintf(
111+
"\nRemoving %i genes with duplicate IDs (ignoring case): %s\n",
112+
length(toRemove) + length(unique(toRemove)),
113+
paste(unique(toRemove), collapse = ", ")
114+
)
115+
)
116+
data <- data[ !(rownames(data) %in% toRemove), , drop = FALSE]
117+
}
105118
.Object@exprData <- data
106119

107120
if (!is.null(unnormalizedData)){
@@ -198,7 +211,7 @@ setMethod("Vision", signature(data = "matrixORSparse"),
198211
Signature objects")
199212
}
200213

201-
.Object@sigData <- processSignatures(.Object@sigData, rownames(.Object@exprData), min_signature_genes)
214+
.Object@sigData <- processSignatures(.Object@sigData, .Object@exprData, min_signature_genes, sig_gene_threshold)
202215

203216
if (!is.null(meta)) {
204217
if(is.matrix(meta)){

docs/reference/VISION-class.html

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/addSignatures.html

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/processSignatures.html

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/VISION-class.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/addSignatures.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/processSignatures.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_signatures.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test_that("Naive Sig Scores computed correctly", {
3737
sep = "\t", header = TRUE)
3838
expr <- data.matrix(expr)
3939
sigList <- readSignaturesInput(c("test_data/published_signatures/h.all.v5.2.symbols.gmt"))
40-
sigList <- processSignatures(sigList, rownames(expr), 1)
40+
sigList <- processSignatures(sigList, expr, 1, .000001)
4141

4242
normData <- getNormalizedCopySparse(expr, "none")
4343

0 commit comments

Comments
 (0)