Skip to content

Commit

Permalink
Merge pull request #5 from ethanbass/dev
Browse files Browse the repository at this point in the history
add openchrom_parser function
  • Loading branch information
ethanbass committed Jun 11, 2022
2 parents 5d0e813 + fea1eae commit 8da0c15
Show file tree
Hide file tree
Showing 19 changed files with 355 additions and 68 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Imports:
reticulate,
stringr,
tidyr,
utils
utils,
magrittr,
xml2
Suggests:
entab,
mzR,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(entab_reader)
export(read_chemstation_csv)
export(read_chromeleon)
export(read_chroms)
export(read_fid)
Expand All @@ -10,7 +11,9 @@ export(read_thermoraw)
export(read_waters_arw)
export(sp_converter)
export(uv_converter)
import(magrittr)
import(reticulate)
import(xml2)
importFrom(purrr,partial)
importFrom(readr,read_lines)
importFrom(readr,read_tsv)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Added `read_entab` function for calling Entab parsers.
* Added `read_thermoraw` function to convert ThermoRawFiles by calling the ThermoRawFileParser.
* Added `read_mzml` function to extract UV data from mzML files using `mzR`.
* Added `openchrom_parser` to call OpenChrom parsers through the commandline interface.

All of the above functions can also be called from `read_chroms` by setting the `format_in` and `parser` arguments.

Expand Down
135 changes: 135 additions & 0 deletions R/openchrom_parser.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#' Parse files with OpenChrom
#'
#' To use this function [OpenChrom](https://lablicate.com/platform/openchrom) must be manually installed.
#'
#' The \code{openchrom_parser} works by creating an \code{xml} batchfile and
#' feeding it to the OpenChrom commandline interface. OpenChrom batchfiles
#' consist of \code{InputEntries} (the files you want to convert) and \code{
#' ProcessEntries} (what you want to do to the files). The parsers are organized
#' into broad categories by detector-type and output format. The detector-types
#' are \code{msd} (mass selective detectors), \code{csd} (current selective
#' detectors, such as FID, ECD, NPD), and \code{wsd} (wavelength selective
#' detectors, such as DAD, and UV/VIS). Thus, when calling the OpenChrom parsers,
#' you must select one of these three options for the input format (\code{format_in}).

#' @import xml2
#' @import magrittr
#' @param files files to parse
#' @param path_out directory to export converted files.
#' @param format_in Either `msd` for mass spectrometry data, `csd` for flame ionization data, or `wsd` for DAD/UV data.
#' @param export_format Either \code{mzml}, \code{cdf}, \code{animl}, or \code{csv}.
#' @return If \code{export_format} is \code{csv}, the function will return a list
#' of chromatograms in \code{data.frame} format. Otherwise it will not return anything.
#' @section Side effects: Chromatograms will be exported in the format specified
#' by \code{export_format} in the folder specified by \code{path_out}.
#' @author Ethan Bass
#' @export openchrom_parser
#' If you want to use the OpenChrom GUI, it is recommended to create a separate
#' command-line version of OpenChrom to call from R.
#'

