annotate lib.r @ 22:01bbba6d57ff draft

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