0

I'm following a tutorial for an R package called "pagoda2" (http://pklab.med.harvard.edu/peterk/p2/walkthrough.nb.html) - the tutorial loads data generated by a bioinformatic company CellRanger, and subsets the data according to a feature of the dataset's metadata (whether the data is from the control or experimental group). I've been trying to load and subset my own data (it's generated in the same way by CellRanger, so it should be more or less equivalent) but I get an error:

CTRL.data <- read.10x.matrices(list(CTRL="/home/atp9753/filtered_feature_bc_matrix/"))[[1]]
STIM.data <- read.10x.matrices(list(CTRL="/home/atp9753/filtered_feature_bc_matrix/"))[[2]]
reading 1 dataset(s) 
.
 done
Error in read.10x.matrices(list(CTRL = "/home/atp9753/filtered_feature_bc_matrix/"))[[1]] : 
  this S4 class is not subsettable

I saw one recommendation to convert the dataset to a dataframe, but that returned an error for my data:

CTRL.data <- as.data.frame(read.10x.matrices(list(CTRL="/home/atp9753/filtered_feature_bc_matrix/")))[[1]]
Error in as.data.frame.default(read.10x.matrices(list(CTRL = "/home/atp9753/filtered_feature_bc_matrix/"))) : cannot coerce class ‘structure("dgCMatrix", package = "Matrix")’ to a data.frame

I've been googling around and have looked around this site but haven't been able to find anything. I've looked at this post about subsetting an S4 matrix in R (Subsetting S4 matrix in R) but it wasn't clear to me how to subset the data according to metadata. Is it possible that the data from the tutorial is not an S4 class? Is it possible to convert my data to some for that could be subsetted?

EDIT:

This is the output of "class(read.10x.matrices(...))":

> class(read.10x.matrices(list(path="/home/atp9753/filtered_feature_bc_matrix/")))
reading 1 dataset(s) 
.
 done
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"
Aaron
  • 113
  • 1
  • 7
  • What is the output of `class(read.10x.matrices(...))`? – Mikael Jagan Dec 24 '21 at 07:03
  • Thanks for your comment @MikaelJagan, I've just added the output of "class(read.10x.matrices(...))" as an edit – Aaron Dec 24 '21 at 07:06
  • The cause of the error is that `read.10x.matrices` returns a sparse matrix of class `"dgCMatrix"`, _not_ a list of 2 such matrices (in which case extracting the matrices with `[[` would work). – Mikael Jagan Dec 24 '21 at 07:14
  • The real question is: why does the document in your link not show the same error? Are you able to reproduce that document's output (e.g., by downloading the data used there and trying the code)? – Mikael Jagan Dec 24 '21 at 07:28
  • Why do you expect to obtain two data sets from one path? That seems contrary to `?read.10x.matrices`. – Mikael Jagan Dec 24 '21 at 07:28
  • @MikaelJagan thanks for your questions and comments, I think I misunderstood what the "read.10x.matrices(...)[[e.g. 1]]" function did. I thought that it would create sub-matrices based off a list of elements of the metadata. As you're saying, it looks like it creates datasets from a list of separate matrices. It wasn't the answer I wanted but you really helped clear some things up for me, thank you! – Aaron Dec 24 '21 at 07:42
  • To be clear, there is no distinction between "matrix" and "data set" here. The matrix _is_ the data set. If you supply more than one path to `read.10x.matrices`, then it returns a list of matrices, and you can extract matrices from that list with `[[`. If you supply exactly one path (as in your case), then it returns a matrix; that matrix is not contained in a list, so there is no need for `[[`. – Mikael Jagan Dec 24 '21 at 08:01

0 Answers0