[fbf06f]: / atlantis / R / PlotATLANTISresults.R

Download this file

311 lines (257 with data), 11.3 kB

  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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#' Generate ATLANTIS plots from intermediate files
#'
#' @param fi fit.info object (or file name)
#' @param ATLANTIS_Summary the structure itself or file to load it from
#' @param code.dir unused -- to be dropped
#' @param output.dir if output.dir is NULL then the plot is written to the current device, otherwise a PDF is written to the output.dir
#' @param annTable.file File containing feature annotations (the gene, location, etc)
#' @param output.prefix a prefix to prepend to all output files
#' @export
PlotATLANTISresults <- function(fi, ATLANTIS_Summary, code.dir, output.dir, annTable.file, output.prefix = NULL){
if(is.character(fi) == TRUE){
load(fi)
}
if(is.character(ATLANTIS_Summary) == TRUE){
load(ATLANTIS_Summary)
}
#read in annTable file stored in libdir
annTable <- read.table(annTable.file,header=TRUE,comment.char="",sep="\t", fill=FALSE,stringsAsFactors = FALSE, check.names = FALSE,row.names=1)
#get analysisID, targetID, targetVec, predMat from ATLANTIS_Summary
targetVec <- fi$targetVec
predMat <- fi$predData
targetID <- ATLANTIS_Summary$targetID
analysisID <- ATLANTIS_Summary$analysisID
additionalFeatures <- ATLANTIS_Summary$additionalFeatures
if(is.null(output.prefix)) {
output.prefix = targetID
}
# create plots
if(!is.null(output.dir)) {
plots.file <- paste(output.prefix, '_plots.pdf', sep='')
pdf(file.path(output.dir, plots.file), w=11*1.5, h=8.5*1.5)
}
report.summary <- fi$report.summary
for (nominalScale in c(F, T)) {
for (orderByPred in c(F,T)) {
tmp <- plots.plotModel(fi, targetVec, targetID,
predMat, annTable,
maxNumPreds=20,
report.summary,
orderByPred=orderByPred,
additionalFeatures=additionalFeatures, nominalScale=nominalScale)
}
}
if(!is.null(output.dir)) {
dev.off()
}
}
# draw text "msg" scaling to fit in the specified bounding box
drawBoundedText <- function(msg, left, bottom, right, top) {
target.width <- right - left;
target.height <- top - bottom;
scale <- min(target.width/strwidth(msg), target.height/strheight(msg))
text(left, bottom, msg, cex=scale, adj=c(0, 0))
}
# generates a plot with a summary of predictive variables used in gbm model
# fi - fit.info of model
# targetVec - target vector (from targetMat)
# targetID - the ID of the target variable (its name in targetMat)
# maxNumPreds - number of features to plot
plots.plotModel <- function(fi, targetVec, targetID, predMat, annTable, maxNumPreds, report.summary, ...) {
library(caTools) # for colAUC
# top 20 features, ordered by cluster & contribution of their clusters
# head(used.features.inf[order(used.features.inf[1:20,"clusterRank"]), ], 20)
# can move the following to model creation GBM too
predProfile <- fi$OOB$prediction
model_quality <- list()
model_quality$value <- fi$OOB$quality
model_quality$metric <- fi$OOB$metric
model.features.ordered <- fi$varImp
# convert targetVec to numeric binary vector if is factor
if (fi$modelType == "Classification") {
targetVec <- ifelse(as.numeric(targetVec) == 1, 0, 1)
}
plots.predictorsBars(targetVec, predMat, model.features.ordered,
targetID, model_quality, annTable=annTable,
report.summary=report.summary,
predProfile=predProfile,
maxNumPreds=maxNumPreds, ...)
}
# model.features - relative influence for predictors. a named vector
# predProfile - predictions of the targetVec, to be plotted on-top
# model_quality - a list. $metric is a string ("R^2" or "AUC"), $value holds the value
plots.predictorsBars <- function(depProfile, predMat, model.features, gsID, model_quality,
annTable, report.summary,
additionalFeatures=NULL, maxNumPreds=10, predProfile=NULL,
orderByPred=FALSE, nominalScale=F) {
# subset influence table to include only top predictive features (whose influence sum to 50)
## order.by.inf <- order(model.features[, "rel.inf"], decreasing=T)
## model.features <- model.features[order.by.inf, ]
# how many features are used by the model?
numModelFeatures <- length(model.features)
#lastPredToPlot <- min(max(which(cumsum(model.features[,"rel.inf"]) < 50)) + 1, maxNumPreds)
lastPredToPlot <- min(numModelFeatures, maxNumPreds)
# we're going to plot only these predictors
features.to.plot <- model.features[1:lastPredToPlot]
stopifnot(all(names(features.to.plot) %in% colnames(predMat)))
feat.data <- predMat[, names(features.to.plot), drop=F]
# plot barplots for all predictors, target GS, influence weights too
num.feats <- length(features.to.plot)
if (orderByPred) {
samp.ord <- order(predProfile, depProfile) # the order of the samples in the plots
} else {
samp.ord <- order(depProfile, predProfile) # order by true then prediction
}
layout(matrix(c(num.feats+4, rep(1, num.feats+2), 2, num.feats+5, 3:(num.feats+3)), ncol=2),
width=c(1, 5),
heights=c(3, 0.5, rep(1, num.feats), 1.5))
# plot rel.inf values
op <- par(mar = c(3, 2, 0, 0))
featCorToTarget <- cor(feat.data,
as.numeric(depProfile),
use="pairwise.complete.obs",
method='spearman')
cols <- rev(c("goldenrod", "deeppink3")[as.numeric(featCorToTarget > 0)+1])
barplot(rev(features.to.plot), horiz=T, col=cols, names.arg="")
par(op)
######
# plot response variable
######
# figure out chr loci for response varibale
responseHUGO <- annTable[gsID, "HUGOsymbol"]
responseChrLoci <- annTable[gsID, "ChrLoci"]
gsLabel <- paste(gsID, "\n", responseChrLoci, sep='')
cols <- ifelse(depProfile[samp.ord] < -2.0, "red", "grey")
op <- par(mar = c(0, 2, 1, 13)) # c(bottom, left, top, right)
mp <- barplot(depProfile[samp.ord], names.arg="", axes=T, col=cols, border=NA)
points(mp, predProfile[samp.ord], pch='+', col="palegreen4", type='l', lwd=2)
mtext(gsLabel, side=4, las=1, cex=0.9)
par(op)
# plot barplots for predictive features
uniqChrLoci <- unique(annTable[names(features.to.plot), "ChrLoci"], border=NA)
chrBands <- sub("\\..+$", "", uniqChrLoci)
names(chrBands) <- uniqChrLoci
colMap <- plots.colorChrLoc(chrBands[annTable[names(features.to.plot), "ChrLoci"]])
for (i in 1:num.feats) {
featID <- names(features.to.plot)[i]
plotdata <- feat.data[samp.ord, i]
if (annTable[featID, "VarType"] == "CN") {
# for CN features
# color amps in red, dels in blue
cols <- ifelse(plotdata > 0.5, "red", "grey")
cols[plotdata < -0.5] <- "blue"
ylim=c(-1,1)
}
if (annTable[featID, "VarType"] %in% c("MUT", "Mmis", "Mbad", "Mcos", "Mall", "RsMmis", "RsMnon")) {
# color mutations in purple
f <- feat.data[samp.ord, i]
stopifnot(all(f %in% c(0,1,2) | is.na(f)))
cols <- c("grey", "sienna4", "darkorange")[f+1]
ylim=c(0,2)
}
if (annTable[featID, "VarType"] %in% c("ACH", "GS", "SS")) {
# color high dependency in red
cols <- ifelse(feat.data[samp.ord, i] > 0.44, "orangered3", "grey")
cols[plotdata < -0.44] <- "seagreen3"
ylim=c(-2,2)
}
if (annTable[featID, "VarType"] %in% c("Exp", "GSE", "CTD", "mirE")) {
# transform data to robust Z
z.plotdata <- (plotdata - median(plotdata, na.rm=T)) / mad(plotdata, na.rm=T)
if(!nominalScale) {
plotdata <- z.plotdata
}
# color high expression
cols <- ifelse(z.plotdata > 2, "orangered3", "grey")
cols[z.plotdata < -2] <- "seagreen3"
ylim=c(-3,3)
}
if (annTable[featID, "VarType"] == "SI") {
if (length(unique(feat.data[,i])) <= 3) { # binary + NA
cols <- ifelse(feat.data[samp.ord, i] > 0, "orange3", "grey")
ylim=c(0,1)
} else {
ylim <- NULL
cols <- NULL
}
}
if (annTable[featID, "VarType"] == "San") {
# transform data to robust Z
plotdata <- (plotdata - median(plotdata, na.rm=T)) / mad(plotdata, na.rm=T)
# color high expression
cols <- ifelse(plotdata > 2, "orangered3", "grey")
cols[plotdata < -2] <- "seagreen3"
ylim=c(-3,3)
# # color high sensitivity in red
# cols <- ifelse(feat.data[samp.ord, i] < 0.3, "orangered3", "grey")
# ylim=c(0,1)
}
feature.barplot <- function(plotdata, axes.range, cols, ylim) {
#par(oma=rep(1,4))
#par(oma=c(1,0,1,0))
orig.mar <- par()$mar
par(mar=c(1,0,1,0)+orig.mar)
par(mgp=c(0,-2,-3))
r.range <- round( c(min(plotdata,na.rm=T), max(plotdata,na.rm=T)), 0)
if(is.null(ylim)) {
ylim <- c( min(c(plotdata, r.range[1]), na.rm=T), max(c(plotdata, r.range[2]), na.rm=T) )
}
if(any(is.infinite(ylim))) {
# hack -- ylim is infinite in some case. Need to investigate where how this happens
ylim <- c(-1e10, 1e10)
warning("ylim was infinite so skipping this bar")
} else {
barplot(plotdata, names.arg="", axes=F, col=cols, ylim=ylim, border=NA)
if(!is.null(axes.range)) {
axes.range <-round( c(min(plotdata,na.rm=T), max(plotdata,na.rm=T)), 0)
# axis(ifelse((i %% 2) == 1,2,4), axes.range, las=2)
axis(2, axes.range, las=2)
}
}
par(mar=orig.mar)
}
op <- par(mar = c(0, 2, 0, 13)) # c(bottom, left, top, right)
axes.range <- NULL
# if(nominalScale) {
ylim <- NULL
axes.range = pretty(plotdata, n=1)
# }
feature.barplot(plotdata, axes.range, cols, ylim)
if (annTable[featID, "VarType"] %in% c("GSE", "SI")) {
first.line.text <- substr(featID,1,15)
second.line.text <- paste(substr(featID,16,32), "\n", substr(featID,33,49), sep='')
} else {
first.line.text <- substr(featID,1,25)
second.line.text <- annTable[featID, "ChrLoci"]
}
feature.label <- paste(first.line.text, "\n", second.line.text, sep='')
mtext(feature.label, side=4, las=1, col=colMap[chrBands[annTable[featID, "ChrLoci"]]], cex=0.9)
par(op)
}
# plot text on top left corner
op <- par(mar = rep(0, 4))
plot.new()
# text(0.85, 0.5, pos=4, paste(report.summary, collapse="\n"))
par(op)
# model info in the top left corner
op <- par(mar = rep(0, 4))
plot.new()
drawBoundedText(paste("CV", model_quality$metric, "=", format(model_quality$value, digits=2)), 0, 0.9, 1, 1)
drawBoundedText(paste(report.summary, collapse="\n"), 0, 0, 1, 0.85)
par(op)
list(feat.data=feat.data, samp.ord=samp.ord)
}
# given a vector of strings containing chromosomal locations, assign
# a color to each one so that chromosomal loci that are represented more
# than once get colored in the same color.
plots.colorChrLoc <- function(chr.loci, def.color="black") {
library(RColorBrewer)
freqs <- table(chr.loci)
num.unique <- length(freqs)
cols <- rep(def.color, num.unique)
names(cols) <- names(freqs)
repeats <- names(freqs)[freqs >= 2]
num.repeats <- length(repeats)
cols[repeats] <- brewer.pal(8, "Set1")[1:length(repeats)]
cols
}