seurat icon indicating copy to clipboard operation
seurat copied to clipboard

Another problem adding Antibody Capture matrix with single feature to Seurat Object

Open mlwarrior opened this issue 2 years ago • 3 comments

Basically, I am following the tutorial here https://satijalab.org/seurat/archive/v3.1/multimodal_vignette.html

Under the section "Loading data from 10X multi-modal experiments" I downloaded the pbmc10k/filtered_feature_bc_matrix/ data set, and ran the following code just fine.

pbmc10k.data <- Read10X(data.dir = "../data/pbmc10k/filtered_feature_bc_matrix/")
rownames(x = pbmc10k.data[["Antibody Capture"]]) <- gsub(pattern = "_[control_]*TotalSeqB", replacement = "", 
    x = rownames(x = pbmc10k.data[["Antibody Capture"]]))

pbmc10k <- CreateSeuratObject(counts = pbmc10k.data[["Gene Expression"]], min.cells = 3, min.features = 200)
pbmc10k <- NormalizeData(pbmc10k)
pbmc10k[["ADT"]] <- CreateAssayObject(pbmc10k.data[["Antibody Capture"]][, colnames(x = pbmc10k)])
pbmc10k <- NormalizeData(pbmc10k, assay = "ADT", normalization.method = "CLR")

No errors.

When I try to run the same code with my dataset, I get errors that I can troubleshoot down to what looks to be a bug. I'm going to remove the antibody renaming, which I can run fine, as well as the data normalization, which also runs fine, to narrow down the issue.

First, let me state what works.

E.data <- Read10X(data.dir = "./filtered_feature_bc_matrix/") E <- CreateSeuratObject(counts = E.data[["Gene Expression"]]) E[["ADT"]] <- CreateAssayObject(counts = E.data[["Antibody Capture"]])

