Skip to content

Commit

Permalink
Lintr error corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCode404 committed Oct 23, 2024
1 parent 217570f commit 0f42d95
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 57 deletions.
11 changes: 2 additions & 9 deletions tools/metams/metaMS_plot/metaMS_plot.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
# created by Yann GUITTON and updated by Julien SAINT-VANNE


# ----- LOG FILE -----
# log_file <- file("log.txt", open = "wt")
# sink(log_file)
# sink(log_file, type = "output")


# ----- PACKAGE -----
cat("\tSESSION INFO\n")

Expand All @@ -33,7 +27,6 @@ cat("\nStart of the '", modNamC, "' Galaxy module call: ", format(Sys.time(), "%
# ----- ARGUMENTS -----
cat("\tARGUMENTS INFO\n\n")
args <- parseCommandArgs(evaluate = FALSE) # interpretation of arguments given in command line as an R list of objects
# write.table(as.matrix(args), col.names=F, quote=F, sep='\t\t')
print(cbind(value = unlist(args)))


Expand All @@ -49,7 +42,7 @@ if (args$selecteic) {
if (args$unkn[1] != "NULL") {
# When unkn = 0 user want to process all unknowns
if (args$unkn[1] == 0) {
args$unkn <- c(1:nrow(resGC$PeakTable))
args$unkn <- seq_len(nrow(resGC$PeakTable))
print("User want to process on all unknown(s) found in metaMS process")
}
# TODO find the biggest number of unkn ask by user cause it can write "1,15,9,8" with a max of 11 unkn. With this code it finds the 8 and it will pass
Expand Down Expand Up @@ -83,7 +76,7 @@ if (!exists("zipfile")) zipfile <- NULL
rawFilePath <- getRawfilePathFromArguments(singlefile, zipfile, args)
zipfile <- rawFilePath$zipfile
singlefile <- rawFilePath$singlefile
directory <- retrieveRawfileInTheWorkingDirectory(singlefile, zipfile)
directory <- retrieveRawfile(singlefile, zipfile)


# ----- MAIN PROCESSING INFO -----
Expand Down
24 changes: 4 additions & 20 deletions tools/metams/metaMS_runGC/metaMS_runGC.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
# use make.names in sampleMetadata to avoid issues with files names


# ----- LOG FILE -----
# log_file=file("log.txt", open = "wt")
# sink(log_file)
# sink(log_file, type = "output")


# ----- PACKAGE -----
cat("\tSESSION INFO\n\n")

Expand Down Expand Up @@ -44,9 +38,9 @@ cat("\n\n\tARGUMENTS PROCESSING INFO\n\n")
# RI parameter
if (args$ri != "NULL") {
RIarg <- read.table(args$ri)
if (ncol(RIarg) < 2) RIarg <- read.table(args$ri, h = T, sep = ";")
if (ncol(RIarg) < 2) RIarg <- read.table(args$ri, h = T, sep = "\t")
if (ncol(RIarg) < 2) RIarg <- read.table(args$ri, h = T, sep = ",")
if (ncol(RIarg) < 2) RIarg <- read.table(args$ri, h = TRUE, sep = ";")
if (ncol(RIarg) < 2) RIarg <- read.table(args$ri, h = TRUE, sep = "\t")
if (ncol(RIarg) < 2) RIarg <- read.table(args$ri, h = TRUE, sep = ",")
if (ncol(RIarg) < 2) {
error_message <- "Your RI file seems not well formatted. The column separators accepted are ; , and tabulation"
print(error_message)
Expand Down Expand Up @@ -145,16 +139,6 @@ if (args$settings == "User_defined") {
RIdiff = 5,
minfeat = minfeatparam
)

# to used if contaminant filter

# metaSetting(GALAXY.GC, "matchIrrelevants") <- list(
# irrelevantClasses = c("Bleeding", "Plasticizers"),
# timeComparison = "RI",
# RIdiff = RIdiffparam,
# rtdiff = rtdiffparam,
# simthresh = simthreshparam)

metaSetting(GALAXY.GC, "betweenSamples") <- list(
min.class.fraction = minclassfractionparam,
min.class.size = minclasssizeparam,
Expand Down Expand Up @@ -188,7 +172,7 @@ if (!exists("zipfile")) zipfile <- NULL
rawFilePath <- getRawfilePathFromArguments(singlefile, zipfile, args)
zipfile <- rawFilePath$zipfile
singlefile <- rawFilePath$singlefile
directory <- retrieveRawfileInTheWorkingDirectory(singlefile, zipfile)
directory <- retrieveRawfile(singlefile, zipfile)


# ----- MAIN PROCESSING INFO -----
Expand Down
53 changes: 25 additions & 28 deletions tools/metams/scripts/lib_metams.r
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ getCorrectFileName <- function(peaktable, sampleMetadata) {
}
}
# i now correspond to the first column with a sample
for (j in 1:(nrow(sampleMetadata))) {
for (j in seq_len(nrow(sampleMetadata))) {
col <- j + i - 1 # minus 1 cause i is the good column to start and j start at 1
if (col <= length(colnames(peaktable))) {
newname <- gsub("(^.*)(\\..*$)", "\\1", colnames(peaktable)[col])
Expand Down Expand Up @@ -112,7 +112,7 @@ getRawfilePathFromArguments <- function(singlefile, zipfile, listArguments) {
singlefile_sampleNames <- unlist(strsplit(singlefile_sampleNames, ","))

singlefile <- NULL
for (singlefile_galaxyPath_i in seq(1:length(singlefile_galaxyPaths))) {
for (singlefile_galaxyPath_i in seq_along(singlefile_galaxyPaths)) {
singlefile_galaxyPath <- singlefile_galaxyPaths[singlefile_galaxyPath_i]
singlefile_sampleName <- singlefile_sampleNames[singlefile_galaxyPath_i]
singlefile[[singlefile_sampleName]] <- singlefile_galaxyPath
Expand All @@ -129,7 +129,7 @@ getRawfilePathFromArguments <- function(singlefile, zipfile, listArguments) {
# This function retrieve the raw file in the working directory
# - if zipfile: unzip the file with its directory tree
# - if singlefiles: set symlink with the good filename
retrieveRawfileInTheWorkingDirectory <- function(singlefile, zipfile) {
retrieveRawfile <- function(singlefile, zipfile) {
if (!is.null(singlefile) && (length("singlefile") > 0)) {
for (singlefile_sampleName in names(singlefile)) {
singlefile_galaxyPath <- singlefile[[singlefile_sampleName]]
Expand All @@ -149,14 +149,11 @@ retrieveRawfileInTheWorkingDirectory <- function(singlefile, zipfile) {
stop(error_message)
}

# list all file in the zip file
# zip_files=unzip(zipfile,list=T)[,"Name"]

# unzip
suppressWarnings(unzip(zipfile, unzip = "unzip"))

# get the directory name
filesInZip <- unzip(zipfile, list = T)
filesInZip <- unzip(zipfile, list = TRUE)
directories <- unique(unlist(lapply(strsplit(filesInZip$Name, "/"), function(x) x[1])))
directories <- directories[!(directories %in% c("__MACOSX")) & file.info(directories)$isdir]
directory <- "."
Expand Down Expand Up @@ -187,7 +184,7 @@ getBPC2s <- function(files, xset = NULL, pdfname = "BPCs.pdf", rt = c("raw", "co
}
class <- unique(sampleMetadata[, "class"]) # create phenoData like table
classnames <- vector("list", length(class))
for (i in 1:length(class)) {
for (i in seq_along(class)) {
classnames[[i]] <- which(sampleMetadata[, "class"] == class[i])
}

Expand Down Expand Up @@ -225,13 +222,13 @@ getBPC2s <- function(files, xset = NULL, pdfname = "BPCs.pdf", rt = c("raw", "co
cat(paste(class[k], "vs", class[l], sep = " ", "\n"))
plot(0, 0, type = "n", xlim = xlim / 60, ylim = ylim, main = paste("Base Peak Chromatograms \n", "BPCs_", class[k], " vs ", class[l], sep = ""), xlab = "Retention Time (min)", ylab = "BPC")
colvect <- NULL
for (j in 1:length(classnames[[k]])) {
for (j in seq_along(classnames[[k]])) {
bpc <- BPC[[classnames[[k]][j]]]
# points(bpc[,1]/60, bpc[,2], col = cols[i], pch = pch[i], type="l")
points(bpc[, 1] / 60, bpc[, 2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type = "l")
colvect <- append(colvect, cols[classnames[[k]][j]])
}
for (j in 1:length(classnames[[l]])) {
for (j in seq_along(classnames[[l]])) {
# i=class2names[j]
bpc <- BPC[[classnames[[l]][j]]]
points(bpc[, 1] / 60, -bpc[, 2], col = cols[classnames[[l]][j]], pch = pch[classnames[[l]][j]], type = "l")
Expand All @@ -248,13 +245,13 @@ getBPC2s <- function(files, xset = NULL, pdfname = "BPCs.pdf", rt = c("raw", "co
colvect <- NULL
plot(0, 0, type = "n", xlim = xlim / 60, ylim = ylim, main = paste("Base Peak Chromatograms \n", "BPCs_", class[k], "vs", class[l], sep = ""), xlab = "Retention Time (min)", ylab = "BPC")

for (j in 1:length(classnames[[k]])) {
for (j in seq_along(classnames[[k]])) {
bpc <- BPC[[classnames[[k]][j]]]
# points(bpc[,1]/60, bpc[,2], col = cols[i], pch = pch[i], type="l")
points(bpc[, 1] / 60, bpc[, 2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type = "l")
colvect <- append(colvect, cols[classnames[[k]][j]])
}
for (j in 1:length(classnames[[l]])) {
for (j in seq_along(classnames[[l]])) {
# i=class2names[j]
bpc <- BPC[[classnames[[l]][j]]]
points(bpc[, 1] / 60, -bpc[, 2], col = cols[classnames[[l]][j]], pch = pch[classnames[[l]][j]], type = "l")
Expand All @@ -271,7 +268,7 @@ getBPC2s <- function(files, xset = NULL, pdfname = "BPCs.pdf", rt = c("raw", "co
colvect <- NULL
plot(0, 0, type = "n", xlim = xlim / 60, ylim = ylim, main = paste("Base Peak Chromatograms \n", "BPCs_", class[k], sep = ""), xlab = "Retention Time (min)", ylab = "BPC")

for (j in 1:length(classnames[[k]])) {
for (j in seq_along(classnames[[k]])) {
bpc <- BPC[[classnames[[k]][j]]]
# points(bpc[,1]/60, bpc[,2], col = cols[i], pch = pch[i], type="l")
points(bpc[, 1] / 60, bpc[, 2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type = "l")
Expand Down Expand Up @@ -301,7 +298,7 @@ getTIC2s <- function(files, xset = NULL, pdfname = "TICs.pdf", rt = c("raw", "co
}
class <- as.vector(levels(sampleMetadata[, "class"])) # create phenoData like table
classnames <- vector("list", length(class))
for (i in 1:length(class)) {
for (i in seq_along(class)) {
classnames[[i]] <- which(sampleMetadata[, "class"] == class[i])
}

Expand Down Expand Up @@ -333,13 +330,13 @@ getTIC2s <- function(files, xset = NULL, pdfname = "TICs.pdf", rt = c("raw", "co
cat(paste(class[k], "vs", class[l], "\n", sep = " "))
plot(0, 0, type = "n", xlim = xlim / 60, ylim = ylim, main = paste("Total Ion Chromatograms \n", "TICs_", class[k], " vs ", class[l], sep = ""), xlab = "Retention Time (min)", ylab = "TIC")
colvect <- NULL
for (j in 1:length(classnames[[k]])) {
for (j in seq_along(classnames[[k]])) {
tic <- TIC[[classnames[[k]][j]]]
# points(tic[,1]/60, tic[,2], col = cols[i], pch = pch[i], type="l")
points(tic[, 1] / 60, tic[, 2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type = "l")
colvect <- append(colvect, cols[classnames[[k]][j]])
}
for (j in 1:length(classnames[[l]])) {
for (j in seq_along(classnames[[l]])) {
# i=class2names[j]
tic <- TIC[[classnames[[l]][j]]]
points(tic[, 1] / 60, -tic[, 2], col = cols[classnames[[l]][j]], pch = pch[classnames[[l]][j]], type = "l")
Expand All @@ -355,13 +352,13 @@ getTIC2s <- function(files, xset = NULL, pdfname = "TICs.pdf", rt = c("raw", "co
l <- 2
plot(0, 0, type = "n", xlim = xlim / 60, ylim = ylim, main = paste("Total Ion Chromatograms \n", "TICs_", class[k], "vs", class[l], sep = ""), xlab = "Retention Time (min)", ylab = "TIC")
colvect <- NULL
for (j in 1:length(classnames[[k]])) {
for (j in seq_along(classnames[[k]])) {
tic <- TIC[[classnames[[k]][j]]]
# points(tic[,1]/60, tic[,2], col = cols[i], pch = pch[i], type="l")
points(tic[, 1] / 60, tic[, 2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type = "l")
colvect <- append(colvect, cols[classnames[[k]][j]])
}
for (j in 1:length(classnames[[l]])) {
for (j in seq_along(classnames[[l]])) {
# i=class2names[j]
tic <- TIC[[classnames[[l]][j]]]
points(tic[, 1] / 60, -tic[, 2], col = cols[classnames[[l]][j]], pch = pch[classnames[[l]][j]], type = "l")
Expand All @@ -375,7 +372,7 @@ getTIC2s <- function(files, xset = NULL, pdfname = "TICs.pdf", rt = c("raw", "co
ylim <- range(sapply(TIC, function(x) range(x[, 2])))
plot(0, 0, type = "n", xlim = xlim / 60, ylim = ylim, main = paste("Total Ion Chromatograms \n", "TICs_", class[k], sep = ""), xlab = "Retention Time (min)", ylab = "TIC")
colvect <- NULL
for (j in 1:length(classnames[[k]])) {
for (j in seq_along(classnames[[k]])) {
tic <- TIC[[classnames[[k]][j]]]
# points(tic[,1]/60, tic[,2], col = cols[i], pch = pch[i], type="l")
points(tic[, 1] / 60, tic[, 2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type = "l")
Expand All @@ -402,23 +399,23 @@ plotUnknowns <- function(resGC, unkn = "", DB = NULL, fileFrom = NULL) {
# Select only pspectra which correpond to them select in metaMS
# col1 = filtered spectra from runGC and col2 = an@spectra
allPCGRPs <- lapply(
1:length(resGC$xset),
seq_along(resGC$xset),
function(i) {
an <- resGC$xset[[i]]
huhn <- an@pspectra[which(sapply(an@pspectra, length) >=
metaSetting(
resGC$settings,
"DBconstruction.minfeat"
))]
matCORR <- cbind(1:length(huhn), match(huhn, an@pspectra))
matCORR <- cbind(seq_along(huhn), match(huhn, an@pspectra))
}
)
# Build a new annotation list with sampnames and pseudospectra number from xset
helpannotation <- list()
for (j in 1:length(resGC$xset)) {
for (j in seq_along(resGC$xset)) {
helpannotation[[j]] <- resGC$annotation[[j]][1:2]
pspvector <- vector()
for (i in 1:nrow(helpannotation[[j]])) {
for (i in seq_len(nrow(helpannotation[[j]]))) {
# Find corresponding pspec
psplink <- allPCGRPs[[j]][match(helpannotation[[j]][i, 1], allPCGRPs[[j]]), 2]
pspvector <- c(pspvector, psplink)
Expand All @@ -429,7 +426,7 @@ plotUnknowns <- function(resGC, unkn = "", DB = NULL, fileFrom = NULL) {
helpannotation[[j]][i, 2] <- new_name
} else {
# It has been found in local database
for (k in 1:length(DB)) {
for (k in seq_along(DB)) {
if (helpannotation[[j]][i, 2] == k) {
helpannotation[[j]][i, 2] <- DB[[k]]$Name
break
Expand All @@ -444,7 +441,7 @@ plotUnknowns <- function(resGC, unkn = "", DB = NULL, fileFrom = NULL) {

par(mar = c(5, 4, 4, 2) + 0.1)
# For each unknown
for (l in 1:length(unkn)) {
for (l in seq_along(unkn)) {
# recordPlot
perpage <- 3 # if change change layout also!
dev.new(width = 21 / 2.54, height = 29.7 / 2.54, file = paste("Unknown_", unkn[l], ".pdf", sep = "")) # A4 pdf
Expand All @@ -457,7 +454,7 @@ plotUnknowns <- function(resGC, unkn = "", DB = NULL, fileFrom = NULL) {
o.par <- par(mar = rep.int(0, 4))
on.exit(par(o.par))
# For each sample
for (c in 1:length(resGC$xset)) {
for (c in seq_along(resGC$xset)) {
# get sample name
sampname <- basename(resGC$xset[[c]]@xcmsSet@filepaths)
# remove .cdf, .mzXML filepattern
Expand All @@ -476,11 +473,11 @@ plotUnknowns <- function(resGC, unkn = "", DB = NULL, fileFrom = NULL) {
# an@xcmsSet@filepaths <- paste0("./",basename(an@xcmsSet@filepaths))
# }
# Find the good annotation for this sample
for (a in 1:length(helpannotation)) {
for (a in seq_len(length(helpannotation))) {
if (gsub(filepattern, "", names(helpannotation)[a]) == paste0("./", sampname)) {
# Find the unkn or the matched std in this sample
findunkn <- FALSE
for (r in 1:nrow(helpannotation[[a]])) {
for (r in seq_len(nrow(helpannotation[[a]]))) {
if (helpannotation[[a]][r, "annotation"] == peaktable[unkn[l], 1]) {
findunkn <- TRUE
pcgrp <- helpannotation[[a]][r, "pspvector"]
Expand Down

0 comments on commit 0f42d95

Please sign in to comment.