Skip to content

Commit 0a5b225

Browse files
committed
add intersectByRowData
1 parent a919ea9 commit 0a5b225

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export(getWithColData)
1717
export(hasAssay)
1818
export(hasRowData)
1919
export(hasRowRanges)
20+
export(intersectByRowData)
2021
export(intersectColumns)
2122
export(intersectRows)
2223
export(listToMap)
@@ -68,6 +69,7 @@ exportMethods(experiments)
6869
exportMethods(exportClass)
6970
exportMethods(hasRowData)
7071
exportMethods(hasRowRanges)
72+
exportMethods(intersectByRowData)
7173
exportMethods(isEmpty)
7274
exportMethods(length)
7375
exportMethods(longForm)

R/subsetBy-methods.R

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,28 @@ NULL
105105
#' * `subsetByColumn`: Select observations by assay or for each assay
106106
#' * `subsetByRow`: Select rows by assay or for each assay
107107
#' * `subsetByAssay`: Select experiments
108+
#' * `subsetByRowData`: Select rows by values in the rowData
109+
#' * `intersectByRowData`: Intersect with values in the rowData
110+
#'
111+
#' @section rowData:
112+
#'
113+
#' Some assays may have additional metadata associated with the rows.
114+
#' This metadata is stored in the `rowData` slot of the object, typically a
115+
#' `SummarizedExperiment` or `RangedSummarizedExperiment`.
116+
#'
117+
#' `subsetByRowData` allows the user to subset the rows of the assays
118+
#' based on the values in the `rowData`.
119+
#'
120+
#' `intersectByRowData` is a special case of `subsetByRowData` where
121+
#' the `rowData` values are intersected with the `y` values. Naturally,
122+
#' the `y` values are expected to be of type `character`.
123+
#'
124+
#' Note that `rowDataCols` allows the user to specify a particular
125+
#' column from which to extract the values for subsetting. This column
126+
#' name must be consistent across assays. If the column is not present
127+
#' in an assay, the assay will be skipped and considered a no-op. Assays
128+
#' are also skipped when there are no values in the `rowData` that match
129+
#' the `y` values.
108130
#'
109131
#' @return `subsetBy*`: operations are endomorphic and return either
110132
#' `MultiAssayExperiment` or `ExperimentList` depending on the
@@ -160,6 +182,23 @@ NULL
160182
#' subsetByRowData(
161183
#' mae, "ENST00000355076", "rownames", i = "Affy"
162184
#' )
185+
#'
186+
#' ## use miniACC as example MAE
187+
#' data("miniACC")
188+
#'
189+
#' ## intersect values of y with rownames in rowData
190+
#' intersectByRowData(
191+
#' x = miniACC,
192+
#' y = c("G6PD", "PETN"),
193+
#' rowDataCol = "rownames",
194+
#' i = c("RNASeq2GeneNorm", "gistict")
195+
#' )
196+
#'
197+
#' ## no-op when rowDataCol is not present or there is no data
198+
#' intersectByRowData(
199+
#' x = miniACC, y = c("G6PD", "PETN"), rowDataCol = "Genes",
200+
#' i = c("RNASeq2GeneNorm", "gistict")
201+
#' )
163202
NULL
164203

165204
# subsetBy Generics -------------------------------------------------------
@@ -434,3 +473,56 @@ setMethod(
434473
subsetByRow(x = x, y = y, i = i)
435474
}
436475
)
476+
477+
478+
# intersectByRowData,MultiAssayExperiment-method --------------------------
479+
480+
#' @rdname subsetBy
481+
#'
482+
#' @aliases intersectByRowData
483+
#'
484+
#' @export
485+
setGeneric(
486+
"intersectByRowData",
487+
function(x, y, rowDataCol, i, ...)
488+
standardGeneric("intersectByRowData")
489+
)
490+
491+
#' @rdname subsetBy
492+
#' @exportMethod intersectByRowData
493+
setMethod(
494+
"intersectByRowData", c("MultiAssayExperiment", "character", "character"),
495+
function(x, y, rowDataCol, i = TRUE, ...) {
496+
if (is.character(i))
497+
logi <- names(x) %in% i
498+
else if (is.logical(i) || is.numeric(i))
499+
logi <- names(x) %in% names(x)[i]
500+
else
501+
stop("Invalid experiment subscript type for 'i'")
502+
i <- hasRowData(x) & logi
503+
if (!any(i))
504+
stop("No 'rowData' available for subsetting")
505+
y <- lapply(
506+
experiments(x)[i],
507+
function(exper) {
508+
rd <- rowData(exper)
509+
if (rowDataCol %in% c("rownames", "row.names"))
510+
intersect(rownames(rd), y)
511+
else if (rowDataCol %in% colnames(rd))
512+
intersect(rd[[rowDataCol]], y)
513+
else
514+
NULL
515+
}
516+
)
517+
noRowData <-
518+
vapply(y, function(z) is.null(z) || !length(z), logical(1))
519+
if (any(noRowData)) {
520+
noRDnames <- paste(shQuote(names(y)[noRowData]), collapse = ", ")
521+
warning(
522+
"No 'rowData' intersected for assays:\n ", noRDnames,
523+
call. = FALSE
524+
)
525+
}
526+
subsetByRow(x = x, y = y, i = i)
527+
}
528+
)

man/subsetBy.Rd

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

0 commit comments

Comments
 (0)