-
Notifications
You must be signed in to change notification settings - Fork 3
/
seurat_tabula_muris.R
65 lines (46 loc) · 2.14 KB
/
seurat_tabula_muris.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
suppressMessages(library(Matrix))
suppressMessages(library(Seurat))
suppressMessages(library(cellrangerRkit))
suppressMessages(library(plyr))
# Load a 10x matrix from a directory.
loadMatrix = function(x) {
mat = load_cellranger_matrix_from_files(mat_fn = paste0(x, "/matrix.mtx")
, gene_fn = paste0(x, "/genes.tsv")
, barcode_fn = paste0(x, "/barcodes.tsv")
)
return(exprs(mat))
}
# Load a 10x matrix from a directory with row and column names.
loadNamedMatrix = function(x) {
gbm = load_cellranger_matrix_from_files(mat_fn = paste0(x, "/matrix.mtx")
, gene_fn = paste0(x, "/genes.tsv")
, barcode_fn = paste0(x, "/barcodes.tsv")
)
mat = exprs(gbm)
colnames(mat) = pData(gbm)$barcode
rownames(mat) = fData(gbm)$id
return(mat)
}
# Execute the Seurat pipeline using guide from Tabula Musa online vignettes
# (less strict filtering) on a matrix with cell barcodes and labels.
seuratPipeline = function(mat, cells) {
sMat = CreateSeuratObject( raw.data = mat
, min.cells = 3
, min.genes = 200
, project = "Random"
)
labelDf = data.frame(label = cells$label)
rownames(labelDf) = cells$item
sMat = AddMetaData(object = sMat, metadata = labelDf)
sMat = FilterCells(object = sMat, subset.names = c("nGene", "nUMI"),
low.thresholds = c(500, 1000))
sMat = NormalizeData(object = sMat, scale.factor = 1e6)
sMat = ScaleData(object = sMat)
sMat = FindVariableGenes(object = sMat, x.high.cutoff = Inf, y.cutoff = 0.5, x.low.cutoff = 0.1)
sMat = RunPCA(object = sMat, do.print = TRUE)
sMat = ProjectPCA(object = sMat, do.print = FALSE)
sMat = FindClusters(object = sMat, reduction.type = "pca", dims.use = 1:10,
resolution = 1, print.output = 0, save.SNN = TRUE) # From vignette
sMat = RunTSNE(object = sMat, dims.use = 1:10, perplexity=30)
return(sMat)
}