comparison quality_report.xml @ 8:bb9500286fe4 draft

"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cardinal commit f986c51abe33c7f622d429a3c4a79ee24b33c1f3"
author galaxyp
date Thu, 23 Apr 2020 08:11:44 -0400
parents f0d1f3e97303
children 0d4d4f16d455
comparison
equal deleted inserted replaced
7:74b61bf5bc4b 8:bb9500286fe4
1 <tool id="cardinal_quality_report" name="MSI Qualitycontrol" version="@VERSION@.4"> 1 <tool id="cardinal_quality_report" name="MSI Qualitycontrol" version="@VERSION@.0">
2 <description> 2 <description>
3 mass spectrometry imaging QC 3 mass spectrometry imaging QC
4 </description> 4 </description>
5 <macros> 5 <macros>
6 <import>macros.xml</import> 6 <import>macros.xml</import>
7 </macros> 7 </macros>
8 <expand macro="requirements"> 8 <expand macro="requirements">
9 <requirement type="package" version="2.3">r-gridextra</requirement>
10 <requirement type="package" version="3.2.1">r-ggplot2</requirement>
9 <requirement type="package" version="1.1_2">r-rcolorbrewer</requirement> 11 <requirement type="package" version="1.1_2">r-rcolorbrewer</requirement>
10 <requirement type="package" version="2.3">r-gridextra</requirement> 12 <requirement type="package" version="2.23_16">r-kernsmooth</requirement>
11 <requirement type="package" version="3.0">r-ggplot2</requirement> 13 <requirement type="package" version="1.1.0">r-scales</requirement>
12 <requirement type="package" version="2.23_15">r-kernsmooth</requirement> 14 <requirement type="package" version="1.0.12"> r-pheatmap</requirement>
13 <requirement type="package" version="1.0.0">r-scales</requirement>
14 <requirement type="package" version="1.0.10"> r-pheatmap</requirement>
15 </expand> 15 </expand>
16 <command detect_errors="exit_code"> 16 <command detect_errors="exit_code">
17 <![CDATA[ 17 <![CDATA[
18 @INPUT_LINKING@ 18 @INPUT_LINKING@
19 cat '${cardinal_qualitycontrol_script}' && 19 cat '${cardinal_qualitycontrol_script}' &&
31 library(gridExtra) 31 library(gridExtra)
32 library(KernSmooth) 32 library(KernSmooth)
33 library(scales) 33 library(scales)
34 library(pheatmap) 34 library(pheatmap)
35 35
36 36 @READING_MSIDATA@
37 @READING_MSIDATA_INRAM@ 37
38 38 ## in case RData input is MSImageSet:
39 if (class(msidata) == "MSImageSet"){
40 msidata = as(msidata, "MSImagingExperiment")
41 run(msidata) = "infile"
42 }
43 print(class(msidata))
39 ## remove duplicated coordinates 44 ## remove duplicated coordinates
40 msidata <- msidata[,!duplicated(coord(msidata))] 45 msidata <- msidata[,!duplicated(coord(msidata))]
41 46
42 ## optional annotation from tabular file to obtain pixel groups (otherwise all pixels are considered to be one sample) 47 ## optional annotation from tabular file to obtain pixel groups (otherwise all pixels are considered to be one sample)
43 48
48 annotation_input = input_tabular[,c($tabular_annotation.column_x, $tabular_annotation.column_y, $tabular_annotation.column_names)] 53 annotation_input = input_tabular[,c($tabular_annotation.column_x, $tabular_annotation.column_y, $tabular_annotation.column_names)]
49 annotation_name = colnames(annotation_input)[3] ##extract header for annotations to later export tabular with same name 54 annotation_name = colnames(annotation_input)[3] ##extract header for annotations to later export tabular with same name
50 colnames(annotation_input) = c("x", "y", "annotation") ## rename annotations header to default name "annotation" 55 colnames(annotation_input) = c("x", "y", "annotation") ## rename annotations header to default name "annotation"
51 56
52 ## merge with coordinate information of msidata 57 ## merge with coordinate information of msidata
53 msidata_coordinates = cbind(coord(msidata)[,1:2], c(1:ncol(msidata))) 58 msidata_coordinates = data.frame(coord(msidata)\$x, coord(msidata)\$y, c(1:ncol(msidata)))
54 colnames(msidata_coordinates)[3] = "pixel_index" 59 colnames(msidata_coordinates) = c("x", "y", "pixel_index")
55 merged_annotation = merge(msidata_coordinates, annotation_input, by=c("x", "y"), all.x=TRUE) 60 merged_annotation = merge(msidata_coordinates, annotation_input, by=c("x", "y"), all.x=TRUE)
56 merged_annotation[is.na(merged_annotation)] = "NA" 61 merged_annotation[is.na(merged_annotation)] = "NA"
57 merged_annotation = merged_annotation[order(merged_annotation\$pixel_index),] 62 merged_annotation = merged_annotation[order(merged_annotation\$pixel_index),]
58 msidata\$annotation = as.factor(merged_annotation[,4]) 63 msidata\$annotation = as.factor(merged_annotation[,4])
59 64
153 158
154 ####################### II) x-y images ####################################### 159 ####################### II) x-y images #######################################
155 ############################################################################## 160 ##############################################################################
156 print("x-y images") 161 print("x-y images")
157 162
163
158 ## only do plots for file with intensity peaks 164 ## only do plots for file with intensity peaks
159 165
160 if (npeaks > 0){ 166 if (npeaks > 0){
161 167
162 ## function for density plots 168 ## function for density plots
179 185
180 if (!is.null(levels(msidata\$annotation))){ 186 if (!is.null(levels(msidata\$annotation))){
181 187
182 number_combined = length(levels(msidata\$annotation)) 188 number_combined = length(levels(msidata\$annotation))
183 189
184 position_df = cbind(coord(msidata)[,1:2], msidata\$annotation) 190 position_df = data.frame(coord(msidata)\$x, coord(msidata)\$y, msidata\$annotation)
185 colnames(position_df)[3] = "annotation" 191 colnames(position_df) = c("x", "y","annotation")
186 192
187 combine_plot = ggplot(position_df, aes(x=x, y=y, fill=annotation))+ 193 combine_plot = ggplot(position_df, aes(x=x, y=y, fill=annotation))+
188 geom_tile() + 194 geom_tile() +
189 coord_fixed()+ 195 coord_fixed()+
190 ggtitle("Spatial orientation of pixel annotations")+ 196 ggtitle("Spatial orientation of pixel annotations")+
209 } 215 }
210 216
211 ################### 1) Pixel order image ################################### 217 ################### 1) Pixel order image ###################################
212 218
213 pixelnumber = 1:pixelcount 219 pixelnumber = 1:pixelcount
214 pixelxyarray=cbind(coord(msidata)[,1:2],pixelnumber) 220 pixelxyarray=data.frame(coord(msidata)\$x, coord(msidata)\$y,pixelnumber)
221 colnames(pixelxyarray) = c("x", "y", "pixelnumber")
215 gg_title = "Pixel order" 222 gg_title = "Pixel order"
216 223
217 print(ggplot(pixelxyarray, aes(x=x, y=y, fill=pixelnumber))+ 224 print(ggplot(pixelxyarray, aes(x=x, y=y, fill=pixelnumber))+
218 geom_tile() + coord_fixed()+ 225 geom_tile() + coord_fixed()+
219 ggtitle(gg_title) + theme_bw()+ 226 ggtitle(gg_title) + theme_bw()+
241 248
242 for (mass in 1:length(inputcalibrantmasses)){ 249 for (mass in 1:length(inputcalibrantmasses)){
243 250
244 filtered_data = msidata[mz(msidata) >= inputcalibrantmasses[mass]-plusminusvalues[mass] & mz(msidata) <= inputcalibrantmasses[mass]+plusminusvalues[mass],] 251 filtered_data = msidata[mz(msidata) >= inputcalibrantmasses[mass]-plusminusvalues[mass] & mz(msidata) <= inputcalibrantmasses[mass]+plusminusvalues[mass],]
245 252
246 if (nrow(filtered_data) > 1 & sum(spectra(filtered_data),na.rm=TRUE) > 0){ 253 if (nrow(filtered_data) > 0 & sum(spectra(filtered_data),na.rm=TRUE) > 0){
247 254
248 ## intensity of all m/z > 0 255 ## intensity of all m/z > 0
249 intensity_sum = colSums(spectra(filtered_data), na.rm=TRUE) > 0 256 intensity_sum = colSums(spectra(filtered_data), na.rm=TRUE) > 0
250 257
251 }else if(nrow(filtered_data) == 1 & sum(spectra(filtered_data), na.rm=TRUE) > 0){ 258 ###}else if(nrow(filtered_data) == 1 & sum(spectra(filtered_data), na.rm=TRUE) > 0){
252 259
253 ## intensity of only m/z > 0 260 ## intensity of only m/z > 0
254 intensity_sum = spectra(filtered_data) > 0 261 intensity_sum = colSums(spectra(filtered_data), na.rm=TRUE) > 0
255 262
256 }else{ 263 }else{
257 264
258 intensity_sum = rep(FALSE, ncol(filtered_data))} 265 intensity_sum = rep(FALSE, ncol(filtered_data))}
259 266
261 pixelmatrix = rbind(pixelmatrix, intensity_sum) 268 pixelmatrix = rbind(pixelmatrix, intensity_sum)
262 } 269 }
263 270
264 ## for each pixel count TRUE (each calibrant m/z range with intensity > 0 is TRUE) 271 ## for each pixel count TRUE (each calibrant m/z range with intensity > 0 is TRUE)
265 countvector= as.factor(colSums(pixelmatrix, na.rm=TRUE)) 272 countvector= as.factor(colSums(pixelmatrix, na.rm=TRUE))
266 countdf= cbind(coord(msidata)[,1:2], countvector) ## add pixel coordinates to counts 273 countdf= data.frame(coord(msidata)\$x, coord(msidata)\$y, countvector) ## add pixel coordinates to counts
274 colnames(countdf) = c("x", "y", "countvector")
267 mycolours = brewer.pal(9, "Set1") 275 mycolours = brewer.pal(9, "Set1")
268 276
269 print(ggplot(countdf, aes(x=x, y=y, fill=countvector))+ 277 print(ggplot(countdf, aes(x=x, y=y, fill=countvector))+
270 geom_tile() + coord_fixed() + 278 geom_tile() + coord_fixed() +
271 ggtitle(paste0("Number of calibrants per pixel (±",$plusminus_ppm, " ppm)")) + 279 ggtitle(paste0("Number of calibrants per pixel (±",$plusminus_ppm, " ppm)")) +
306 mzup1 = features(msidata, mz = mass1+3) 314 mzup1 = features(msidata, mz = mass1+3)
307 mzdown2 = features(msidata, mz = mass2-2) 315 mzdown2 = features(msidata, mz = mass2-2)
308 mzup2 = features(msidata, mz = mass2+3) 316 mzup2 = features(msidata, mz = mass2+3)
309 317
310 ### plot for first m/z 318 ### plot for first m/z
311 par(mfrow=c(2,1), oma=c(0,0,2,0)) 319 par(oma=c(0,0,2,0))
312 plot(msidata[mzdown1:mzup1,], pixel = 1:pixelcount, main=paste0("Average spectrum ", mass1, " Da")) 320 print(plot(msidata[mzdown1:mzup1,], run="infile", layout=c(2,1), strip=FALSE, main=paste0("Average spectrum ", mass1, " Da")))
313 abline(v=c(mass1-distance1, mass1, mass1+distance1), col="blue",lty=c(3,6,3)) 321 abline(v=c(mass1-distance1, mass1, mass1+distance1), col="blue",lty=c(3,6,3))
314 322
315 ### plot for second m/z 323 ### plot for second m/z
316 plot(msidata[mzdown2:mzup2,], pixel = 1:pixelcount, main= paste0("Average spectrum ", mass2, " Da")) 324 print(plot(msidata[mzdown2:mzup2,], run="infile", layout=FALSE, strip=FALSE, main= paste0("Average spectrum ", mass2, " Da")))
317 abline(v=c(mass2-distance2, mass2, mass2+distance2), col="blue", lty=c(3,6,3)) 325 abline(v=c(mass2-distance2, mass2, mass2+distance2), col="blue", lty=c(3,6,3))
318 title("Control of fold change plot", outer=TRUE) 326 title("Control of fold change plot", outer=TRUE)
319 327
320 ### filter spectra for max m/z to have two vectors, which can be divided 328 ### filter spectra for max m/z to have two vectors, which can be divided
321 ### plot spatial distribution of fold change 329 ### plot spatial distribution of fold change
322 330
323 ## calculate mean intensity for each m/z over the ppm range; then calculate log2 foldchange 331 ## calculate mean intensity for each m/z over the ppm range; then calculate log2 foldchange
324 mass1vector = colMeans(spectra(filtered_data1), na.rm =TRUE) 332 mass1vector = colMeans(spectra(filtered_data1), na.rm =TRUE)
325 mass2vector = colMeans(spectra(filtered_data2), na.rm = TRUE) 333 mass2vector = colMeans(spectra(filtered_data2), na.rm = TRUE)
326 foldchange= log2(mass1vector/mass2vector) 334 foldchange= log2(mass1vector/mass2vector)
327 fcmatrix = cbind(foldchange, coord(msidata)[,1:2]) 335 fcmatrix = data.frame(coord(msidata)\$x, coord(msidata)\$y,foldchange)
336 colnames(fcmatrix) = c("x", "y", "foldchange")
328 337
329 print(ggplot(fcmatrix, aes(x=x, y=y, fill=foldchange))+ 338 print(ggplot(fcmatrix, aes(x=x, y=y, fill=foldchange))+
330 geom_tile() + coord_fixed()+ 339 geom_tile() + coord_fixed()+
331 ggtitle("$label")+ 340 ggtitle("$label")+
332 theme_bw()+ 341 theme_bw()+
343 352
344 #end for 353 #end for
345 #end if 354 #end if
346 355
347 #################### 4) m/z heatmaps ####################################### 356 #################### 4) m/z heatmaps #######################################
348
349 par(mfrow=c(1,1), mar=c(5.1, 4.1, 4.1, 2.1), mgp=c(3, 1, 0), las=0) 357 par(mfrow=c(1,1), mar=c(5.1, 4.1, 4.1, 2.1), mgp=c(3, 1, 0), las=0)
350 if (length(inputcalibrants[,1]) != 0){ 358 if (length(inputcalibrants[,1]) != 0){
351 for (mass in 1:length(inputcalibrants[,1])){ 359 for (mass in 1:length(inputcalibrants[,1])){
352 360 par(oma=c(0,0,0,1))## margin for image legend
353 361
354 image(msidata, mz=inputcalibrants[,1][mass], plusminus=plusminusvalues[mass], 362 tryCatch(
363 {
364 print(image(msidata, mz=inputcalibrants[,1][mass], plusminus=plusminusvalues[mass],
355 main= paste0(inputcalibrants[,2][mass], ": ", round(inputcalibrants[,1][mass], digits = 2)," (±",$plusminus_ppm, " ppm)"), 365 main= paste0(inputcalibrants[,2][mass], ": ", round(inputcalibrants[,1][mass], digits = 2)," (±",$plusminus_ppm, " ppm)"),
356 contrast.enhance = "histogram", ylim= c(maximumy+0.2*maximumy,minimumy-1)) 366 contrast.enhance = "histogram", strip=FALSE, ylim= c(maximumy,minimumy)))
367 },
368 error=function(cond) {
369 ## if there are not enough intensities in the mz range skip creating an image
370 print(paste0("Not enough intensities > 0 for m/z ", inputcalibrants[,1][mass]))
371 }
372 )
357 } 373 }
358 } else {print("4) The input peptide and calibrant m/z were not provided or outside the m/z range")} 374 } else {print("4) The input peptide and calibrant m/z were not provided or outside the m/z range")}
359 375
360 #################### 5) Number of peaks per pixel - image ################## 376 #################### 5) Number of peaks per pixel - image ##################
361 377
362 ## here every intensity value > 0 counts as peak 378 ## here every intensity value > 0 counts as peak
363 peaksperpixel = colSums(spectra(msidata)> 0, na.rm=TRUE) 379 peaksperpixel = colSums(spectra(msidata)> 0, na.rm=TRUE)
364 peakscoordarray=cbind(coord(msidata)[,1:2], peaksperpixel) 380 peakscoordarray=data.frame(coord(msidata)\$x, coord(msidata)\$y, peaksperpixel)
381 colnames(peakscoordarray) = c("x", "y", "peaksperpixel")
365 382
366 print(ggplot(peakscoordarray, aes(x=x, y=y, fill=peaksperpixel))+ 383 print(ggplot(peakscoordarray, aes(x=x, y=y, fill=peaksperpixel))+
367 geom_tile() + coord_fixed() + 384 geom_tile() + coord_fixed() +
368 ggtitle("Number of peaks per spectrum")+ 385 ggtitle("Number of peaks per spectrum")+
369 theme_bw() + 386 theme_bw() +
377 gc() 394 gc()
378 395
379 396
380 ############################### 6) TIC image ############################### 397 ############################### 6) TIC image ###############################
381 398
382 TICcoordarray=cbind(coord(msidata)[,1:2], TICs) 399 TICcoordarray=data.frame(coord(msidata)\$x, coord(msidata)\$y, TICs)
400 colnames(TICcoordarray) = c("x", "y", "peaksperpixel")
383 401
384 print(ggplot(TICcoordarray, aes(x=x, y=y, fill=TICs))+ 402 print(ggplot(TICcoordarray, aes(x=x, y=y, fill=TICs))+
385 geom_tile() + coord_fixed() + 403 geom_tile() + coord_fixed() +
386 ggtitle("Total Ion Current")+ 404 ggtitle("Total Ion Current")+
387 theme_bw() + 405 theme_bw() +
394 rm(TICcoordarray) 412 rm(TICcoordarray)
395 gc() 413 gc()
396 414
397 ############################### 6b) median int image ############################### 415 ############################### 6b) median int image ###############################
398 416
399 median_int = apply(spectra(msidata),2,median) 417 median_int = pixelApply(msidata, median)
400 median_coordarray=cbind(coord(msidata)[,1:2], median_int) 418
401 419 median_coordarray=data.frame(coord(msidata)\$x, coord(msidata)\$y, median_int)
420 colnames(median_coordarray) = c("x", "y", "median_int")
402 print(ggplot(median_coordarray, aes(x=x, y=y, fill=median_int))+ 421 print(ggplot(median_coordarray, aes(x=x, y=y, fill=median_int))+
403 geom_tile() + coord_fixed() + 422 geom_tile() + coord_fixed() +
404 ggtitle("Median intensity per spectrum")+ 423 ggtitle("Median intensity per spectrum")+
405 theme_bw() + 424 theme_bw() +
406 theme(plot.title = element_text(hjust = 0.5))+ 425 theme(plot.title = element_text(hjust = 0.5))+
412 rm(median_coordarray) 431 rm(median_coordarray)
413 gc() 432 gc()
414 433
415 ############################### 6c) max int image ############################### 434 ############################### 6c) max int image ###############################
416 435
417 max_int = apply(spectra(msidata),2,max) 436 max_int = pixelApply(msidata, max)
418 max_coordarray=cbind(coord(msidata)[,1:2], max_int) 437
419 438 max_coordarray=data.frame(coord(msidata)\$x, coord(msidata)\$y, max_int)
439 colnames(max_coordarray) = c("x", "y", "max_int")
420 print(ggplot(max_coordarray, aes(x=x, y=y, fill=max_int))+ 440 print(ggplot(max_coordarray, aes(x=x, y=y, fill=max_int))+
421 geom_tile() + coord_fixed() + 441 geom_tile() + coord_fixed() +
422 ggtitle("Maximum intensity per spectrum")+ 442 ggtitle("Maximum intensity per spectrum")+
423 theme_bw() + 443 theme_bw() +
424 theme(plot.title = element_text(hjust = 0.5))+ 444 theme(plot.title = element_text(hjust = 0.5))+
431 gc() 451 gc()
432 452
433 ############################### 7) Most abundant m/z image ################# 453 ############################### 7) Most abundant m/z image #################
434 454
435 ## for each spectrum find the row (m/z) with the highest intensity 455 ## for each spectrum find the row (m/z) with the highest intensity
436 highestmz = apply(spectra(msidata),2,which.max) 456 highestmz = pixelApply(msidata, which.max)
457
437 ## in case for some spectra max returns integer(0), highestmz is a list and integer(0) have to be replaced with NA and unlisted 458 ## in case for some spectra max returns integer(0), highestmz is a list and integer(0) have to be replaced with NA and unlisted
438 if (class(highestmz) == "list"){ 459 if (class(highestmz) == "list"){
439 ##find zero-length values 460 ##find zero-length values
440 zero_entry <- !(sapply(highestmz, length)) 461 zero_entry <- !(sapply(highestmz, length))
441 ### replace these values with NA 462 ### replace these values with NA
442 highestmz[zero_entry] <- NA 463 highestmz[zero_entry] <- NA
443 ### unlist list to get a vector 464 ### unlist list to get a vector
444 highestmz = unlist(highestmz)} 465 highestmz = unlist(highestmz)}
445 466
446 highestmz_matrix = cbind(coord(msidata)[,1:2],mz(msidata)[highestmz]) 467 highestmz_matrix = data.frame(coord(msidata)\$x, coord(msidata)\$y,mz(msidata)[highestmz])
447 colnames(highestmz_matrix)[3] = "highestmzinDa" 468 colnames(highestmz_matrix) = c("x", "y", "highestmzinDa")
448 469
449 print(ggplot(highestmz_matrix, aes(x=x, y=y, fill=highestmzinDa))+ 470 print(ggplot(highestmz_matrix, aes(x=x, y=y, fill=highestmzinDa))+
450 geom_tile() + coord_fixed() + 471 geom_tile() + coord_fixed() +
451 ggtitle("Most abundant m/z in each spectrum")+ 472 ggtitle("Most abundant m/z in each spectrum")+
452 theme_bw() + 473 theme_bw() +
466 487
467 set.seed(1) 488 set.seed(1)
468 pca = PCA(msidata, ncomp=2) 489 pca = PCA(msidata, ncomp=2)
469 490
470 ## plot overview image and plot and PC1 and 2 images 491 ## plot overview image and plot and PC1 and 2 images
471 par(mfrow = c(2,1)) 492 print(plot(pca, col=c("black", "darkgrey"), main="PCA for two components", layout=c(2,1), strip=FALSE))
472 plot(pca, col=c("black", "darkgrey"), main="PCA for two components") 493 print(image(pca, run="infile", col=c("black", "white"), strip=FALSE, ylim= c(maximumy+0.2*maximumy,minimumy-1), layout=FALSE))
473 image(pca, col=c("black", "white"), strip=FALSE, ylim= c(maximumy+0.2*maximumy,minimumy-1)) 494
474 495 par(oma=c(0,0,0,1))## margin for image legend
475 for (PCs in 1:2){ 496 print(image(pca, column = "PC1" , strip=FALSE, superpose = FALSE, main="PC1", col.regions = risk.colors(100), layout=c(2,1), ylim= c(maximumy+0.2*maximumy,minimumy-1)))
476 print(image(pca, column = c(paste0("PC",PCs)) , strip=FALSE, superpose = FALSE, main=paste0("PC", PCs), col.regions = risk.colors(100), ylim=c(maximumy+2, minimumy-2)))} 497 print(image(pca, column = "PC2" , strip=FALSE, superpose = FALSE, main="PC2", col.regions = risk.colors(100), layout=FALSE, ylim= c(maximumy+0.2*maximumy,minimumy-1)))
477
478 ## remove pca to clean up RAM space 498 ## remove pca to clean up RAM space
479 rm(pca) 499 rm(pca)
480 gc() 500 gc()
481 501
482 #end if 502 #end if
611 ########################## 14) Intensity distribution ###################### 631 ########################## 14) Intensity distribution ######################
612 632
613 par(mfrow = c(2,1), mar=c(5,6,4,2)) 633 par(mfrow = c(2,1), mar=c(5,6,4,2))
614 634
615 ## 14a) Median intensity over spectra 635 ## 14a) Median intensity over spectra
616 636 medianint_spectra = apply(spectra(msidata), 2, median, na.rm=TRUE)
617 medianint_spectra = apply(spectra(msidata), 2, median, na.rm=TRUE)
618 plot(medianint_spectra, main="Median intensity per spectrum",las=1, xlab="Spectra index", ylab="") 637 plot(medianint_spectra, main="Median intensity per spectrum",las=1, xlab="Spectra index", ylab="")
619 title(ylab="Median spectrum intensity", line=4) 638 title(ylab="Median spectrum intensity", line=4)
620 if (!is.null(levels(msidata\$annotation))){ 639 if (!is.null(levels(msidata\$annotation))){
621 abline(v=abline_vector, lty = 3)} 640 abline(v=abline_vector, lty = 3)}
622 641
623 ## 14b) histogram: 642 ## 14b) histogram:
624 hist(spectra(msidata), main="", xlab = "", ylab="", las=1) 643 hist(as.matrix(spectra(msidata)), main="", xlab = "", ylab="", las=1)
625 title(main="Intensity histogram", line=2) 644 title(main="Intensity histogram", line=2)
626 title(xlab="intensities") 645 title(xlab="intensities")
627 title(ylab="Frequency", line=4) 646 title(ylab="Frequency", line=4)
628 abline(v=median(spectra(msidata)[(spectra(msidata)>0)], na.rm=TRUE), col="blue") 647 abline(v=median(as.matrix(spectra(msidata))[(as.matrix(spectra(msidata))>0)], na.rm=TRUE), col="blue")
629 648
630 649
631 ## 14c) histogram to show contribution of annotation groups 650 ## 14c) histogram to show contribution of annotation groups
632 651
633 if (!is.null(levels(msidata\$annotation))){ 652 if (!is.null(levels(msidata\$annotation))){
666 ## 14e) Heatmap of pearson correlation on mean intensities between annotation groups 685 ## 14e) Heatmap of pearson correlation on mean intensities between annotation groups
667 686
668 corr_matrix = mean_matrix 687 corr_matrix = mean_matrix
669 corr_matrix[corr_matrix == 0] <- NA 688 corr_matrix[corr_matrix == 0] <- NA
670 colnames(corr_matrix) = levels(msidata\$annotation) 689 colnames(corr_matrix) = levels(msidata\$annotation)
690
671 ## pearson correlation is only possible if there are at least 2 groups 691 ## pearson correlation is only possible if there are at least 2 groups
672 if (length(colnames)>1) 692 if (length(colnames)>1)
673 { 693 {
674 corr_matrix = cor(corr_matrix, method= "pearson",use="complete.obs") 694 corr_matrix = cor(corr_matrix, method= "pearson",use="complete.obs")
675 695
686 706
687 ############################ 15) Mass spectra ############################## 707 ############################ 15) Mass spectra ##############################
688 708
689 ## replace any NA with 0, otherwise plot function will not work at all 709 ## replace any NA with 0, otherwise plot function will not work at all
690 msidata_no_NA = msidata 710 msidata_no_NA = msidata
691 spectra(msidata_no_NA)[is.na(spectra(msidata_no_NA))] = 0
692 711
693 ## find three equal m/z ranges for the average mass spectra plots: 712 ## find three equal m/z ranges for the average mass spectra plots:
694 third_mz_range = nrow(msidata_no_NA)/3 713 third_mz_range = nrow(msidata_no_NA)/3
695 714
696 par(mfrow = c(2, 2), cex.axis=1, cex.lab=1, mar=c(5.1,4.1,4.1,2.1)) 715 par(cex.axis=1, cex.lab=1, mar=c(5.1,4.1,4.1,2.1))
697 plot(msidata_no_NA, pixel = 1:ncol(msidata_no_NA), main= "Average spectrum") 716 print(plot(msidata_no_NA, run="infile", layout=c(2,2), strip=FALSE, main= "Average spectrum"))
698 plot(msidata_no_NA[1:third_mz_range,], pixel = 1:ncol(msidata_no_NA), main= "Zoomed average spectrum") 717 print(plot(msidata_no_NA[1:third_mz_range,], run="infile", layout=FALSE, strip=FALSE, main= "Zoomed average spectrum"))
699 plot(msidata_no_NA[third_mz_range:(2*third_mz_range),], pixel = 1:ncol(msidata_no_NA), main= "Zoomed average spectrum") 718 print(plot(msidata_no_NA[third_mz_range:(2*third_mz_range),], run="infile", layout=FALSE, strip=FALSE, main= "Zoomed average spectrum"))
700 plot(msidata_no_NA[(2*third_mz_range):nrow(msidata_no_NA),], pixel = 1:ncol(msidata_no_NA), main= "Zoomed average spectrum") 719 print(plot(msidata_no_NA[(2*third_mz_range):nrow(msidata_no_NA),], run="infile", layout=FALSE, strip=FALSE, main= "Zoomed average spectrum"))
701 720
702 ## plot one average mass spectrum for each pixel annotation group 721 ## plot one average mass spectrum for each pixel annotation group
703 722
704 if (!is.null(levels(msidata\$annotation))){ 723 if (!is.null(levels(msidata\$annotation))){
705 ## print legend only for less than 10 samples 724 ## print legend only for less than 10 samples
706 if (length(levels(msidata\$annotation)) < 10){ 725 if (length(levels(msidata\$annotation)) < 10){
707 key_legend = TRUE 726 key_legend = TRUE
708 }else{key_legend = FALSE} 727 }else{key_legend = FALSE}
709 par(mfrow = c(1,1), cex.axis=1, cex.lab=1, mar=c(5.1,4.1,4.1,2.1)) 728 par(mfrow = c(1,1), cex.axis=1, cex.lab=1, mar=c(5.1,4.1,4.1,2.1))
710 plot(msidata, pixel=1:ncol(msidata), pixel.groups=msidata\$annotation, key=key_legend, col=hue_pal()(length(levels(msidata\$annotation))),superpose=TRUE, main="Average mass spectra for annotation groups") 729 print(plot(msidata, run="infile", pixel.groups=msidata\$annotation, key=key_legend, col=hue_pal()(length(levels(msidata\$annotation))),superpose=TRUE, main="Average mass spectra for annotation groups"))
711 } 730 }
712 731
713 ## plot 4 random mass spectra 732 ## plot 4 random mass spectra
714 ## find four random pixel to plot their spectra in the following plots: 733 ## find four random, not empty pixel to plot their spectra in the following plots:
715 pixel1 = sample(pixelnumber,1) 734 pixel_vector = sample(which(TICs != 0),4)
716 pixel2 = sample(pixelnumber,1)
717 pixel3 = sample(pixelnumber,1)
718 pixel4 = sample(pixelnumber,1)
719 735
720 par(mfrow = c(2, 2), cex.axis=1, cex.lab=1, mar=c(5.1,4.1,4.1,2.1)) 736 par(mfrow = c(2, 2), cex.axis=1, cex.lab=1, mar=c(5.1,4.1,4.1,2.1))
721 plot(msidata_no_NA, pixel = pixel1, main=paste0("Spectrum at ", rownames(coord(msidata_no_NA)[pixel1,1:2]))) 737 print(plot(msidata_no_NA, pixel = pixel_vector))
722 plot(msidata_no_NA, pixel = pixel2, main=paste0("Spectrum at ", rownames(coord(msidata_no_NA)[pixel2,1:2]))) 738
723 plot(msidata_no_NA, pixel = pixel3, main= paste0("Spectrum at ", rownames(coord(msidata_no_NA)[pixel3,1:2])))
724 plot(msidata_no_NA, pixel = pixel4, main= paste0("Spectrum at ", rownames(coord(msidata_no_NA)[pixel4,1:2])))
725 739
726 ################### 16) Zoomed in mass spectra for calibrants ############## 740 ################### 16) Zoomed in mass spectra for calibrants ##############
727 741
728 count = 1 742 count = 1
729 differencevector = numeric() 743 differencevector = numeric()
739 ### define the plot window with xmin und xmax 753 ### define the plot window with xmin und xmax
740 minmasspixel1 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]-0.5) 754 minmasspixel1 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]-0.5)
741 maxmasspixel1 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]+1.5) 755 maxmasspixel1 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]+1.5)
742 minmasspixel2 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]-0.25) 756 minmasspixel2 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]-0.25)
743 maxmasspixel2 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]+0.5) 757 maxmasspixel2 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]+0.5)
744 minmasspixel3 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]-3) 758 minmasspixel3 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]-1.5)
745 maxmasspixel3 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]+3) 759 maxmasspixel3 = features(msidata_no_NA, mz=inputcalibrantmasses[mass]+3)
746 760
747 ### find m/z with the highest mean intensity in m/z range (red line in plot 16) and calculate ppm difference for plot 17 761 ### find m/z with the highest mean intensity in m/z range (red line in plot 16) and calculate ppm difference for plot 17
748 filtered_data = msidata_no_NA[mz(msidata_no_NA) >= inputcalibrantmasses[mass]-plusminusvalues[mass] & mz(msidata_no_NA) <= inputcalibrantmasses[mass]+plusminusvalues[mass],] 762 filtered_data = msidata_no_NA[mz(msidata_no_NA) >= inputcalibrantmasses[mass]-plusminusvalues[mass] & mz(msidata_no_NA) <= inputcalibrantmasses[mass]+plusminusvalues[mass],]
749 763
763 mzdifference2 = mzvalue - inputcalibrantmasses[mass] 777 mzdifference2 = mzvalue - inputcalibrantmasses[mass]
764 ppmdifference2 = mzdifference2/inputcalibrantmasses[mass]*1000000 778 ppmdifference2 = mzdifference2/inputcalibrantmasses[mass]*1000000
765 differencevector2[mass] = round(ppmdifference2, digits=2) 779 differencevector2[mass] = round(ppmdifference2, digits=2)
766 780
767 ## plotting of 4 spectra in one page 781 ## plotting of 4 spectra in one page
768 par(mfrow = c(2, 2), oma=c(0,0,2,0)) 782 par(oma=c(0,0,2,0))
769 ## average plot 783 ## average plot
770 plot(msidata_no_NA[minmasspixel1:maxmasspixel1,], pixel = 1:length(pixelnumber), main= "Average spectrum") 784
785 print(plot(msidata_no_NA[minmasspixel1:maxmasspixel1,], run="infile", layout=c(2,2), strip=FALSE, main= "Average spectrum"))
771 abline(v=c(inputcalibrantmasses[mass] -plusminusvalues[count], inputcalibrantmasses[mass] ,inputcalibrantmasses[mass] +plusminusvalues[count]), col="blue", lty=c(3,5,3)) 786 abline(v=c(inputcalibrantmasses[mass] -plusminusvalues[count], inputcalibrantmasses[mass] ,inputcalibrantmasses[mass] +plusminusvalues[count]), col="blue", lty=c(3,5,3))
772 abline(v=c(maxvalue), col="red", lty=2) 787 abline(v=c(maxvalue), col="red", lty=2)
773 abline(v=c(mzvalue), col="green2", lty=4) 788 abline(v=c(mzvalue), col="green2", lty=4)
774 ## average plot including points per data point 789 ## average plot including points per data point
775 plot(msidata_no_NA[minmasspixel1:maxmasspixel1,], pixel = 1:length(pixelnumber), main="Average spectrum with data points") 790 print(plot(msidata_no_NA[minmasspixel1:maxmasspixel1,], run="infile", layout=FALSE, strip=FALSE, main="Average spectrum with data points"))
776 points(mz(msidata_no_NA[minmasspixel1:maxmasspixel1,]), rowMeans(spectra(msidata_no_NA)[minmasspixel1:maxmasspixel1,,drop=FALSE]), col="blue", pch=20) 791 points(mz(msidata_no_NA[minmasspixel1:maxmasspixel1,]), rowMeans(spectra(msidata_no_NA)[minmasspixel1:maxmasspixel1,,drop=FALSE]), col="blue", pch=20)
777 ## plot of third average plot 792 ## plot of third average plot
778 plot(msidata_no_NA[minmasspixel2:maxmasspixel2,], pixel = 1:length(pixelnumber), main= "Average spectrum") 793 print(plot(msidata_no_NA[minmasspixel2:maxmasspixel2,], run="infile", layout=FALSE, strip=FALSE, main= "Average spectrum"))
779 abline(v=c(inputcalibrantmasses[mass] -plusminusvalues[count], inputcalibrantmasses[mass] ,inputcalibrantmasses[mass] +plusminusvalues[count]), col="blue", lty=c(3,5,3)) 794 abline(v=c(inputcalibrantmasses[mass] -plusminusvalues[count], inputcalibrantmasses[mass] ,inputcalibrantmasses[mass] +plusminusvalues[count]), col="blue", lty=c(3,5,3))
780 abline(v=c(maxvalue), col="red", lty=2) 795 abline(v=c(maxvalue), col="red", lty=2)
781 abline(v=c(mzvalue), col="green2", lty=4) 796 abline(v=c(mzvalue), col="green2", lty=4)
782 ## plot of fourth average plot 797 ## plot of fourth average plot
783 plot(msidata_no_NA[minmasspixel3:maxmasspixel3,], pixel = 1:length(pixelnumber), main= "Average spectrum") 798 print(plot(msidata_no_NA[minmasspixel3:maxmasspixel3,], run="infile", layout=FALSE, strip=FALSE, main= "Average spectrum"))
784 abline(v=c(inputcalibrantmasses[mass] -plusminusvalues[count], inputcalibrantmasses[mass] ,inputcalibrantmasses[mass] +plusminusvalues[count]), col="blue", lty=c(3,5,3)) 799 abline(v=c(inputcalibrantmasses[mass] -plusminusvalues[count], inputcalibrantmasses[mass] ,inputcalibrantmasses[mass] +plusminusvalues[count]), col="blue", lty=c(3,5,3))
785 abline(v=c(maxvalue), col="red", lty=2) 800 abline(v=c(maxvalue), col="red", lty=2)
786 abline(v=c(mzvalue), col="green2", lty=4) 801 abline(v=c(mzvalue), col="green2", lty=4)
787 title(paste0("theor. m/z: ", round(inputcalibrants[count,1], digits=4)), col.main="blue", outer=TRUE, line=0, adj=0.074) 802 title(paste0("theor. m/z: ", round(inputcalibrants[count,1], digits=4)), col.main="blue", outer=TRUE, line=0, adj=0.074)
788 title(paste0("most abundant m/z: ", round(maxvalue, digits=4)), col.main="red", outer=TRUE, line=0, adj=0.49) 803 title(paste0("most abundant m/z: ", round(maxvalue, digits=4)), col.main="red", outer=TRUE, line=0, adj=0.49)
793 if (!is.null(levels(msidata\$annotation))){ 808 if (!is.null(levels(msidata\$annotation))){
794 if (number_combined < 10){ 809 if (number_combined < 10){
795 key_zoomed = TRUE 810 key_zoomed = TRUE
796 }else{key_zoomed = FALSE} 811 }else{key_zoomed = FALSE}
797 par(mfrow = c(1, 1)) 812 par(mfrow = c(1, 1))
798 plot(msidata_no_NA[minmasspixel1:maxmasspixel1,], pixel=1:ncol(msidata_no_NA),main="Average spectrum per annotation group", 813 print(plot(msidata_no_NA[minmasspixel1:maxmasspixel1,], run="infile", strip=FALSE,main="Average spectrum per annotation group",
799 pixel.groups=msidata\$annotation, key=key_zoomed, col=hue_pal()(number_combined),superpose=TRUE) 814 pixel.groups=msidata\$annotation, key=key_zoomed, col=hue_pal()(number_combined),superpose=TRUE))
800 abline(v=c(inputcalibrantmasses[mass] -plusminusvalues[count], inputcalibrantmasses[mass] ,inputcalibrantmasses[mass] +plusminusvalues[count]), col="black", lty=c(3,1,3)) 815 abline(v=c(inputcalibrantmasses[mass] -plusminusvalues[count], inputcalibrantmasses[mass] ,inputcalibrantmasses[mass] +plusminusvalues[count]), col="black", lty=c(3,1,3))
801 } 816 }
802 count=count+1 817 count=count+1
803 } 818 }
804 819
890 if (!is.null(levels(msidata\$annotation))){ 905 if (!is.null(levels(msidata\$annotation))){
891 abline(v=abline_vector, lty = 3)}} 906 abline(v=abline_vector, lty = 3)}}
892 907
893 ### make x-y-images for mz accuracy 908 ### make x-y-images for mz accuracy
894 909
895 ppm_dataframe = cbind(coord(msidata)[,1:2], ppm_df) 910 ppm_dataframe = data.frame(coord(msidata)\$x, coord(msidata)\$y, ppm_df)
911 colnames(ppm_dataframe) = c("x", "y", "ppm_df")
896 912
897 for (each_cal in 1:ncol(ppm_df)){ 913 for (each_cal in 1:ncol(ppm_df)){
898 tmp_ppm = ppm_dataframe[,c(1,2,each_cal+2)] 914 tmp_ppm = ppm_dataframe[,c(1,2,each_cal+2)]
899 tmp_ppm[,3] = as.numeric(tmp_ppm[,3]) 915 tmp_ppm[,3] = as.numeric(tmp_ppm[,3])
900 colnames(tmp_ppm) = c("x","y", "ppm_each_cal") 916 colnames(tmp_ppm) = c("x","y", "ppm_each_cal")
939 <param name="distance" value="200" type="float" label="ppm range" help="Will be added in both directions to input calibrant m/z and intensities will be averaged in this range."/> 955 <param name="distance" value="200" type="float" label="ppm range" help="Will be added in both directions to input calibrant m/z and intensities will be averaged in this range."/>
940 <param name="filenameratioplot" type="text" optional="true" label="Title" help="Optional title for fold change plot."> 956 <param name="filenameratioplot" type="text" optional="true" label="Title" help="Optional title for fold change plot.">
941 <sanitizer invalid_char=""> 957 <sanitizer invalid_char="">
942 <valid initial="string.ascii_letters,string.digits"> 958 <valid initial="string.ascii_letters,string.digits">
943 <add value="_" /> 959 <add value="_" />
960 <add value=" " />
944 </valid> 961 </valid>
945 </sanitizer> 962 </sanitizer>
946 </param> 963 </param>
947 </repeat> 964 </repeat>
948 </inputs> 965 </inputs>
971 <param name="do_pca" value="True"/> 988 <param name="do_pca" value="True"/>
972 <repeat name="calibrantratio"> 989 <repeat name="calibrantratio">
973 <param name="mass1" value="328.9"/> 990 <param name="mass1" value="328.9"/>
974 <param name="mass2" value="398.8"/> 991 <param name="mass2" value="398.8"/>
975 <param name="distance" value="500"/> 992 <param name="distance" value="500"/>
976 <param name="filenameratioplot" value = "Ratio of mass1 (328.9) / mass2 (398.8)"/> 993 <param name="filenameratioplot" value = "Ratio of mz 328.9 and mz 398.8"/>
977 </repeat> 994 </repeat>
978 <output name="QC_report" file="QC_imzml.pdf" compare="sim_size"/> 995 <output name="QC_report" file="QC_imzml.pdf" compare="sim_size"/>
979 </test> 996 </test>
980 997
981 <test> 998 <test>
1116 ]]> 1133 ]]>
1117 </help> 1134 </help>
1118 <expand macro="citations"/> 1135 <expand macro="citations"/>
1119 </tool> 1136 </tool>
1120 1137
1138