I have a R script that subsets out the tumor cells from 6 different individual seurat objects.
It gets a list of file paths for all RDS files containing the expression data for multiple datasets in a directory. It creates two empty lists, datasets and dataset_index, to hold the subsets of tumor cells from each dataset and their corresponding indices, respectively. It loops through each file path, reads the RDS file, and subsets the tumor cells from the expression data. If the dataset does not contain any tumor cells, it prints a message indicating that fact. The subsets of tumor cells are appended to the datasets list and their corresponding indices are appended to the dataset_index list.
scfiles = list.files('/LungDataSets/',pattern = '.*SCTransform_Symphony.*.RDS',recursive=TRUE)
scfiles = paste('/LungDataSets/',scfiles,sep='')
datasets = list()
dataset_index = list()
for (i in 1:length(scfiles)) {
scData = readRDS(scfiles[i])
if (is.element('Tumor', unique(scData$cell_ann))) {
tumorData = subset(scData,subset = cell_ann=='Tumor')
datasets = append(datasets,tumorData)
dataset_index = append(dataset_index,toString(i))
}
else {
print(c(scfiles[i], 'does not contain Tumor cells'))
}
rm(scData)
}
Now I want to merge all those tumor cells from the 6 datasets into one seurat object.
I've tried merge
and rbind
but they don't work.
megaObject <- merge(datasets)
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ‘structure("Seurat", package = "SeuratObject")’ to a data.frame