satijalab / seurat

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

merge 4 objects with two conditions, then split by condition.. need help #5413

Closed silkezimm closed 2 years ago

silkezimm commented 2 years ago

Hello, i am hoping someone can help me soon..

i am struggling with my snRNAseq data analysis using Seurat.

i have in total four objects: two are wildtype condition, two are treated group. i want to merge the wildtypes as one group and the treated as another group. i struggle with the IDs, since i want later to merge all four and then split by "conditions, treated and wildtype"... i always get error, will show my script in the following. can someone help me or even provide an example... basically i want to compare after split.by the wildtypes (2) with the treatment (2) group and create then clusters etc.. in the end you will see the occuring error... can someone help me>?

thank you so much.,,

a.data <- Read10X(data.dir = "D:/210619CKD-3vs1/c/")

b.data <- Read10X(data.dir = "D:/210619CKD-3vs1/ckd/") a <- CreateSeuratObject(counts = a.data, project = "silke", min.cells = 5) b <- CreateSeuratObject(counts = b.data, project = "silke", min.cells = 5) a$b <- "a" b$b <- "b"

read data and create two further seurat objects

c.data <- Read10X(data.dir = "D:/210626CKD-5vs4/c/") d.data <- Read10X(data.dir = "D:/210626CKD-5vs4/ckd/") c <- CreateSeuratObject(counts = c.data, project = "silke2", min.cells = 5) d <- CreateSeuratObject(counts = d.data, project = "silke2", min.cells = 5) c$d <- "c" d$d <- "d"

normalize, find variable features, subset

a <- NormalizeData(a, verbose = FALSE) a <- FindVariableFeatures(a, selection.method = "vst", nfeatures = 2000) Calculating gene variances 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| Calculating feature variances of standardized and clipped values 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| a <- subset(a, subset = nFeature_RNA > 500)

b <- subset(b, subset = nFeature_RNA > 500) b <- NormalizeData(b, verbose = FALSE) b <- FindVariableFeatures(b, selection.method = "vst", nfeatures = 2000) Calculating gene variances 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| Calculating feature variances of standardized and clipped values 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **|

c <- NormalizeData(c, verbose = FALSE) c <- FindVariableFeatures(c, selection.method = "vst", nfeatures = 2000) Calculating gene variances 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| Calculating feature variances of standardized and clipped values 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| c <- subset(c, subset = nFeature_RNA > 500)

d <- subset(d, subset = nFeature_RNA > 500) d <- NormalizeData(d, verbose = FALSE) d <- FindVariableFeatures(d, selection.method = "vst", nfeatures = 2000) Calculating gene variances 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| Calculating feature variances of standardized and clipped values 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **|

create a merged object of two seurat objects (a and b)

ab.combined <- merge(a, y = b, add.cell.ids = c("A", "B"), project = "ab") ab.combined An object of class Seurat 20036 features across 6889 samples within 1 assay Active assay: RNA (20036 features, 0 variable features)

create a merged object of two seurat objects (c and d)

cd.combined <- merge(c, y = d, add.cell.ids = c("C", "D"), project = "cd") cd.combined An object of class Seurat 20573 features across 7890 samples within 1 assay Active assay: RNA (20573 features, 0 variable features) head(colnames(ab.combined)) [1] "A_AAACCCAAGGTACTGG-1" "A_AAACCCAGTGAATAAC-1" "A_AAACGAACAAGAGTGC-1" "A_AAACGAAGTAAGACCG-1" "A_AAACGAAGTGGCGCTT-1" [6] "A_AAACGAATCAATCCGA-1" table(ab.combined$orig.ident)

silke 6889

head(colnames(cd.combined)) [1] "C_AAACCCAAGTCATGAA-1" "C_AAACCCATCAACTCTT-1" "C_AAACCCATCCGACGGT-1" "C_AAACGAACACCAGTAT-1" "C_AAACGAATCGCCAGAC-1" [6] "C_AAACGCTCACCACATA-1" table(cd.combined$orig.ident)

silke2 7890

create a merged object of two further seurat objects (ckd)

ckd <- merge(ab.combined, y = cd.combined, add.cell.ids = c("ab.combined", "cd.combined"), project = "ckd") ckd An object of class Seurat 20932 features across 14779 samples within 1 assay Active assay: RNA (20932 features, 0 variable features)

head(colnames(ckd)) [1] "ab.combined_A_AAACCCAAGGTACTGG-1" "ab.combined_A_AAACCCAGTGAATAAC-1" "ab.combined_A_AAACGAACAAGAGTGC-1" [4] "ab.combined_A_AAACGAAGTAAGACCG-1" "ab.combined_A_AAACGAAGTGGCGCTT-1" "ab.combined_A_AAACGAATCAATCCGA-1" table(ckd$orig.ident)

silke silke2 6889 7890

ckd.list <- SplitObject(ckd, split.by = "cd.combined") Fehler in FetchData(object = object, vars = split.by) : None of the requested variables were found: cd.combined

samuel-marsh commented 2 years ago

Hi,

Not member of the dev team but hopefully can be helpful. The reason for the error is because cd.combined is not a meta data variable but a prefix that was added to cell names. You need to create meta.data column that can be used to split the object.

I'm not 100% sure I understand exactly the way you are trying to split the object but the method I show below can be easily changed to whatever you need.

One way this can be accomplished is by running the following just before running the SplitObject call that threw the error. COLUMN_NAME can be replaced by whatever you would like to name the meta.data column.

# Get cell names
cd.combined_cell_names <- grep(pattern = "^cd.combined", x = colnames(ckd), value = T)
ab.combined_cell_names <- grep(pattern = "^ab.combined", x = colnames(ckd), value = T)

# Create new meta.data column
ckd@meta.data$COLUMN_NAME[colnames(ckd) %in% cd.combined_cell_names] <- "cd.combined"
ckd@meta.data$COLUMN_NAME[colnames(ckd) %in% ab.combined_cell_names] <- "ab.combined"

# Split the object
ckd.list <- SplitObject(ckd, split.by = "COLUMN_NAME")

Best, Sam