-
-
Save chasemc/d37a81a560e33af736605609214e4319 to your computer and use it in GitHub Desktop.
#!/usr/bin/env Rscript | |
args = commandArgs(trailingOnly=TRUE) | |
message("Installing necessary libraries if not already installed") | |
if (!requireNamespace("BiocManager", quietly = TRUE)) | |
install.packages("BiocManager") | |
if (!requireNamespace("mzR", quietly = TRUE)) | |
install.packages("mzR") | |
if (!requireNamespace("data.table", quietly = TRUE)) | |
install.packages("data.table") | |
library(data.table) | |
if (length(args) != 3) { | |
stop(length(args)) | |
stop('Three input arguments required:\n1: mzml-path\n2: outfile-path\n3: separator ("comma" or "tab")') | |
} | |
infile_path <- args[1] | |
outfile_path <- args[2] | |
sep <- switch(args[3], | |
"comma" = ",", | |
"tab" = "\t") | |
if (! sep %in% c(",", "\t")) { | |
sep <- args[3] | |
} | |
if (!file.exists(infile_path)) { | |
stop("Couldn't find file") | |
} | |
if (!dir.exists(dirname(outfile_path))) { | |
stop("Couldn't find outfile_path directory") | |
} | |
if (file.exists(outfile_path)) { | |
stop("outfile_path already exists") | |
} | |
message(paste0("Reading ", basename(infile_path))) | |
ms_file_pointer <- mzR::openMSfile(infile_path) | |
message("Parsing file") | |
mz_header <- mzR::header(ms_file_pointer) # provided file is only MS1 | |
mz_header <- as.data.table(mz_header) | |
mz_data <- mzR::peaks(ms_file_pointer) | |
mz_data <- lapply(seq_along(mz_data), | |
function(x){ | |
data.table(mass = mz_data[[x]][ , 1], | |
intensity = mz_data[[x]][ , 2], | |
scan = x) | |
}) | |
mz_data <- do.call(rbind, | |
mz_data) | |
mz_data <- merge(mz_data, | |
mz_header[ , c("acquisitionNum", "msLevel")], | |
by.x = "scan", | |
by.y = "acquisitionNum") | |
message("Writing new file") | |
fwrite(mz_data, | |
outfile_path, | |
sep = "\t") | |
message("Done") | |
modified MIT License (no attribution required)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The script takes three arguments:
1: Path to the mzml/mzxml file. Use quotes around path.
2: Path where new file will be created. Use quotes around path.
3. Separator ("tab" or "comma")
Example use:
Sometimes it's hard to install the mzR library (automatically done in script). If installation fails try installing netcdf
To install netcdf with conda:
conda install -c anaconda netcdf4