This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Unable to extract count matrices from multi-layered Seurat object

Hello, I have a merged R object with 18 samples

all_combined <- merge(
  sample_1MI1_so,
  y = c(
    sample_1MI3_so,
    sample_2MI1_so,
    sample_2MI3_so,
    sample_3C_MI2_so,
    sample_3MI1_so,
    sample_3MI3_so,
    sample_4D_MI2_so,
    sample_4MI1_so,
    sample_4MI3_so,
    sample_5MI1_so,
    sample_5MI3_so,
    sample_6MI1_so,
    sample_6MI3_so,
    sample_7MI1_so,
    sample_7MI3_so,
    sample_8MI1_so,
    sample_8MI3_so
  ),
  add.cell.ids = c(
    "sample_1MI1_so",
    "sample_1MI3_so",
    "sample_2MI1_so",
    "sample_2MI3_so",
    "sample_3C_MI2_so",
    "sample_3MI1_so",
    "sample_3MI3_so",
    "sample_4D_MI2_so",
    "sample_4MI1_so",
    "sample_4MI3_so",
    "sample_5MI1_so",
    "sample_5MI3_so",
    "sample_6MI1_so",
    "sample_6MI3_so",
    "sample_7MI1_so",
    "sample_7MI3_so",
    "sample_8MI1_so",
    "sample_8MI3_so"
  ),
  merge.data = TRUE
)

View object

all_combined
An object of class Seurat 
23477 features across 135704 samples within 1 assay 
Active assay: RNA (23477 features, 2000 variable features)
 55 layers present: counts.1, counts.2, counts.3, counts.4, counts.5, counts.6, counts.7, counts.8, counts.9, counts.10, counts.11, counts.12, counts.13, counts.14, counts.15, counts.16, counts.17, counts.18, data.1, scale.data.1, data.2, scale.data.2, data.3, scale.data.3, data.4, scale.data.4, data.5, scale.data.5, data.6, scale.data.6, data.7, scale.data.7, data.8, scale.data.8, data.9, scale.data.9, data.10, scale.data.10, data.11, scale.data.11, data.12, scale.data.12, data.13, scale.data.13, data.14, scale.data.14, data.15, scale.data.15, data.16, scale.data.16, data.17, scale.data.17, data.18, scale.data.18, scale.data
 2 dimensional reductions calculated: pca, umap

In the all_combined object, I', trying to extract the counts matrices from it, but I'm not sure how to do this? Below is what I've tried

all_combined_count_matrix <- LayerData(object = all_combined, assay = "RNA", layer = "counts")
Warning: multiple layers are identified by counts.1 counts.2 counts.3 counts.4 counts.5 counts.6 counts.7 counts.8 counts.9 counts.10 counts.11 counts.12 counts.13 counts.14 counts.15 counts.16 counts.17 counts.18
 only the first layer is used

# only 1st sample counts matrix is extracted....want to extract all 18 counts matrices...all_combined object contains 18 seurat objects merged into one

`

Manually combine all 18 count matrices into one

# List all layers that start with "counts"
all_counts_layers <- Layers(all_combined[["RNA"]])
all_counts_layers <- counts_layers[grepl("^counts", all_counts_layers)]

# # view all_count_layers
#  [1] "counts.1"  "counts.2"  "counts.3"  "counts.4"  "counts.5"  "counts.6"  "counts.7"  "counts.8"  "counts.9"  "counts.10" "counts.11" "counts.12" "counts.13" "counts.14"
# [15] "counts.15" "counts.16" "counts.17" "counts.18"



# Extract each layer and combine
all_count_matrices <- lapply(all_counts_layers, function(layer) {
  LayerData(all_combined, assay = "RNA", layer = layer)
})


# View all_count_matrices (prints out all 18 count matrices. NOTE: each matrices has different number of rows/genes and different number of columns. However, total rows adds up to 23,447 genes and total columns add to 135,704 cells)
# all_count_matrices



# Combine into one gene x cell matrix
all_combined_counts <- do.call(cbind, all_count_matrices)

# Error in cbind.Matrix(x, y, deparse.level = 0L) : 
#   number of rows of matrices must match

Any advice on how to do this effectively would be greatly appreciated. I'm using Seurat v5.3.0

seurat

1 answer

Alright, I fixed the issue by joining the counts layers in all_combined. I saved it to a new seurat object. Hopefully this helps someone else!

all_combined_join_layers <- JoinLayers(all_combined)

This joins the 18 counts layers into one layer (and also the 18 normalized data layers)

all_combined_join_layers
An object of class Seurat 
23477 features across 135704 samples within 1 assay 
Active assay: RNA (23477 features, 2000 variable features)
 21 layers present: data, counts, scale.data.1, scale.data.2, scale.data.3, scale.data.4, scale.data.5, scale.data.6, scale.data.7, scale.data.8, scale.data.9, scale.data.10, scale.data.11, scale.data.12, scale.data.13, scale.data.14, scale.data.15, scale.data.16, scale.data.17, scale.data.18, scale.data
 2 dimensional reductions calculated: pca, umap

I then extracted the counts matrix from all_combined_join_layers

all_combined_count_matrix_2 <- LayerData(object = all_combined_join_layers, assay = "RNA", layer = "counts")

all_combined_count_matrix_2

23477 x 135704 sparse Matrix of class "dgCMatrix"
  [[ suppressing 75 column names 'sample_1MI1_so_AAACCCAAGAGACAAG-1', 'sample_1MI1_so_AAACCCAAGTCCCGAC-1', 'sample_1MI1_so_AAACCCAAGTGAGGCT-1' ... ]]
  [[ suppressing 75 column names 'sample_1MI1_so_AAACCCAAGAGACAAG-1', 'sample_1MI1_so_AAACCCAAGTCCCGAC-1', 'sample_1MI1_so_AAACCCAAGTGAGGCT-1' ... ]]

ENSMMUG00000023296 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
ZNF692             . . . . . . . . . 1 . . . . . 1 2 . . . . . . . . . . . . . . 1 . . . 1 . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . .

Log in to answer this question.