-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seurat.Multicore.Examples.R
140 lines (106 loc) · 4.21 KB
/
Seurat.Multicore.Examples.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
######################################################################
# Seurat.Multicore.Examples.R
######################################################################
# source('~/GitHub/Packages/Seurat.multicore/Seurat.Multicore.Examples.R')
# DO NOT RUN - these are just code snippets not a ready to go pipeline
# rm(list = ls())
require(tictoc)
require(dplyr)
require(gtools)
require(cowplot)
require(stringr)
require(tibble)
library(Seurat)
source(".../Seurat.Functions.R")
library(Stringendo)
library(CodeAndRoll2)
require(MarkdownReports)
require(doMC)
registerDoMC(6)
OutDir= "/Users/..."
setup_MarkdownReports(OutDir = OutDir)
p=NULL
p$min.cells = 10
p$min.features = 200
# filtering -----
p$thr.lp.nGene = 6000
p$thr.hp.nGene = 1000
p$thr.lp.mito = 0.15
p$thr.lp.ribo = 0.3
p$'min.pct' = 0.2
p$'logfc.threshold' = 0.5
# Load the dataset
InputDir = ""
Tag = "filtered_feature_bc_matrix."
inNames = c( "A"
, "B"
, "C"
, "D")
# v.project = str_split_fixed(inNames, '\\.', 3)[,3]
# genotype = str_split_fixed(inNames, '\\.', 4)[,4]
# batch = as.factor(str_split_fixed(v.project, '', 3)[,2])
# read in FILTERED -------------------------------------------------------------------------------------
if (FirstTime) {
ls.input <- ls.Seurat <- list.fromNames(inNames)
# Read 10x
ls.input <- foreach(i=1:length(inNames)) %dopar% {
read10x(paste0(InputDir, Tag, inNames[i]))
};
# Create Seurat
ls.Seurat <- foreach(i=1:length(inNames)) %dopar% {
CreateSeuratObject(raw.data = ls.input[[i]], project = v.project[i],
min.cells = p$min.cells, min.features = p$min.features)
};
# Save Seurat
OutputDir = "/Users/..."
foreach(i=1:length(inNames)) %dopar% {
saveRDS(ls.Seurat[[i]], file = ppp(paste0(OutputDir, 'filtered.', inNames[i]), 'min.cells', p$min.cells, 'min.features', p$min.features,"Rds") )
}; sayy()
} else {
# Read Seurat
ls.Seurat <- foreach(i=1:length(inNames)) %dopar% {
readRDS(file = ppp(paste0(InputDir, 'filtered.', inNames[i]), 'min.cells.10.min.features.200.Rds') )
};
}
# Add metadata ------------------------------
batch = as.factor(1:3)
ls.META<- foreach(i=1:length(ls.Seurat)) %dopar% {
META = ls.Seurat[[i]]@meta.data
mito.genes <- grep(pattern = "^MT\\.", x = rownames(x = ls.Seurat[[i]]@data), value = TRUE)
percent.mito <- Matrix::colSums(ls.Seurat[[i]]@data[mito.genes, ])/Matrix::colSums(ls.Seurat[[i]]@data)
ribo.genes <- grep(pattern = "^RPL|^RPS", x = rownames(x = ls.Seurat[[i]]@data), value = TRUE)
percent.ribo <- Matrix::colSums(ls.Seurat[[i]]@data[ribo.genes, ])/Matrix::colSums(ls.Seurat[[i]]@data)
META$'percent.mito' <- percent.mito
META$'percent.ribo' <- percent.ribo
META$'log10.nUMI' <- as.named.vector(META[,'nUMI', drop=F])
META$'log10.nGenes' <- as.named.vector(META[,'nGene', drop=F])
nCells = nrow(META)
META$'batch' <- rep(batch[i], nCells)
META
}
# calculation can take long, and subsettign back only worked in normal for loop AFAIR
for (i in 1:3) { ls.Seurat[[i]]@meta.data <- ls.META[[i]] }
# FilterCells ------------------------------
ls2<- foreach(i=1:length(ls2)) %dopar% {
FilterCells(object = ls2[[i]], subset.names = c("nGene", "percent.mito", "percent.ribo"),
low.thresholds = c(p$thr.hp.nGene, -Inf, -Inf),
high.thresholds = c(p$thr.lp.nGene, p$thr.lp.mito, p$thr.lp.ribo))
}; say()
length(ls2); ls2
# NormalizeData ------------------------------
ls2<- foreach(i=1:length(ls2)) %dopar% {
NormalizeData(object = ls2[[i]], normalization.method = "LogNormalize", scale.factor = 10000)
}; say()
length(ls2); ls20
# FindVariableGenes cannot be parallelized sadly ------------------------------
tic()
for (i in 1:length(inNames)) { print(i)
ls2[[i]] <- FindVariableGenes(object = ls2[[i]], mean.function = ExpMean, dispersion.function = LogVMR,
x.low.cutoff = 0.0125, x.high.cutoff = 3, y.cutoff = 0.5 )
wplot_save_this(plotname = ppp("Var.genes.",inNames[i]), PNG = T)
}; say()
toc()
# ScaleData is parallelizedialready ------------------------------
for (i in 1:length(inNames)) { print(i)
ls2[[i]] <- ScaleData(object = ls2[[i]], vars.to.regress = ls.vars2regress[[i]], do.par = T, num.cores = 6)
}; say()