|
a |
|
b/R/part02.R |
|
|
1 |
################################################################################ |
|
|
2 |
# Function which: |
|
|
3 |
# - calculates the correlations between drug response profiles |
|
|
4 |
# - produces correlation matrix (either symmetric or 2 combined*) |
|
|
5 |
# |
|
|
6 |
# * for example, for two sets of samples coming from patients with different |
|
|
7 |
# diagnosis |
|
|
8 |
################################################################################ |
|
|
9 |
|
|
|
10 |
makeCorrHeatmap = function(mt, mt2=NA, colsc, concNo="one", |
|
|
11 |
ctab=BloodCancerMultiOmics2017::conctab, |
|
|
12 |
dtab=BloodCancerMultiOmics2017::drugs) { |
|
|
13 |
|
|
|
14 |
# quiets concerns of R CMD check "no visible binding for global variable" |
|
|
15 |
NameX=NULL; NameY=NULL; Measure=NULL; x=NULL; y=NULL; xend=NULL; |
|
|
16 |
yend=NULL |
|
|
17 |
|
|
|
18 |
# Function which reorders the clustered matrix based on |
|
|
19 |
# computed singular value decomposition. |
|
|
20 |
callback = function(hc, mat){ |
|
|
21 |
sv = svd(t(mat))$v[,1] |
|
|
22 |
dend = reorder(as.dendrogram(hc), wts = sv) |
|
|
23 |
rv <- as.hclust(dend) |
|
|
24 |
rv |
|
|
25 |
} |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
# cluster the drugs |
|
|
29 |
mt = mt[apply(mt, 1, sd)!=0, ] |
|
|
30 |
mt = cor(t(mt)) |
|
|
31 |
hc = hclust(as.dist(1-mt)) |
|
|
32 |
hc = callback(hc, mt) |
|
|
33 |
ord = rev(hc$label[hc$order]) |
|
|
34 |
|
|
|
35 |
# prepare data in LF |
|
|
36 |
mt = meltWholeDF(mt) |
|
|
37 |
mt$NameX = factor(mt$Y, levels=rev(ord)) |
|
|
38 |
if(concNo=="one") { |
|
|
39 |
mt$NameY = factor(dtab[mt$X,"name"], levels=dtab[ord,"name"]) |
|
|
40 |
} else if(concNo=="all") { |
|
|
41 |
mt$NameY = factor(giveDrugLabel(mt$X, ctab, dtab), |
|
|
42 |
levels=giveDrugLabel(ord, ctab, dtab)) |
|
|
43 |
} |
|
|
44 |
|
|
|
45 |
# take care of the secoond heatmap if present |
|
|
46 |
if(!is.na(mt2)[1]) { |
|
|
47 |
# obtain the correlation coefficients |
|
|
48 |
mt2 = mt2[apply(mt2, 1, sd)!=0, ] |
|
|
49 |
mt2 = cor(t(mt2)) |
|
|
50 |
|
|
|
51 |
# order the matrix in the same way as mt is ordered |
|
|
52 |
mt2 = mt2[levels(mt$NameX), levels(mt$NameX)] |
|
|
53 |
# put 0 in lower triangle of matrix, include the diagonal |
|
|
54 |
mt2[upper.tri(mt2, diag=TRUE)] = NA |
|
|
55 |
|
|
|
56 |
mt2 = meltWholeDF(mt2) |
|
|
57 |
# remove NA rows |
|
|
58 |
mt2 = mt2[!is.na(mt2$Measure),] |
|
|
59 |
# insert top half of matrix data to plotting data frame |
|
|
60 |
idx = match(paste(mt2$X, mt2$Y, sep="."), paste(mt$X, mt$Y, sep=".")) |
|
|
61 |
mt[idx, "Measure"] = mt2$Measure |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
# color the drug names on the axis |
|
|
65 |
pathColor = pathColor |
|
|
66 |
pathColor["Other"] = "black" |
|
|
67 |
if(concNo=="one") { |
|
|
68 |
drcol = unname(pathColor[match(dtab[rev(levels(mt$NameX)),"pathway"], |
|
|
69 |
names(pathColor))]) |
|
|
70 |
} else if(concNo=="all") { |
|
|
71 |
drcol = unname(pathColor[match(dtab[rev(substring(levels(mt$NameX), 1, 5)), |
|
|
72 |
"pathway"], names(pathColor))]) |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
# construct the main plot |
|
|
76 |
mainPlot = ggplot() + |
|
|
77 |
geom_tile(data=mt, aes(x=NameX, y=NameY, fill=Measure)) + |
|
|
78 |
theme_bw() + |
|
|
79 |
theme(axis.text.y = element_text(size=14, colour=drcol), |
|
|
80 |
panel.background=element_rect(fill=NA), |
|
|
81 |
legend.title=element_text(size=14), |
|
|
82 |
legend.text=element_text(size=14)) + |
|
|
83 |
scale_fill_gradientn(name="Correlation coefficient", limits=c(-1,1), |
|
|
84 |
colours=colsc) + |
|
|
85 |
xlab("") + ylab("") + coord_fixed() |
|
|
86 |
|
|
|
87 |
# construct the dendrogram |
|
|
88 |
dhc = as.dendrogram(hc) |
|
|
89 |
ddata = dendro_data(dhc, type = "rectangle") |
|
|
90 |
ddata$segments = ddata$segments - 0.5 |
|
|
91 |
nXY = nrow(ddata$labels) |
|
|
92 |
dendPlot = ggplot(segment(ddata)) + |
|
|
93 |
geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) + |
|
|
94 |
theme_dendro() + |
|
|
95 |
scale_x_continuous(limits=c(0, nXY), expand = c(0,0)) + |
|
|
96 |
scale_y_reverse() |
|
|
97 |
|
|
|
98 |
# construct annotation for drug labels |
|
|
99 |
annoPlot = ggplot(data.frame(x=1, y=factor(names(pathColor), |
|
|
100 |
levels=names(pathColor))), |
|
|
101 |
aes(x, y, fill=y)) + |
|
|
102 |
geom_tile() + |
|
|
103 |
scale_fill_manual("Targeted pathway", values=pathColor) + |
|
|
104 |
theme(legend.title=element_text(size=14), legend.text=element_text(size=14)) |
|
|
105 |
|
|
|
106 |
# construct gtable |
|
|
107 |
wdths = c(3, 0.22*nXY, 0.1) |
|
|
108 |
hghts = c(0.1, 0.22*nXY, 1) |
|
|
109 |
|
|
|
110 |
gt = gtable(widths=unit(wdths, "in"), heights=unit(hghts, "in")) |
|
|
111 |
|
|
|
112 |
# make grobs |
|
|
113 |
heat = ggplotGrob(mainPlot) |
|
|
114 |
denX = ggplotGrob(dendPlot) |
|
|
115 |
anno = ggplotGrob(annoPlot) |
|
|
116 |
|
|
|
117 |
# fill in the gtable |
|
|
118 |
gt = gtable_add_grob(gt, gtable_filter(heat, "panel"), 2, 2) |
|
|
119 |
gt = gtable_add_grob(gt, denX$grobs[[whichInGrob(denX, "panel")]], 3, 2) |
|
|
120 |
gt = gtable_add_grob(gt, heat$grobs[[whichInGrob(heat, "axis-l")]], 2, 1) |
|
|
121 |
|
|
|
122 |
# gtable with legends |
|
|
123 |
wdthsl = c(3, 3) |
|
|
124 |
hghtsl = c(2.5) |
|
|
125 |
gtl = gtable(widths=unit(wdthsl, "in"), heights=unit(hghtsl, "in")) |
|
|
126 |
gtl = gtable_add_grob(gtl, gtable_filter(heat, "guide-box"), 1, 1) |
|
|
127 |
gtl = gtable_add_grob(gtl, gtable_filter(anno, "guide-box"), 1, 2) |
|
|
128 |
|
|
|
129 |
return(list("figure"=list(width=sum(wdths), height=sum(hghts), plot=gt), |
|
|
130 |
"legend"=list(width=sum(wdthsl), height=sum(hghtsl), plot=gtl))) |
|
|
131 |
} |