Skip to content

Commit

Permalink
Removes tidyverse dependencies; adopts SeuratObject package for acces…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
joseah committed Jul 11, 2021
1 parent f577e28 commit 86e6230
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 170 deletions.
8 changes: 2 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: scPred
Type: Package
Title: scPred - accurate supervised method for cell-type classification from single-cell RNA-seq data
Version: 1.9.0
Version: 1.9.1
Author: c(person("Jose", "Alquicira-Hernandez", email = "[email protected]", rol = "aut, cre", comment = c(ORCID = "0000-0002-9049-7780")))
Maintainer: Jose Alquicira-Hernandez <[email protected]>
Description: Provides a series of functions to train a prediction model based on gene expression data to classify single cells.
Expand All @@ -11,19 +11,15 @@ Encoding: UTF-8
LazyData: true
Imports:
harmony,
Seurat,
SeuratObject,
methods,
stats,
ggbeeswarm,
MLmetrics,
dplyr,
tidyr,
magrittr,
caret,
kernlab,
pROC,
ggplot2,
tibble,
knitr,
pbapply,
cli,
Expand Down
35 changes: 10 additions & 25 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,21 @@ exportMethods(get_metadata)
exportMethods(get_probabilities)
exportMethods(get_scpred)
exportMethods(show)
importFrom(Seurat,AddMetaData)
importFrom(Seurat,CreateDimReducObject)
importFrom(Seurat,DefaultAssay)
importFrom(Seurat,Embeddings)
importFrom(Seurat,GetAssayData)
importFrom(Seurat,Loadings)
importFrom(Seurat,ScaleData)
importFrom(Seurat,Stdev)
importFrom(Seurat,VariableFeatures)
importFrom(SeuratObject,AddMetaData)
importFrom(SeuratObject,Cells)
importFrom(SeuratObject,CreateDimReducObject)
importFrom(SeuratObject,DefaultAssay)
importFrom(SeuratObject,Embeddings)
importFrom(SeuratObject,GetAssayData)
importFrom(SeuratObject,Loadings)
importFrom(SeuratObject,Reductions)
importFrom(SeuratObject,Stdev)
importFrom(SeuratObject,VariableFeatures)
importFrom(caret,prSummary)
importFrom(caret,train)
importFrom(caret,trainControl)
importFrom(caret,twoClassSummary)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,full_join)
importFrom(dplyr,group_by_)
importFrom(dplyr,if_else)
importFrom(dplyr,mutate)
importFrom(dplyr,mutate_all)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(ggbeeswarm,geom_quasirandom)
importFrom(ggplot2,aes)
importFrom(ggplot2,element_blank)
Expand All @@ -53,14 +44,8 @@ importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(harmony,HarmonyMatrix)
importFrom(knitr,kable)
importFrom(magrittr,"%>%")
importFrom(methods,is)
importFrom(methods,setMethod)
importFrom(methods,show)
importFrom(pbapply,pblapply)
importFrom(stats,predict)
importFrom(tibble,column_to_rownames)
importFrom(tibble,rownames_to_column)
importFrom(tidyr,gather)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,spread)
30 changes: 12 additions & 18 deletions R/getFeatureSpace.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
#' }
#' @keywords informative, significant, features
#' @importFrom methods is
#' @importFrom tidyr gather
#' @importFrom magrittr "%>%"
#' @importFrom dplyr mutate arrange filter distinct
#' @importFrom pbapply pblapply
#' @importFrom SeuratObject DefaultAssay Embeddings Loadings Reductions Cells
#'
#' @export
#' @author
Expand Down Expand Up @@ -49,7 +47,7 @@ getFeatureSpace <- function(object, pvar, correction = "fdr", sig = 1, reduction
stop("Invalid multiple testing correction method. See ?p.adjust function")
}

if(!pvar %in% names(object@meta.data)){
if(!pvar %in% names(object[[]])){
stop("Prediction variable is not stored in metadata slot")
}

Expand Down Expand Up @@ -141,8 +139,8 @@ getFeatureSpace <- function(object, pvar, correction = "fdr", sig = 1, reduction
}else{

res <- pblapply(levels(classes), .getFeatures, classes, cellEmbeddings, correction, sig)
dict <- data.frame(classes, original_classes) %>%
distinct()
dict <- data.frame(classes, original_classes)
dict <- unique(dict)

i <- match(levels(classes), dict$classes)
names(res) <- as.character(dict$original_classes[i])
Expand All @@ -162,10 +160,7 @@ getFeatureSpace <- function(object, pvar, correction = "fdr", sig = 1, reduction

}



# Create scPred object

spmodel@pvar <- pvar
spmodel@features <- res
spmodel@cell_embeddings <- cellEmbeddings
Expand Down Expand Up @@ -193,16 +188,15 @@ getFeatureSpace <- function(object, pvar, correction = "fdr", sig = 1, reduction
# Get indices for positive and negative class cells
positiveCells <- newClasses == positiveClass
negativeCells <- newClasses == "other"


# Get informative features
apply(cellEmbeddings, 2, function(d) stats::wilcox.test(d[positiveCells], d[negativeCells])) %>%
lapply('[[', "p.value") %>% # Extract p-values
as.data.frame() %>%
gather(key = "feature", value = "pValue") %>%
mutate(pValueAdj = stats::p.adjust(pValue, method = correction, n = nrow(.))) %>% # Perform multiple test correction
arrange(pValueAdj) %>%
filter(pValueAdj < sig) -> sigDims

sigDims
wt_res <- apply(cellEmbeddings, 2, function(d) stats::wilcox.test(d[positiveCells], d[negativeCells]))
wt_res <- lapply(wt_res, '[[', "p.value")
wt_res <- data.frame(feature = names(wt_res), pValue = as.numeric(wt_res))
wt_res$pValueAdj <- stats::p.adjust(wt_res$pValue, method = correction, n = nrow(wt_res))
wt_res <- wt_res[order(wt_res$pValueAdj), ]
wt_res <- wt_res[wt_res$pValueAdj < sig, ]
wt_res
}

Loading

0 comments on commit 86e6230

Please sign in to comment.