Diff of /R/part04.R [000000] .. [226bc8]

Switch to unified view

a b/R/part04.R
1
################################################################################
2
# VOLCANO PLOTS
3
################################################################################
4
5
#_______________________________________________________________________________
6
## color by number of sign concentration steps / one dot per drug
7
#-------------------------------------------------------------------------------
8
9
## MAIN FUNCTION
10
ggvolc2 = function(df, title, Ycut, color=NA, xlab, maxX, maxY, expY, hghBox,
11
                   axisMarkY, breaksX, arrowLength, Xhang, minConc, fixedHght) {
12
13
  # quiets concerns of R CMD check "no visible binding for global variable"
14
  X=NULL; Y=NULL; labX=NULL; labY=NULL; Label=NULL;
15
  hjust=NULL; x=NULL; y=NULL; xend=NULL; yend=NULL;
16
  Color=NULL; .x=NULL; Fill=NULL                 
17
                   
18
  # color palette
19
  pal = setNames(rev(c(brewer.pal(11, "RdYlBu")[1:5], "#000000",
20
                       brewer.pal(11, "RdBu")[7:11])), nm=1:11)
21
22
  # check if dataq.frame have required columns
23
  stopifnot(all(c("X","Y","Label","Grey") %in% colnames(df)))
24
25
  # check if colors are for the palette # if not use the default
26
  col.default = c(pal[2], pal[9])
27
  color = if(all(color %in% colors())) color else  col.default
28
29
  # x axis
30
  Xlims = c(-maxX, maxX)
31
32
  # y axis
33
  Ylims = c(-0.05, maxY)
34
35
  # direction of the effect
36
  df$Direction = 0
37
  df$Direction[df$Y>=Ycut & df$X<0] = -1
38
  df$Direction[df$Y>=Ycut & df$X>0] = 1
39
40
  # add column saying if association is significant
41
  df$IsSignificant = df$Y>=Ycut & !df$Grey
42
43
  # decide what should have label
44
  df$haveLabel = df$IsSignificant & df$signConc >= minConc
45
46
  # positions for labels
47
  calcY = function(y.org) {
48
    rng = c(Ycut+0.1,maxY-0.1)
49
    if(length(y.org)==1) {
50
      y.org = rng[1]+(rng[2]-rng[1])/2
51
    } else {
52
      inc = (rng[2]-rng[1])/length(y.org)
53
      newY = rng[1]+inc*(1:length(y.org))
54
      y.org[order(y.org)] = newY
55
    }
56
    y.org
57
  }
58
  df$labY[df$haveLabel] = calcY(y.org=df$Y[df$haveLabel])
59
  df$labX = with(df, ifelse(haveLabel,
60
                            ifelse(Direction==1, Xlims[2]-Xhang,
61
                                   Xlims[1]+Xhang), NA))
62
  df$hjust = ifelse(sign(df$labX)==1, 0, 1)
63
  labNo = length(df$Y[df$haveLabel])
64
  df$Color = factor(6+(df$signConc*df$Direction), levels=1:11)
65
  # make grey dots really grey
66
  df$Color[df$Grey] = 6
67
68
  # construct the plot
69
  if(sum(df$haveLabel)>0) {
70
    # construct the labels
71
    df2 = df[df$haveLabel,]
72
    gg = ggplot() +
73
      geom_segment(data=df2, aes(x=X, y=Y, xend=labX, yend=labY),
74
                   colour="darkgrey", alpha=0.7, linetype="longdash", size=0.1) +
75
      geom_text(data=df2, mapping=aes(x=labX, y=labY, label=Label, hjust=hjust),
76
                size=2.5) # dotted, size=2.5
77
    # and arrows
78
    gg = gg +
79
      geom_segment(data=data.frame(x=0, y=0, xend=-arrowLength, yend=0),
80
                   aes(x=x,y=y,xend=xend,yend=yend),
81
                   arrow=arrow(length = unit(0.25, "cm"), type="open"),
82
                   colour=color[1], size=0.8) +
83
      geom_segment(data=data.frame(x=0, y=0, xend=arrowLength, yend=0),
84
                   aes(x=x,y=y,xend=xend,yend=yend),
85
                   arrow=arrow(length = unit(0.25, "cm"), type="open"),
86
                   colour=color[2], size=0.8)
87
  } else {
88
    gg = ggplot()
89
  }
90
  gg = gg + geom_hline(yintercept=Ycut, colour="dimgrey", linetype="dashed") +
91
    geom_vline(xintercept=0, colour="dimgrey", size=0.1) +
92
    geom_point(data=df, aes(x=X, y=Y, fill=Color, color=Color), shape=21,
93
               size=3) +
94
    scale_fill_manual(values=col2hex(pal, alpha=0.7)) +
95
    scale_color_manual(values=pal) + theme_bw() +
96
    xlab(xlab) + ggtitle(title) +
97
    scale_y_continuous(
98
      expression(italic(p)*"-value"), breaks=seq(0,maxY,axisMarkY),
99
      labels=math_format(expr=10^.x)(-seq(0,maxY,axisMarkY)), limits=Ylims,
100
      expand = c(expY,0)) +
101
    scale_x_continuous(limits=Xlims, breaks=breaksX, labels=percentAxisScale) +
102
    theme(axis.text.y=element_text(size=8), axis.text.x=element_text(size=8),
103
          axis.title.x=element_text(size=8), axis.title.y=element_text(size=8),
104
          plot.title= element_text(size=8))
105
106
  # construct the gtable
107
  wdths = c(0.2, 0.4, 3.5*(Xlims[2]-Xlims[1]), 0.2)
108
  hghts = c(0.3, ifelse(is.na(fixedHght), hghBox*nrow(df2), fixedHght), 0.3, 0.2)
109
  gt = gtable(widths=unit(wdths, "in"), heights=unit(hghts, "in"))
110
  ## make grobs
111
  ggr = ggplotGrob(gg)
112
  ## fill in the gtable
113
  gt = gtable_add_grob(gt, gtable_filter(ggr, "panel"), 2, 3)
114
  gt = gtable_add_grob(gt, ggr$grobs[[whichInGrob(ggr, "axis-l")]], 2, 2)
115
  gt = gtable_add_grob(gt, ggr$grobs[[whichInGrob(ggr, "axis-b")]], 3, 3)
116
  gt = gtable_add_grob(gt, ggr$grobs[[whichInGrob(ggr, "xlab-b")]], 4, 3)
117
  gt = gtable_add_grob(gt, ggr$grobs[[whichInGrob(ggr, "ylab-l")]], 2, 1)
118
  gt = gtable_add_grob(gt, gtable_filter(ggr, "title"), 1, 3) # title
119
120
  # legend # it should adjust depending on minConc
121
  pal = setNames(pal[-ceiling(length(pal)/2)], nm=c(-5:-1,1:5))
122
123
  fakeDF = data.frame(X=1:length(pal), Y=LETTERS[1:length(pal)], Fill=names(pal))
124
125
  gl = ggplot() +
126
    geom_bar(data=fakeDF, aes(x=X, y=Y, fill=Fill),
127
             stat="identity", position="identity") +
128
    scale_fill_manual(name="Number of\nsignificant\nconcentrations",
129
                      values=pal,
130
                      labels=c(`-5`="", `-4`="", `-3`="", `-2`="", `-1`="",
131
                               `1`="1 conc.", `2`="2 conc.", `3`="3 conc.",
132
                               `4`="4 conc.", `5`="5 conc"), drop = FALSE) +
133
    guides(fill=guide_legend(ncol=2)) +
134
    theme(legend.text=element_text(size=8),
135
          legend.title=element_text(size=8, face="bold"),
136
          legend.title.align=0.5)
137
138
  # construct the gtable
139
  wdthsL = c(2)
140
  hghtsL = 2
141
  gtL = gtable(widths=unit(wdthsL, "in"), heights=unit(hghtsL, "in"))
142
  ## make grobs
143
  ggl = ggplotGrob(gl)
144
  ## fill in the gtable
145
  gtL = gtable_add_grob(gtL, ggl$grobs[[whichInGrob(ggl, "guide-box-right")]], 1, 1)
146
147
  return(list("figure"=list(width=sum(wdths), height=sum(hghts), plot=gt),
148
              "legend"=list(width=sum(wdthsL), height=sum(hghtsL), plot=gtL)))
149
}
150
151
152
## RUNNING FUNCTION
153
154
run.ggvolcGr2 = function(results, effects, screen, mts, fdr, maxX, maxY,
155
                         expY, hghBox, axisMarkY, breaksX, arrowLength,
156
                         Xhang, minConc, dtab=BloodCancerMultiOmics2017::drugs, fixedHght=NA) {
157
158
  # select appropriate data to plot
159
  results = results[[screen]]
160
  effects = effects[[screen]]
161
162
  # merge the results and effects
163
  reseff = merge(results, effects, by=c("DrugID","TestFac","FacDr"))
164
165
  # filter the data to only those lines which will be plotted
166
  reseff = reseff[reseff$TestFac %in% mts,]
167
168
  # mark significant cases
169
  reseff$fdr = reseff$adj.pval <= fdr
170
  # find out the p-value threshold
171
  cut = max(reseff$pval[reseff$fdr])
172
173
  # iterate on each mutation
174
  waste = tapply(1:nrow(reseff), reseff$TestFac, function(idx) {
175
    # mutation to be plotted
176
    mt = reseff[idx[1], "TestFac"]
177
    # select the data to be plotted
178
    re = reseff[idx,]
179
    ## for each drug select the association with the biggest difference in effects
180
    re = do.call(rbind, tapply(1:nrow(re), re$DrugID2, function(i) {
181
      tmp = re[i,]
182
      if(all(tmp$fdr==FALSE)) {
183
        return(cbind(tmp[which.max(abs(tmp$WM)),], signConc=0))
184
      } else {
185
        tmp = tmp[tmp$fdr,]
186
        return(cbind(tmp[which.max(abs(tmp$WM)),], signConc=nrow(tmp)))
187
      }
188
    }))
189
190
    # create input data frame
191
    plotDF = with(re, data.frame(X=(-1)*WM, Y=-log10(pval),
192
                                 Label=dtab[DrugID2,"name"],
193
                                 Grey= mean.0 <0.1 & mean.1 <0.1 & fdr,
194
                                 signConc))
195
    # maxY
196
    if(is.na(maxY)) maxY=max(ceiling(plotDF$Y))
197
    # additional paremeters (labels, colors)
198
    xlab = "Difference of effects"
199
    # run the plotting function
200
    ggvolc2(df=plotDF, title=mt, Ycut=-log10(cut), color=NA, xlab=xlab,
201
            maxX=maxX, maxY=maxY, expY=expY, hghBox=hghBox,
202
            axisMarkY=axisMarkY, breaksX=breaksX, arrowLength=arrowLength,
203
            Xhang=Xhang, minConc=minConc, fixedHght=fixedHght)
204
  })
205
206
  # names(waste) = paste(names(waste), screen, sep=".")
207
  waste
208
}
209
210
211
################################################################################
212
# HEAT MAP
213
################################################################################
214
215
ggheat = function(results, effects, dtab=BloodCancerMultiOmics2017::drugs, ctab=BloodCancerMultiOmics2017::conctab) {
216
217
  # quiets concerns of R CMD check "no visible binding for global variable"
218
  diag.conc=NULL; Drug2=NULL; Fill=NULL; x=NULL; fill=NULL
219
220
  # COLORS
221
  # color palette
222
  pal = c(brewer.pal(11, "PiYG")[2], brewer.pal(11, "RdBu")[10]) # only 2 colors!
223
  # border of whole plot
224
  cPanb = "black"
225
  sPanb = 0.5
226
  # minor & major grid
227
  cGrid = c("grey", "dimgrey")
228
229
  # apply the FDR threshold
230
  FDR = 0.1
231
232
  plotDF = do.call(rbind, lapply(names(results), function(nm) {
233
    df = merge(results[[nm]], effects[[nm]], by=c("FacDr", "TestFac", "DrugID"))
234
    df$plot.pval = ifelse(df$adj.pval <= FDR, df$pval*sign(df$WM), 1)
235
    df$plot.simple.pval = ifelse(df$adj.pval <= FDR, 1*sign(df$WM), 0)
236
    df$plot.simple.pval = factor(df$plot.simple.pval, levels=c(-1,1,0))
237
    df
238
  }))
239
240
  # add column which orders the coulumns of the plot
241
  plotDF$diag = sapply(plotDF$TestFac, function(tf)
242
    strsplit(tf, ".", fixed=TRUE)[[1]][[2]])
243
  plotDF$conc = sapply(plotDF$DrugID, function(drid) {
244
    splits = strsplit(drid, "-")[[1]]
245
    colnames(ctab)[which(ctab[splits[1],] == splits[2])]
246
  })
247
  plotDF$diag.conc = paste(plotDF$diag, plotDF$conc, sep=".")
248
  diagOrder = names(colDiagS)[names(colDiagS) %in% unique(plotDF$diag)]
249
  plotDF$diag.conc = factor(plotDF$diag.conc,
250
                            levels=as.vector(sapply(diagOrder,
251
                                                    function(d)
252
                                                      paste(d, paste0("c", 1:5),
253
                                                            sep="."))))
254
255
  # names of drugs / order them
256
  plotDF$Drug2 = dtab[plotDF$DrugID2, "name"]
257
  plotDF$Drug2 = factor(plotDF$Drug2, levels=dtab[rev(mainDrOrd),"name"])
258
259
  # data frame for vline
260
  vltab = table(sapply(unique(plotDF$diag), function(x) grep(x, diagAmt)))
261
  vldf = data.frame(x=seq(5.5, length(levels(plotDF$diag.conc)), 5), col=1)
262
  vldf$col[cumsum(vltab[1:(length(vltab)-1)])] = 2 # remove latest
263
  vldf$col = factor(vldf$col, levels=c(1,2))
264
265
  plotDF$Fill = log10div(plotDF$plot.pval) # transform the values to log10 scale
266
  plotDF$Fill = pmax(pmin(plotDF$Fill, 12), -12) # censore
267
  # color palette
268
  pal = rev(c(brewer.pal(11,"PiYG")[1:6], brewer.pal(11,"RdBu")[7:11]))
269
  pal[6] = "gray95"
270
271
  gMain2 = ggplot() + geom_tile(data=plotDF,
272
                                aes(x=diag.conc, y=Drug2, fill=Fill)) +
273
    scale_fill_gradientn(expression(italic(p)*"-value"),
274
                         colours=pal, limits=c(-12, 12),
275
                         breaks=c(-12,-8,-4,0,4,8,12), labels=exp10div) +
276
    theme_bw() +
277
    geom_vline(xintercept=seq(0.5, length(levels(plotDF$diag.conc))+1, 1),
278
               color="white", size=1.5) +
279
    geom_hline(yintercept=seq(0.5, length(levels(plotDF$Drug2))+1, 1),
280
               color="white", size=1.5) +
281
    geom_vline(data=vldf, aes(xintercept=x, color=col)) +
282
    scale_color_manual(values=c("1"=cGrid[1], "2"=cGrid[2])) +
283
    coord_equal() + xlab("") + ylab("") +
284
    scale_y_discrete(expand=c(0,0)) +
285
    scale_x_discrete(expand=c(0,0)) +
286
    theme(axis.title=element_blank(),
287
          axis.text.x=element_blank(),
288
          axis.text.y=element_text(size=10),
289
          axis.ticks.x=element_blank(),
290
          panel.border=element_rect(colour=cPanb, fill=NA, size=sPanb),
291
          legend.key=element_rect(color="black"),
292
          legend.text=element_text(size=10),
293
          legend.title=element_text(size=10, face="bold")) +
294
    guides(color=FALSE)
295
296
  #################################################
297
298
  # concentrations
299
  concDF = data.frame(x=factor(levels(plotDF$diag.conc)),
300
                      fill=factor(rep(paste0("c",1:5)), levels=paste0("c",1:5)))
301
  concpal = setNames(paste0("grey", seq(35, 95, 15)), nm=paste0("c",1:5))
302
  gConc = ggplot() + geom_tile(data=concDF, aes(x=x, y=1, fill=fill)) +
303
    scale_fill_manual("Drug\nconcentration", values=concpal) +
304
    scale_y_continuous(expand=c(0,0)) +
305
    scale_x_discrete(expand=c(0,0)) + theme_bw() +
306
    theme(axis.title=element_blank(), axis.text=element_blank(),
307
          axis.ticks=element_blank(), legend.text=element_text(size=10),
308
          legend.title=element_text(size=10, face="bold"),
309
          legend.key=element_rect(color="black"),
310
          panel.border=element_rect(colour=cPanb, fill=NA, size=sPanb)) +
311
    geom_vline(data=vldf, aes(xintercept=x, color=col)) +
312
    scale_color_manual(values=c("1"=cGrid[1], "2"=cGrid[2])) +
313
    guides(color=FALSE)
314
315
  # annotation of cell of origin
316
  diagOrd = sapply(unique(plotDF$diag), function(x) grep(x, diagAmt))
317
  diagOrd = diagOrd[diagOrder]
318
  annoDF = data.frame(x=names(diagAmt)[diagOrd],
319
                      diag=factor(names(diagOrd), levels=names(diagOrd)))
320
  annoDF$x = factor(annoDF$x, levels=unique(annoDF$x))
321
  # vline
322
  vldf2 = data.frame(x=seq(1.5, length(levels(annoDF$diag)), 1), col=1)
323
  vldf2$col[cumsum(table(diagOrd)[1:(length(table(diagOrd))-1)])] = 2
324
  vldf2$col = factor(vldf2$col, levels=c(1,2))
325
  gAnno = ggplot() + geom_tile(data=annoDF, aes(x=diag, y=1, fill=x)) +
326
    scale_fill_manual("Disease origin", values=colDiagL) +
327
    scale_y_continuous(expand=c(0,0)) + scale_x_discrete(expand=c(0,0)) +
328
    theme_bw() +
329
    theme(axis.title=element_blank(),
330
          axis.text=element_blank(),
331
          axis.ticks=element_blank(),
332
          legend.text=element_text(size=10),
333
          legend.title=element_text(size=10, face="bold"),
334
          panel.border=element_rect(colour=cPanb, fill=NA, size=sPanb),
335
          legend.key=element_rect(color="black")) +
336
    geom_vline(data=vldf2, aes(xintercept=x, color=col)) +
337
    scale_color_manual(values=c("1"=cGrid[1], "2"=cGrid[2])) +
338
    guides(color=FALSE) +
339
    geom_text(data=annoDF, aes(label=diag, y=1, x=diag))
340
341
342
  ## MERGE PLOTS INTO ONE FIGURE
343
344
  # prepare grobs
345
  ggM = ggplotGrob(gMain2)
346
  ggA = ggplotGrob(gAnno)
347
  ggC = ggplotGrob(gConc)
348
349
  # prepare gtable
350
  nX = length(levels(plotDF$diag.conc))
351
  nY = length(levels(plotDF$Drug2))
352
  unM = 0.15 # unit for main heat map
353
  unA = 0.25 # unit for y-axis of the annotation - diagnosis
354
  unC = 0.15 # unit for y-axis of the concentration
355
  sp = 0.0 # space
356
357
  wdths = c(1.5, nX*unM, 2)
358
  hghts = c(unA, sp, nY*unM, unC)
359
  gt = gtable(widths=unit(wdths, "in"), heights=unit(hghts, "in"))
360
361
  # add pieces to gtable
362
  gt = gtable_add_grob(gt, ggA$grobs[[whichInGrob(ggA, "panel")]], 1, 2)
363
  gt = gtable_add_grob(gt, ggM$grobs[[whichInGrob(ggM, "panel")]], 3, 2)
364
  gt = gtable_add_grob(gt, ggC$grobs[[whichInGrob(ggC, "panel")]], 4, 2)
365
  gt = gtable_add_grob(gt, ggM$grobs[[whichInGrob(ggM, "axis-l")]], 3, 1)
366
367
  # legend cell origin & concentration & effect
368
  wdthsL = c(2,2,2)
369
  hghtsL = 2
370
  gtL = gtable(widths=unit(wdthsL, "in"), heights=unit(hghtsL, "in"))
371
  gtL = gtable_add_grob(gtL, ggA$grobs[[whichInGrob(ggA, "guide-box-right")]], 1, 1)
372
  gtL = gtable_add_grob(gtL, ggM$grobs[[whichInGrob(ggM, "guide-box-right")]], 1, 2)
373
  gtL = gtable_add_grob(gtL, ggC$grobs[[whichInGrob(ggC, "guide-box-right")]], 1, 3)
374
375
  return(list("figure"=list(width=sum(wdths), height=sum(hghts), plot=gt),
376
              "legend"=list(width=sum(wdthsL), height=sum(hghtsL), plot=gtL)))
377
}
378
379
380
################################################################################
381
# BEE SWARM PLOTS
382
################################################################################
383
384
385
beeF <- function(drug, mut, cs, diag, y1, y2, custc,
386
                 lpd=BloodCancerMultiOmics2017::lpdAll,
387
                 ctab=BloodCancerMultiOmics2017::conctab,
388
                 dtab=BloodCancerMultiOmics2017::drugs) {
389
390
  col1 <- vector(); col2 <- vector()
391
  dr <- lpd[ , lpd$Diagnosis %in% diag   ]
392
  p = t.test( Biobase::exprs(dr)[drug,] ~ Biobase::exprs(dr)[mut,], var.equal = TRUE)$p.value
393
394
  #clonsize
395
  af <- fData(BloodCancerMultiOmics2017::mutCOM)[colnames(dr), paste0(mut, "cs")]
396
397
  #Create a function to generate a continuous color palette
398
  rbPal <- colorRampPalette(c('coral1','blue4'))
399
400
  # This adds a cont. color code
401
  if (cs==T) {
402
    col2 <- rbPal(100)[as.numeric(cut( af, breaks = 100 ))]
403
  } else {
404
    col2 <- "blue4"
405
  }
406
407
  if (custc==T) {
408
    col1 <- ifelse(Biobase::exprs(dr)[mut,]==1, col2,"coral1")
409
  } else {
410
    col1 <- ifelse(Biobase::exprs(dr)[mut,]==1, "green","magenta")
411
  }
412
413
  beeswarm( Biobase::exprs(dr)[drug,] ~ Biobase::exprs(dr)[mut,],
414
            method = 'swarm',
415
            pch = 19, pwcol = col1, cex = 1.6,
416
            xlab = '', ylab = 'Viability', cex.axis=1.6, cex.lab=1.9,
417
            ylim=c(y1,y2),
418
            labels = c("AF=0", "AF>0"),
419
            main=(paste0( giveDrugLabel(drug, ctab, dtab), " ~ ", mut,
420
                          "\n (p = ",
421
                          digits = format.pval(p,
422
                                               max(1, getOption("digits") - 4)),
423
                          ")")),
424
            cex.main=2.0,
425
            bty="n"
426
  )
427
428
  boxplot(Biobase::exprs(dr)[drug,] ~ Biobase::exprs(dr)[mut,],  add = T, names = c("",""),
429
          col="#0000ff22", axes = 0, outline=FALSE)
430
}
431
432
433
# BEESWARM FOR PRETREATMENT
434
beePretreatment = function(lpd, drug, y1, y2, fac, val, name) {
435
436
  dr <- lpd[  , Biobase::exprs(lpd)[fac,] %in% val]
437
  pretreat <- BloodCancerMultiOmics2017::patmeta[colnames(dr),
438
    "IC50beforeTreatment"]
439
  p = t.test( Biobase::exprs(dr)[drug,] ~ pretreat, var.equal = TRUE)$p.value
440
441
  beeswarm( Biobase::exprs(dr)[drug,] ~ pretreat,
442
            method = 'swarm',
443
            pch = 19,  cex = 1.2, pwcol=ifelse(pretreat, "blue4", "coral1"),
444
            xlab = '', ylab = 'Viability', cex.axis=1.6, cex.lab=1.8,
445
            labels = c("pre-treated", "not treated"),
446
            main=paste0(
447
              name," (p = ", digits = format.pval(
448
                p, max(1, getOption("digits") - 4)), ")"),  ylim=c(y1,y2),
449
            cex.main=2,
450
            bty="n"
451
  )
452
  boxplot(Biobase::exprs(dr)[drug,] ~ pretreat,  add = T, names = c("",""),
453
          col="#0000ff22", axes = 0, outline=FALSE)
454
}