waldronlab / MultiAssayExperiment

Bioconductor package for management of multi-assay data
https://waldronlab.io/MultiAssayExperiment/
70 stars 32 forks source link

Error when subsetting features from a `SingleCellExperiment` object from `experiments(x)` #276

Closed cvanderaa closed 4 years ago

cvanderaa commented 4 years ago

Hello, I encounter an error when I subset features from a SingleCellExperiment object that is contained in a MultiAssayExperiment object. I want to perform this by subsetting the output of experiments(x) which (from the traceback()) calls to subset_List_by_List. Note it works perfectly fine for a SummarizedExperiment object.

Here is some reproducible example:

## Create a mock expression matrix
matrix(1:100, 10) %>%
  `rownames<-`(letters[1:10]) %>%
  `colnames<-`(LETTERS[1:10]) -> exprs
## Build SE and SCE objects for comparison 
se <- SummarizedExperiment(SimpleList(exprs))
sce <- SingleCellExperiment(SimpleList(exprs))
## Build the MAE object containing the SE and SCE as assays
mae <- MultiAssayExperiment(ExperimentList(se = se, 
                                           sce = sce))

## Subsetting works fine for SE
experiments(mae)[list(se = "a")]
## ExperimentList class object of length 1:
##  [1] se: SummarizedExperiment with 1 rows and 10 columns

## Error with SCE
experiments(mae)[list(sce = "a")]
## Error in .wrap_in_length_one_list_like_object(value, names(x)[[i]], x) : 
##   failed to coerce 'list(value)' to a ExperimentList object of length 1
LiNk-NY commented 4 years ago

Hi @cvanderaa We don't have explicit methods for ExperimentList so any [ subsetting will use the List methods and those probably are not meant to handle the input. I can look into adding an explicit [ method for ExperimentList. There are a couple of alternatives in the meantime:

## Use MAE-level subsetting
mae[list(sce = "a"), ]

## Use subsetByRow method for ExperimentList
subsetByRow(experiments(mae), list(sce = "a"))
lwaldron commented 4 years ago

@cvanderaa what do you want experiments(mae)[list(sce = "a")] to do? Is it this?

> subsetByRow(experiments(mae), "a") %>% subsetByAssay(., "sce")
ExperimentList class object of length 1:
 [1] sce: SingleCellExperiment with 1 rows and 10 columns
cvanderaa commented 4 years ago

Thank you for your answers @LiNk-NY and @lwaldron. My goal is to include and use SingleCellExperiment objects in a Features object that is based on your MultiAssayExperiment class. Features (developed by @lgatto) integrates various levels of linked proteomic expression data (e.g. proteins expression levels computed from peptide expression levels). I want to adapt Features for single-cell proteomics usage.

One very interesting functionality of Features is to subset, let's say, a protein over all levels of expression, typically PSM, peptide and protein. Of course in my reproducible example, it is strange to have a list with only 1 entry, but a real case scenario would look like this:

experiments(mae)[list(PSM = c("psm1", "psm2" , "psm3", "psm4"),
                      peptide = c("peptide1", "peptide2"), 
                      protein = c("protein1"))]

See this line in Features where the error occurs. featurename_list is a list as given in the code chunk above.

Since this works perfectly for SummarizedExperiments, I was hoping it would be possible to make it also work for SingleCellExperiments without too much effort.

cvanderaa commented 4 years ago

Sorry, don't mind my last comment, I was able to adapt Features to solve the issue with the solution of @LiNk-NY

experiments(x[list(sce = "a"), ])    

Thanks again for your help!

lwaldron commented 4 years ago

I do think row subsetting should be supported for ExperimentList, and it is when using subsetByRow(). @LiNk-NY I would propose that the following should be equivalent (and should be documented in ?ExperimentList-class and the cheatsheet).

 subsetByRow(experiments(mae), "a")
ExperimentList class object of length 2:
 [1] se: SummarizedExperiment with 1 rows and 10 columns
 [2] sce: SingleCellExperiment with 1 rows and 10 columns
> experiments(mae)["a", ]
Error: subscript contains invalid names
>
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

LiNk-NY commented 4 years ago

You can test in this branch: https://github.com/waldronlab/MultiAssayExperiment/tree/expsubset

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

hpages commented 4 years ago

Hi,

FWIW the root of the problem reported by the OP was a bug in the SingleCellExperiment class that got fixed 9 days ago: https://github.com/drisso/SingleCellExperiment/commit/bc220cab41b7112347dda5e094ebb2a9c987fb23 This was a bug in the extractROWS() method for SingleCellExperiment objects. The bug was breaking things based on extractROWS() like head(sce, n=2) or experiments(mae)[list(sce="a")].

Note that changing the behavior of subsetting on ExperimentList objects was not needed in order to address the problem.

H.