Skip to content

Commit

Permalink
#1 support for tic/basepeak chromatogram
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanse committed Oct 22, 2020
1 parent 8e096d5 commit eb40303
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 65 deletions.
115 changes: 63 additions & 52 deletions R/rawR.R
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ readSpectrum <- function(rawfile, scan = NULL, tmpdir=tempdir(), validate=FALSE)
#' @param tol tolerance in ppm.
#' @param filter defines the scan filter, default is \code{filter="ms"} if a
#' wrong filter is set the function will return \code{NULL} and gives a warning.
#' @param type xic for extracted ion chromatogram otherwise the function returns
#' a \code{data.frame} with total ion chromatogram and a base peak chromatogram.
#' @param mono if the mono enviroment should be used.
#' @param exe the exe file user by mono.
#'
Expand Down Expand Up @@ -369,74 +371,83 @@ readSpectrum <- function(rawfile, scan = NULL, tmpdir=tempdir(), validate=FALSE)
#' }
#'
readChromatogram <- function(rawfile,
mass = NULL,
tol = 10,
filter = "ms",
mono = if(Sys.info()['sysname'] %in% c("Darwin", "Linux")) TRUE else FALSE,
exe = file.path(path.package(package = "rawR"), "exec", "rawR.exe")){

mass = NULL,
tol = 10,
filter = "ms",
type = 'xic',
mono = if(Sys.info()['sysname'] %in% c("Darwin", "Linux")) TRUE else FALSE,
exe = file.path(path.package(package = "rawR"), "exec", "rawR.exe")){

if (!file.exists(rawfile)){
stop(paste0('file ', rawfile, ' is not available. return.'))
}
if (is.null(mass)){
stop('no mass vector is proived.')
}

if (!.isMonoAssemblyWorking()){
stop('the mono assembly are not available.')
}

tfstdout <- tempfile()
tfi <- tempfile()
tfo <- tempfile()
tfstdout <- tempfile()

cat(mass, file=tfi, sep="\n")

cmd <- exe

if (mono){
rvs <- system2("mono", args = c(shQuote(exe), shQuote(rawfile), "xic",
shQuote(tfi), tol, shQuote(tfo), shQuote(filter)))
}else{
rvs <- system2(exe, args = c(shQuote(rawfile), "xic", shQuote(tfi), tol,
shQuote(tfo), shQuote(filter)))
}


rv <- try({
e <- new.env();
e$chromatogram <- list()
source(tfo, local = TRUE)
if(type == 'xic'){
if (is.null(mass)){
stop('no mass vector is proived.')
}
cat(mass, file=tfi, sep="\n")

cmd <- exe

if ('warning' %in% names(e)){
warning(e$warning)
if (mono){
rvs <- system2("mono", args = c(shQuote(exe), shQuote(rawfile), "xic",
shQuote(tfi), tol, shQuote(tfo), shQuote(filter)))
}else{
rvs <- system2(exe, args = c(shQuote(rawfile), "xic", shQuote(tfi), tol,
shQuote(tfo), shQuote(filter)))
}

if ('message' %in% names(e)){
message(e$message)
}
rv <- try({
e <- new.env();
e$chromatogram <- list()
source(tfo, local = TRUE)


if ('warning' %in% names(e)){
warning(e$warning)
}

if ('message' %in% names(e)){
message(e$message)
}

if ('error' %in% names(e)){
warning(e$error)
return(NULL)
}

e$chromatogram
}, NULL)

if ('error' %in% names(e)){
warning(e$error)
return(NULL)
}

e$chromatogram
}, NULL)
#message(paste(c(tfi, tfo, tfstdout), collapse = ",\n"))
#message(length(rv))
rv <- lapply(rv,
function(x){
x$filename <- rawfile
x$tol <- tol
x$filter <- filter
x$times.max <- x$times[x$intensities==max(x$intensities)][1]
class(x) <- c(class(x), 'rawRchromatogram');
x})
class(rv) <- 'rawRchromatogramSet'
}else{
if (mono){
rvs <- system2("mono", args = c(shQuote(exe), shQuote(rawfile), "chromatogram", shQuote(filter)), stdout=tfstdout)
}else{
rvs <- system2(exe, args = c(shQuote(rawfile), "chromatogram", shQuote(filter)),stdout=tfstdout)
}
rv <- read.csv(tfstdout, header = TRUE, comment.char = "#")
}
unlink(c(tfi, tfo, tfstdout))

#message(paste(c(tfi, tfo, tfstdout), collapse = ",\n"))
#message(length(rv))
rv <- lapply(rv,
function(x){
x$filename <- rawfile
x$tol <- tol
x$filter <- filter
x$times.max <- x$times[x$intensities==max(x$intensities)][1]
class(x) <- c(class(x), 'rawRchromatogram');
x})

class(rv) <- 'rawRchromatogramSet'
rv
}

Expand Down
4 changes: 4 additions & 0 deletions man/readChromatogram.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 32 additions & 12 deletions src/rawR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ private static void Main(string[] args)
{"infoR", "print the raw file's meta data as R code."},
{"chromatogram", "base peak chromatogram."},
{"xic", "prints xic unfiltered."},
{"tic", "prints xic unfiltered."},
{"scans", "print spectrum of scanid as Rcode."}
};

