Created
May 2, 2022 08:37
-
-
Save jonocarroll/cbdfadc48308741fd427b793f80c7512 to your computer and use it in GitHub Desktop.
Quick hack to enable some assay-level metadata filtering on SummarizedExperiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(SummarizedExperiment) | |
| ## create a SummarizedExperiment | |
| se <- SummarizedExperiment(assays = | |
| list( | |
| counts = matrix(0:99, 10, 10), | |
| cpm = matrix(200:299, 10, 10) | |
| ) | |
| ) | |
| ## add tabular metadata with assay-specific properties | |
| metadata(se) <- list( | |
| assay_metadata = DataFrame( | |
| property1 = c("A", "B"), | |
| property2 = c(3, 5), | |
| row.names = assayNames(se) | |
| ) | |
| ) | |
| ## perform filtering at the assay level based on metadata | |
| subset_by_assay <- function(se, property, value) { | |
| mse <- metadata(se)$assay_metadata | |
| ids <- mse[[property]] %in% value | |
| assays(se)[ids] | |
| } | |
| subset_by_assay(se, "property1", c("A", "B")) | |
| #> List of length 2 | |
| #> names(2): counts cpm | |
| subset_by_assay(se, "property1", "A") | |
| #> List of length 1 | |
| #> names(1): counts | |
| subset_by_assay(se, "property1", "B") | |
| #> List of length 1 | |
| #> names(1): cpm | |
| subset_by_assay(se, "property2", 3) | |
| #> List of length 1 | |
| #> names(1): counts | |
| subset_by_assay(se, "property2", 5) | |
| #> List of length 1 | |
| #> names(1): cpm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment