... | ... |
@@ -63,7 +63,7 @@ seWrap <- function(sce, min.cells=10, min.features=0){ |
63 | 63 |
min.features=min.features, |
64 | 64 |
meta.data=as.data.frame(colData(sce)), |
65 | 65 |
project = "scRNAseq" ) |
66 |
- Misc(se)$rowData <- as.data.frame(rowData(sce)) |
|
66 |
+ Misc(se, "rowData") <- as.data.frame(rowData(sce)) |
|
67 | 67 |
if("logcounts" %in% assayNames(sce)){ |
68 | 68 |
se <- ScaleData(se, verbose = FALSE) |
69 | 69 |
sce <- sce[row.names(se),] |
... | ... |
@@ -92,7 +92,7 @@ sceWrap <- function(seu) { |
92 | 92 |
sce <- sce[row.names(norm),] |
93 | 93 |
logcounts(sce) <- norm |
94 | 94 |
} |
95 |
- rowData(sce) <- Misc(seu)$rowData[row.names(sce),] |
|
95 |
+ rowData(sce) <- Misc(seu, "rowData")[row.names(sce),] |
|
96 | 96 |
if(length(VariableFeatures(seu))) |
97 | 97 |
metadata(sce)$VariableFeats <- VariableFeatures(seu) |
98 | 98 |
if(length(Reductions(seu))>0){ |
... | ... |
@@ -37,7 +37,7 @@ getDimensionality <- function(dat, method, maxDims=NULL){ |
37 | 37 |
pcaLocal.fan=pcaLocalDimEst(x, ver="fan"), |
38 | 38 |
pcaLocal.maxgap=pcaLocalDimEst(x, ver="maxgap"), |
39 | 39 |
maxLikGlobal=maxLikGlobalDimEst(x, k=20, unbiased=TRUE), |
40 |
- maxLikGlobal10=maxLikGlobalDimEst(x, k=10, unbiased=TRUE), |
|
40 |
+ maxLikGlobal10=maxLikGlobalDimEst(x, k=10, unbiased=TRUE), |
|
41 | 41 |
pcaOtpmPointwise.max=pcaOtpmPointwiseDimEst(x,N=10), |
42 | 42 |
elbow=farthestPoint(sdv)-1, |
43 | 43 |
ifelse( !is.function(method) && |
... | ... |
@@ -15,7 +15,7 @@ getDimensionality <- function(dat, method, maxDims=NULL){ |
15 | 15 |
if(is.numeric(method) || grepl("^[0-9]+$", method)) |
16 | 16 |
return(as.integer(round(as.numeric(method)))) |
17 | 17 |
if(is(dat, "Seurat")){ |
18 |
- x <- dat[["pca"]]@cell.embeddings |
|
18 |
+ x <- Embeddings(dat[["pca"]]) |
|
19 | 19 |
sdv <- Stdev(dat, "pca") |
20 | 20 |
} else { |
21 | 21 |
if(method=="jackstraw.elbow") |
... | ... |
@@ -62,10 +62,10 @@ seWrap <- function(sce, min.cells=10, min.features=0){ |
62 | 62 |
min.features=min.features, |
63 | 63 |
meta.data=as.data.frame(colData(sce)), |
64 | 64 |
project = "scRNAseq" ) |
65 |
- se@misc$rowData <- as.data.frame(rowData(sce)) |
|
65 |
+ Misc(se)$rowData <- as.data.frame(rowData(sce)) |
|
66 | 66 |
if("logcounts" %in% assayNames(sce)){ |
67 | 67 |
se <- ScaleData(se, verbose = FALSE) |
68 |
- se@assays$RNA@data <- logcounts(sce) |
|
68 |
+ se <- SetAssayData(se, slot="data", new.data=logcounts(sce)) |
|
69 | 69 |
} |
70 | 70 |
if(!is.null(metadata(sce)$VariableFeats)) |
71 | 71 |
VariableFeatures(se) <- metadata(sce)$VariableFeats |
... | ... |
@@ -89,12 +89,11 @@ sceWrap <- function(seu) { |
89 | 89 |
sce <- sce[row.names(norm),] |
90 | 90 |
logcounts(sce) <- norm |
91 | 91 |
} |
92 |
- rowData(sce) <- seu@misc$rowData[row.names(sce),] |
|
92 |
+ rowData(sce) <- Misc(seu)$rowData[row.names(sce),] |
|
93 | 93 |
if(length(VariableFeatures(seu))) |
94 | 94 |
metadata(sce)$VariableFeats <- VariableFeatures(seu) |
95 | 95 |
if(length(Reductions(seu))>0){ |
96 |
- reducedDims(sce) <- lapply( seu@reductions, |
|
97 |
- FUN=function(x) [email protected] ) |
|
96 |
+ reducedDims(sce) <- lapply( Reductions(seu), FUN=Embeddings ) |
|
98 | 97 |
} |
99 | 98 |
sce |
100 | 99 |
} |
... | ... |
@@ -37,6 +37,7 @@ getDimensionality <- function(dat, method, maxDims=NULL){ |
37 | 37 |
pcaLocal.fan=pcaLocalDimEst(x, ver="fan"), |
38 | 38 |
pcaLocal.maxgap=pcaLocalDimEst(x, ver="maxgap"), |
39 | 39 |
maxLikGlobal=maxLikGlobalDimEst(x, k=20, unbiased=TRUE), |
40 |
+ maxLikGlobal10=maxLikGlobalDimEst(x, k=10, unbiased=TRUE), |
|
40 | 41 |
pcaOtpmPointwise.max=pcaOtpmPointwiseDimEst(x,N=10), |
41 | 42 |
elbow=farthestPoint(sdv)-1, |
42 | 43 |
ifelse( !is.function(method) && |
... | ... |
@@ -65,7 +66,9 @@ seWrap <- function(sce, min.cells=10, min.features=0){ |
65 | 66 |
se@misc$rowData <- as.data.frame(rowData(sce)) |
66 | 67 |
if("logcounts" %in% assayNames(sce)){ |
67 | 68 |
se <- ScaleData(se, verbose = FALSE) |
68 |
- se@assays$RNA@data <- logcounts(sce) |
|
69 |
+ sce <- sce[row.names(se),] |
|
70 |
+ se <- SetAssayData(se, slot="data", new.data=logcounts(sce)) |
|
71 |
+ se <- SetAssayData(se, slot="scale.data", new.data=logcounts(sce)) |
|
69 | 72 |
} |
70 | 73 |
if(!is.null(metadata(sce)$VariableFeats)) |
71 | 74 |
VariableFeatures(se) <- metadata(sce)$VariableFeats |
... | ... |
@@ -12,6 +12,8 @@ |
12 | 12 |
#' @importFrom SingleCellExperiment reducedDim |
13 | 13 |
#' @import intrinsicDimension |
14 | 14 |
getDimensionality <- function(dat, method, maxDims=NULL){ |
15 |
+ if(is.numeric(method) || grepl("^[0-9]+$", method)) |
|
16 |
+ return(as.integer(round(as.numeric(method)))) |
|
15 | 17 |
if(is(dat, "Seurat")){ |
16 | 18 |
x <- dat[["pca"]]@cell.embeddings |
17 | 19 |
sdv <- Stdev(dat, "pca") |
... | ... |
@@ -55,7 +55,6 @@ getDimensionality <- function(dat, method, maxDims=NULL){ |
55 | 55 |
seWrap <- function(sce, min.cells=10, min.features=0){ |
56 | 56 |
if(is(sce,"Seurat")) return(sce) |
57 | 57 |
if(!is(sce,"SingleCellExperiment")) stop("not a SingleCellExperiment!") |
58 |
- suppressPackageStartupMessages(library(Seurat)) |
|
59 | 58 |
se <- CreateSeuratObject( counts=counts(sce), |
60 | 59 |
min.cells=min.cells, |
61 | 60 |
min.features=min.features, |
... | ... |
@@ -54,6 +54,7 @@ getDimensionality <- function(dat, method, maxDims=NULL){ |
54 | 54 |
#' @importFrom SummarizedExperiment assayNames |
55 | 55 |
seWrap <- function(sce, min.cells=10, min.features=0){ |
56 | 56 |
if(is(sce,"Seurat")) return(sce) |
57 |
+ if(!is(sce,"SingleCellExperiment")) stop("not a SingleCellExperiment!") |
|
57 | 58 |
suppressPackageStartupMessages(library(Seurat)) |
58 | 59 |
se <- CreateSeuratObject( counts=counts(sce), |
59 | 60 |
min.cells=min.cells, |
... | ... |
@@ -78,10 +79,8 @@ seWrap <- function(sce, min.cells=10, min.features=0){ |
78 | 79 |
#' @import SingleCellExperiment Seurat |
79 | 80 |
#' @importFrom SummarizedExperiment rowData<- |
80 | 81 |
sceWrap <- function(seu) { |
81 |
- suppressPackageStartupMessages({ |
|
82 |
- library(SingleCellExperiment) |
|
83 |
- library(Seurat) |
|
84 |
- }) |
|
82 |
+ if(is(seu,"SingleCellExperiment")) return(seu) |
|
83 |
+ if(!is(seu,"Seurat")) stop("not a Seurat object!") |
|
85 | 84 |
sce <- SingleCellExperiment( |
86 | 85 |
list(counts=GetAssayData(seu, assay="RNA", slot="counts")), |
87 | 86 |
colData = seu[[]] ) |
... | ... |
@@ -51,6 +51,7 @@ getDimensionality <- function(dat, method, maxDims=NULL){ |
51 | 51 |
# sce2se conversion |
52 | 52 |
# not exported |
53 | 53 |
#' @import SingleCellExperiment Seurat |
54 |
+#' @importFrom SummarizedExperiment assayNames |
|
54 | 55 |
seWrap <- function(sce, min.cells=10, min.features=0){ |
55 | 56 |
if(is(sce,"Seurat")) return(sce) |
56 | 57 |
suppressPackageStartupMessages(library(Seurat)) |
... | ... |
@@ -75,13 +76,15 @@ seWrap <- function(sce, min.cells=10, min.features=0){ |
75 | 76 |
# se2sce conversion |
76 | 77 |
# not exported |
77 | 78 |
#' @import SingleCellExperiment Seurat |
79 |
+#' @importFrom SummarizedExperiment rowData<- |
|
78 | 80 |
sceWrap <- function(seu) { |
79 | 81 |
suppressPackageStartupMessages({ |
80 | 82 |
library(SingleCellExperiment) |
81 | 83 |
library(Seurat) |
82 | 84 |
}) |
83 |
- sce <- SingleCellExperiment(list(counts=GetAssayData(seu, assay="RNA", slot="counts")), |
|
84 |
- colData = seu[[]]) |
|
85 |
+ sce <- SingleCellExperiment( |
|
86 |
+ list(counts=GetAssayData(seu, assay="RNA", slot="counts")), |
|
87 |
+ colData = seu[[]] ) |
|
85 | 88 |
if(nrow(norm <- GetAssayData(seu, slot="scale.data"))>0){ |
86 | 89 |
sce <- sce[row.names(norm),] |
87 | 90 |
logcounts(sce) <- norm |
... | ... |
@@ -90,7 +93,8 @@ sceWrap <- function(seu) { |
90 | 93 |
if(length(VariableFeatures(seu))) |
91 | 94 |
metadata(sce)$VariableFeats <- VariableFeatures(seu) |
92 | 95 |
if(length(Reductions(seu))>0){ |
93 |
- reducedDims(sce) <- lapply(seu@reductions, FUN=function(x) [email protected]) |
|
96 |
+ reducedDims(sce) <- lapply( seu@reductions, |
|
97 |
+ FUN=function(x) [email protected] ) |
|
94 | 98 |
} |
95 | 99 |
sce |
96 | 100 |
} |
... | ... |
@@ -2,8 +2,8 @@ |
2 | 2 |
#' |
3 | 3 |
#' Returns the estimated intrinsic dimensionality of a dataset. |
4 | 4 |
#' |
5 |
-#' @param dat A Seurat or SCE object |
|
6 |
-#' @param method The dimensionality method to use |
|
5 |
+#' @param dat A Seurat or SCE object with a pca embedding. |
|
6 |
+#' @param method The dimensionality method to use. |
|
7 | 7 |
#' @param maxDims Deprecated and ignored. |
8 | 8 |
#' |
9 | 9 |
#' @return An integer. |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,96 @@ |
1 |
+#' getDimensionality |
|
2 |
+#' |
|
3 |
+#' Returns the estimated intrinsic dimensionality of a dataset. |
|
4 |
+#' |
|
5 |
+#' @param dat A Seurat or SCE object |
|
6 |
+#' @param method The dimensionality method to use |
|
7 |
+#' @param maxDims Deprecated and ignored. |
|
8 |
+#' |
|
9 |
+#' @return An integer. |
|
10 |
+#' |
|
11 |
+#' @importFrom Seurat Stdev |
|
12 |
+#' @importFrom SingleCellExperiment reducedDim |
|
13 |
+#' @import intrinsicDimension |
|
14 |
+getDimensionality <- function(dat, method, maxDims=NULL){ |
|
15 |
+ if(is(dat, "Seurat")){ |
|
16 |
+ x <- dat[["pca"]]@cell.embeddings |
|
17 |
+ sdv <- Stdev(dat, "pca") |
|
18 |
+ } else { |
|
19 |
+ if(method=="jackstraw.elbow") |
|
20 |
+ stop("the jackstraw.elbow method is only available for Seurat objects.") |
|
21 |
+ x <- reducedDim(dat, "PCA") |
|
22 |
+ sdv <- attr(reducedDim(dat, "PCA"), "percentVar") |
|
23 |
+ } |
|
24 |
+ # conversion for backward compatibility: |
|
25 |
+ conv <- c( "fisherSeparability"="FisherSeparability", |
|
26 |
+ "scran.denoisePCA"="scran.ndims.wrapper", |
|
27 |
+ "jackstraw.elbow"="js.wrapper" |
|
28 |
+ ) |
|
29 |
+ if(method %in% names(conv)) method <- as.character(conv[method]) |
|
30 |
+ |
|
31 |
+ x <- switch(method, |
|
32 |
+ essLocal.a=essLocalDimEst(x), |
|
33 |
+ essLocal.b=essLocalDimEst(x, ver="b"), |
|
34 |
+ pcaLocal.FO=pcaLocalDimEst(x,ver="FO"), |
|
35 |
+ pcaLocal.fan=pcaLocalDimEst(x, ver="fan"), |
|
36 |
+ pcaLocal.maxgap=pcaLocalDimEst(x, ver="maxgap"), |
|
37 |
+ maxLikGlobal=maxLikGlobalDimEst(x, k=20, unbiased=TRUE), |
|
38 |
+ pcaOtpmPointwise.max=pcaOtpmPointwiseDimEst(x,N=10), |
|
39 |
+ elbow=farthestPoint(sdv)-1, |
|
40 |
+ ifelse( !is.function(method) && |
|
41 |
+ !( is.character(method) && |
|
42 |
+ is.function(method <- get(method)) ), |
|
43 |
+ stop("Unknown dimensionality method!"), |
|
44 |
+ method(dat) ) |
|
45 |
+ ) |
|
46 |
+ if(is.list(x) && "dim.est" %in% names(x)) x <- max(x$dim.est) |
|
47 |
+ as.integer(round(x)) |
|
48 |
+} |
|
49 |
+ |
|
50 |
+ |
|
51 |
+# sce2se conversion |
|
52 |
+# not exported |
|
53 |
+#' @import SingleCellExperiment Seurat |
|
54 |
+seWrap <- function(sce, min.cells=10, min.features=0){ |
|
55 |
+ if(is(sce,"Seurat")) return(sce) |
|
56 |
+ suppressPackageStartupMessages(library(Seurat)) |
|
57 |
+ se <- CreateSeuratObject( counts=counts(sce), |
|
58 |
+ min.cells=min.cells, |
|
59 |
+ min.features=min.features, |
|
60 |
+ meta.data=as.data.frame(colData(sce)), |
|
61 |
+ project = "scRNAseq" ) |
|
62 |
+ se@misc$rowData <- as.data.frame(rowData(sce)) |
|
63 |
+ if("logcounts" %in% assayNames(sce)){ |
|
64 |
+ se <- ScaleData(se, verbose = FALSE) |
|
65 |
+ se@assays$RNA@data <- logcounts(sce) |
|
66 |
+ } |
|
67 |
+ if(!is.null(metadata(sce)$VariableFeats)) |
|
68 |
+ VariableFeatures(se) <- metadata(sce)$VariableFeats |
|
69 |
+ if(length(reducedDimNames(sce)) != 0) |
|
70 |
+ se[["pca"]] <- CreateDimReducObject(embeddings=reducedDim(sce), key="PC_", |
|
71 |
+ assay="RNA") |
|
72 |
+ se |
|
73 |
+} |
|
74 |
+ |
|
75 |
+# se2sce conversion |
|
76 |
+# not exported |
|
77 |
+#' @import SingleCellExperiment Seurat |
|
78 |
+sceWrap <- function(seu) { |
|
79 |
+ suppressPackageStartupMessages({ |
|
80 |
+ library(SingleCellExperiment) |
|
81 |
+ library(Seurat) |
|
82 |
+ }) |
|
83 |
+ sce <- SingleCellExperiment(list(counts=GetAssayData(seu, assay="RNA", slot="counts")), |
|
84 |
+ colData = seu[[]]) |
|
85 |
+ if(nrow(norm <- GetAssayData(seu, slot="scale.data"))>0){ |
|
86 |
+ sce <- sce[row.names(norm),] |
|
87 |
+ logcounts(sce) <- norm |
|
88 |
+ } |
|
89 |
+ rowData(sce) <- seu@misc$rowData[row.names(sce),] |
|
90 |
+ if(length(VariableFeatures(seu))) |
|
91 |
+ metadata(sce)$VariableFeats <- VariableFeatures(seu) |
|
92 |
+ if(length(Reductions(seu))>0){ |
|
93 |
+ reducedDims(sce) <- lapply(seu@reductions, FUN=function(x) [email protected]) |
|
94 |
+ } |
|
95 |
+ sce |
|
96 |
+} |