Mercurial > repos > lecorguille > xcms_retcor
comparison lib.r @ 38:67ee46ce9781 draft
planemo upload for repository https://github.com/workflow4metabolomics/xcms commit e131bacd37bfaf2c4132fd214c81db9b8a9df513
author | lecorguille |
---|---|
date | Mon, 17 Sep 2018 08:46:01 -0400 |
parents | 35a20d7c9f33 |
children | 931db5e555cc |
comparison
equal
deleted
inserted
replaced
37:35a20d7c9f33 | 38:67ee46ce9781 |
---|---|
132 variableMetadata <- cbind(name=variableMetadata$name, namecustom=namecustom, variableMetadata[,!(colnames(variableMetadata) %in% c("name"))]) | 132 variableMetadata <- cbind(name=variableMetadata$name, namecustom=namecustom, variableMetadata[,!(colnames(variableMetadata) %in% c("name"))]) |
133 return(variableMetadata) | 133 return(variableMetadata) |
134 } | 134 } |
135 | 135 |
136 #@author G. Le Corguille | 136 #@author G. Le Corguille |
137 # This function convert the remain NA to 0 in the dataMatrix | |
138 naTOzeroDataMatrix <- function(dataMatrix, naTOzero) { | |
139 if (naTOzero){ | |
140 dataMatrix[is.na(dataMatrix)] <- 0 | |
141 } | |
142 return (dataMatrix) | |
143 } | |
144 | |
145 #@author G. Le Corguille | |
137 # Draw the plotChromPeakDensity 3 per page in a pdf file | 146 # Draw the plotChromPeakDensity 3 per page in a pdf file |
138 getPlotChromPeakDensity <- function(xdata, mzdigit=4) { | 147 getPlotChromPeakDensity <- function(xdata, mzdigit=4) { |
139 pdf(file="plotChromPeakDensity.pdf", width=16, height=12) | 148 pdf(file="plotChromPeakDensity.pdf", width=16, height=12) |
140 | 149 |
141 par(mfrow = c(3, 1), mar = c(4, 4, 1, 0.5)) | 150 par(mfrow = c(3, 1), mar = c(4, 4, 1, 0.5)) |
175 dev.off() | 184 dev.off() |
176 } | 185 } |
177 | 186 |
178 #@author G. Le Corguille | 187 #@author G. Le Corguille |
179 # value: intensity values to be used into, maxo or intb | 188 # value: intensity values to be used into, maxo or intb |
180 getPeaklistW4M <- function(xdata, intval="into", convertRTMinute=F, numDigitsMZ=4, numDigitsRT=0, variableMetadataOutput, dataMatrixOutput) { | 189 getPeaklistW4M <- function(xdata, intval="into", convertRTMinute=F, numDigitsMZ=4, numDigitsRT=0, naTOzero=T, variableMetadataOutput, dataMatrixOutput) { |
181 dataMatrix <- featureValues(xdata, method="medret", value=intval) | 190 dataMatrix <- featureValues(xdata, method="medret", value=intval) |
182 colnames(dataMatrix) <- tools::file_path_sans_ext(colnames(dataMatrix)) | 191 colnames(dataMatrix) <- tools::file_path_sans_ext(colnames(dataMatrix)) |
183 dataMatrix = cbind(name=groupnamesW4M(xdata), dataMatrix) | 192 dataMatrix = cbind(name=groupnamesW4M(xdata), dataMatrix) |
184 variableMetadata <- featureDefinitions(xdata) | 193 variableMetadata <- featureDefinitions(xdata) |
185 colnames(variableMetadata)[1] = "mz"; colnames(variableMetadata)[4] = "rt" | 194 colnames(variableMetadata)[1] = "mz"; colnames(variableMetadata)[4] = "rt" |
186 variableMetadata = data.frame(name=groupnamesW4M(xdata), variableMetadata) | 195 variableMetadata = data.frame(name=groupnamesW4M(xdata), variableMetadata) |
187 | 196 |
188 variableMetadata <- RTSecondToMinute(variableMetadata, convertRTMinute) | 197 variableMetadata <- RTSecondToMinute(variableMetadata, convertRTMinute) |
189 variableMetadata <- formatIonIdentifiers(variableMetadata, numDigitsRT=numDigitsRT, numDigitsMZ=numDigitsMZ) | 198 variableMetadata <- formatIonIdentifiers(variableMetadata, numDigitsRT=numDigitsRT, numDigitsMZ=numDigitsMZ) |
199 dataMatrix <- naTOzeroDataMatrix(dataMatrix, naTOzero) | |
190 | 200 |
191 write.table(variableMetadata, file=variableMetadataOutput,sep="\t",quote=F,row.names=F) | 201 write.table(variableMetadata, file=variableMetadataOutput,sep="\t",quote=F,row.names=F) |
192 write.table(dataMatrix, file=dataMatrixOutput,sep="\t",quote=F,row.names=F) | 202 write.table(dataMatrix, file=dataMatrixOutput,sep="\t",quote=F,row.names=F) |
193 | 203 |
194 } | 204 } |
496 else | 506 else |
497 sampclass(xset) <- "." | 507 sampclass(xset) <- "." |
498 return (xset) | 508 return (xset) |
499 } | 509 } |
500 } | 510 } |
501 | |
502 | |
503 #@TODO: remove this function as soon as we can use xcms 3.x.x from Bioconductor 3.7 | |
504 # https://github.com/sneumann/xcms/issues/250 | |
505 groupnamesW4M <- function(xdata, mzdec = 0, rtdec = 0) { | |
506 mzfmt <- paste("%.", mzdec, "f", sep = "") | |
507 rtfmt <- paste("%.", rtdec, "f", sep = "") | |
508 | |
509 gnames <- paste("M", sprintf(mzfmt, featureDefinitions(xdata)[,"mzmed"]), "T", | |
510 sprintf(rtfmt, featureDefinitions(xdata)[,"rtmed"]), sep = "") | |
511 | |
512 if (any(dup <- duplicated(gnames))) | |
513 for (dupname in unique(gnames[dup])) { | |
514 dupidx <- which(gnames == dupname) | |
515 gnames[dupidx] <- paste(gnames[dupidx], seq(along = dupidx), sep = "_") | |
516 } | |
517 | |
518 return (gnames) | |
519 } | |
520 | |
521 #@TODO: remove this function as soon as we can use xcms 3.x.x from Bioconductor 3.7 | |
522 # https://github.com/sneumann/xcms/issues/247 | |
523 .concatenate_XCMSnExp <- function(...) { | |
524 x <- list(...) | |
525 if (length(x) == 0) | |
526 return(NULL) | |
527 if (length(x) == 1) | |
528 return(x[[1]]) | |
529 ## Check that all are XCMSnExp objects. | |
530 if (!all(unlist(lapply(x, function(z) is(z, "XCMSnExp"))))) | |
531 stop("All passed objects should be 'XCMSnExp' objects") | |
532 new_x <- as(.concatenate_OnDiskMSnExp(...), "XCMSnExp") | |
533 ## If any of the XCMSnExp has alignment results or detected features drop | |
534 ## them! | |
535 x <- lapply(x, function(z) { | |
536 if (hasAdjustedRtime(z)) { | |
537 z <- dropAdjustedRtime(z) | |
538 warning("Adjusted retention times found, had to drop them.") | |
539 } | |
540 if (hasFeatures(z)) { | |
541 z <- dropFeatureDefinitions(z) | |
542 warning("Feature definitions found, had to drop them.") | |
543 } | |
544 z | |
545 }) | |
546 ## Combine peaks | |
547 fls <- lapply(x, fileNames) | |
548 startidx <- cumsum(lengths(fls)) | |
549 pks <- lapply(x, chromPeaks) | |
550 procH <- lapply(x, processHistory) | |
551 for (i in 2:length(fls)) { | |
552 pks[[i]][, "sample"] <- pks[[i]][, "sample"] + startidx[i - 1] | |
553 procH[[i]] <- lapply(procH[[i]], function(z) { | |
554 z@fileIndex <- as.integer(z@fileIndex + startidx[i - 1]) | |
555 z | |
556 }) | |
557 } | |
558 pks <- do.call(rbind, pks) | |
559 new_x@.processHistory <- unlist(procH) | |
560 chromPeaks(new_x) <- pks | |
561 if (validObject(new_x)) | |
562 new_x | |
563 } | |
564 | |
565 #@TODO: remove this function as soon as we can use xcms 3.x.x from Bioconductor 3.7 | |
566 # https://github.com/sneumann/xcms/issues/247 | |
567 .concatenate_OnDiskMSnExp <- function(...) { | |
568 x <- list(...) | |
569 if (length(x) == 0) | |
570 return(NULL) | |
571 if (length(x) == 1) | |
572 return(x[[1]]) | |
573 ## Check that all are XCMSnExp objects. | |
574 if (!all(unlist(lapply(x, function(z) is(z, "OnDiskMSnExp"))))) | |
575 stop("All passed objects should be 'OnDiskMSnExp' objects") | |
576 ## Check processingQueue | |
577 procQ <- lapply(x, function(z) z@spectraProcessingQueue) | |
578 new_procQ <- procQ[[1]] | |
579 is_ok <- unlist(lapply(procQ, function(z) | |
580 !is.character(all.equal(new_procQ, z)) | |
581 )) | |
582 if (any(!is_ok)) { | |
583 warning("Processing queues from the submitted objects differ! ", | |
584 "Dropping the processing queue.") | |
585 new_procQ <- list() | |
586 } | |
587 ## processingData | |
588 fls <- lapply(x, function(z) z@processingData@files) | |
589 startidx <- cumsum(lengths(fls)) | |
590 ## featureData | |
591 featd <- lapply(x, fData) | |
592 ## Have to update the file index and the spectrum names. | |
593 for (i in 2:length(featd)) { | |
594 featd[[i]]$fileIdx <- featd[[i]]$fileIdx + startidx[i - 1] | |
595 rownames(featd[[i]]) <- MSnbase:::formatFileSpectrumNames( | |
596 fileIds = featd[[i]]$fileIdx, | |
597 spectrumIds = featd[[i]]$spIdx, | |
598 nSpectra = nrow(featd[[i]]), | |
599 nFiles = length(unlist(fls)) | |
600 ) | |
601 } | |
602 featd <- do.call(rbind, featd) | |
603 featd$spectrum <- 1:nrow(featd) | |
604 ## experimentData | |
605 expdata <- lapply(x, function(z) { | |
606 ed <- z@experimentData | |
607 data.frame(instrumentManufacturer = ed@instrumentManufacturer, | |
608 instrumentModel = ed@instrumentModel, | |
609 ionSource = ed@ionSource, | |
610 analyser = ed@analyser, | |
611 detectorType = ed@detectorType, | |
612 stringsAsFactors = FALSE) | |
613 }) | |
614 expdata <- do.call(rbind, expdata) | |
615 expdata <- new("MIAPE", | |
616 instrumentManufacturer = expdata$instrumentManufacturer, | |
617 instrumentModel = expdata$instrumentModel, | |
618 ionSource = expdata$ionSource, | |
619 analyser = expdata$analyser, | |
620 detectorType = expdata$detectorType) | |
621 | |
622 ## protocolData | |
623 protodata <- lapply(x, function(z) z@protocolData) | |
624 if (any(unlist(lapply(protodata, nrow)) > 0)) | |
625 warning("Found non-empty protocol data, but merging protocol data is", | |
626 " currently not supported. Skipped.") | |
627 ## phenoData | |
628 pdata <- do.call(rbind, lapply(x, pData)) | |
629 res <- new( | |
630 "OnDiskMSnExp", | |
631 phenoData = new("NAnnotatedDataFrame", data = pdata), | |
632 featureData = new("AnnotatedDataFrame", featd), | |
633 processingData = new("MSnProcess", | |
634 processing = paste0("Concatenated [", date(), "]"), | |
635 files = unlist(fls), smoothed = NA), | |
636 experimentData = expdata, | |
637 spectraProcessingQueue = new_procQ) | |
638 if (validObject(res)) | |
639 res | |
640 } | |
641 | |
642 #@TODO: remove this function as soon as we can use xcms 3.x.x from Bioconductor 3.7 | |
643 # https://github.com/sneumann/xcms/issues/247 | |
644 c.XCMSnExp <- function(...) { | |
645 .concatenate_XCMSnExp(...) | |
646 } | |
647 | |
648 #@TODO: remove this function as soon as we can use xcms 3.x.x from Bioconductor 3.7 | |
649 # https://github.com/sneumann/xcms/issues/247 | |
650 c.MSnbase <- function(...) { | |
651 .concatenate_OnDiskMSnExp(...) | |
652 } |