annotate lib.r @ 1:ea15115a5b3f draft

"planemo upload commit 4fcbbcbc6d6b0a59e801870d31fe886a920ef429"
author workflow4metabolomics
date Thu, 13 Feb 2020 17:23:27 -0500
parents 139ff66b0b5d
children c4c13745e797
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
1 # lib.r
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
2
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
3 #@author G. Le Corguille
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
4 # solve an issue with batch if arguments are logical TRUE/FALSE
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
5 parseCommandArgs <- function(...) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
6 args <- batch::parseCommandArgs(...)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
7 for (key in names(args)) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
8 if (args[key] %in% c("TRUE","FALSE"))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
9 args[key] = as.logical(args[key])
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
10 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
11 return(args)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
12 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
13
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
14 #@author G. Le Corguille
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
15 # This function will
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
16 # - load the packages
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
17 # - display the sessionInfo
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
18 loadAndDisplayPackages <- function(pkgs) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
19 for(pkg in pkgs) suppressPackageStartupMessages( stopifnot( library(pkg, quietly=TRUE, logical.return=TRUE, character.only=TRUE)))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
20
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
21 sessioninfo = sessionInfo()
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
22 cat(sessioninfo$R.version$version.string,"\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
23 cat("Main packages:\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
24 for (pkg in names(sessioninfo$otherPkgs)) { cat(paste(pkg,packageVersion(pkg)),"\t") }; cat("\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
25 cat("Other loaded packages:\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
26 for (pkg in names(sessioninfo$loadedOnly)) { cat(paste(pkg,packageVersion(pkg)),"\t") }; cat("\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
27 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
28
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
29 # This function retrieve a xset like object
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
30 #@author Gildas Le Corguille lecorguille@sb-roscoff.fr
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
31 getxcmsSetObject <- function(xobject) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
32 # XCMS 1.x
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
33 if (class(xobject) == "xcmsSet")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
34 return (xobject)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
35 # XCMS 3.x
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
36 if (class(xobject) == "XCMSnExp") {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
37 # Get the legacy xcmsSet object
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
38 suppressWarnings(xset <- as(xobject, 'xcmsSet'))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
39 if (is.null(xset@phenoData$sample_group))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
40 sampclass(xset) = "."
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
41 else
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
42 sampclass(xset) <- xset@phenoData$sample_group
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
43 if (!is.null(xset@phenoData$sample_name))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
44 rownames(xset@phenoData) = xset@phenoData$sample_name
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
45 return (xset)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
46 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
47 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
48
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
49 #@author G. Le Corguille
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
50 #The function create a pdf from the different png generated by diffreport
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
51 diffreport_png2pdf <- function(filebase) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
52 dir.create("pdf")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
53
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
54 pdfEicOutput = paste0("pdf/",filebase,"-eic_pdf.pdf")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
55 pdfBoxOutput = paste0("pdf/",filebase,"-box_pdf.pdf")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
56
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
57 system(paste0("gm convert ",filebase,"_eic/*.png ",pdfEicOutput))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
58 system(paste0("gm convert ",filebase,"_box/*.png ",pdfBoxOutput))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
59
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
60 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
61
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
62 #@author G. Le Corguille
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
63 #The function create a zip archive from the different png generated by diffreport
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
64 diffreport_png2zip <- function() {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
65 zip("eic.zip", dir(pattern="_eic"), zip=Sys.which("zip"))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
66 zip("box.zip", dir(pattern="_box"), zip=Sys.which("zip"))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
67 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
68
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
69 #The function create a zip archive from the different tabular generated by diffreport
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
70 diffreport_tabular2zip <- function() {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
71 zip("tabular.zip", dir(pattern="tabular/*"), zip=Sys.which("zip"))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
72 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
73
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
74 #@author G. Le Corguille
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
75 #This function convert if it is required the Retention Time in minutes
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
76 RTSecondToMinute <- function(variableMetadata, convertRTMinute) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
77 if (convertRTMinute){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
78 #converting the retention times (seconds) into minutes
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
79 print("converting the retention times into minutes in the variableMetadata")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
80 variableMetadata[,"rt"]=variableMetadata[,"rt"]/60
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
81 variableMetadata[,"rtmin"]=variableMetadata[,"rtmin"]/60
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
82 variableMetadata[,"rtmax"]=variableMetadata[,"rtmax"]/60
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
83 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
84 return (variableMetadata)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
85 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
86
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
87 #@author G. Le Corguille
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
88 #This function format ions identifiers
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
89 formatIonIdentifiers <- function(variableMetadata, numDigitsRT=0, numDigitsMZ=0) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
90 splitDeco = strsplit(as.character(variableMetadata$name),"_")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
91 idsDeco = sapply(splitDeco, function(x) { deco=unlist(x)[2]; if (is.na(deco)) return ("") else return(paste0("_",deco)) })
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
92 namecustom = make.unique(paste0("M",round(variableMetadata[,"mz"],numDigitsMZ),"T",round(variableMetadata[,"rt"],numDigitsRT),idsDeco))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
93 variableMetadata=cbind(name=variableMetadata$name, namecustom=namecustom, variableMetadata[,!(colnames(variableMetadata) %in% c("name"))])
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
94 return(variableMetadata)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
95 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
96
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
97 #The function annotateDiffreport without the corr function which bugs
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
98 annotatediff <- function(xset=xset, args=args, variableMetadataOutput="variableMetadata.tsv") {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
99 # Resolve the bug with x11, with the function png
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
100 options(bitmapType='cairo')
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
101
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
102 #Check if the fillpeaks step has been done previously, if it hasn't, there is an error message and the execution is stopped.
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
103 res=try(is.null(xset@filled))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
104
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
105 # ------ annot -------
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
106 args$calcCiS=as.logical(args$calcCiS)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
107 args$calcIso=as.logical(args$calcIso)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
108 args$calcCaS=as.logical(args$calcCaS)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
109
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
110 # common parameters
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
111 args4annotate = list(object=xset,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
112 nSlaves=args$nSlaves,sigma=args$sigma,perfwhm=args$perfwhm,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
113 maxcharge=args$maxcharge,maxiso=args$maxiso,minfrac=args$minfrac,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
114 ppm=args$ppm,mzabs=args$mzabs,quick=args$quick,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
115 polarity=args$polarity,max_peaks=args$max_peaks,intval=args$intval)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
116
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
117 # quick == FALSE
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
118 if(args$quick==FALSE) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
119 args4annotate = append(args4annotate,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
120 list(graphMethod=args$graphMethod,cor_eic_th=args$cor_eic_th,pval=args$pval,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
121 calcCiS=args$calcCiS,calcIso=args$calcIso,calcCaS=args$calcCaS))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
122 # no ruleset
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
123 if (!is.null(args$multiplier)) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
124 args4annotate = append(args4annotate,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
125 list(multiplier=args$multiplier))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
126 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
127 # ruleset
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
128 else {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
129 rulset=read.table(args$rules, h=T, sep=";")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
130 if (ncol(rulset) < 4) rulset=read.table(args$rules, h=T, sep="\t")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
131 if (ncol(rulset) < 4) rulset=read.table(args$rules, h=T, sep=",")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
132 if (ncol(rulset) < 4) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
133 error_message="Your ruleset file seems not well formatted. The column separators accepted are ; , and tabulation"
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
134 print(error_message)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
135 stop(error_message)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
136 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
137
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
138 args4annotate = append(args4annotate,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
139 list(rules=rulset))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
140 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
141 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
142
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
143
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
144 # launch annotate
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
145 xa = do.call("annotate", args4annotate)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
146 peakList=getPeaklist(xa,intval=args$intval)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
147 peakList=cbind(groupnames(xa@xcmsSet),peakList); colnames(peakList)[1] = c("name");
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
148
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
149 # --- Multi condition : diffreport ---
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
150 diffrepOri=NULL
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
151 if (!is.null(args$runDiffreport) & nlevels(sampclass(xset))>=2) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
152 #Check if the fillpeaks step has been done previously, if it hasn't, there is an error message and the execution is stopped.
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
153 res=try(is.null(xset@filled))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
154 classes=levels(sampclass(xset))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
155 x=1:(length(classes)-1)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
156 for (i in seq(along=x) ) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
157 y=1:(length(classes))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
158 for (n in seq(along=y)){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
159 if(i+n <= length(classes)){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
160 filebase=paste(classes[i],class2=classes[i+n],sep="-vs-")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
161
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
162 diffrep=diffreport(
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
163 object=xset,class1=classes[i],class2=classes[i+n],
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
164 filebase=filebase,eicmax=args$eicmax,eicwidth=args$eicwidth,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
165 sortpval=TRUE,value=args$value,h=args$h,w=args$w,mzdec=args$mzdec,missing=0)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
166
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
167 diffrepOri = diffrep
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
168
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
169 # renamming of the column rtmed to rt to fit with camera peaklist function output
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
170 colnames(diffrep)[colnames(diffrep)=="rtmed"] <- "rt"
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
171 colnames(diffrep)[colnames(diffrep)=="mzmed"] <- "mz"
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
172
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
173 # combines results and reorder columns
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
174 diffrep = merge(peakList, diffrep[,c("name","fold","tstat","pvalue")], by.x="name", by.y="name", sort=F)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
175 diffrep = cbind(diffrep[,!(colnames(diffrep) %in% c(sampnames(xa@xcmsSet)))],diffrep[,(colnames(diffrep) %in% c(sampnames(xa@xcmsSet)))])
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
176
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
177 diffrep = RTSecondToMinute(diffrep, args$convertRTMinute)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
178 diffrep = formatIonIdentifiers(diffrep, numDigitsRT=args$numDigitsRT, numDigitsMZ=args$numDigitsMZ)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
179
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
180 if(args$sortpval){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
181 diffrep=diffrep[order(diffrep$pvalue), ]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
182 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
183
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
184 dir.create("tabular", showWarnings = FALSE)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
185 write.table(diffrep, sep="\t", quote=FALSE, row.names=FALSE, file=paste("tabular/",filebase,"_tsv.tabular",sep=""))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
186
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
187 if (args$eicmax != 0) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
188 if (args$png2 == "pdf")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
189 diffreport_png2pdf(filebase)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
190 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
191 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
192 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
193 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
194 if (args$png2 == "zip")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
195 diffreport_png2zip()
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
196 if (args$tabular2 == "zip")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
197 diffreport_tabular2zip()
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
198 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
199
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
200 # --- variableMetadata ---
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
201 variableMetadata=peakList[,!(make.names(colnames(peakList)) %in% c(make.names(sampnames(xa@xcmsSet))))]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
202 variableMetadata = RTSecondToMinute(variableMetadata, args$convertRTMinute)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
203 variableMetadata = formatIonIdentifiers(variableMetadata, numDigitsRT=args$numDigitsRT, numDigitsMZ=args$numDigitsMZ)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
204 # if we have 2 conditions, we keep stat of diffrep
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
205 if (!is.null(args$runDiffreport) & nlevels(sampclass(xset))==2) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
206 variableMetadata = merge(variableMetadata, diffrep[,c("name","fold","tstat","pvalue")],by.x="name", by.y="name", sort=F)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
207 if(exists("args[[\"sortpval\"]]")){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
208 variableMetadata=variableMetadata[order(variableMetadata$pvalue), ]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
209 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
210 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
211
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
212 variableMetadataOri=variableMetadata
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
213 write.table(variableMetadata, sep="\t", quote=FALSE, row.names=FALSE, file=variableMetadataOutput)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
214
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
215 return(list("xa"=xa,"diffrep"=diffrepOri,"variableMetadata"=variableMetadataOri));
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
216
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
217 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
218
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
219
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
220 combinexsAnnos_function <- function(xaP, xaN, diffrepP=NULL,diffrepN=NULL,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
221 pos=TRUE,tol=2,ruleset=NULL,keep_meta=TRUE, convertRTMinute=F, numDigitsMZ=0,
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
222 numDigitsRT=0, variableMetadataOutput="variableMetadata.tsv"){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
223
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
224 #Load the two Rdata to extract the xset objects from positive and negative mode
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
225 cat("\tObject xset from positive mode\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
226 print(xaP)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
227 cat("\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
228
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
229 cat("\tObject xset from negative mode\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
230 print(xaN)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
231 cat("\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
232
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
233 cat("\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
234 cat("\tCombining...\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
235 #Convert the string to numeric for creating matrix
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
236 row=as.numeric(strsplit(ruleset,",")[[1]][1])
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
237 column=as.numeric(strsplit(ruleset,",")[[1]][2])
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
238 ruleset=cbind(row,column)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
239 #Test if the file comes from an older version tool
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
240 if ((!is.null(xaP)) & (!is.null(xaN))) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
241 #Launch the combinexsannos function from CAMERA
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
242 cAnnot=combinexsAnnos(xaP, xaN,pos=pos,tol=tol,ruleset=ruleset)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
243 } else {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
244 stop("You must relauch the CAMERA.annotate step with the lastest version.")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
245 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
246
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
247 if(pos){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
248 xa=xaP
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
249 mode="neg. Mode"
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
250 } else {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
251 xa=xaN
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
252 mode="pos. Mode"
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
253 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
254
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
255 peakList=getPeaklist(xa)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
256 peakList=cbind(groupnames(xa@xcmsSet),peakList); colnames(peakList)[1] = c("name");
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
257 variableMetadata=cbind(peakList, cAnnot[, c("isotopes", "adduct", "pcgroup",mode)]);
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
258 variableMetadata=variableMetadata[,!(colnames(variableMetadata) %in% c(sampnames(xa@xcmsSet)))]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
259
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
260 #Test if there are more than two classes (conditions)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
261 if ( nlevels(sampclass(xaP@xcmsSet))==2 & (!is.null(diffrepN)) & (!is.null(diffrepP))) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
262 diffrepP = diffrepP[,c("name","fold","tstat","pvalue")]; colnames(diffrepP) = paste("P.",colnames(diffrepP),sep="")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
263 diffrepN = diffrepN[,c("name","fold","tstat","pvalue")]; colnames(diffrepN) = paste("N.",colnames(diffrepN),sep="")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
264
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
265 variableMetadata = merge(variableMetadata, diffrepP, by.x="name", by.y="P.name")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
266 variableMetadata = merge(variableMetadata, diffrepN, by.x="name", by.y="N.name")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
267 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
268
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
269 rownames(variableMetadata) = NULL
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
270 #TODO: checker
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
271 #colnames(variableMetadata)[1:2] = c("name","mz/rt");
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
272
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
273 variableMetadata = RTSecondToMinute(variableMetadata, convertRTMinute)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
274 variableMetadata = formatIonIdentifiers(variableMetadata, numDigitsRT=numDigitsRT, numDigitsMZ=numDigitsMZ)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
275
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
276 #If the user want to keep only the metabolites which match a difference
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
277 if(keep_meta){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
278 variableMetadata=variableMetadata[variableMetadata[,c(mode)]!="",]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
279 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
280
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
281 #Write the output into a tsv file
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
282 write.table(variableMetadata, sep="\t", quote=FALSE, row.names=FALSE, file=variableMetadataOutput)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
283 return(variableMetadata);
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
284
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
285 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
286
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
287 # This function get the raw file path from the arguments
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
288 getRawfilePathFromArguments <- function(singlefile, zipfile, args) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
289 if (!is.null(args$zipfile)) zipfile = args$zipfile
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
290 if (!is.null(args$zipfilePositive)) zipfile = args$zipfilePositive
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
291 if (!is.null(args$zipfileNegative)) zipfile = args$zipfileNegative
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
292
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
293 if (!is.null(args$singlefile_galaxyPath)) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
294 singlefile_galaxyPaths = args$singlefile_galaxyPath;
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
295 singlefile_sampleNames = args$singlefile_sampleName
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
296 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
297 if (!is.null(args$singlefile_galaxyPathPositive)) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
298 singlefile_galaxyPaths = args$singlefile_galaxyPathPositive;
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
299 singlefile_sampleNames = args$singlefile_sampleNamePositive
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
300 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
301 if (!is.null(args$singlefile_galaxyPathNegative)) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
302 singlefile_galaxyPaths = args$singlefile_galaxyPathNegative;
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
303 singlefile_sampleNames = args$singlefile_sampleNameNegative
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
304 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
305 if (exists("singlefile_galaxyPaths")){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
306 singlefile_galaxyPaths = unlist(strsplit(singlefile_galaxyPaths,","))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
307 singlefile_sampleNames = unlist(strsplit(singlefile_sampleNames,","))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
308
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
309 singlefile=NULL
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
310 for (singlefile_galaxyPath_i in seq(1:length(singlefile_galaxyPaths))) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
311 singlefile_galaxyPath=singlefile_galaxyPaths[singlefile_galaxyPath_i]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
312 singlefile_sampleName=singlefile_sampleNames[singlefile_galaxyPath_i]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
313 singlefile[[singlefile_sampleName]] = singlefile_galaxyPath
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
314 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
315 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
316 for (argument in c("zipfile", "zipfilePositive", "zipfileNegative",
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
317 "singlefile_galaxyPath", "singlefile_sampleName",
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
318 "singlefile_galaxyPathPositive", "singlefile_sampleNamePositive",
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
319 "singlefile_galaxyPathNegative","singlefile_sampleNameNegative")) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
320 args[[argument]]=NULL
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
321 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
322 return(list(zipfile=zipfile, singlefile=singlefile, args=args))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
323 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
324
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
325
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
326 # This function retrieve the raw file in the working directory
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
327 # - if zipfile: unzip the file with its directory tree
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
328 # - if singlefiles: set symlink with the good filename
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
329 retrieveRawfileInTheWorkingDirectory <- function(singlefile, zipfile) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
330 if(!is.null(singlefile) && (length("singlefile")>0)) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
331 for (singlefile_sampleName in names(singlefile)) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
332 singlefile_galaxyPath = singlefile[[singlefile_sampleName]]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
333 if(!file.exists(singlefile_galaxyPath)){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
334 error_message=paste("Cannot access the sample:",singlefile_sampleName,"located:",singlefile_galaxyPath,". Please, contact your administrator ... if you have one!")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
335 print(error_message); stop(error_message)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
336 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
337
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
338 file.symlink(singlefile_galaxyPath,singlefile_sampleName)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
339 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
340 directory = "."
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
341
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
342 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
343 if(!is.null(zipfile) && (zipfile!="")) {
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
344 if(!file.exists(zipfile)){
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
345 error_message=paste("Cannot access the Zip file:",zipfile,". Please, contact your administrator ... if you have one!")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
346 print(error_message)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
347 stop(error_message)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
348 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
349
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
350 #list all file in the zip file
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
351 #zip_files=unzip(zipfile,list=T)[,"Name"]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
352
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
353 #unzip
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
354 suppressWarnings(unzip(zipfile, unzip="unzip"))
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
355
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
356 #get the directory name
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
357 filesInZip=unzip(zipfile, list=T);
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
358 directories=unique(unlist(lapply(strsplit(filesInZip$Name,"/"), function(x) x[1])));
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
359 directories=directories[!(directories %in% c("__MACOSX")) & file.info(directories)$isdir]
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
360 directory = "."
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
361 if (length(directories) == 1) directory = directories
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
362
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
363 cat("files_root_directory\t",directory,"\n")
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
364
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
365 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
366 return (directory)
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
367 }
139ff66b0b5d planemo upload commit f69695e76674862ed9c77c1c127f459b4df42464
workflow4metabolomics
parents:
diff changeset
368