-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.R
212 lines (198 loc) · 7.73 KB
/
plotting.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# to be sourced
library(sROC)
library(ggplot2)
library(grid)
library(gridExtra)
library(reshape2)
source("utilities.R")
# data and utilities for plotting cluster profiles
magenta <- "#FF00FF"
cyan <- "#00FFFF"
grey <- "#AAAAAA"
yellow <- "#FFD800"
# get the 95% CI
cdf2cimin <- function(mycdf) {
mycdf$x[which.min(abs(mycdf$Fhat - 0.05))]
}
cdf2cimax <- function(mycdf) {
mycdf$x[which.min(abs(mycdf$Fhat - 0.95))]
}
distsum.plot <- function(kdsdf) {
ggplot(kdsdf, aes(x = k, y = distsum)) +
geom_bar(stat = "identity") +
xlab("Input k") +
ylab("Sum of within cluster distances") +
theme_bw()
}
distsum.delta.plot <- function(kdsdf) {
ggplot(kdsdf, aes(x = k, y = distsum.delta)) +
geom_bar(stat = "identity") +
xlab("Input k") +
ylab("Difference of sum of within\ncluster distances to next smaller k") +
theme_bw()
}
cluster.size.plot <- function(kclust) {
csdf <- as.data.frame(table(kclust@cluster))
ggplot(csdf, aes(x = Var1, y = Freq)) +
geom_bar(stat = "identity") +
ylab("Cluster size") +
xlab("Cluster index") +
theme_bw()
}
makeClusterProfilePlot <- function(profile.data, title, y.range.adj = 0, simple = F, focus = F,
display.motif.gene.profile = F, motifs = F, motif.colors = F,
display.tracks = F, tracks = F, alt.sample.names = F) {
pd.min <- min(profile.data)
pd.max <- max(profile.data)
cmin<-apply(profile.data, 2, min)
cmean<-apply(profile.data, 2, mean)
cmax<-apply(profile.data, 2, max)
if (identical(simple, F)) {
if (dim(profile.data)[1] > 4) {
# estimate CDF from data
cis <- apply(profile.data, 2, kCDF)
# get min at 95% CI
lwr <- sapply(cis, cdf2cimin)
# get max at 95% CI
upr <- sapply(cis, cdf2cimax)
} else {
lwr <- NA
upr <- NA
}
# cluster data frame
clustdf <- data.frame(min = cmin, max = cmax, mean = cmean, lwr = lwr, upr = upr, Sample = names(cmean))
clustdf$Sample <- factor(clustdf$Sample, levels = names(cmean))
myplot <- ggplot(clustdf, aes(x = Sample))
if (!is.null(title)) {
myplot <- myplot + ggtitle(title)
}
myplot <- myplot +
ylab("Log2(Sample / T0)\nNormalized counts")
if (!is.na(lwr) && !is.na(upr)) {
myplot <- myplot +
geom_ribbon(aes(ymax = upr, ymin = lwr, group = 1, alpha=0.7), colour = magenta)
}
myplot <- myplot +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
legend.position = "none",
plot.margin = unit(c(0,0.5,0,0), "lines")
)
if (!identical(alt.sample.names, F)) {
myplot <- myplot + scale_x_discrete(labels=alt.sample.names)
}
} else {
clustdf <- data.frame(min = cmin, max = cmax, mean = cmean, Sample = names(cmean))
clustdf$Sample <- factor(clustdf$Sample, levels = names(cmean))
# cluster label for center of plot
clusterGrob <- grobTree(textGrob(title, x = .5, y = .5, gp = gpar(col = "black", fontsize=30)))
clusterGrobGenes <- grobTree(textGrob(paste(length(profile.data[,1]), "genes"), x = .5, y = .1, gp = gpar(col = "black", fontsize = 15)))
myplot <- ggplot(clustdf, aes(x = Sample)) +
theme_bw() +
theme(axis.title.x = element_blank(), axis.title.y = element_blank(), axis.ticks = element_blank(), axis.text.x = element_blank()) +
annotation_custom(clusterGrob) +
annotation_custom(clusterGrobGenes) +
ylim(pd.min - y.range.adj, pd.max + y.range.adj)
}
if (!identical(focus, F)) {
focusdf <- data.frame(t(profile.data[focus,]), Sample = names(profile.data))
focusdf$Sample <- factor(focusdf$Sample, levels = names(profile.data))
mdf <- melt(focusdf, id.vars = c("Sample"))
myplot <- myplot +
geom_point(data = mdf, aes(x = Sample, y = value, group = variable), color = yellow) +
geom_line(data = mdf, aes(x = Sample, y = value, group = variable), color = yellow)
}
# for each motif in display.motif.gene.profile vector use the provided motif data to show profiles
# using the colors in motif.colors
# only attempt after some basic binary checks on variables
if (!identical(display.motif.gene.profile, F) && length(display.motif.gene.profile) > 0
&& !identical(motifs, F) && length(motifs) > 0
&& !identical(motif.colors, F) && length(motif.colors) > 0) {
for (m in 1:length(display.motif.gene.profile)) {
motif <- motifs[[display.motif.gene.profile[m]]]
motifdf <- data.frame(t(profile.data[as.character(motif$positions$gene),]), Sample = names(profile.data))
motifdf$Sample <- factor(names(profile.data), levels = names(profile.data))
mdf <- melt(motifdf, id.vars = c("Sample"))
myplot <- myplot +
geom_point(data = mdf,
aes(x = Sample, y = value, group = variable),
color = motif.colors[display.motif.gene.profile[m]]
) +
geom_line(data = mdf,
aes(x = Sample, y = value, group = variable),
color = motif.colors[display.motif.gene.profile[m]]
)
}
}
# show min, mean and max
myplot <- myplot +
geom_point(aes(y = mean), colour = cyan) +
geom_line(aes(x = Sample, y = mean, group = 1), colour = cyan) +
geom_line(aes(x = Sample, y = min, group = 1), colour = grey) +
geom_line(aes(x = Sample, y = max, group = 1), colour = grey)
# for each track in display.tracks plot the track above the profile
if (identical(simple, F) && !identical(display.tracks, F) && length(display.tracks) > 0
&& !identical(tracks, F) && length(tracks) > 0) {
track.data <- data.frame(cbind(tracks[env$samples$ordering, display.tracks]), row.names=env$samples$ordering)
names(track.data) <- display.tracks
track.data$Sample <- factor(rownames(track.data), levels = rownames(track.data))
#print(head(track.data))
trackplot <- ggplot(track.data, aes(x = Sample))
for (y in display.tracks) {
trackplot <- trackplot + geom_bar(stat = "identity", aes_string(y = y))
}
trackplot <- trackplot +
theme_bw() +
theme(axis.title.x = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
plot.margin = unit(c(0,0.5,0,0.05), "lines")
)
ggplot_gtable(ggplot_build(trackplot))
ggplot_gtable(ggplot_build(myplot))
#max.width = unit.pmax(trackplot$widths[2:3], myplot$widths[2:3])
#trackplot$widths[2:3] <- max.width
#myplot$widths[2:3] <- max.width
return(grid.arrange(trackplot, myplot, heights=c(1,5)))
}
return(myplot)
}
renderMotifPlots <- function(dir, genes, upstream.seqs, upstream.start, upstream.end, motifs, motif.colors, msc = F) {
dir.create(dir, recursive = T, showWarnings = F)
if (identical(msc, F)) {
msc <- meme.positions.to.sites(motifs, upstream.seqs, upstream.start)
}
names(motif.colors) <- levels(msc$motif)
for (g in levels(msc$gene)) {
mscg <- msc[msc$gene == g,]
gene_file <- paste(dir, paste(g, "png", sep = "."), sep = "/")
png(filename = gene_file, width = 180, height = 18)
ul <- upstream.seqs[as.character(g),"uplength"]
sline <- data.frame(x = c(upstream.start - ul, upstream.start), y = c(.5,.5))
print(
ggplot(mscg) +
scale_fill_manual(name = "nmotif", values = motif.colors) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin=0, ymax = 1, fill = motif, group = 1)) +
geom_line(data=sline, aes(x = x, y = y, group = 1), color = "#000000") +
theme(
legend.position = "none",
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin = unit(0,"null"),
plot.margin = rep(unit(0,"null"),4),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.length = unit(0,"null"),
axis.ticks.margin = unit(0,"null")
) +
labs(x = NULL, y = NULL) +
xlim(upstream.end - 1, upstream.start + 1) + ylim(0,1)
)
dev.off()
}
return(msc)
}