-
Notifications
You must be signed in to change notification settings - Fork 1
/
my.seurat.DE.R
106 lines (90 loc) · 4.72 KB
/
my.seurat.DE.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
my.seurat.FindAllMarkers <-function(object, identity="RNA_snn_res.0.5", name="sample",
outpath.prefix,
min.pct=0.25,
logfc.threshold=0.25,
pval.thres=0.01,
adjpval.thres=0.005,...){
# 0. setting clustering information and do DEG analysis
Idents(object)<-identity
tic("Perform differential expression analysis")
markers <- FindAllMarkers(object, only.pos = FALSE, min.pct = min.pct,
logfc.threshold = logfc.threshold, return.thresh = pval.thres)
toc()
# 1. filtering for significant marker genes (all, positive only, and negative only)
markers.sig <- markers[ markers$p_val_adj<adjpval.thres , ]
markers.sig["abs_avg_logFC"] <- abs( markers.sig["avg_logFC"] )
markers.sigpos <- markers.sig[ markers.sig$"avg_logFC">0, ]
markers.signeg <- markers.sig[ markers.sig$"avg_logFC"<0, ]
markers.sig <- markers.sig[ order(markers.sig$cluster, -markers.sig$abs_avg_logFC), ]
markers.sigpos <- markers.sigpos[order(markers.sigpos$cluster, -markers.sigpos$avg_logFC) , ]
markers.signeg <- markers.signeg[order(markers.signeg$cluster, markers.signeg$avg_logFC) , ]
# 2. store all marker genes to file
fp_allmarkerlist <- paste0(outpath.prefix,
"/DEG.", name, ".", identity, "_",
"all.csv", sep = "")
write.csv(markers.sig, file = fp_allmarkerlist)
print(paste("Write to ",fp_allmarkerlist))
fp_posmarkerlist <- paste0(outpath.prefix,
"/DEG.", name, ".", identity, "_",
"pos.csv", sep = "")
write.csv(markers.sigpos, file = fp_posmarkerlist)
print(paste("Write to ",fp_posmarkerlist))
fp_negmarkerlist <- paste0(outpath.prefix,
"/DEG.", name, ".", identity, "_",
"neg.csv", sep = "")
write.csv(markers.signeg, file = fp_negmarkerlist)
print(paste("Write to ",fp_negmarkerlist))
# 3. prepare a list object to return
DE.result <- list()
DE.result[["all_markers"]] <- markers.sig
DE.result[["positive_markers"]] <- markers.sigpos
DE.result[["negative_markers"]] <- markers.signeg
return(DE.result)
}
my.seurat.DEGplot <- function(object, DE.result, w, h, outpath.prefix,
identity="RNA_snn_res.0.5", name="sample",
given.identity.order=NULL, topNGenes=40L,...){
library("dplyr")
DE.result[["all_markers"]] -> markers.sig
DE.result[["positive_markers"]] -> markers.sigpos
DE.result[["negative_markers"]] -> markers.signeg
markers.sig.top <- markers.sig[ order( markers.sig$cluster ,
-markers.sig$abs_avg_logFC), ] %>%
group_by(cluster) %>% top_n(n = topNGenes, wt = abs_avg_logFC)
markers.sigpos.top <- markers.sigpos[ order( markers.sigpos$cluster ,
-markers.sigpos$avg_logFC), ] %>%
group_by(cluster) %>% top_n(n = topNGenes, wt = avg_logFC)
markers.signeg.top <- markers.signeg[ order( markers.signeg$cluster ,
-markers.signeg$avg_logFC), ] %>%
group_by(cluster) %>% top_n(n = topNGenes, wt = avg_logFC)
# ---------- Draw all markers on heatmap --------------
fp <- paste0(outpath.prefix,
"/DEG_HM.", name, ".", identity, "_",
"allMarkers.pdf", sep = "")
tic("rendering")
pdf(fp, width=w, height=h)
my.seurat.heatmap(object, genes=markers.sig.top$gene, given.identity.order=given.identity.order,
annotation_height=2, group.by = identity,...)
dev.off()
toc()
# ---------- Draw positive markers on heatmap ----------
fp <- paste0(outpath.prefix,
"/DEG_HM.", name, ".", identity, "_",
"posMarkers.pdf", sep = "")
tic("rendering")
pdf(fp, width=w, height=h)
my.seurat.heatmap(object, genes=markers.sigpos.top$gene, given.identity.order=given.identity.order,
annotation_height=2, group.by = identity,...)
dev.off()
toc()
# ---------- Draw negative markers on heatmap ----------
fp <- paste0(outpath.prefix,
"/DEG_HM.", name, ".", identity, "_",
"negMarkers.pdf", sep = "")
tic("rendering")
pdf(fp, width=w, height=h)
my.seurat.heatmap(object, genes=markers.signeg.top$gene, given.identity.order=given.identity.order,
annotation_height=2, group.by = identity,...)
dev.off()
toc()
}