-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsce_to_seurat.R
88 lines (66 loc) · 2.59 KB
/
sce_to_seurat.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#' Convert SingleCellExperiment object to Seurat object
#'
#' @param sce SingleCellExperiment object
#'
#' @return Seurat object
#'
#' @import SingleCellExperiment
#'
#' @export
#'
#' @examples
#' \dontrun{
#' sce_to_seurat(sce = sce_object)
#' }
sce_to_seurat <- function(sce){
if (!requireNamespace("Seurat", quietly = TRUE)) {
stop("The Seurat package must be installed to create a Seurat object. No output returned.")
}
if(!is(sce,"SingleCellExperiment")){
stop("Input must be a SingleCellExperiment object.")
}
# remove miQC model from metadata
if(!is.null(metadata(sce)$miQC_model)){
metadata(sce)$miQC_model <- NULL
message("miQC model will not be included in Seurat object.")
}
# Seurat counts need to be integers
sce_counts <- round(counts(sce))
# Seurat will not like zero count calls
sce_sum <- Matrix::colSums(sce_counts)
seurat_cells <- names(sce_sum)[sce_sum > 0]
sce_counts <- sce_counts[, seurat_cells]
# convert metadata to Seurat compatible formats
coldata <- as.data.frame(colData(sce))
rowdata <- as.data.frame(rowData(sce))
# create seurat object
seurat_obj <- Seurat::CreateSeuratObject(counts = sce_counts,
meta.data = coldata)
# add rowdata and metadata separately adding it while creating leaves the slots empty without warning
seurat_obj[["RNA"]]@var.features <- rowdata
seurat_obj@misc <- metadata(sce)
# grab names of altExp, if any
alt_names <- altExpNames(sce)
# add each altExp from the SCE to the Seurat object
for (name in alt_names){
# we want to make sure that we are only adding counts for the cells
# that currently exist in the object
seurat_obj_cells <- colnames(seurat_obj)
# round counts and calculate total counts
alt_counts <- round(counts(altExp(sce, name)))
# subset altExp counts and seurat object to shared cells
cells_to_keep <- intersect(seurat_obj_cells, colnames(alt_counts))
alt_counts <- alt_counts[, cells_to_keep]
# subset seurat object to only have cells that will be in alt_counts before adding new assay
seurat_obj <- seurat_obj[, cells_to_keep]
# add new assay along with associated row data, if any
seurat_obj[[name]] <- Seurat::CreateAssayObject(counts = alt_counts)
rowdata <- as.data.frame(rowData(altExp(sce, name)))
seurat_obj[[name]]@var.features <- rowdata
# check that altExp data is present as a new assay, since Seurat sometimes fails without warning
if(is.null(seurat_obj[[name]])){
warning("Unable to convert altExp data found in SCE to Seurat.")
}
}
return(seurat_obj)
}