This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to filter columns in a raw sparse matrix in R

Hello,

I have a question about how I can filter a dgCMatrix in R by columns, which I got from the 10x cellranger count pipeline. Below is the counts matrix, which has 69,801 cells/columns.

combined_rhemac10_count_matrix

37242 x 69801 sparse Matrix of class "dgCMatrix"
  [[ suppressing 88 column names ‘3_C_MI2_S1_AAACCCAAGCTAGATA-1’, ‘3_C_MI2_S1_AAACCCAAGCTCACTA-1’, ‘3_C_MI2_S1_AAACCCACAACAAGAT-1’ ... ]]
  [[ suppressing 88 column names ‘3_C_MI2_S1_AAACCCAAGCTAGATA-1’, ‘3_C_MI2_S1_AAACCCAAGCTCACTA-1’, ‘3_C_MI2_S1_AAACCCACAACAAGAT-1’ ... ]]

PGBD2      . . . . . . . . . . . . . . . . . . 1 . . . . 1 1 . . 1 . . 1 . . . . . . . . . 1 1 . . . 1 . . 1 . . . . . 1 . 1 . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . ......
TRE-CTC1-7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
TRL-CAA4-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
ZNF692     1 . . . . . . . . . 3 . . . . 1 . . . . . . 1 . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . ......
ZNF672     . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . ......
SH3BP5L    . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . 1 . 1 . . . . . ......

 ..............................
 ........suppressing 69713 columns and 37231 rows in show(); maybe adjust options(max.print=, width=)
 ..............................
  [[ suppressing 88 column names ‘3_C_MI2_S1_AAACCCAAGCTAGATA-1’, ‘3_C_MI2_S1_AAACCCAAGCTCACTA-1’, ‘3_C_MI2_S1_AAACCCACAACAAGAT-1’ ... ]]

LOC105377241   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
LOC105379264   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
LOC105377237   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
LOC101929148   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
LOC105377236.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......

The rows of the matrix are features/genes and the columns are cell barcodes. I have a csc file that just contains a list of cell barcodes that passed QC, and I would like to filter the raw count matrix to only keep the cell barcodes in the csv file. A preview of the csv file (which I read into R as an object) is below.

head(all_good_cells_rhemac10)
                           V1
1 1_MI1_S3_AAACCCAAGAGACAAG-1
2 1_MI1_S3_AAACCCAAGAGTATAC-1
3 1_MI1_S3_AAACCCAAGTCCCGAC-1
4 1_MI1_S3_AAACCCAAGTGAGGCT-1
5 1_MI1_S3_AAACCCACAGAAGCTG-1
6 1_MI1_S3_AAACCCACATCTATCT-1

The list of good cell barcodes has 56,020 cells. How can I filter the matrix (columns only) using this csv file?

matrix r

1 answer

You can do this-

good_cells <-c(all_good_cells_rhemac10$V1)
combined_rhemac10_count_matrix_good_cells <- combined_rhemac10_count_matrix[,good_cells]

Hi, it worked, thank you so much!

Log in to answer this question.