-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCENIC_S3_1_NwActivity.R
145 lines (116 loc) · 6.5 KB
/
SCENIC_S3_1_NwActivity.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
139
140
141
142
143
# source("~/profile.R")
source("/BIGDATA1/pku_hkdeng_1/R/R-3.4.2/profile.R")
library(data.table)
library(Biobase)
library(AUCell)
library(SCENIC)
suppressWarnings(library(NMF, verbose=FALSE, warn.conflicts=FALSE, quietly=TRUE))
setwd("SCENIC_Mouse")
## ----loadEset------------------------------------------------------------
load("data/esetMouse.RData")
exprMat <- exprs(esetMouse)
dim(exprMat)
# load("data/colVars.RData")
# cellInfo <- pData(esetMouse)[,names(colVars), drop=F]
## ----loadRegulons--------------------------------------------------------
load("int/2.6_regulons_asGeneSet.RData")
regulons <- regulons[order(lengths(regulons), decreasing=TRUE)]
regulons <- regulons[lengths(regulons)>=10]
# Add the TF & rename
regulons <- setNames(lapply(names(regulons), function(tf) sort(unique(c(gsub("_extended", "", tf), regulons[[tf]])))), names(regulons))
names(regulons) <- paste(names(regulons), " (",lengths(regulons), "g)", sep="")
save(regulons, file="int/3.0_regulons_forAUCell.RData")
length(regulons)
cbind(names(regulons)[1:10])
## ----runAUCell, eval=TRUE------------------------------------------------
# 1. Create rankings
# increase Plots window
aucellRankings <- AUCell.buildRankings(exprMat, nCores=8 * parallel::detectCores(logical = FALSE) - 2, plotStats=TRUE)
# abline(v=aucellRankings@nGenesDetected["1%"], col="skyblue3", lwd=5, lty=3)
save(aucellRankings, file="int/3.1_aucellRankings.RData")
# 2. Calculate AUC
regulonAUC <- AUCell.calcAUC(regulons, aucellRankings, aucMaxRank=aucellRankings@nGenesDetected["1%"], nCores=8 * parallel::detectCores(logical = FALSE) - 2)
## ----sortAUC-------------------------------------------------------------
# Order the modules by similarity, for easier exploration in the upcoming steps & save
variableRegulons <- names(which(apply(getAuc(regulonAUC), 1, sd) > 0))
reguDist <-as.dist(1-cor(t(getAuc(regulonAUC)[variableRegulons,]), method="spear"))
reguClust <- hclust(reguDist, method="ward.D2")
regulonClusters <- setNames(dynamicTreeCut::cutreeDynamic(reguClust, distM=as.matrix(reguDist), verbose = FALSE), reguClust$labels)
regulonOrder <- reguClust$labels[reguClust$order]
regulonOrder <- regulonOrder[order(regulonClusters[regulonOrder], decreasing = TRUE)]
regulonAUC@matrix <- regulonAUC@matrix[regulonOrder,]
save(regulonAUC, file="int/3.2_regulonAUC.RData")
load("int/3.0_regulons_forAUCell.RData")
load("int/3.1_aucellRankings.RData")
load("int/3.2_regulonAUC.RData")
## ----tSNE_AUC, eval=TRUE-------------------------------------------------
# (It is recommended to try different perplexity values)
regulonAUC_subset <- subset(regulonAUC, onlyNonDirectExtended(rownames(regulonAUC)))
# PCA-based t-SNE
set.seed(123)
tsneAUC <- Rtsne::Rtsne(t(getAuc(regulonAUC_subset)), initial_dims=10, perplexity=10)
rownames(tsneAUC$Y) <- colnames(regulonAUC_subset)
colnames(tsneAUC$Y) <- c("tsne1", "tsne2")
save(tsneAUC, file="int/3.3_tsneRegulonAUC_PCA.RData")
# Alternative: Distance-based t-SNE:
corDist <- as.dist(1-cor(getAuc(regulonAUC_subset)))
set.seed(123)
tsneAUC <- Rtsne::Rtsne(corDist, is_distance=TRUE, perplexity=10)
rownames(tsneAUC$Y) <- labels(corDist)
colnames(tsneAUC$Y) <- c("tsne1", "tsne2")
save(tsneAUC, file="int/3.3_tsneRegulonAUC_Dist.RData")
## ----tSNE_plot, fig.height=5, fig.width=10-------------------------------
load("int/3.3_tsneRegulonAUC_PCA.RData")
tSNE <- tsneAUC$Y
# par(mfrow=c(1,2))
# tSNE2 <- read.csv("../tSNE2.csv", row.names = 1)
# tSNE2 <- as.matrix(tSNE2)
# # Number of genes detected:
# nGenesPerCell <- apply(exprMat, 2, function(x) sum(x>0))
# colorPal <- grDevices::colorRampPalette(c("darkgreen", "yellow","red"))
# cellColorNgenes <- setNames(adjustcolor(colorPal(10), alpha=.8)[as.numeric(cut(nGenesPerCell,breaks=10, right=F,include.lowest=T))], names(nGenesPerCell))
# plot(tSNE, col=cellColorNgenes[rownames(tSNE)], pch=16, main="nGenes", sub="t-SNE on regulon activity (AUC)")
# # Other known properties:
# for(varName in names(colVars))
# {
# cellColor <- setNames(colVars[[varName]][cellInfo[,varName]], rownames(cellInfo))
# plot(tSNE, col=cellColor[rownames(tSNE)], pch=16, main=varName, sub="t-SNE on regulon activity (AUC)")
# }
## ----histogramsTsne, eval=TRUE-------------------------------------------
# Cairo::CairoPDF("output/Step3_RegulonActivity_AUCtSNE.pdf", width=20, height=5)
# par(mfrow=c(1,4))
# # tSNE (colored by number of genes detected per cell)
# plot(tSNE, col=cellColorNgenes[rownames(tSNE)], pch=16, main="nGenes", sub="t-SNE on regulon activity (AUC)")
# plot(tSNE, col=cellColor[rownames(tSNE)], pch=16, main=varName, sub="t-SNE on regulon activity (AUC)")
# plot.new(); plot.new()
# Plot module activity, thresholds & assignment:
cells_AUCellThresholds <- plot_aucTsne(tSNE=tSNE, exprMat=exprMat, regulonAUC=regulonAUC, alphaOff=0.1)
# dev.off()
save(cells_AUCellThresholds, file="int/3.4_AUCellThresholds.RData")
## ----htmlPreview, echo=FALSE, fig.height=6, fig.width=7, eval=TRUE-------
x <- as.data.frame(rownames(regulonAUC))
regOrder <- c("Sox2 (142g)", "Nanog (200g)", "Pou5f1 (32g)")
# tiff(file=paste0("SIII_tSNE2_", today,".tiff"), height = 6, width = 8, units = 'in', res = 300, compression = 'none')
# par(mfrow=c(3,4))
# tmp <- plot_aucTsne(tSNE=tSNE2, exprMat=exprMat, regulonAUC=regulonAUC[regOrder,], alphaOff=0.1, cex=.8)
# dev.off()
## ----thresholds2edit-----------------------------------------------------
load("int/3.4_AUCellThresholds.RData")
# Get cells assigned to each regulon
regulonsCells <- lapply(cells_AUCellThresholds, function(x) x$assignment)
### Save threshold info as text (e.g. to edit/modify...)
trhAssignment <- sapply(cells_AUCellThresholds, function(x) unname(x$aucThr$selected))
commentsThresholds <- sapply(cells_AUCellThresholds, function(x) unname(x$aucThr$comment))
table2edit <- cbind(regulon=names(trhAssignment),
threshold=trhAssignment,
nCellsAssigned=lengths(regulonsCells)[names(trhAssignment)],
AUCellComment=commentsThresholds,
nGenes=gsub("[\\(g\\)]", "", regmatches(names(cells_AUCellThresholds), gregexpr("\\(.*?\\)", names(cells_AUCellThresholds)))),
clusteringOrder=1:length(trhAssignment),
clusterGroup=regulonClusters[names(trhAssignment)],
onlyNonDirectExtended=(names(trhAssignment) %in% onlyNonDirectExtended(names(trhAssignment))),
personalNotes="")
write.table(table2edit, file="int/3.5_1_AUCellThresholds.txt", row.names=F, quote=F, sep="\t")
## ----sessionInfo---------------------------------------------------------
date()
sessionInfo()