Mercurial > repos > lecorguille > xcms_fillpeaks
comparison lib.r @ 34:d8bac1291473 draft
planemo upload for repository https://github.com/workflow4metabolomics/xcms commit f01148783819c37e474790dbd56619862960448a
| author | lecorguille |
|---|---|
| date | Tue, 03 Apr 2018 11:40:32 -0400 |
| parents | ea611367e1da |
| children | e67cbb96d9e9 |
comparison
equal
deleted
inserted
replaced
| 33:ea611367e1da | 34:d8bac1291473 |
|---|---|
| 23 cat(sessioninfo$R.version$version.string,"\n") | 23 cat(sessioninfo$R.version$version.string,"\n") |
| 24 cat("Main packages:\n") | 24 cat("Main packages:\n") |
| 25 for (pkg in names(sessioninfo$otherPkgs)) { cat(paste(pkg,packageVersion(pkg)),"\t") }; cat("\n") | 25 for (pkg in names(sessioninfo$otherPkgs)) { cat(paste(pkg,packageVersion(pkg)),"\t") }; cat("\n") |
| 26 cat("Other loaded packages:\n") | 26 cat("Other loaded packages:\n") |
| 27 for (pkg in names(sessioninfo$loadedOnly)) { cat(paste(pkg,packageVersion(pkg)),"\t") }; cat("\n") | 27 for (pkg in names(sessioninfo$loadedOnly)) { cat(paste(pkg,packageVersion(pkg)),"\t") }; cat("\n") |
| 28 } | |
| 29 | |
| 30 #@author G. Le Corguille | |
| 31 # This function merge several xdata into one. | |
| 32 mergeXData <- function(args) { | |
| 33 for(image in args$images) { | |
| 34 load(image) | |
| 35 # Handle infiles | |
| 36 if (!exists("singlefile")) singlefile <- NULL | |
| 37 if (!exists("zipfile")) zipfile <- NULL | |
| 38 rawFilePath <- getRawfilePathFromArguments(singlefile, zipfile, args) | |
| 39 zipfile <- rawFilePath$zipfile | |
| 40 singlefile <- rawFilePath$singlefile | |
| 41 retrieveRawfileInTheWorkingDirectory(singlefile, zipfile) | |
| 42 if (exists("raw_data")) xdata <- raw_data | |
| 43 if (!exists("xdata")) stop("\n\nERROR: The RData doesn't contain any object called 'xdata'. This RData should have been created by an old version of XMCS 2.*") | |
| 44 cat(sampleNamesList$sampleNamesOrigin,"\n") | |
| 45 if (!exists("xdata_merged")) { | |
| 46 xdata_merged <- xdata | |
| 47 singlefile_merged <- singlefile | |
| 48 md5sumList_merged <- md5sumList | |
| 49 sampleNamesList_merged <- sampleNamesList | |
| 50 } else { | |
| 51 if (is(xdata, "XCMSnExp")) xdata_merged <- c(xdata_merged,xdata) | |
| 52 else if (is(xdata, "OnDiskMSnExp")) xdata_merged <- .concatenate_OnDiskMSnExp(xdata_merged,xdata) | |
| 53 else stop("\n\nERROR: The RData either a OnDiskMSnExp object called raw_data or a XCMSnExp object called xdata") | |
| 54 singlefile_merged <- c(singlefile_merged,singlefile) | |
| 55 md5sumList_merged$origin <- rbind(md5sumList_merged$origin,md5sumList$origin) | |
| 56 sampleNamesList_merged$sampleNamesOrigin <- c(sampleNamesList_merged$sampleNamesOrigin,sampleNamesList$sampleNamesOrigin) | |
| 57 sampleNamesList_merged$sampleNamesMakeNames <- c(sampleNamesList_merged$sampleNamesMakeNames,sampleNamesList$sampleNamesMakeNames) | |
| 58 } | |
| 59 } | |
| 60 rm(image) | |
| 61 xdata <- xdata_merged; rm(xdata_merged) | |
| 62 singlefile <- singlefile_merged; rm(singlefile_merged) | |
| 63 md5sumList <- md5sumList_merged; rm(md5sumList_merged) | |
| 64 sampleNamesList <- sampleNamesList_merged; rm(sampleNamesList_merged) | |
| 65 | |
| 66 if (!is.null(args$sampleMetadata)) { | |
| 67 cat("\tXSET PHENODATA SETTING...\n") | |
| 68 sampleMetadataFile <- args$sampleMetadata | |
| 69 sampleMetadata <- getDataFrameFromFile(sampleMetadataFile, header=F) | |
| 70 xdata@phenoData@data$sample_group=sampleMetadata$V2[match(xdata@phenoData@data$sample_name,sampleMetadata$V1)] | |
| 71 | |
| 72 if (any(is.na(pData(xdata)$sample_group))) { | |
| 73 sample_missing <- pData(xdata)$sample_name[is.na(pData(xdata)$sample_group)] | |
| 74 error_message <- paste("Those samples are missing in your sampleMetadata:", paste(sample_missing, collapse=" ")) | |
| 75 print(error_message) | |
| 76 stop(error_message) | |
| 77 } | |
| 78 } | |
| 79 return(list("xdata"=xdata, "singlefile"=singlefile, "md5sumList"=md5sumList,"sampleNamesList"=sampleNamesList)) | |
| 28 } | 80 } |
| 29 | 81 |
| 30 #@author G. Le Corguille | 82 #@author G. Le Corguille |
| 31 # This function convert if it is required the Retention Time in minutes | 83 # This function convert if it is required the Retention Time in minutes |
| 32 RTSecondToMinute <- function(variableMetadata, convertRTMinute) { | 84 RTSecondToMinute <- function(variableMetadata, convertRTMinute) { |
| 77 | 129 |
| 78 pdf(file="raw_vs_adjusted_rt.pdf", width=16, height=12) | 130 pdf(file="raw_vs_adjusted_rt.pdf", width=16, height=12) |
| 79 | 131 |
| 80 # Color by group | 132 # Color by group |
| 81 group_colors <- brewer.pal(3, "Set1")[1:length(unique(xdata$sample_group))] | 133 group_colors <- brewer.pal(3, "Set1")[1:length(unique(xdata$sample_group))] |
| 82 names(group_colors) <- unique(xdata$sample_group) | 134 if (length(group_colors) > 1) { |
| 83 plotAdjustedRtime(xdata, col = group_colors[xdata$sample_group]) | 135 names(group_colors) <- unique(xdata$sample_group) |
| 84 legend("topright", legend=names(group_colors), col=group_colors, cex=0.8, lty=1) | 136 plotAdjustedRtime(xdata, col = group_colors[xdata$sample_group]) |
| 137 legend("topright", legend=names(group_colors), col=group_colors, cex=0.8, lty=1) | |
| 138 } | |
| 85 | 139 |
| 86 # Color by sample | 140 # Color by sample |
| 87 plotAdjustedRtime(xdata, col = rainbow(length(xdata@phenoData@data$sample_name))) | 141 plotAdjustedRtime(xdata, col = rainbow(length(xdata@phenoData@data$sample_name))) |
| 88 legend("topright", legend=xdata@phenoData@data$sample_name, col=rainbow(length(xdata@phenoData@data$sample_name)), cex=0.8, lty=1) | 142 legend("topright", legend=xdata@phenoData@data$sample_name, col=rainbow(length(xdata@phenoData@data$sample_name)), cex=0.8, lty=1) |
| 89 | 143 |
| 107 write.table(dataMatrix, file=dataMatrixOutput,sep="\t",quote=F,row.names=F) | 161 write.table(dataMatrix, file=dataMatrixOutput,sep="\t",quote=F,row.names=F) |
| 108 | 162 |
| 109 } | 163 } |
| 110 | 164 |
| 111 #@author G. Le Corguille | 165 #@author G. Le Corguille |
| 166 # It allow different of field separators | |
| 167 getDataFrameFromFile <- function(filename, header=T) { | |
| 168 myDataFrame <- read.table(filename, header=header, sep=";", stringsAsFactors=F) | |
| 169 if (ncol(myDataFrame) < 2) myDataFrame <- read.table(filename, header=header, sep="\t", stringsAsFactors=F) | |
| 170 if (ncol(myDataFrame) < 2) myDataFrame <- read.table(filename, header=header, sep=",", stringsAsFactors=F) | |
| 171 if (ncol(myDataFrame) < 2) { | |
| 172 error_message="Your tabular file seems not well formatted. The column separators accepted are ; , and tabulation" | |
| 173 print(error_message) | |
| 174 stop(error_message) | |
| 175 } | |
| 176 return(myDataFrame) | |
| 177 } | |
| 178 | |
| 112 getPlotChromatogram <- function(xdata, pdfname="Chromatogram.pdf", aggregationFun = "max") { | 179 getPlotChromatogram <- function(xdata, pdfname="Chromatogram.pdf", aggregationFun = "max") { |
| 113 | 180 |
| 114 chrom <- chromatogram(xdata, aggregationFun = aggregationFun) | 181 chrom <- chromatogram(xdata, aggregationFun = aggregationFun) |
| 115 if (aggregationFun == "sum") | 182 if (aggregationFun == "sum") |
| 116 type="Total Ion Chromatograms" | 183 type="Total Ion Chromatograms" |
| 125 | 192 |
| 126 pdf(pdfname, width=16, height=10) | 193 pdf(pdfname, width=16, height=10) |
| 127 | 194 |
| 128 # Color by group | 195 # Color by group |
| 129 group_colors <- brewer.pal(3, "Set1")[1:length(unique(xdata$sample_group))] | 196 group_colors <- brewer.pal(3, "Set1")[1:length(unique(xdata$sample_group))] |
| 130 names(group_colors) <- unique(xdata$sample_group) | 197 if (length(group_colors) > 1) { |
| 131 plot(chrom, col = group_colors[chrom$sample_group], main=main) | 198 names(group_colors) <- unique(xdata$sample_group) |
| 132 legend("topright", legend=names(group_colors), col=group_colors, cex=0.8, lty=1) | 199 plot(chrom, col = group_colors[chrom$sample_group], main=main) |
| 200 legend("topright", legend=names(group_colors), col=group_colors, cex=0.8, lty=1) | |
| 201 } | |
| 133 | 202 |
| 134 # Color by sample | 203 # Color by sample |
| 135 plot(chrom, col = rainbow(length(xdata@phenoData@data$sample_name)), main=main) | 204 plot(chrom, col = rainbow(length(xdata@phenoData@data$sample_name)), main=main) |
| 136 legend("topright", legend=xdata@phenoData@data$sample_name, col=rainbow(length(xdata@phenoData@data$sample_name)), cex=0.8, lty=1) | 205 legend("topright", legend=xdata@phenoData@data$sample_name, col=rainbow(length(xdata@phenoData@data$sample_name)), cex=0.8, lty=1) |
| 137 | 206 |
| 343 singlefile_galaxyPath <- singlefile_galaxyPaths[singlefile_galaxyPath_i] | 412 singlefile_galaxyPath <- singlefile_galaxyPaths[singlefile_galaxyPath_i] |
| 344 singlefile_sampleName <- singlefile_sampleNames[singlefile_galaxyPath_i] | 413 singlefile_sampleName <- singlefile_sampleNames[singlefile_galaxyPath_i] |
| 345 singlefile[[singlefile_sampleName]] <- singlefile_galaxyPath | 414 singlefile[[singlefile_sampleName]] <- singlefile_galaxyPath |
| 346 } | 415 } |
| 347 } | 416 } |
| 348 for (argument in c("zipfile","zipfilePositive","zipfileNegative","singlefile_galaxyPath","singlefile_sampleName","singlefile_galaxyPathPositive","singlefile_sampleNamePositive","singlefile_galaxyPathNegative","singlefile_sampleNameNegative")) { | 417 return(list(zipfile=zipfile, singlefile=singlefile)) |
| 349 args[[argument]] <- NULL | |
| 350 } | |
| 351 return(list(zipfile=zipfile, singlefile=singlefile, args=args)) | |
| 352 } | 418 } |
| 353 | 419 |
| 354 | 420 |
| 355 # This function retrieve the raw file in the working directory | 421 # This function retrieve the raw file in the working directory |
| 356 # - if zipfile: unzip the file with its directory tree | 422 # - if zipfile: unzip the file with its directory tree |
| 557 #@TODO: remove this function as soon as we can use xcms 3.x.x from Bioconductor 3.7 | 623 #@TODO: remove this function as soon as we can use xcms 3.x.x from Bioconductor 3.7 |
| 558 # https://github.com/sneumann/xcms/issues/247 | 624 # https://github.com/sneumann/xcms/issues/247 |
| 559 c.XCMSnExp <- function(...) { | 625 c.XCMSnExp <- function(...) { |
| 560 .concatenate_XCMSnExp(...) | 626 .concatenate_XCMSnExp(...) |
| 561 } | 627 } |
| 628 | |
| 629 #@TODO: remove this function as soon as we can use xcms 3.x.x from Bioconductor 3.7 | |
| 630 # https://github.com/sneumann/xcms/issues/247 | |
| 631 c.MSnbase <- function(...) { | |
| 632 .concatenate_OnDiskMSnExp(...) | |
| 633 } |