Expand Down Expand Up @@ -504,7 +505,13 @@ private static void Main(string[] args)
if (mode == "chromatogram")
{
// Get the BasePeak chromatogram for the MS data
GetChromatogram(rawFile, firstScanNumber, lastScanNumber, true);
string filter = "ms";
try {
filter = args[2];
}
catch{
}
GetChromatogram(rawFile, firstScanNumber, lastScanNumber, true, filter);
Environment.Exit(0);
}

Expand Down Expand Up @@ -557,7 +564,7 @@ private static void Main(string[] args)
massList.Add(Convert.ToDouble(line));
}

ExtractChromatogram(rawFile, -1, -1, massList, ppmError, outputFilename, filter);
ExtractIonChromatogramAsRcode(rawFile, -1, -1, massList, ppmError, outputFilename, filter);
}

return;
Expand Down Expand Up @@ -602,28 +609,41 @@ private static void Main(string[] args)
/// <param name="outputData">
/// The output data flag.
/// </param>
private static void GetChromatogram(IRawDataPlus rawFile, int startScan, int endScan, bool outputData)
private static void GetChromatogram(IRawDataPlus rawFile, int startScan, int endScan, bool outputData, string filter = "ms")
{
if (IsValidFilter(rawFile, filter) == false){
Console.WriteLine("# '{0}' is not a valid filter string.", filter);
return;
}

// Define the settings for getting the Base Peak chromatogram
ChromatogramTraceSettings settings = new ChromatogramTraceSettings(TraceType.BasePeak);
ChromatogramTraceSettings settingsTIC = new ChromatogramTraceSettings(TraceType.TIC){Filter=filter};
ChromatogramTraceSettings settingsBasePeak = new ChromatogramTraceSettings(TraceType.BasePeak){Filter=filter};
ChromatogramTraceSettings settingsMassRange = new ChromatogramTraceSettings(TraceType.MassRange){Filter=filter};

// Get the chromatogram from the RAW file.
var data = rawFile.GetChromatogramData(new IChromatogramSettings[] {settings}, startScan, endScan);
var dataTIC = rawFile.GetChromatogramData(new IChromatogramSettings[] {settingsTIC}, startScan, endScan);
var dataMassRange = rawFile.GetChromatogramData(new IChromatogramSettings[] {settingsMassRange}, startScan, endScan);
var dataBasePeak = rawFile.GetChromatogramData(new IChromatogramSettings[] {settingsBasePeak}, startScan, endScan);

// Split the data into the chromatograms
var trace = ChromatogramSignal.FromChromatogramData(data);
var traceTIC = ChromatogramSignal.FromChromatogramData(dataTIC);
var traceMassRange = ChromatogramSignal.FromChromatogramData(dataMassRange);
var traceBasePeak = ChromatogramSignal.FromChromatogramData(dataBasePeak);

if (trace[0].Length > 0)
if (traceBasePeak[0].Length > 0)
{
// Print the chromatogram data (time, intensity values)
Console.WriteLine("# Base Peak chromatogram ({0} points)", trace[0].Length);
Console.WriteLine("# TIC chromatogram ({0} points)", traceTIC[0].Length);
Console.WriteLine("# Base Peak chromatogram ({0} points)", traceBasePeak[0].Length);
Console.WriteLine("# MassRange chromatogram ({0} points)", traceMassRange[0].Length);

Console.WriteLine("scan,rt,intensity");
Console.WriteLine("scan,rt,intensity.BasePeak,intensity.TIC,intensity.MassRange");
if (outputData)
{
for (int i = 0; i < trace[0].Length; i++)
for (int i = 0; i < traceBasePeak[0].Length; i++)
{
Console.WriteLine("{0},{1:F3},{2:F0}", i, trace[0].Times[i], trace[0].Intensities[i]);
Console.WriteLine("{0},{1:F3},{2:F0},{3:F0},{4:F0}", i, traceBasePeak[0].Times[i], traceBasePeak[0].Intensities[i], traceTIC[0].Intensities[i], traceMassRange[0].Intensities[i]);
}
}
}
Expand All @@ -638,7 +658,7 @@ private static bool IsValidFilter(IRawDataPlus rawFile, string filter)
return true;
}

private static void ExtractChromatogram(IRawDataPlus rawFile, int startScan, int endScan, List<double> massList,
private static void ExtractIonChromatogramAsRcode(IRawDataPlus rawFile, int startScan, int endScan, List<double> massList,
double ppmError, string filename, string filter = "ms")
{

Expand Down
2 changes: 1 addition & 1 deletion vignettes/JPR_TechnicalNote.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ author: "Tobias Kockmann^1‡^ & Christian Panse^1,2‡^"
date: "`r Sys.Date()`"
bibliography: rawR.bib
output:
pdf_document: default
html_document:
df_print: paged
citation_package: natbib
pdf_document: default
vignette: |
%\usepackage[utf8]{inputenc}
%\VignetteEncoding{UTF-8}
Expand Down

0 comments on commit eb40303

Please sign in to comment.