satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.24k stars 902 forks source link

Extract molecules from seurat object or cellranger outputs #5993

Closed raman91 closed 2 years ago

raman91 commented 2 years ago

Hello,

Is there a way to extract molecules from seurat object or cellranger outputs?

Thank you

samuel-marsh commented 2 years ago

Hi,

Could you clarify what you mean by molecules?

Best, Sam

raman91 commented 2 years ago

Hi,

When i read a 10X cellranger file, i have feature x cell sparse matrix. A molecule is a single entry in the matrix, i.e. each feature (i.e. gene; row) that are detected in each cell (column)

Thanks

samuel-marsh commented 2 years ago

Hi,

Sorry still little confused. So are you asking how to extract a single value (expression of one gene in one cell)? Expression of all genes across one cell or expression of one gene across all cells? Or something different?

Best, Sam

raman91 commented 2 years ago

Yes, want to extract a single value (expression of one gene in one cell) and select only non-zero values

samuel-marsh commented 2 years ago

Hi,

Ok thanks for clarifying!

So for cell ranger outputs as you mentioned you can read them in using Read10X to get a sparse matrix. You can then select values using normal R matrix operations. Here is example:

mat <- Matrix::Matrix(nrow = 3, ncol = 3, data = c(1, 2, 3, 0, 5, 0, 6, 9, 10), sparse = T)

rownames(mat) <- letters[1:3]
colnames(mat) <- letters[24:26]
print(mat)

3 x 3 sparse Matrix of class "dgCMatrix"
  x y  z
a 1 .  6
b 2 5  9
c 3 . 10

# select using positional identifiers
    # value <- mat[row_number, column_number]
value <- mat[1,3]

# select using row and column names
    # value <- mat[row_name, column_name]
value <- mat["a", "z"]

To do this from Seurat object you have couple options. First you could simply pull the sparse matrix back out of the Seurat object (just make sure to use the correct assay and slot for your desired application). Then just use normal R matrix operations as above.

# Load Seurat Object
pbmc <- pbmc3k.SeuratData::pbmc3k

# Extract raw counts matrix
pbmc_mat <- pbmc@assays$RNA@counts

# Extract single value for CD4 in a specific cell using feature name and cell barcode (can also just use row/column position as shown above)
value <- pbmc_mat["CD4", "AAACATACAACCAC"]

You can also do this from Seurat using the FetchData function (again make sure the Seurat object is set to the assay that you want to use and make sure to specify the slot to ensure you are pulling the right data for your downstream application).

# Load Seurat Object
pbmc <- pbmc3k.SeuratData::pbmc3k

value <- FetchData(object = pbmc, vars = "CD4", cells = "AAACATACAACCAC", slot = "counts")

Hope that helps! Best, Sam