Merge remote-tracking branch 'upstream/devel' into devel
# Conflicts:
# NAMESPACE
# R/htmlReports.R
# R/importCellRanger.R
# R/importSTARSolo.R
# R/subsetSCE.R
# inst/shiny/ui_09_3_celdaWorkflow.R
# vignettes/articles/differential_expression.Rmd
... | ... |
@@ -179,5 +179,8 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
inSCE <- inSCE[,final.ix] |
182 |
+ if(!is.null(metadata(inSCE)$seurat$obj)){ |
|
183 |
+ metadata(inSCE)$seurat$obj@meta.data <- metadata(inSCE)$seurat$obj@meta.data[final.ix, ] |
|
184 |
+ } |
|
182 | 185 |
return(inSCE) |
183 | 186 |
} |
... | ... |
@@ -179,5 +179,9 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
inSCE <- inSCE[,final.ix] |
182 |
+ |
|
183 |
+ if(!is.null(S4Vectors::metadata(inSCE)$seurat$obj)){ |
|
184 |
+ S4Vectors::metadata(inSCE)$seurat$obj@meta.data <- S4Vectors::metadata(inSCE)$seurat$obj@meta.data[final.ix, ] |
|
185 |
+ } |
|
182 | 186 |
return(inSCE) |
183 | 187 |
} |
... | ... |
@@ -52,7 +52,7 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
52 | 52 |
if(is.null(index) & is.null(bool) & is.null(rowData)) { |
53 | 53 |
stop("At least one of 'index', 'bool', or 'rowData' must be supplied.") |
54 | 54 |
} |
55 |
- final.ix <- rep(FALSE, nrow(inSCE)) |
|
55 |
+ final.ix <- rep(TRUE, nrow(inSCE)) |
|
56 | 56 |
|
57 | 57 |
# Parse index containing integers |
58 | 58 |
if(!is.null(index)) { |
... | ... |
@@ -60,7 +60,7 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
60 | 60 |
stop("'index' must contain integers between 1 and the number of rows ", |
61 | 61 |
"in 'inSCE': ", nrow(inSCE)) |
62 | 62 |
} |
63 |
- final.ix[index] <- TRUE |
|
63 |
+ final.ix[!seq(nrow(inSCE)) %in% index] <- FALSE |
|
64 | 64 |
} |
65 | 65 |
|
66 | 66 |
# Parse Boolean vector |
... | ... |
@@ -69,12 +69,11 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
69 | 69 |
stop("'bool' must be a logical vector the same length as the number of ", |
70 | 70 |
"rows in 'inSCE': ", nrow(inSCE)) |
71 | 71 |
} |
72 |
- final.ix[bool] <- TRUE |
|
72 |
+ final.ix[!bool] <- FALSE |
|
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
# Parse expressions for rowData variables |
76 | 76 |
if(!is.null(rowData)) { |
77 |
- temp <- rep(TRUE, nrow(inSCE)) |
|
78 | 77 |
for(i in seq_along(rowData)) { |
79 | 78 |
temp <- eval(parse(text = as.character(rowData[i])), |
80 | 79 |
envir = as.data.frame(rowData(inSCE))) |
... | ... |
@@ -107,9 +106,6 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
107 | 106 |
return(inSCE) |
108 | 107 |
} |
109 | 108 |
|
110 |
- |
|
111 |
- |
|
112 |
- |
|
113 | 109 |
#' @title Subset a SingleCellExperiment object by columns |
114 | 110 |
#' @description Used to peform subsetting of a |
115 | 111 |
#' \linkS4class{SingleCellExperiment} object using a variety of methods that |
... | ... |
@@ -146,7 +142,7 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
146 | 142 |
if(is.null(index) & is.null(bool) & is.null(colData)) { |
147 | 143 |
stop("At least one of 'index', 'bool', or 'colData' must be supplied.") |
148 | 144 |
} |
149 |
- final.ix <- rep(FALSE, ncol(inSCE)) |
|
145 |
+ final.ix <- rep(TRUE, ncol(inSCE)) |
|
150 | 146 |
|
151 | 147 |
# Parse index containing integers |
152 | 148 |
if(!is.null(index)) { |
... | ... |
@@ -154,7 +150,7 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
154 | 150 |
stop("'index' must contain integers between 1 and the number of columns ", |
155 | 151 |
"in 'inSCE': ", ncol(inSCE)) |
156 | 152 |
} |
157 |
- final.ix[index] <- TRUE |
|
153 |
+ final.ix[!seq(ncol(inSCE)) %in% index] <- FALSE |
|
158 | 154 |
} |
159 | 155 |
|
160 | 156 |
# Parse Boolean vector |
... | ... |
@@ -163,12 +159,11 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
163 | 159 |
stop("'bool' must be a logical vector the same length as the number of ", |
164 | 160 |
"colmns in 'inSCE': ", ncol(inSCE)) |
165 | 161 |
} |
166 |
- final.ix[bool] <- TRUE |
|
162 |
+ final.ix[!bool] <- FALSE |
|
167 | 163 |
} |
168 | 164 |
|
169 | 165 |
# Parse expressions for colData variables |
170 | 166 |
if(!is.null(colData)) { |
171 |
- final.ix <- rep(TRUE, ncol(inSCE)) |
|
172 | 167 |
for(i in seq_along(colData)) { |
173 | 168 |
temp <- eval(parse(text = as.character(colData[i])), |
174 | 169 |
envir = as.data.frame(colData(inSCE))) |
... | ... |
@@ -74,6 +74,7 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
74 | 74 |
|
75 | 75 |
# Parse expressions for rowData variables |
76 | 76 |
if(!is.null(rowData)) { |
77 |
+ temp <- rep(TRUE, nrow(inSCE)) |
|
77 | 78 |
for(i in seq_along(rowData)) { |
78 | 79 |
temp <- eval(parse(text = as.character(rowData[i])), |
79 | 80 |
envir = as.data.frame(rowData(inSCE))) |
... | ... |
@@ -84,7 +85,7 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
84 | 85 |
"trying to use matches one of the column names in ", |
85 | 86 |
"'rowData(inSCE)' and that your expression is valid.") |
86 | 87 |
} |
87 |
- final.ix[temp] = TRUE |
|
88 |
+ final.ix[!temp] <- FALSE |
|
88 | 89 |
} |
89 | 90 |
} |
90 | 91 |
|
... | ... |
@@ -167,6 +168,7 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
167 | 168 |
|
168 | 169 |
# Parse expressions for colData variables |
169 | 170 |
if(!is.null(colData)) { |
171 |
+ final.ix <- rep(TRUE, ncol(inSCE)) |
|
170 | 172 |
for(i in seq_along(colData)) { |
171 | 173 |
temp <- eval(parse(text = as.character(colData[i])), |
172 | 174 |
envir = as.data.frame(colData(inSCE))) |
... | ... |
@@ -177,7 +179,7 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
177 | 179 |
"trying to use matches one of the column names in ", |
178 | 180 |
"'colData(inSCE)' and that your expression is valid.") |
179 | 181 |
} |
180 |
- final.ix[temp] = TRUE |
|
182 |
+ final.ix[!temp] <- FALSE |
|
181 | 183 |
} |
182 | 184 |
} |
183 | 185 |
|
... | ... |
@@ -6,7 +6,7 @@ |
6 | 6 |
#' with one another. If \code{returnAsAltExp} is set to \code{TRUE}, |
7 | 7 |
#' then the returned object will have the same number of rows as the input |
8 | 8 |
#' \code{inSCE} as the subsetted object will be stored in the |
9 |
-#' \code{\link[SingleCellExperiment]{altExp}} slot. |
|
9 |
+#' \code{\link{altExp}} slot. |
|
10 | 10 |
#' @param inSCE Input \linkS4class{SingleCellExperiment} object. |
11 | 11 |
#' @param index Integer vector. Vector of indicies indicating which rows |
12 | 12 |
#' to keep. If \code{NULL}, this will not be used for subsetting. |
... | ... |
@@ -6,7 +6,7 @@ |
6 | 6 |
#' with one another. If \code{returnAsAltExp} is set to \code{TRUE}, |
7 | 7 |
#' then the returned object will have the same number of rows as the input |
8 | 8 |
#' \code{inSCE} as the subsetted object will be stored in the |
9 |
-#' \code{\link[SingleCellExperiment]{altExp}} slot. |
|
9 |
+#' \code{\link[SingleCellExperiment]{altExp}} slot. |
|
10 | 10 |
#' @param inSCE Input \linkS4class{SingleCellExperiment} object. |
11 | 11 |
#' @param index Integer vector. Vector of indicies indicating which rows |
12 | 12 |
#' to keep. If \code{NULL}, this will not be used for subsetting. |
... | ... |
@@ -18,13 +18,13 @@ |
18 | 18 |
#' @param rowData Character. An expression that will identify a subset of rows |
19 | 19 |
#' using variables found in the \code{rowData} of \code{inSCE}. For example, |
20 | 20 |
#' if \code{x} is a numeric vector in \code{rowData}, then \code{"x < 5"} will |
21 |
-#' return all rows with x less than 5. Single quotes should be used for |
|
21 |
+#' return all rows with x less than 5. Single quotes should be used for |
|
22 | 22 |
#' character strings. For example, \code{"y == 'yes'"} will return all |
23 | 23 |
#' rows where y is "yes". Multiple expressions can be evaluated by placing them |
24 | 24 |
#' in a vector. For example \code{c("x < 5", "y =='yes'")} will apply both |
25 | 25 |
#' operations for subsetting. If \code{NULL}, this will not be used for |
26 | 26 |
#' subsetting. Default \code{NULL}. |
27 |
-#' @param returnAsAltExp Boolean. If \code{TRUE}, the subsetted |
|
27 |
+#' @param returnAsAltExp Boolean. If \code{TRUE}, the subsetted |
|
28 | 28 |
#' \linkS4class{SingleCellExperiment} object will be returned in the |
29 | 29 |
#' \code{altExp} slot of \code{inSCE}. If \code{FALSE}, the subsetted |
30 | 30 |
#' \linkS4class{SingleCellExperiment} object will be directly returned. |
... | ... |
@@ -34,24 +34,26 @@ |
34 | 34 |
#' be added to the beginning of the assay names in the \code{altExp} object. |
35 | 35 |
#' This is only utilized if \code{returnAsAltExp = TRUE}. Default \code{TRUE}. |
36 | 36 |
#' @author Joshua D. Campbell |
37 |
+#' @return A \link[SingleCellExperiment]{SingleCellExperiment} object that has |
|
38 |
+#' been subsetted by rowData. |
|
37 | 39 |
#' @examples |
38 | 40 |
#' data(scExample) |
39 |
-#' |
|
41 |
+#' |
|
40 | 42 |
#' # Set a variable up in the rowData indicating mitochondrial genes |
41 | 43 |
#' rowData(sce)$isMito <- ifelse(grepl("^MT-", rowData(sce)$feature_name), |
42 | 44 |
#' "yes", "no") |
43 |
-#' sce <- subsetSCERows(sce, rowData = "isMito == 'yes'") |
|
45 |
+#' sce <- subsetSCERows(sce, rowData = "isMito == 'yes'") |
|
44 | 46 |
#' @export |
45 | 47 |
#' @importFrom SummarizedExperiment assays assays<- |
46 | 48 |
subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
47 | 49 |
returnAsAltExp = TRUE, altExpName = "subset", |
48 | 50 |
prependAltExpName = TRUE) { |
49 |
- |
|
51 |
+ |
|
50 | 52 |
if(is.null(index) & is.null(bool) & is.null(rowData)) { |
51 | 53 |
stop("At least one of 'index', 'bool', or 'rowData' must be supplied.") |
52 | 54 |
} |
53 | 55 |
final.ix <- rep(FALSE, nrow(inSCE)) |
54 |
- |
|
56 |
+ |
|
55 | 57 |
# Parse index containing integers |
56 | 58 |
if(!is.null(index)) { |
57 | 59 |
if(min(index) < 1 | max(index) > nrow(inSCE)) { |
... | ... |
@@ -60,7 +62,7 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
60 | 62 |
} |
61 | 63 |
final.ix[index] <- TRUE |
62 | 64 |
} |
63 |
- |
|
65 |
+ |
|
64 | 66 |
# Parse Boolean vector |
65 | 67 |
if(!is.null(bool)) { |
66 | 68 |
if(length(bool) != nrow(inSCE) | !is.logical(bool)) { |
... | ... |
@@ -69,14 +71,14 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
69 | 71 |
} |
70 | 72 |
final.ix[bool] <- TRUE |
71 | 73 |
} |
72 |
- |
|
74 |
+ |
|
73 | 75 |
# Parse expressions for rowData variables |
74 | 76 |
if(!is.null(rowData)) { |
75 | 77 |
for(i in seq_along(rowData)) { |
76 | 78 |
temp <- eval(parse(text = as.character(rowData[i])), |
77 | 79 |
envir = as.data.frame(rowData(inSCE))) |
78 | 80 |
if(length(temp) != nrow(inSCE) | !is.logical(temp)) { |
79 |
- stop("The expression ", rowData[i], " did not produce a boolean ", |
|
81 |
+ stop("The expression ", rowData[i], " did not produce a boolean ", |
|
80 | 82 |
"vector the same length as the number of rows in 'inSCE'. ", |
81 | 83 |
"Please ensure that the spelling of the variable you are ", |
82 | 84 |
"trying to use matches one of the column names in ", |
... | ... |
@@ -85,7 +87,7 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
85 | 87 |
final.ix[temp] = TRUE |
86 | 88 |
} |
87 | 89 |
} |
88 |
- |
|
90 |
+ |
|
89 | 91 |
# Filter object and return in different ways depending on flag |
90 | 92 |
temp.SCE <- inSCE[final.ix,] |
91 | 93 |
if(isTRUE(returnAsAltExp)) { |
... | ... |
@@ -95,7 +97,7 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
95 | 97 |
is going to be saved as an ", "'altExp' object.") |
96 | 98 |
} |
97 | 99 |
if(isTRUE(prependAltExpName)) { |
98 |
- names(assays(temp.SCE)) <- paste0(altExpName, names(assays(temp.SCE))) |
|
100 |
+ names(assays(temp.SCE)) <- paste0(altExpName, names(assays(temp.SCE))) |
|
99 | 101 |
} |
100 | 102 |
SingleCellExperiment::altExp(inSCE, altExpName) <- temp.SCE |
101 | 103 |
} else { |
... | ... |
@@ -112,7 +114,7 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
112 | 114 |
#' \linkS4class{SingleCellExperiment} object using a variety of methods that |
113 | 115 |
#' indicate the correct columns to keep. The various methods, |
114 | 116 |
#' \code{index}, \code{bool}, and \code{colData}, can be used in conjunction |
115 |
-#' with one another. |
|
117 |
+#' with one another. |
|
116 | 118 |
#' @param inSCE Input \linkS4class{SingleCellExperiment} object. |
117 | 119 |
#' @param index Integer vector. Vector of indicies indicating which columns |
118 | 120 |
#' to keep. If \code{NULL}, this will not be used for subsetting. |
... | ... |
@@ -128,21 +130,23 @@ subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
128 | 130 |
#' Single quotes should be used for character strings. For example, |
129 | 131 |
#' \code{"y == 'yes'"} will return all columns where y is "yes". |
130 | 132 |
#' Multiple expressions can be evaluated by placing them in a vector. |
131 |
-#' For example \code{c("x < 5", "y =='yes'")} will apply both operations for |
|
133 |
+#' For example \code{c("x < 5", "y =='yes'")} will apply both operations for |
|
132 | 134 |
#' subsetting. If \code{NULL}, this will not be used for subsetting. |
133 | 135 |
#' Default \code{NULL}. |
136 |
+#' @return A \link[SingleCellExperiment]{SingleCellExperiment} object that has |
|
137 |
+#' been subsetted by colData. |
|
134 | 138 |
#' @author Joshua D. Campbell |
135 | 139 |
#' @examples |
136 | 140 |
#' data(scExample) |
137 |
-#' sce <- subsetSCECols(sce, colData = "type != 'EmptyDroplet'") |
|
141 |
+#' sce <- subsetSCECols(sce, colData = "type != 'EmptyDroplet'") |
|
138 | 142 |
#' @export |
139 | 143 |
subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
140 |
- |
|
144 |
+ |
|
141 | 145 |
if(is.null(index) & is.null(bool) & is.null(colData)) { |
142 | 146 |
stop("At least one of 'index', 'bool', or 'colData' must be supplied.") |
143 | 147 |
} |
144 | 148 |
final.ix <- rep(FALSE, ncol(inSCE)) |
145 |
- |
|
149 |
+ |
|
146 | 150 |
# Parse index containing integers |
147 | 151 |
if(!is.null(index)) { |
148 | 152 |
if(min(index) < 1 | max(index) > ncol(inSCE)) { |
... | ... |
@@ -151,7 +155,7 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
151 | 155 |
} |
152 | 156 |
final.ix[index] <- TRUE |
153 | 157 |
} |
154 |
- |
|
158 |
+ |
|
155 | 159 |
# Parse Boolean vector |
156 | 160 |
if(!is.null(bool)) { |
157 | 161 |
if(length(bool) != ncol(inSCE) | !is.logical(bool)) { |
... | ... |
@@ -160,14 +164,14 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
160 | 164 |
} |
161 | 165 |
final.ix[bool] <- TRUE |
162 | 166 |
} |
163 |
- |
|
167 |
+ |
|
164 | 168 |
# Parse expressions for colData variables |
165 | 169 |
if(!is.null(colData)) { |
166 | 170 |
for(i in seq_along(colData)) { |
167 | 171 |
temp <- eval(parse(text = as.character(colData[i])), |
168 | 172 |
envir = as.data.frame(colData(inSCE))) |
169 | 173 |
if(length(temp) != ncol(inSCE) | !is.logical(temp)) { |
170 |
- stop("The expression ", colData[i], " did not produce a boolean ", |
|
174 |
+ stop("The expression ", colData[i], " did not produce a boolean ", |
|
171 | 175 |
"vector the same length as the number of columns in 'inSCE'. ", |
172 | 176 |
"Please ensure that the spelling of the variable you are ", |
173 | 177 |
"trying to use matches one of the column names in ", |
... | ... |
@@ -176,7 +180,7 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
176 | 180 |
final.ix[temp] = TRUE |
177 | 181 |
} |
178 | 182 |
} |
179 |
- |
|
183 |
+ |
|
180 | 184 |
inSCE <- inSCE[,final.ix] |
181 | 185 |
return(inSCE) |
182 | 186 |
} |
... | ... |
@@ -42,6 +42,7 @@ |
42 | 42 |
#' "yes", "no") |
43 | 43 |
#' sce <- subsetSCERows(sce, rowData = "isMito == 'yes'") |
44 | 44 |
#' @export |
45 |
+#' @importFrom SummarizedExperiment assays assays<- |
|
45 | 46 |
subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
46 | 47 |
returnAsAltExp = TRUE, altExpName = "subset", |
47 | 48 |
prependAltExpName = TRUE) { |
... | ... |
@@ -178,4 +179,4 @@ subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
178 | 179 |
|
179 | 180 |
inSCE <- inSCE[,final.ix] |
180 | 181 |
return(inSCE) |
181 |
- } |
|
182 | 182 |
\ No newline at end of file |
183 |
+ } |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,181 @@ |
1 |
+#' @title Subset a SingleCellExperiment object by rows |
|
2 |
+#' @description Used to peform subsetting of a |
|
3 |
+#' \linkS4class{SingleCellExperiment} object using a variety of methods that |
|
4 |
+#' indicate the correct rows to keep. The various methods, |
|
5 |
+#' \code{index}, \code{bool}, and \code{rowData}, can be used in conjunction |
|
6 |
+#' with one another. If \code{returnAsAltExp} is set to \code{TRUE}, |
|
7 |
+#' then the returned object will have the same number of rows as the input |
|
8 |
+#' \code{inSCE} as the subsetted object will be stored in the |
|
9 |
+#' \code{\link[SingleCellExperiment]{altExp}} slot. |
|
10 |
+#' @param inSCE Input \linkS4class{SingleCellExperiment} object. |
|
11 |
+#' @param index Integer vector. Vector of indicies indicating which rows |
|
12 |
+#' to keep. If \code{NULL}, this will not be used for subsetting. |
|
13 |
+#' Default \code{NULL}. |
|
14 |
+#' @param bool Boolean vector. Vector of \code{TRUE} or \code{FALSE} |
|
15 |
+#' indicating which rows should be kept. Needs to be the same length as the |
|
16 |
+#' number of rows in \code{inSCE}. If \code{NULL}, this will not be used |
|
17 |
+#' for subsetting. Default \code{NULL}. |
|
18 |
+#' @param rowData Character. An expression that will identify a subset of rows |
|
19 |
+#' using variables found in the \code{rowData} of \code{inSCE}. For example, |
|
20 |
+#' if \code{x} is a numeric vector in \code{rowData}, then \code{"x < 5"} will |
|
21 |
+#' return all rows with x less than 5. Single quotes should be used for |
|
22 |
+#' character strings. For example, \code{"y == 'yes'"} will return all |
|
23 |
+#' rows where y is "yes". Multiple expressions can be evaluated by placing them |
|
24 |
+#' in a vector. For example \code{c("x < 5", "y =='yes'")} will apply both |
|
25 |
+#' operations for subsetting. If \code{NULL}, this will not be used for |
|
26 |
+#' subsetting. Default \code{NULL}. |
|
27 |
+#' @param returnAsAltExp Boolean. If \code{TRUE}, the subsetted |
|
28 |
+#' \linkS4class{SingleCellExperiment} object will be returned in the |
|
29 |
+#' \code{altExp} slot of \code{inSCE}. If \code{FALSE}, the subsetted |
|
30 |
+#' \linkS4class{SingleCellExperiment} object will be directly returned. |
|
31 |
+#' @param altExpName Character. Name of the alternative experiment object to |
|
32 |
+#' add if \code{returnAsAltExp = TRUE}. Default \code{subset}. |
|
33 |
+#' @param prependAltExpName Boolean. If \code{TRUE}, \code{altExpName} will |
|
34 |
+#' be added to the beginning of the assay names in the \code{altExp} object. |
|
35 |
+#' This is only utilized if \code{returnAsAltExp = TRUE}. Default \code{TRUE}. |
|
36 |
+#' @author Joshua D. Campbell |
|
37 |
+#' @examples |
|
38 |
+#' data(scExample) |
|
39 |
+#' |
|
40 |
+#' # Set a variable up in the rowData indicating mitochondrial genes |
|
41 |
+#' rowData(sce)$isMito <- ifelse(grepl("^MT-", rowData(sce)$feature_name), |
|
42 |
+#' "yes", "no") |
|
43 |
+#' sce <- subsetSCERows(sce, rowData = "isMito == 'yes'") |
|
44 |
+#' @export |
|
45 |
+subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL, |
|
46 |
+ returnAsAltExp = TRUE, altExpName = "subset", |
|
47 |
+ prependAltExpName = TRUE) { |
|
48 |
+ |
|
49 |
+ if(is.null(index) & is.null(bool) & is.null(rowData)) { |
|
50 |
+ stop("At least one of 'index', 'bool', or 'rowData' must be supplied.") |
|
51 |
+ } |
|
52 |
+ final.ix <- rep(FALSE, nrow(inSCE)) |
|
53 |
+ |
|
54 |
+ # Parse index containing integers |
|
55 |
+ if(!is.null(index)) { |
|
56 |
+ if(min(index) < 1 | max(index) > nrow(inSCE)) { |
|
57 |
+ stop("'index' must contain integers between 1 and the number of rows ", |
|
58 |
+ "in 'inSCE': ", nrow(inSCE)) |
|
59 |
+ } |
|
60 |
+ final.ix[index] <- TRUE |
|
61 |
+ } |
|
62 |
+ |
|
63 |
+ # Parse Boolean vector |
|
64 |
+ if(!is.null(bool)) { |
|
65 |
+ if(length(bool) != nrow(inSCE) | !is.logical(bool)) { |
|
66 |
+ stop("'bool' must be a logical vector the same length as the number of ", |
|
67 |
+ "rows in 'inSCE': ", nrow(inSCE)) |
|
68 |
+ } |
|
69 |
+ final.ix[bool] <- TRUE |
|
70 |
+ } |
|
71 |
+ |
|
72 |
+ # Parse expressions for rowData variables |
|
73 |
+ if(!is.null(rowData)) { |
|
74 |
+ for(i in seq_along(rowData)) { |
|
75 |
+ temp <- eval(parse(text = as.character(rowData[i])), |
|
76 |
+ envir = as.data.frame(rowData(inSCE))) |
|
77 |
+ if(length(temp) != nrow(inSCE) | !is.logical(temp)) { |
|
78 |
+ stop("The expression ", rowData[i], " did not produce a boolean ", |
|
79 |
+ "vector the same length as the number of rows in 'inSCE'. ", |
|
80 |
+ "Please ensure that the spelling of the variable you are ", |
|
81 |
+ "trying to use matches one of the column names in ", |
|
82 |
+ "'rowData(inSCE)' and that your expression is valid.") |
|
83 |
+ } |
|
84 |
+ final.ix[temp] = TRUE |
|
85 |
+ } |
|
86 |
+ } |
|
87 |
+ |
|
88 |
+ # Filter object and return in different ways depending on flag |
|
89 |
+ temp.SCE <- inSCE[final.ix,] |
|
90 |
+ if(isTRUE(returnAsAltExp)) { |
|
91 |
+ if(is.null(altExpName) || length(altExpName) > 1 |
|
92 |
+ || !is.character(altExpName)) { |
|
93 |
+ stop("'altExpName' needs to be a character of length 1 if the subset |
|
94 |
+ is going to be saved as an ", "'altExp' object.") |
|
95 |
+ } |
|
96 |
+ if(isTRUE(prependAltExpName)) { |
|
97 |
+ names(assays(temp.SCE)) <- paste0(altExpName, names(assays(temp.SCE))) |
|
98 |
+ } |
|
99 |
+ SingleCellExperiment::altExp(inSCE, altExpName) <- temp.SCE |
|
100 |
+ } else { |
|
101 |
+ inSCE <- temp.SCE |
|
102 |
+ } |
|
103 |
+ return(inSCE) |
|
104 |
+} |
|
105 |
+ |
|
106 |
+ |
|
107 |
+ |
|
108 |
+ |
|
109 |
+#' @title Subset a SingleCellExperiment object by columns |
|
110 |
+#' @description Used to peform subsetting of a |
|
111 |
+#' \linkS4class{SingleCellExperiment} object using a variety of methods that |
|
112 |
+#' indicate the correct columns to keep. The various methods, |
|
113 |
+#' \code{index}, \code{bool}, and \code{colData}, can be used in conjunction |
|
114 |
+#' with one another. |
|
115 |
+#' @param inSCE Input \linkS4class{SingleCellExperiment} object. |
|
116 |
+#' @param index Integer vector. Vector of indicies indicating which columns |
|
117 |
+#' to keep. If \code{NULL}, this will not be used for subsetting. |
|
118 |
+#' Default \code{NULL}. |
|
119 |
+#' @param bool Boolean vector. Vector of \code{TRUE} or \code{FALSE} |
|
120 |
+#' indicating which columns should be kept. Needs to be the same length as the |
|
121 |
+#' number of columns in \code{inSCE}. If \code{NULL}, this will not be used |
|
122 |
+#' for subsetting. Default \code{NULL}. |
|
123 |
+#' @param colData Character. An expression that will identify a subset of |
|
124 |
+#' columns using variables found in the \code{colData} of \code{inSCE}. |
|
125 |
+#' For example, if \code{x} is a numeric vector in \code{colData}, |
|
126 |
+#' then \code{"x < 5"} will return all columns with x less than 5. |
|
127 |
+#' Single quotes should be used for character strings. For example, |
|
128 |
+#' \code{"y == 'yes'"} will return all columns where y is "yes". |
|
129 |
+#' Multiple expressions can be evaluated by placing them in a vector. |
|
130 |
+#' For example \code{c("x < 5", "y =='yes'")} will apply both operations for |
|
131 |
+#' subsetting. If \code{NULL}, this will not be used for subsetting. |
|
132 |
+#' Default \code{NULL}. |
|
133 |
+#' @author Joshua D. Campbell |
|
134 |
+#' @examples |
|
135 |
+#' data(scExample) |
|
136 |
+#' sce <- subsetSCECols(sce, colData = "type != 'EmptyDroplet'") |
|
137 |
+#' @export |
|
138 |
+subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) { |
|
139 |
+ |
|
140 |
+ if(is.null(index) & is.null(bool) & is.null(colData)) { |
|
141 |
+ stop("At least one of 'index', 'bool', or 'colData' must be supplied.") |
|
142 |
+ } |
|
143 |
+ final.ix <- rep(FALSE, ncol(inSCE)) |
|
144 |
+ |
|
145 |
+ # Parse index containing integers |
|
146 |
+ if(!is.null(index)) { |
|
147 |
+ if(min(index) < 1 | max(index) > ncol(inSCE)) { |
|
148 |
+ stop("'index' must contain integers between 1 and the number of columns ", |
|
149 |
+ "in 'inSCE': ", ncol(inSCE)) |
|
150 |
+ } |
|
151 |
+ final.ix[index] <- TRUE |
|
152 |
+ } |
|
153 |
+ |
|
154 |
+ # Parse Boolean vector |
|
155 |
+ if(!is.null(bool)) { |
|
156 |
+ if(length(bool) != ncol(inSCE) | !is.logical(bool)) { |
|
157 |
+ stop("'bool' must be a logical vector the same length as the number of ", |
|
158 |
+ "colmns in 'inSCE': ", ncol(inSCE)) |
|
159 |
+ } |
|
160 |
+ final.ix[bool] <- TRUE |
|
161 |
+ } |
|
162 |
+ |
|
163 |
+ # Parse expressions for colData variables |
|
164 |
+ if(!is.null(colData)) { |
|
165 |
+ for(i in seq_along(colData)) { |
|
166 |
+ temp <- eval(parse(text = as.character(colData[i])), |
|
167 |
+ envir = as.data.frame(colData(inSCE))) |
|
168 |
+ if(length(temp) != ncol(inSCE) | !is.logical(temp)) { |
|
169 |
+ stop("The expression ", colData[i], " did not produce a boolean ", |
|
170 |
+ "vector the same length as the number of columns in 'inSCE'. ", |
|
171 |
+ "Please ensure that the spelling of the variable you are ", |
|
172 |
+ "trying to use matches one of the column names in ", |
|
173 |
+ "'colData(inSCE)' and that your expression is valid.") |
|
174 |
+ } |
|
175 |
+ final.ix[temp] = TRUE |
|
176 |
+ } |
|
177 |
+ } |
|
178 |
+ |
|
179 |
+ inSCE <- inSCE[,final.ix] |
|
180 |
+ return(inSCE) |
|
181 |
+ } |
|
0 | 182 |
\ No newline at end of file |