-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMNN_Cortex_HVG.R
executable file
·60 lines (43 loc) · 1.58 KB
/
MNN_Cortex_HVG.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
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
if("Seurat" %in% rownames(installed.packages()) == F) {
install.packages('Seurat')
}
if("reticulate" %in% rownames(installed.packages()) == F) {
install.packages('reticulate')
}
if("batchelor" %in% rownames(installed.packages()) == F) {
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("batchelor")
}
if("scater" %in% rownames(installed.packages()) == F) {
BiocManager::install("scater")
}
reticulate::use_condaenv("cardec", required = T)
reticulate::source_python("mnn_reticulate.py")
library(Seurat)
library(batchelor)
library(scater)
library(Matrix)
path = "../Data/cortex"
mydata = read_cortex(path)
meta_data = mydata[[2]]
genes = mydata[[3]]
mydata = mydata[[1]]
rownames(mydata) = rownames(meta_data)
colnames(mydata) = genes
mydata = CreateSeuratObject(t(mydata))
hvgs = get_hvgs(mydata@assays$RNA@data, meta_data$Method, rownames(mydata@assays$RNA@data))
mydata = subset(mydata, features = hvgs)
sf = librarySizeFactors(mydata@assays$RNA@counts)
countmatrix = normalizeCounts(mydata@assays$RNA@counts, size_factors = sf)
countmatrix = Matrix::as.matrix(countmatrix)
batch_vec = meta_data$Method
nbatch = length(unique(batch_vec))
start = Sys.time()
output = mnnCorrect(countmatrix, batch = as.factor(batch_vec), merge.order = c(1:nbatch))
Sys.time() - start
path = "MNNcorrected_hvg"
dir.create(path)
write.csv(output@assays@data$corrected, paste0(path, "/corrected_data_Cortex.csv"))
write.csv(meta_data, paste0(path, "/corrected_metadata_Cortex.csv"))