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