If I run this, I get a Seurat object with two assays, "RNA" and "ADT", the column, or cell names appear to be matched between the two data sets, which is what I want. RNA is an S4 object with dimensions [32285 x 6806]. ADT is an object with dimensions [1 x 6806.

The problem is that if I filter the cells when creating the seurat object, which I need to do (I think anyway) to remove rare features in cells as standard by running

E<- CreateSeuratObject(counts = E.data[["Gene Expression"]], min.cells = 3, min.features = 200)

Indeed I get a filtered matrix with [17266x6745] dimensions.

I can't run

E[["ADT"]] <- CreateAssayObject(counts = E.data[["Antibody Capture"]])

because as we are all aware, the number of cells was reduced, and rightly gives the error "Error: Cannot add a different number of cells than already present"

I understand that this is solved in the code provided

pbmc10k[["ADT"]] <- CreateAssayObject(pbmc10k.data[["Antibody Capture"]][, colnames(x = pbmc10k)])

by adding [, colnames(x = pbmc10k)]

However, when I run

E[["ADT"]] <- CreateAssayObject(counts = E.data[["Antibody Capture"]][, colnames(x = E)])

I get the error

"Error in CreateAssayObject(counts = E.data[["Antibody Capture"]][, colnames(x = E)]) : No cell names (colnames) names present in the input matrix"

This doesn't happen with the PBMC example above, and doesn't happen when I don't add min.cells = 3, min.features = 200 to the CreateSeuratObject function.

Am I doing something wrong? If this is a bug, then is there a way to simply filter my data with min.cells = 3, min.features = 200 after creating the multi-modal Seurat object as I was successfully able to to above, and ensure that the cells are matched from the RNA assay to the antibody labels?

Thanks for your help and these great tools!

mlwarrior avatar May 03 '22 20:05 mlwarrior

Hi,

Not member of dev team but hopefully can be helpful. What are the results of this code both before and after you rename the rownames. head(rownames(x = E.data[["Antibody Capture"]]), 5)

Best, Sam

samuel-marsh avatar May 04 '22 14:05 samuel-marsh

Hi Samuel,

So, this may have been unclear from the original post, but I am not including the rename rows in the code I am trying to run yet, the code I am having trouble with is:

E.data <- Read10X(data.dir = "./filtered_feature_bc_matrix/")
E<- CreateSeuratObject(counts = E.data[["Gene Expression"]], min.cells = 3, min.features = 200)
E[["ADT"]] <- CreateAssayObject(counts = E.data[["Antibody Capture"]][, colnames(x = E)])

Error in CreateAssayObject(counts = E.data[["Antibody Capture"]][, colnames(x = E)]) : No cell names (colnames) names present in the input matrix

As mentioned, if I run

E.data <- Read10X(data.dir = "./filtered_feature_bc_matrix/")
E<- CreateSeuratObject(counts = E.data[["Gene Expression"]])
E[["ADT"]] <- CreateAssayObject(counts = E.data[["Antibody Capture"]])

This works fine. However, I can't use this because I can't filter the data set using the min.cells = 3, min.features = 200, because I will get an error "Error: Cannot add a different number of cells than already present"

The [, colnames(x = E)] portion of code from the tutorial is to address this to make the create the ADT assay object only with the columns (cells) that are present in the Gene expression assay (as some cells were filtered).

As per your suggestion, I also ran

E.data <- Read10X(data.dir = "./filtered_feature_bc_matrix/")
head(rownames(x = E.data[["Antibody Capture"]]), 5)
rownames(x = E.data[["Antibody Capture"]]) <- gsub(pattern = "[control_]*CD45.2_Antibody", replacement = "Vascular", 
    x = rownames(x = E.data[["Antibody Capture"]]))
E<- CreateSeuratObject(counts = E.data[["Gene Expression"]], min.cells = 3, min.features = 200)
E[["ADT"]] <- CreateAssayObject(counts = E.data[["Antibody Capture"]][, colnames(x = E)])

and

E.data <- Read10X(data.dir = "./filtered_feature_bc_matrix/")
rownames(x = E.data[["Antibody Capture"]]) <- gsub(pattern = "[control_]*CD45.2_Antibody", replacement = "Vascular", 
    x = rownames(x = E.data[["Antibody Capture"]]))
head(rownames(x = E.data[["Antibody Capture"]]), 5)
E<- CreateSeuratObject(counts = E.data[["Gene Expression"]], min.cells = 3, min.features = 200)
E[["ADT"]] <- CreateAssayObject(counts = E.data[["Antibody Capture"]][, colnames(x = E)])

both of which still give the error "Error in CreateAssayObject(counts = E.data[["Antibody Capture"]][, colnames(x = E)]) : No cell names (colnames) names present in the input matrix"

This seems very similar to the problem described here: https://github.com/satijalab/seurat/issues/2274

Which appeared to be a bug that was solved in a patch, but I am running Seurat version 4.0.5 which should be a much later release.

Honestly, if there is just a way to filter the multi modal Seurat object I can successfully create by running

E.data <- Read10X(data.dir = "./filtered_feature_bc_matrix/")
E<- CreateSeuratObject(counts = E.data[["Gene Expression"]])
E[["ADT"]] <- CreateAssayObject(counts = E.data[["Antibody Capture"]])

and ensure that the cells from both assays match, that would work too.

mlwarrior avatar May 04 '22 16:05 mlwarrior

Similar to the workaround posted in the thread I linked in the last comment https://github.com/satijalab/seurat/issues/2274

I am able to work around the issue by using this code.

abmat <- as.matrix(J.data$`Antibody Capture`)
abmat <- t(abmat[colnames(Jobj),])
rownames(abmat) <- c("SLC3a2ab")
Jobj[['SLC3A2ab']] <- CreateAssayObject(counts = abmat)

Please note that I changed the line (keeping variable names consistent with what he posted, obviously mine are different).

from

abmat <- t(abmat[colnames(Jobj),])

to

abmat <- t(abmat[, colnames(x = Jobj)])

which allows the column names to be properly transposed to the Seurat 'Gene Expression' column/cell names that are filtered when we run the CreateSeuratObject with the min.cells = 3, min.features = 200 modifier.

mlwarrior avatar May 09 '22 15:05 mlwarrior

Hii, I have a similar data type but with 3 different conditions and more than 70K cells in the Antibody capture assay. I tried to use this chunk of code and I am getting an error when I run the 3rd line.

abmat <- as.matrix(J.data$`Antibody Capture`)
abmat <- t(abmat[colnames(Jobj),])
rownames(abmat) <- c("SLC3a2ab")
Jobj[['SLC3A2ab']] <- CreateAssayObject(counts = abmat)

Error: Error in dimnames(x) <- dn : length of 'dimnames' [1] not equal to array extent

However, I tried to transpose my Gene Expression data with (32K x 70K matrix) the above-mentioned code, so that I don't need to change anything in the Antibody capture data. But it seems that it computationally overwhelming to transpose such a big matrix and I get the following error:

Error: cannot allocate vector of size 174.7 Gb In addition: Warning message: In asMethod(object) : sparse->dense coercion: allocating vector of size 174.7 GiB

All in all, how should I read this data and map my Antibody capture data with 3 different conditions with my Gene Expression data in a way that they fit with each other after filtering with min.cells and min.features parameters?

GauravJethva0 avatar Nov 13 '23 10:11 GauravJethva0