|
| 1 | +## Functions to visualize the regions |
| 2 | +fortify_seqlmplot = function(segment, values, annotation, genome_information, expand){ |
| 3 | + # Get rows from the matrix |
| 4 | + segment_expanded = segment |
| 5 | + start(segment_expanded) = start(segment_expanded) - expand |
| 6 | + end(segment_expanded) = end(segment_expanded) + expand |
| 7 | + |
| 8 | + gi = subsetByOverlaps(genome_information, segment) |
| 9 | + gi_expanded = subsetByOverlaps(genome_information, segment_expanded) |
| 10 | + |
| 11 | + values0 = as.matrix(values[names(gi_expanded), ]) |
| 12 | + |
| 13 | + # Bring into long format |
| 14 | + df = melt(values0, varnames = c("Probe", "Sample")) |
| 15 | + |
| 16 | + # Add annotations |
| 17 | + a = data.frame(Sample = colnames(values), Annotation = annotation) |
| 18 | + df = merge(df, a) |
| 19 | + |
| 20 | + # Add region information |
| 21 | + probe_det = data.frame(Probe = names(gi_expanded), Region = !(gi_expanded %outside% gi), Position = start(gi_expanded)) |
| 22 | + df = merge(df, probe_det) |
| 23 | + |
| 24 | + # Calculate the box that shows region |
| 25 | + reg = probe_det[probe_det$Region,] |
| 26 | + nonreg = probe_det[!probe_det$Region,] |
| 27 | + |
| 28 | + smaller = nonreg[nonreg$Position < min(reg$Position),] |
| 29 | + bigger = nonreg[nonreg$Position > max(reg$Position),] |
| 30 | + |
| 31 | + if(nrow(smaller) > 0){ |
| 32 | + diff = min(reg$Position) - max(smaller$Position) |
| 33 | + start = min(reg$Position) - min(100, diff / 2) |
| 34 | + } |
| 35 | + else{ |
| 36 | + start = min(reg$Position) - 100 |
| 37 | + } |
| 38 | + |
| 39 | + if(nrow(bigger) > 0){ |
| 40 | + diff = min(bigger$Position) - max(reg$Position) |
| 41 | + end = max(reg$Position) + min(100, diff / 2) |
| 42 | + } |
| 43 | + else{ |
| 44 | + end = max(reg$Position) + 100 |
| 45 | + } |
| 46 | + |
| 47 | + box = data.frame(start = start, end = end) |
| 48 | + |
| 49 | + return(list(df = df, box = box)) |
| 50 | +} |
| 51 | + |
| 52 | +draw_seqlmplot = function(df, box, ylim, expand){ |
| 53 | + plot = qplot(x = Position, y = value, geom = c("line", "point"), colour = Annotation, group = Sample, data = df) + geom_rect(aes(xmin = box$start, xmax = box$end, ymin = -Inf, ymax = Inf), colour = "grey20", fill = "grey95") + geom_point() + geom_line() + geom_jitter(position = position_jitter(width = .1)) + scale_y_continuous(limits = ylim) + scale_x_continuous(limits = c(box$start - expand, box$end + expand)) + theme_bw() |
| 54 | +} |
| 55 | + |
| 56 | +seqlmplot = function(segment, values, annotation, genome_information, expand, ylim = extendrange(values), filename = NA, ...){ |
| 57 | + data = fortify_seqlmplot(segment = segment, values = values, annotation = annotation, genome_information = genome_information, expand = expand) |
| 58 | + |
| 59 | + plot = draw_seqlmplot(df = data$df, box = data$box, ylim, expand = expand) |
| 60 | + |
| 61 | + if(is.na(filename)){ |
| 62 | + print(plot) |
| 63 | + } |
| 64 | + else{ |
| 65 | + ggsave(filename, plot, ...) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +#' Visualise the regions |
| 71 | +#' |
| 72 | +#' Generate plots about the seqlm results |
| 73 | +#' |
| 74 | +#' The number of results from \code{\link{seqlm}} can be large |
| 75 | +#' and visualising all these regions might not be desirable. |
| 76 | +#' Therefore, it is advisable to filter the results befor |
| 77 | +#' plotting. |
| 78 | +#' |
| 79 | +#' @param segments selection of significant regions by \code{\link{seqlm}} function |
| 80 | +#' @param values same values matrix that was used in \code{seqlm} |
| 81 | +#' @param genome_information same genome_information object that was used in \code{seqlm} |
| 82 | +#' @param annotation same annotation vector that was used in \code{seqlm} |
| 83 | +#' @param expand number of basepairs to extend the region on plot |
| 84 | +#' @param ylim two element vector giving the lower and higher limit of the y axis |
| 85 | +#' @param dir directory where to put the images, of NA then plots are drawn into the plotting window |
| 86 | +#' @param filetype picture filetype |
| 87 | +#' @param ... extra parameters to \code{\link{ggsave}} |
| 88 | +#' @author Raivo Kolde <rkolde@@gmail.com> |
| 89 | +#' |
| 90 | +#' @export |
| 91 | +seqlmplots = function(segments, values, genome_information, annotation, expand = 100, ylim = extendrange(values), dir = NA, filetype = "png", ...){ |
| 92 | + # Match values and genome_information |
| 93 | + mp = match_positions(values, genome_information) |
| 94 | + values = mp$values |
| 95 | + genome_information = mp$genome_information |
| 96 | + |
| 97 | + # Draw pictures |
| 98 | + for(i in 1:length(segments)){ |
| 99 | + if(is.na(dir)){ |
| 100 | + filename = NA |
| 101 | + } |
| 102 | + else{ |
| 103 | + filename = file.path(dir, sprintf("%d.%s", i, filetype)) |
| 104 | + } |
| 105 | + seqlmplot(segment = segments[i], values = values, annotation = annotation, genome_information = genome_information, expand = expand, ylim = ylim, filename = filename, ...) |
| 106 | + |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +## seqlm raport |
| 112 | +raport_template = ' |
| 113 | +<!DOCTYPE html> |
| 114 | +<html> |
| 115 | +<head> |
| 116 | +<style type="text/css">.knitr.inline { |
| 117 | + background-color: #f7f7f7; |
| 118 | + border: solid 0px #b0b0b0 |
| 119 | +} |
| 120 | +.message { |
| 121 | + font-style: italic |
| 122 | +} |
| 123 | +.source,.output,.warning,.error,.message { |
| 124 | + padding: 0em 1em; |
| 125 | + border: solid 1px #f7f7f7 |
| 126 | +} |
| 127 | +.source { |
| 128 | + background-color: #f7f7f7 |
| 129 | +} |
| 130 | +.rimage.left { |
| 131 | + text-align: left |
| 132 | +} |
| 133 | +.rimage.right { |
| 134 | + text-align: right |
| 135 | +} |
| 136 | +.rimage.center { |
| 137 | + text-align: center |
| 138 | +} |
| 139 | +.source { |
| 140 | + color: #333 |
| 141 | +} |
| 142 | +.background { |
| 143 | + color: #f7f7f7 |
| 144 | +} |
| 145 | +</style> |
| 146 | +<title>%s</title> |
| 147 | +</head> |
| 148 | +<body> |
| 149 | +
|
| 150 | +<code class="knitr inline"> |
| 151 | +<h1> %s </h1> |
| 152 | +
|
| 153 | +%s |
| 154 | +</code> |
| 155 | +</body> |
| 156 | +</html> |
| 157 | +' |
| 158 | +chunk_template = ' |
| 159 | +<h2> Segment %d </h2> |
| 160 | +
|
| 161 | +<table> |
| 162 | + <tr> |
| 163 | + <td><b>Location</b></td> |
| 164 | + <td>%s</td> |
| 165 | + </tr> |
| 166 | + %s |
| 167 | +</table> |
| 168 | +
|
| 169 | +<div class="rimage default"><img src="%s" class="plot"/></div> |
| 170 | +' |
| 171 | + |
| 172 | +annotation_template = ' |
| 173 | +<tr> |
| 174 | + <td><b>%s</b></td> |
| 175 | + <td>%s</td> |
| 176 | +</tr> |
| 177 | +' |
| 178 | + |
| 179 | +location_template = 'chr%s:%d-%d' |
| 180 | + |
| 181 | +annotation_table = function(x){ |
| 182 | + res = paste(sprintf(annotation_template, "Coefficient", round(x$coef, 3)), |
| 183 | + sprintf(annotation_template, "FDR", sprintf("%.3g", x$fdr)), |
| 184 | + sprintf(annotation_template, "Bonferroni", sprintf("%.3g", x$bonferroni)), |
| 185 | + sprintf(annotation_template, "Length in probes", x$length), |
| 186 | + sprintf(annotation_template, "Length in bp", end(x) - start(x)) |
| 187 | + ) |
| 188 | + |
| 189 | + xx = as.data.frame(elementMetadata(x)) |
| 190 | + n = which(colnames(xx) == "bonferroni") |
| 191 | + |
| 192 | + if(!(n == ncol(xx))){ |
| 193 | + for(i in (n + 1):ncol(xx)){ |
| 194 | + res = paste(res, sprintf(annotation_template, colnames(xx)[i], xx[1, i]), sep = "\n") |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + return(res) |
| 199 | +} |
| 200 | + |
| 201 | + |
| 202 | +#' Generate the HTML report for the seqlm results |
| 203 | +#' |
| 204 | +#' Generate the HTML report for the seqlm results |
| 205 | +#' |
| 206 | +#' @param segments selection of significant regions by \code{\link{seqlm}} function |
| 207 | +#' @param values same values matrix that was used in \code{seqlm} |
| 208 | +#' @param genome_information same genome_information object that was used in \code{seqlm} |
| 209 | +#' @param annotation same annotation vector that was used in \code{seqlm} |
| 210 | +#' @param expand number of basepairs to extend the region on plot |
| 211 | +#' @param ylim two element vector giving the lower and higher limit of the y axis |
| 212 | +#' @param dir directory where to put the page, if the directory does not exist it will be created |
| 213 | +#' @param width picture width in inches |
| 214 | +#' @param height picture height in inches |
| 215 | +#' @param dpi dots per inch, to calibrate the picture size in pixels |
| 216 | +#' @param main title for the report |
| 217 | +#' |
| 218 | +#' @author Kaspar Martens <kmartens@@ut.ee> Raivo Kolde <rkolde@@gmail.com> |
| 219 | +#' |
| 220 | +#' @export |
| 221 | +seqlmreport = function(segments, values, genome_information, annotation, ylim = extendrange(values), dir = NA, expand = 100, width = 8, height = 5, dpi = 100, main = "seqlm results"){ |
| 222 | + # Create main directory |
| 223 | + if(!file.exists(dir)){ |
| 224 | + dir.create(dir) |
| 225 | + } |
| 226 | + |
| 227 | + # Create image directory |
| 228 | + img_dir = file.path(dir, "img") |
| 229 | + if(!file.exists(img_dir)){ |
| 230 | + dir.create(img_dir) |
| 231 | + } |
| 232 | + |
| 233 | + # Create images |
| 234 | + seqlmplots(segments, values, genome_information, annotation, ylim = ylim, dir = img_dir, expand = expand, width = width, height = height, dpi = dpi) |
| 235 | + |
| 236 | + # Create HTML file |
| 237 | + chunks = '' |
| 238 | + |
| 239 | + for(i in 1:length(segments)){ |
| 240 | + location = sprintf(location_template, seqnames(segments[i]), start(segments[i]), end(segments[i])) |
| 241 | + |
| 242 | + chunk = sprintf(chunk_template, i, location, annotation_table(segments[i]), sprintf("img/%d.png", i)) |
| 243 | + |
| 244 | + chunks = paste(chunks, chunk, sep = "\n\n") |
| 245 | + } |
| 246 | + |
| 247 | + page = sprintf(raport_template, main, main, chunks) |
| 248 | + |
| 249 | + cat(page, file = file.path(dir, "index.html")) |
| 250 | +} |
| 251 | + |
| 252 | + |
| 253 | +## |
0 commit comments