-
Notifications
You must be signed in to change notification settings - Fork 10
/
NumericTrack-class.R
158 lines (151 loc) · 6.87 KB
/
NumericTrack-class.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
#' @include RangeTrack-class.R
NULL
## NumericTrack-class --------------------------------------------------------
#' NumericTrack class and methods
#'
#' The virtual parent class for all track items in the Gviz package designed to
#' contain numeric data. This class merely exists for dispatching purpose.
#'
#' @template GdObject-class_slot
#' @template RangeTrack-class_slot
#'
#' @template NumericTrack_param
#'
#' @name NumericTrack-class
#'
#' @return A virtual class: No objects may be created from it.
#'
#' @author Florian Hahne
#'
#' @inherit GdObject-class seealso
#'
#' @exportClass NumericTrack
setClass("NumericTrack",
representation = representation("VIRTUAL"),
prototype = prototype(
name = "NumericTrack",
dp = DisplayPars()
),
contains = "RangeTrack"
)
## NumericTrack Methods drawAxis ---------------------------------------------
#' @describeIn NumericTrack-class add a y-axis to the title panel of a track.
#' @importFrom grDevices extendrange colorRampPalette
#' @export
setMethod("drawAxis", signature(GdObject = "NumericTrack"), function(GdObject, from, to, ...) {
type <- match.arg(.dpOrDefault(GdObject, "type", "p"), .PLOT_TYPES, several.ok = TRUE)
yvals <- values(GdObject)
ylim <- .dpOrDefault(GdObject, "ylim", if (!is.null(yvals) && length(yvals)) {
range(yvals, na.rm = TRUE, finite = TRUE)
} else {
c(-1, 1)
})
if (diff(ylim) == 0) {
ylim <- ylim + c(-1, 1)
}
hSpaceAvail <- vpLocation()$isize["width"] / 6
yscale <- extendrange(r = ylim, f = 0.05)
col <- .dpOrDefault(GdObject, "col.axis", "white")
acex <- .dpOrDefault(GdObject, "cex.axis")
acol <- .dpOrDefault(GdObject, "col.axis", "white")
at <- .dpOrDefault(GdObject, "yTicksAt", pretty(yscale))
at <- at[which(at >= sort(ylim)[1] & at <= sort(ylim)[2])]
if (!length(at)) {
at <- sort(ylim)[c(1, 2)]
}
if (is.null(acex)) {
vSpaceNeeded <- max(as.numeric(convertWidth(stringHeight(at), "inches"))) * length(at) * 1.5
hSpaceNeeded <- max(as.numeric(convertWidth(stringWidth(at), "inches")))
vSpaceAvail <- abs(diff(range(at))) / abs(diff(yscale)) * vpLocation()$isize["height"]
acex <- max(0.6, min(vSpaceAvail / vSpaceNeeded, hSpaceAvail / hSpaceNeeded))
}
nlevs <- max(1, nlevels(factor(.dpOrDefault(GdObject, "groups"))))
if (any(type %in% c("heatmap", "horizon")) && .dpOrDefault(GdObject, "showSampleNames", FALSE)) {
groups <- .dpOrDefault(GdObject, "groups")
sn <- if (is.null(groups)) rownames(values(GdObject)) else rev(unlist(split(rownames(values(GdObject)), factor(groups))))
cex.sn <- .dpOrDefault(GdObject, "cex.sampleNames", acex)
col.cn <- .dpOrDefault(GdObject, "col.sampleNames", "white")
wd <- max(as.numeric(convertWidth(stringWidth(sn) + unit(10, "points"), "npc"))) * cex.sn
samNames <- viewport(x = 1, width = wd, just = 1, yscale = c(-0.05, 1.05))
pushViewport(samNames)
nr <- nrow(values(GdObject))
if (nr > 1) {
yy <- head(seq(0.05, 0.95, len = nr + 1), -1)
yy <- yy + diff(yy)[[1]] / 2
} else {
yy <- 0.5
}
grid.text(x = rep(0.5, nr), y = yy, label = rev(sn), just = 0.5, gp = gpar(cex = cex.sn, col = col.cn))
popViewport(1)
samAxis <- viewport(x = 1 - wd, width = 1 - wd, just = 1)
pushViewport(samAxis)
on.exit(popViewport(1))
}
## if any of the types are gradient or heatmap we want the gradient scale
if (any(type %in% c("gradient", "heatmap")) && .dpOrDefault(GdObject, "showColorBar", TRUE)) {
## viewport to hold the color strip
shift <- ifelse(all(type %in% c("gradient", "heatmap")), 1, 0)
pcols <- .getPlottingFeatures(GdObject)
ncolor <- .dpOrDefault(GdObject, "ncolor", 100)
vpAxisCont <- viewport(x = unit(1, "npc") - unit(2 - shift, "points"), width = unit(1, "npc") - unit(2 - shift, "points"), just = 1)
pushViewport(vpAxisCont)
for (i in seq_len(nlevs)) {
## create color palette
cr <- c("white", pcols$col[i])
if (nlevs < 2) {
cr <- .dpOrDefault(GdObject, "gradient", cr)
}
palette <- colorRampPalette(cr)(ncolor + 5)[-seq_len(5)]
pshift <- ifelse(i == nlevs, 1 - shift, 0)
vpTitleAxis <- viewport(
x = unit(1, "npc") - unit(4 * (i - 1), "points"), width = unit(4 + pshift, "points"),
yscale = yscale, just = 1
)
pushViewport(vpTitleAxis)
## draw a rectangle for each color
if (all(type %in% c("gradient", "heatmap"))) {
if (i == nlevs) {
suppressWarnings(grid.yaxis(gp = gpar(col = acol, cex = acex), at = at))
}
grid.rect(
y = unit(seq(ylim[1], ylim[2], length.out = ncolor + 1), "native")[-(ncolor + 1)], x = unit(0, "npc") - unit(1, "points"),
width = 1, height = 1 / ncolor, gp = gpar(fill = palette, lty = 0), just = c("left", "bottom")
)
} else {
grid.rect(
y = unit(seq(ylim[1], ylim[2], length.out = ncolor + 1), "native")[-(ncolor + 1)], x = 0,
width = 1, height = 1 / ncolor, gp = gpar(fill = palette, lty = 0), just = c("left", "bottom")
)
if (i == nlevs) {
suppressWarnings(grid.yaxis(gp = gpar(col = acol, cex = acex), at = at))
grid.lines(x = c(0, 0), y = ylim, gp = gpar(col = acol), default.units = "native")
}
}
popViewport(1)
}
popViewport(1)
} else {
vpTitleAxis <- viewport(x = 0.95, width = 0.2, yscale = yscale, just = 0)
pushViewport(vpTitleAxis)
suppressWarnings(grid.yaxis(gp = gpar(col = acol, cex = acex), at = at))
grid.lines(x = c(0, 0), y = ylim, gp = gpar(col = acol), default.units = "native")
popViewport(1)
}
})
## NumericTrack Methods drawGrid ---------------------------------------------
#' @describeIn NumericTrack-class superpose a grid on top of a track.
setMethod("drawGrid", signature(GdObject = "NumericTrack"), function(GdObject, from, to) {
if (.dpOrDefault(GdObject, "grid", FALSE)) {
vals <- score(GdObject)
ylim <- .dpOrDefault(GdObject, "ylim", range(vals, na.rm = TRUE, finite = TRUE))
if (diff(ylim)) {
pushViewport(dataViewport(xData = c(from, to), yData = ylim, extension = c(0, 0.1), clip = TRUE))
panel.grid(
h = .dpOrDefault(GdObject, "h", -1), v = .dpOrDefault(GdObject, "v", -1),
col = .dpOrDefault(GdObject, "col.grid", "#e6e6e6"), lty = .dpOrDefault(GdObject, "lty.grid", 1),
lwd = .dpOrDefault(GdObject, "lwd.grid", 1)
)
popViewport(1)
}
}
})