Mercurial > repos > marie-tremblay-metatoul > nmr_preprocessing
comparison DrawFunctions.R @ 7:122df1bf0a8c draft default tip
planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 3d328007dd7716848ec2eeb6c2a472f27eeb2995
| author | workflow4metabolomics | 
|---|---|
| date | Fri, 11 Jul 2025 08:33:38 +0000 | 
| parents | 5e64657b4fe5 | 
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 6:6e837e9352a2 | 7:122df1bf0a8c | 
|---|---|
| 1 require(ggplot2) | 1 library(ggplot2) # nice plots | 
| 2 require(gridExtra) | 2 library(gridExtra) # nice plots | 
| 3 require(reshape2) | 3 library(reshape2) # data manipulation | 
| 4 | 4 | 
| 5 | 5 Draw <- function(Signal_data, type.draw = c("signal", "pca"), output = c( | 
| 6 Draw <- function(Signal_data, type.draw = c("signal", "pca"), output = c("default", | 6 "default", | 
| 7 "window", "png", "pdf"), dirpath = ".", filename = "%003d", height = 480, | 7 "window", "png", "pdf" | 
| 8 ), dirpath = ".", filename = "%003d", height = 480, | |
| 8 width = 640, pdf.onefile = TRUE, ...) { | 9 width = 640, pdf.onefile = TRUE, ...) { | 
| 9 | 10 # Data initialisation and checks ---------------------------------------------- | 
| 10 # Data initialisation and checks ---------------------------------------------- | 11 type.draw <- match.arg(type.draw) | 
| 11 type.draw <- match.arg(type.draw) | 12 output <- match.arg(output) | 
| 12 output <- match.arg(output) | 13 fullpath <- paste(file.path(dirpath, filename), output, sep = ".") | 
| 13 fullpath <- paste(file.path(dirpath, filename), output, sep = ".") | 14 createFile <- TRUE | 
| 14 createFile <- TRUE | 15 createWindow <- FALSE | 
| 15 createWindow <- FALSE | 16 | 
| 16 | 17 # Drawing -------------------------------------------------------------------- | 
| 17 # Drawing -------------------------------------------------------------------- | 18 # output | 
| 18 # output | 19 switch(output, | 
| 19 switch(output, default = { | 20 default = { | 
| 20 createFile <- FALSE | 21 createFile <- FALSE | 
| 21 }, window = { | 22 }, | 
| 22 createWindow <- TRUE | 23 window = { | 
| 23 createFile <- FALSE | 24 createWindow <- TRUE | 
| 24 }, png = { | 25 createFile <- FALSE | 
| 25 grDevices::png(fullpath, width, height) | 26 }, | 
| 26 }, pdf = { | 27 png = { | 
| 27 grDevices::pdf(fullpath, width = width/72, height = height/72, | 28 grDevices::png(fullpath, width, height) | 
| 28 onefile = pdf.onefile) | 29 }, | 
| 29 }, { | 30 pdf = { | 
| 30 stop("Unknown output type.") | 31 grDevices::pdf(fullpath, | 
| 31 }) | 32 width = width / 72, height = height / 72, | 
| 32 | 33 onefile = pdf.onefile | 
| 33 # Drawing type (signal/spectrum or PCA) | 34 ) | 
| 34 funs <- list(signal = DrawSignal, pca = DrawPCA) | 35 }, | 
| 35 if (type.draw %in% names(funs)) { | 36 { | 
| 36 fun <- funs[[type.draw]] | 37 stop("Unknown output type.") | 
| 37 } else { | 38 } | 
| 38 stop(paste("Unknown type:", type.draw)) | 39 ) | 
| 39 } | 40 | 
| 40 | 41 # Drawing type (signal/spectrum or PCA) | 
| 41 # Plot finalisation ---------------------------------------------- | 42 funs <- list(signal = DrawSignal, pca = DrawPCA) | 
| 42 if (is.vector(Signal_data)) { | 43 if (type.draw %in% names(funs)) { | 
| 43 Signal_data <- vec2mat(Signal_data) | 44 fun <- funs[[type.draw]] | 
| 44 } | 45 } else { | 
| 45 fun(Signal_data, createWindow = createWindow, ...) | 46 stop(paste("Unknown type:", type.draw)) | 
| 46 if (createFile) { | 47 } | 
| 47 grDevices::dev.off() | 48 | 
| 48 } | 49 # Plot finalisation ---------------------------------------------- | 
| 50 if (is.vector(Signal_data)) { | |
| 51 Signal_data <- vec2mat(Signal_data) | |
| 52 } | |
| 53 fun(Signal_data, createWindow = createWindow, ...) | |
| 54 if (createFile) { | |
| 55 grDevices::dev.off() | |
| 56 } | |
| 49 } | 57 } | 
| 50 | 58 | 
| 51 | 59 | 
| 52 | 60 | 
| 53 ##### DrawSignal | 61 ##### DrawSignal | 
| 54 | 62 | 
| 55 DrawSignal <- function(Signal_data, subtype = c("stacked", "together", | 63 DrawSignal <- function(Signal_data, subtype = c( | 
| 56 "separate", "diffmean", "diffmedian", "diffwith"), | 64 "stacked", "together", | 
| 57 ReImModArg = c(TRUE, FALSE, FALSE, FALSE), vertical = T, | 65 "separate", "diffmean", "diffmedian", "diffwith" | 
| 58 xlab = "rowname", RowNames = NULL, row = 1, num.stacked = 4, | 66 ), | 
| 67 ReImModArg = c(TRUE, FALSE, FALSE, FALSE), vertical = T, | |
| 68 xlab = "rowname", RowNames = NULL, row = 1, num.stacked = 4, | |
| 59 main = NULL, createWindow) { | 69 main = NULL, createWindow) { | 
| 60 # nticks | 70 # nticks | 
| 61 | 71 | 
| 62 # Data initialisation and checks ---------------------------------------------- | 72 # Data initialisation and checks ---------------------------------------------- | 
| 63 | 73 | 
| 64 subtype <- match.arg(subtype) | 74 subtype <- match.arg(subtype) | 
| 65 vec <- is.vector(Signal_data) | 75 vec <- is.vector(Signal_data) | 
| 66 if (vec) { | 76 if (vec) { | 
| 67 Signal_data <- vec2mat(Signal_data) | 77 Signal_data <- vec2mat(Signal_data) | 
| 68 } | 78 } | 
| 69 | 79 | 
| 70 n <- nrow(Signal_data) | 80 n <- nrow(Signal_data) | 
| 71 m <- ncol(Signal_data) | 81 m <- ncol(Signal_data) | 
| 72 | 82 | 
| 73 if (n < num.stacked){ | 83 if (n < num.stacked) { | 
| 74 num.stacked <- n | 84 num.stacked <- n | 
| 75 } | 85 } | 
| 76 | 86 | 
| 77 scale <- colnames(Signal_data) | 87 scale <- colnames(Signal_data) | 
| 78 | 88 | 
| 79 num.plot <- sum(ReImModArg) | 89 num.plot <- sum(ReImModArg) | 
| 80 | 90 | 
| 81 Var <- rowname <- value <- NULL # only for R CMD check | 91 Var <- rowname <- value <- NULL # only for R CMD check | 
| 82 | 92 | 
| 83 # Drawing array | 93 # Drawing array | 
| 84 if (num.plot <= 0) { | 94 if (num.plot <= 0) { | 
| 85 stop("Nothing selected in ReImModArg.") | 95 stop("Nothing selected in ReImModArg.") | 
| 86 } else if (num.plot <= 2) { | 96 } else if (num.plot <= 2) { | 
| 87 if (vertical) { | 97 if (vertical) { | 
| 88 nrow <- num.plot | 98 nrow <- num.plot | 
| 89 ncol <- 1 | 99 ncol <- 1 | 
| 90 } else { | 100 } else { | 
| 91 nrow <- 1 | 101 nrow <- 1 | 
| 92 ncol <- num.plot | 102 ncol <- num.plot | 
| 93 } | 103 } | 
| 94 } else { | 104 } else { | 
| 95 nrow <- 2 | 105 nrow <- 2 | 
| 96 ncol <- 2 | 106 ncol <- 2 | 
| 97 } | 107 } | 
| 98 | 108 | 
| 99 # RowNames | 109 # RowNames | 
| 100 if (is.null(RowNames)) { | 110 if (is.null(RowNames)) { | 
| 101 RowNames <- rownames(Signal_data) | 111 RowNames <- rownames(Signal_data) | 
| 102 if (is.null(RowNames)) { | 112 if (is.null(RowNames)) { | 
| 103 RowNames <- 1:n | 113 RowNames <- 1:n | 
| 104 } | 114 } | 
| 105 } else { | 115 } else { | 
| 106 if (!is.vector(RowNames)) { | 116 if (!is.vector(RowNames)) { | 
| 107 stop("RowNames is not a vector") | 117 stop("RowNames is not a vector") | 
| 108 } | 118 } | 
| 109 if (length(RowNames) != n) { | 119 if (length(RowNames) != n) { | 
| 110 stop(paste("RowNames has length", length(RowNames), "and there are", n, "FIDs.")) | 120 stop(paste("RowNames has length", length(RowNames), "and there are", n, "FIDs.")) | 
| 111 } | 121 } | 
| 112 } | 122 } | 
| 113 | 123 | 
| 114 if (n == 1) { | 124 if (n == 1) { | 
| 115 RowNames <- deparse(substitute(Signal_data)) | 125 RowNames <- deparse(substitute(Signal_data)) | 
| 116 } | 126 } | 
| 117 | 127 | 
| 118 elements <- list() | 128 elements <- list() | 
| 119 if (ReImModArg[1]) { | 129 if (ReImModArg[1]) { | 
| 120 elements[["Re"]] <- Re(Signal_data) | 130 elements[["Re"]] <- Re(Signal_data) | 
| 121 rownames(elements[["Re"]]) <- RowNames | 131 rownames(elements[["Re"]]) <- RowNames | 
| 122 } | 132 } | 
| 123 if (ReImModArg[2]) { | 133 if (ReImModArg[2]) { | 
| 124 elements[["Im"]] <- Im(Signal_data) | 134 elements[["Im"]] <- Im(Signal_data) | 
| 125 rownames(elements[["Im"]]) <- RowNames | 135 rownames(elements[["Im"]]) <- RowNames | 
| 126 } | 136 } | 
| 127 if (ReImModArg[3]) { | 137 if (ReImModArg[3]) { | 
| 128 elements[["Mod"]] <- Mod(Signal_data) | 138 elements[["Mod"]] <- Mod(Signal_data) | 
| 129 rownames(elements[["Mod"]]) <- RowNames | 139 rownames(elements[["Mod"]]) <- RowNames | 
| 130 } | 140 } | 
| 131 if (ReImModArg[4]) { | 141 if (ReImModArg[4]) { | 
| 132 elements[["Arg"]] <- Arg(Signal_data) | 142 elements[["Arg"]] <- Arg(Signal_data) | 
| 133 rownames(elements[["Arg"]]) <- RowNames | 143 rownames(elements[["Arg"]]) <- RowNames | 
| 134 } | 144 } | 
| 135 | 145 | 
| 136 | 146 | 
| 137 | 147 | 
| 138 | 148 | 
| 139 # Drawing -------------------------------------------------------------------- | 149 # Drawing -------------------------------------------------------------------- | 
| 140 | 150 | 
| 141 y = x = NULL # only for R CMD check | 151 y <- x <- NULL # only for R CMD check | 
| 142 | 152 | 
| 143 | 153 | 
| 144 # SEPARATE or STACKED =============== | 154 # SEPARATE or STACKED =============== | 
| 145 if (subtype == "separate" | subtype == "stacked") { | 155 if (subtype == "separate" | subtype == "stacked") { | 
| 146 | 156 i <- 1 | 
| 147 i <- 1 | 157 while (i <= n) { | 
| 148 while (i <= n) { | 158 if (createWindow) { | 
| 149 if (createWindow) { | 159 grDevices::dev.new(noRStudioGD = TRUE) | 
| 150 grDevices::dev.new(noRStudioGD = TRUE) | 160 } | 
| 151 } | 161 if (subtype == "separate") { | 
| 152 if (subtype == "separate") { | 162 # The other uses gridExtra to do that | 
| 153 # The other uses gridExtra to do that | 163 graphics::par(mfrow = c(nrow, ncol)) | 
| 164 } | |
| 165 plots <- list() | |
| 166 if (subtype == "separate") { | |
| 167 last <- i | |
| 168 } else { | |
| 169 last <- min(i + num.stacked - 1, n) | |
| 170 } | |
| 171 for (name in names(elements)) { | |
| 172 if (subtype == "separate") { | |
| 173 if (n == 1) { | |
| 174 df <- data.frame(x = as.numeric(scale), y = elements[[name]]) | |
| 175 } else { | |
| 176 df <- data.frame(x = as.numeric(scale), y = elements[[name]][i, ]) | |
| 177 } | |
| 178 | |
| 179 plots[[name]] <- ggplot2::ggplot(data = df, ggplot2::aes(x = x, y = y)) + | |
| 180 ggplot2::geom_line(size = 1) + | |
| 181 ggplot2::theme(legend.position = "none") + | |
| 182 ggplot2::labs(x = xlab, y = name) + | |
| 183 ggplot2::ggtitle(RowNames[i]) + | |
| 184 ggplot2::theme_bw() | |
| 185 | |
| 186 if ((df[1, "x"] - df[(dim(df)[1]), "x"]) > 0) { | |
| 187 plots[[name]] <- plots[[name]] + ggplot2::scale_x_reverse() | |
| 188 } | |
| 189 } else { | |
| 190 if (n == 1) { | |
| 191 melted <- data.frame( | |
| 192 rowname = rep(name, m), | |
| 193 Var = as.numeric(scale), value = elements[[name]][i, ] | |
| 194 ) | |
| 195 } else if (last == i) { | |
| 196 melted <- data.frame( | |
| 197 rowname = rep(rownames(elements[[name]])[i], m), | |
| 198 Var = as.numeric(scale), value = elements[[name]][i, ] | |
| 199 ) | |
| 200 } else { | |
| 201 melted <- reshape2::melt(elements[[name]][i:last, ], varnames = c("rowname", "Var")) | |
| 202 } | |
| 203 | |
| 204 | |
| 205 plots[[name]] <- ggplot2::ggplot(data = melted, ggplot2::aes(x = Var, y = value)) + | |
| 206 ggplot2::geom_line(size = 0.3) + | |
| 207 ggplot2::facet_grid(rowname ~ ., scales = "free_y") + | |
| 208 ggplot2::theme(legend.position = "none") + | |
| 209 ggplot2::labs(x = xlab, y = name) + | |
| 210 ggplot2::ggtitle(label = main) + | |
| 211 ggplot2::theme_bw() | |
| 212 | |
| 213 if ((melted[1, "Var"] - melted[(dim(melted)[1]), "Var"]) > 0) { | |
| 214 plots[[name]] <- plots[[name]] + ggplot2::scale_x_reverse() | |
| 215 } | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 if (subtype == "stacked") { | |
| 220 do.call(gridExtra::grid.arrange, c(plots, list(nrow = nrow, ncol = ncol))) | |
| 221 } | |
| 222 | |
| 223 i <- last + 1 | |
| 224 } | |
| 225 } else if (subtype %in% c("together", "diffmean", "diffmedian", "diffwith")) { | |
| 226 # TOGHETER or DIFFMEAN or DIFFMEDIAN or DIFFWITH =============== | |
| 227 | |
| 228 rainbow_colors <- grDevices::rainbow(n) | |
| 229 | |
| 230 if (createWindow) { | |
| 231 grDevices::dev.new(noRStudioGD = TRUE) | |
| 232 } | |
| 154 graphics::par(mfrow = c(nrow, ncol)) | 233 graphics::par(mfrow = c(nrow, ncol)) | 
| 155 } | 234 | 
| 156 plots <- list() | 235 plots <- list() | 
| 157 if (subtype == "separate") { | 236 | 
| 158 last <- i | 237 # Loop for Re, Im, Mod and Arg | 
| 159 } else { | 238 for (name in names(elements)) { | 
| 160 last <- min(i + num.stacked - 1, n) | 239 # Get this part of the signal | 
| 161 } | 240 element <- elements[[name]] | 
| 162 for (name in names(elements)) { | 241 | 
| 163 if (subtype == "separate") { | 242 # Express the signal according to a reference if asked by `subtype' | 
| 164 if (n == 1) { | 243 if (subtype == "diffmean") { | 
| 165 df <- data.frame(x = as.numeric(scale), y = elements[[name]]) | 244 element <- sweep(element, MARGIN = 2, colMeans(element), `-`) | 
| 166 } else {df <- data.frame(x = as.numeric(scale), y = elements[[name]][i, ]) | 245 } else if (subtype == "diffmedian") { | 
| 167 } | 246 element <- sweep(element, MARGIN = 2, matrixStats::colMedians(element), `-`) | 
| 168 | 247 } else if (subtype == "diffwith") { | 
| 169 plots[[name]] <- ggplot2::ggplot(data = df, ggplot2::aes(x = x, y = y)) + | 248 element <- sweep(element, MARGIN = 2, element[row, ], `-`) | 
| 170 ggplot2::geom_line(size = 1) + | 249 if (row == 1 & n > 1) { | 
| 171 ggplot2::theme(legend.position = "none") + | 250 # Since we use plot on the first row and lines on the following, the y | 
| 172 ggplot2::labs(x = xlab, y = name) + | 251 # scale is calculated at the first row so if the first row is all 0, it | 
| 173 ggplot2::ggtitle(RowNames[i]) + | 252 # causes problems | 
| 174 ggplot2::theme_bw() | 253 tmp <- element[1, ] | 
| 175 | 254 element[1, ] <- element[2, ] | 
| 176 if ((df[1, "x"] - df[(dim(df)[1]), "x"]) > 0) { | 255 element[2, ] <- tmp | 
| 177 plots[[name]] <- plots[[name]] + ggplot2::scale_x_reverse() | 256 } | 
| 178 } | 257 } | 
| 179 | 258 melted <- reshape2::melt(elements[[name]][i:last, ], varnames = c("rowname", "Var")) | 
| 180 } else { | 259 | 
| 181 | 260 plots[[name]] <- ggplot2::ggplot(melted, ggplot2::aes( | 
| 182 if (n == 1 ) { | 261 x = Var, | 
| 183 melted <- data.frame(rowname = rep(name, m), | 262 y = value, group = rowname, colour = rowname | 
| 184 Var = as.numeric(scale), value = elements[[name]][i,]) | 263 )) + | 
| 185 } else if (last==i){ | 264 ggplot2::geom_line() + | 
| 186 melted <- data.frame(rowname = rep(rownames(elements[[name]])[i], m), | 265 ggplot2::labs(x = xlab, y = name) + | 
| 187 Var = as.numeric(scale), value = elements[[name]][i,]) | 266 ggplot2::scale_colour_discrete(name = NULL) + | 
| 188 } else {melted <- reshape2::melt(elements[[name]][i:last, ], | 267 ggplot2::ggtitle(main) | 
| 189 varnames = c("rowname", "Var")) | 268 | 
| 190 } | 269 if ((melted[1, "Var"] - melted[(dim(melted)[1]), "Var"]) > | 
| 191 | 270 0) { | 
| 192 | 271 plots[[name]] <- plots[[name]] + ggplot2::scale_x_reverse() | 
| 193 plots[[name]] <- ggplot2::ggplot(data = melted, ggplot2::aes(x = Var, y = value)) + | 272 } | 
| 194 ggplot2::geom_line(size = 0.3) + | 273 | 
| 195 ggplot2::facet_grid(rowname ~ ., scales = "free_y") + | 274 do.call(gridExtra::grid.arrange, c(plots, list( | 
| 196 ggplot2::theme(legend.position = "none") + | 275 nrow = nrow, | 
| 197 ggplot2::labs(x = xlab, y = name) + | 276 ncol = ncol | 
| 198 ggplot2::ggtitle(label = main) + | 277 ))) | 
| 199 ggplot2::theme_bw() | 278 } | 
| 200 | 279 } | 
| 201 if ((melted[1, "Var"] - melted[(dim(melted)[1]), "Var"]) > 0) { | |
| 202 plots[[name]] <- plots[[name]] + ggplot2::scale_x_reverse() | |
| 203 } | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 if (subtype == "stacked") { | |
| 208 do.call(gridExtra::grid.arrange, c(plots, list(nrow = nrow, ncol = ncol))) | |
| 209 } | |
| 210 | |
| 211 i <- last + 1 | |
| 212 } | |
| 213 } else if (subtype %in% c("together", "diffmean", "diffmedian", "diffwith")) { | |
| 214 | |
| 215 # TOGHETER or DIFFMEAN or DIFFMEDIAN or DIFFWITH =============== | |
| 216 | |
| 217 rainbow_colors <- grDevices::rainbow(n) | |
| 218 | |
| 219 if (createWindow) { | |
| 220 grDevices::dev.new(noRStudioGD = TRUE) | |
| 221 } | |
| 222 graphics::par(mfrow = c(nrow, ncol)) | |
| 223 | |
| 224 plots <- list() | |
| 225 | |
| 226 # Loop for Re, Im, Mod and Arg | |
| 227 for (name in names(elements)) { | |
| 228 # Get this part of the signal | |
| 229 element <- elements[[name]] | |
| 230 | |
| 231 # Express the signal according to a reference if asked by `subtype' | |
| 232 if (subtype == "diffmean") { | |
| 233 element <- sweep(element, MARGIN = 2, colMeans(element), `-`) | |
| 234 } else if (subtype == "diffmedian") { | |
| 235 element <- sweep(element, MARGIN = 2, matrixStats::colMedians(element), `-`) | |
| 236 } else if (subtype == "diffwith") { | |
| 237 element <- sweep(element, MARGIN = 2, element[row, ], `-`) | |
| 238 if (row == 1 & n > 1) { | |
| 239 # Since we use plot on the first row and lines on the following, the y | |
| 240 # scale is calculated at the first row so if the first row is all 0, it | |
| 241 # causes problems | |
| 242 tmp <- element[1, ] | |
| 243 element[1, ] <- element[2, ] | |
| 244 element[2, ] <- tmp | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 | |
| 249 melted <- reshape2::melt(elements[[name]], varnames = c("rowname", "Var")) | |
| 250 | |
| 251 | |
| 252 plots[[name]] <- ggplot2::ggplot(melted, ggplot2::aes(x = Var, | |
| 253 y = value, group = rowname, colour = rowname)) + ggplot2::geom_line() + | |
| 254 ggplot2::labs(x = xlab, y = name) + ggplot2::scale_colour_discrete(name = NULL) + | |
| 255 ggplot2::ggtitle(main) | |
| 256 | |
| 257 if ((melted[1, "Var"] - melted[(dim(melted)[1]), "Var"]) > | |
| 258 0) { | |
| 259 plots[[name]] <- plots[[name]] + ggplot2::scale_x_reverse() | |
| 260 } | |
| 261 | |
| 262 do.call(gridExtra::grid.arrange, c(plots, list(nrow = nrow, | |
| 263 ncol = ncol))) | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 | |
| 268 } | 280 } | 
