forked from normjam/controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoNorm_trajectory_example.R
145 lines (111 loc) · 4.48 KB
/
noNorm_trajectory_example.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
## No normalization showing trajectory is driven by Sequencing Depth.
setwd("~/Desktop/normjam/")
da1 = readRDS("pbmc3k.rds")
rdat <- data.matrix(GetAssayData(object = da1, slot = "counts"))
library(scater)
rdat <- SingleCellExperiment(assays = list(counts = rdat, logcounts=log(rdat+1)))
rdat <- calculateQCMetrics(rdat)
rdat <- runPCA(rdat)
set.seed(99)
rdat <- runUMAP(rdat)
plotReducedDim(rdat, use_dimred = "UMAP", colour_by="log10_total_counts")
library(scran)
rdat.norm2 <- computeSumFactors(rdat)
rdat.norm2 <- normalize(rdat.norm2)
rdat.norm2 <- runPCA(rdat.norm2)
set.seed(99)
rdat.norm2 <- runUMAP(rdat.norm2)
p1 <- plotReducedDim(rdat, use_dimred = "UMAP", colour_by="log10_total_counts")
p2 <- plotReducedDim(rdat.norm2, use_dimred = "UMAP", colour_by="log10_total_counts")
library(gridExtra)
pdf("fulldata_umap.pdf", height=3, width=8)
grid.arrange(p1, p2, nrow = 1)
dev.off()
### Focus on a specific cluster to show the trajectory
sdat <- rdat[,which(rdat@reducedDims$UMAP[,1] > -5 & rdat@reducedDims$UMAP[,2] > 10)]
dim(subt)
set.seed(99)
sdat <- runPCA(sdat)
sdat <- runUMAP(sdat)
p1 <- plotReducedDim(sdat, use_dimred = "UMAP", colour_by="log10_total_counts")
library(scran)
sdat.norm2 <- rdat.norm2[,which(rdat@reducedDims$UMAP[,1] > -5 & rdat@reducedDims$UMAP[,2] > 10)]
sdat.norm2 <- runPCA(sdat.norm2)
set.seed(99)
sdat.norm2 <- runUMAP(sdat.norm2)
p2 <- plotReducedDim(sdat.norm2, use_dimred = "UMAP", colour_by="log10_total_counts")
library(gridExtra)
pdf("subdata_umap.pdf", height=3, width=8)
grid.arrange(p1, p2, nrow = 1)
dev.off()
library(slingshot)
sdat.sling <- slingshot(sdat, reducedDim = 'UMAP')
XX = plotReducedDim(sdat, use_dimred = "UMAP", colour_by="log10_total_counts")
CURVE = data.frame(SlingshotDataSet(sdat.sling)@curves$curve1$s)
colnames(CURVE) <- c("x", "y")
CURVE <- CURVE[SlingshotDataSet(sdat.sling)@curves$curve1$ord,]
p1 <- XX + geom_segment(CURVE, mapping = aes(x = x, xend = dplyr::lead(x), y = y, yend = dplyr::lead(y)),
size = 0.5, color='black')
sdat.norm2.sling <- slingshot(sdat.norm2, reducedDim = 'UMAP')
XX = plotReducedDim(sdat.norm2, use_dimred = "UMAP", colour_by="log10_total_counts")
CURVE.norm = data.frame(SlingshotDataSet(sdat.norm2.sling)@curves$curve1$s)
colnames(CURVE.norm) <- c("x", "y")
CURVE.norm <- CURVE.norm[SlingshotDataSet(sdat.norm2.sling)@curves$curve1$ord,]
CURVE.norm <- unique(CURVE.norm)
p2 <- XX + geom_segment(CURVE.norm, mapping = aes(x = x,
xend = dplyr::lead(x),
y = y, yend = dplyr::lead(y)),
size = 0.5, color='black')
pdf("subdata_slingshot.pdf", height=3, width=8)
grid.arrange(p1, p2, nrow = 1)
dev.off()
plot(reducedDims(sdat.norm2.sling)$UMAP, asp = 1, pch = 16)
lines(SlingshotDataSet(sdat.norm2.sling), lwd = 3, col = 'black')
t1 <- sdat.sling$slingPseudotime_1
# for time, only look at the 100 most variable genes
Y <- (assays(sdat.sling)$logcounts)
Y <- Y[which(apply(Y, 1, function(x) sum(x!=0)) > 0),]
library(gam)
# fit a GAM with a loess term for pseudotime
gam.pval <- apply(Y,1,function(z){
d <- data.frame(z=z, t=t1)
suppressWarnings({
tmp <- suppressWarnings(gam(z ~ lo(t1), data=d))
})
p <- summary(tmp)[3][[1]][2,3]
p
})
t1 <- sdat.norm2.sling$slingPseudotime_1
# for time, only look at the 100 most variable genes
Y <- (assays(sdat.norm2.sling)$logcounts)
Y <- Y[which(apply(Y, 1, function(x) sum(x!=0)) > 0),]
library(gam)
# fit a GAM with a loess term for pseudotime
gam.pval.norm <- apply(Y,1,function(z){
d <- data.frame(z=z, t=t1)
suppressWarnings({
tmp <- suppressWarnings(gam(z ~ lo(t1), data=d))
})
p <- summary(tmp)[3][[1]][2,3]
p
})
gam.pval.adj <- p.adjust(gam.pval, method = 'fdr')
gam.pval.norm.adj <- p.adjust(gam.pval.norm, method = 'fdr')
sum(gam.pval.adj < .05)
sum(gam.pval.norm.adj < .05)
X = names(gam.pval.adj[gam.pval.adj < .05])
Y = names(gam.pval.adj[gam.pval.norm.adj < .05])
write.table(X, file="~/sig_nonorm.txt", quote=F, row.names = F, col.names = F)
write.table(Y, file="~/sig_withnorm.txt", quote=F, row.names = F, col.names = F)
#
# ### Try to focus on really different depths...
#
# subt <- rdat[,which(rdat@reducedDims$UMAP[,1] > -5 & rdat@reducedDims$UMAP[,2] > 10)]
# subt <- subt[,which(subt$log10_total_counts > 3.4 | subt$log10_total_counts < 3.1)]
# dim(subt)
# set.seed(99)
# sdat <- runPCA(sdat)
# sdat <- runUMAP(sdat)
# plotReducedDim(sdat, use_dimred = "UMAP", colour_by="log10_total_counts")
#
# ## Doesn't break apart as two clusters.