openchrom_parser <- function(files, path_out, format_in,
export_format=c("mzml", "cdf", "animl", "csv"),
export_paths = FALSE){
if (missing(format_in))
stop("Format must be specified. The options are `msd` for mass spectrometry, `csd` for flame ionization (FID),
or `wsd` for DAD/UV data.")
export_format <- match.arg(export_format, c("mzml", "cdf", "animl", "csv"))
if (missing(path_out)){
path_out <- set_temp_directory()
}
if(!file.exists(path_out)){
stop("'path_out' not found. Make sure directory exists.")
}
openchrom_path <- configure_openchrom_parser()
path_template <- system.file("openchrom_template.xml", package = "chromConverter")
x <- xml2::read_xml(x = path_template)
# add files to InputEntries
for (file in files){
x %>% xml_children %>% .[[3]] %>%
xml_add_child(.value = "InputEntry") %>% xml_add_child(xml_cdata(file))
}
# add parser to ProcessEntries
msd_mzml_converter<-'ProcessEntry id="msd.export.org.eclipse.chemclipse.msd.converter.supplier.mzml" name="mzML Chromatogram (*.mzML)" description="Reads mzML Chromatograms" jsonSettings="{&quot;Filename&quot;:&quot;{chromatogram_name}{extension}&quot;,&quot;Export Folder&quot;:&quot;path_out&quot;}" symbolicName="" className="" dataTypes=""'
msd_netcdf_converter<-'ProcessEntry id="msd.export.net.openchrom.msd.converter.supplier.cdf" name="ANDI/AIA CDF Chromatogram (*.CDF)" description="Reads an writes ANDI/AIA CDF Chromatograms." jsonSettings="{&quot;Filename&quot;:&quot;{chromatogram_name}{extension}&quot;,&quot;Export Folder&quot;:&quot;path_out&quot;}" symbolicName="" className="" dataTypes=""'
msd_animl_converter <- 'ProcessEntry id="msd.export.net.openchrom.msd.converter.supplier.animl.chromatogram" name="AnIML MSD Chromatogram (*.animl)" description="Reads and writes Analytical Information Markup Language Chromatograms" jsonSettings="{&quot;Filename&quot;:&quot;{chromatogram_name}{extension}&quot;,&quot;Export Folder&quot;:&quot;path_out&quot;}" symbolicName="" className="" dataTypes=""'
msd_csv_converter <- 'ProcessEntry id="msd.export.org.eclipse.chemclipse.msd.converter.supplier.csv" name="CSV Chromatogram (*.csv)" description="Reads and Writes Chromatograms to CSV." jsonSettings="{&quot;Filename&quot;:&quot;{chromatogram_name}{extension}&quot;,&quot;Export Folder&quot;:&quot;path_out&quot;}" symbolicName="" className="" dataTypes=""'
csd_csv_converter <- 'ProcessEntry id="csd.export.org.eclipse.chemclipse.csd.converter.supplier.csv" name="CSV Chromatogram (*.csv)" description="Reads and Writes Chromatograms to CSV." jsonSettings="{&quot;Filename&quot;:&quot;{chromatogram_name}{extension}&quot;,&quot;Export Folder&quot;:&quot;path_out&quot;}" symbolicName="" className="" dataTypes=""'
csd_animl_converter <- 'ProcessEntry id="csd.export.net.openchrom.csd.converter.supplier.animl" name="AnIML FID Chromatogram (*.animl)" description="Writes AnIML Chromatograms." jsonSettings="{&quot;Filename&quot;:&quot;{chromatogram_name}{extension}&quot;,&quot;Export Folder&quot;:&quot;path_out&quot;}" symbolicName="" className="" dataTypes=""'
wsd_animl_converter <- 'ProcessEntry id="wsd.export.net.openchrom.wsd.converter.supplier.animl.chromatogram" name="AnIML UV-Vis Chromatogram (*.animl)" description="Reads Analytical Information Markup Language Chromatograms" jsonSettings="{&quot;Filename&quot;:&quot;{chromatogram_name}{extension}&quot;,&quot;Export Folder&quot;:&quot;path_out&quot;}" symbolicName="" className="" dataTypes=""'
wsd_csv_converter <- 'ProcessEntry id="wsd.export.org.eclipse.chemclipse.csd.converter.supplier.csv" name="CSV Chromatogram (*.csv)" description="Reads and Writes Chromatograms to CSV." jsonSettings="{&quot;Filename&quot;:&quot;{chromatogram_name}{extension}&quot;,&quot;Export Folder&quot;:&quot;path_out&quot;}" symbolicName="" className="" dataTypes=""'
if (format_in == "msd"){
parser <- switch(export_format,
"mzml" = msd_mzml_converter,
"cdf" = msd_netcdf_converter,
"animl" = msd_animl_converter,
"csv" = msd_csv_converter)
} else if (format_in == "csd"){
parser <- switch(export_format,
"csv" = csd_csv_converter,
"animl" = csd_animl_converter)
} else if (format_in == "wsd"){
parser <- switch(export_format,
"csv" = wsd_csv_converter,
"animl" = wsd_animl_converter)
}
x %>% xml_children %>% .[[4]] %>% xml_add_child(.value=gsub("path_out", path_out, parser))
path_xml <- paste0(path_out, "batchfile_", strftime(Sys.time(),format = "%Y-%m-%d_%H-%M-%S"), ".xml")
write_xml(x, file = path_xml)
openchrom_path <- "/Applications/OpenChrom_CL.app/Contents/MacOS/openchrom"
system(paste0(openchrom_path, " -nosplash -cli -batchfile ", path_xml))
new_files <- paste0(path_out, sapply(strsplit(basename(files), "\\."), function(x) x[1]), ".", export_format)
if (export_paths){
new_files
} else{
if (export_format == "csv"){
lapply(new_files, read.csv)
}
}
}

#' Configure OpenChrom parser
#'
#' @name configure_openchrom_parser
#' @param cli Defaults to NULL. If "true", R will rewrite openchrom ini file to enable CLI.
#' If "false", R will disable CLI. If NULL, R will not modify the ini file.
#' @return No return value.
#' @author Ethan Bass
#' @noRd
configure_openchrom_parser <- function(cli = c(NULL, "true", "false")){
cli <- match.arg(cli, c(NULL, "true", "false"))
path_parser <- readLines(system.file("shell/path_to_openchrom_commandline.txt", package = 'chromConverter'))
if (!file.exists(path_parser)){
warning("OpenChrom not found!", immediate. = TRUE)
path_parser <- readline(prompt="Please provide path to `OpenChrom` command line):")
writeLines(path_parser, con = system.file('shell/path_to_openchrom_commandline.txt', package='chromConverter'))
}
path_ini <- switch(.Platform$OS.type,
"unix" = paste0(gsub("MacOS/openchrom", "", path_parser), "Eclipse/openchrom.ini"),
"linux" = paste0(path_parser, ".uni"),
"windows" = paste0(gsub(".exe", "", path_parser), ".ini"))
ini <- readLines(path_ini)
cli_index <- grep("-Denable.cli.support",ini)
ini_split <- strsplit(ini[cli_index],"=")[[1]]
cli_tf <- ini_split[2]
if(cli_tf == "false"){
message(" The OpenChrom command-line interface is turned off!
Update `openchrom.ini` to activate the command-line interface (y/n)?
(Warning: This will deactivate the GUI on your OpenChrom installation!)")
ans <- readline()
if (ans %in% c("y","Y", "yes", "Yes", "YES")){
cli <- "true"
} else{
stop("-Denable.cli.support must be enabled to use the OpenChrom parsers from R.")
}
}
if (cli %in% c("true", "false")){
ini_split[2] <- cli
ini[cli_index] <- paste(ini_split, collapse="=")
writeLines(ini, path_ini)
}
path_parser[1]
}

utils::globalVariables(names = c('.'))

Loading

0 comments on commit 8da0c15

Please sign in to comment.