diff --git a/NAMESPACE b/NAMESPACE index b1741fb..02d726b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,8 +2,12 @@ export(.massbankParseProcessBuffer) export(.massbankParseRecord) +export(MassbankParser) export(parserMassBank) export(schema_export) export(schema_import) +exportClasses(MassbankParser) +exportClasses(SpectrumParser) +exportMethods(importSpectrum) import(MSnbase) import(yaml) diff --git a/R/SpectrumParser.R b/R/SpectrumParser.R new file mode 100644 index 0000000..44f399a --- /dev/null +++ b/R/SpectrumParser.R @@ -0,0 +1,50 @@ + +#' @title Spectrum parser +#' +#' @name SpectrumParser +#' +#' @description +#' +#' Classes extending the base `SpectrumParser` object are supposed to read +#' spectrum data from a certain input format and return a standardized output +#' format. +#' +#' @md +#' +#' @author Johannes Rainer +#' +#' @exportClass SpectrumParser +NULL + +setClass("SpectrumParser", + slots = c(schema = "list"), + prototype = prototype(schema = list()), + contains = "VIRTUAL") + +#' @description +#' +#' `importSpectrum` reads spectrum data from a file, extracted all fields and +#' maps them to the corresponding *standard* fields using the parser's schema +#' definition. +#' +#' @param file `character(1)` with the name of the file from which spectrum +#' data should be imported. +#' +#' @param object object extending `SpectrumParser`. +#' +#' @return a `Spectra` object - TODO needs to be discussed! +#' +#' @md +#' +#' @exportMethod importSpectrum +#' +#' @rdname SpectrumParser +setGeneric("importSpectrum", function(object, file, ...) + standardGeneric("importSpectrum")) +setMethod("importSpectrum", "SpectrumParser", function(object, file, ...) + stop("importSpectrum not implemented for ", class(object))) + +setMethod("show", "SpectrumParser", function(object) { + cat("Object of class", class(object), "\n") +}) + diff --git a/R/massbankParser.R b/R/massbankParser.R index 9b5ac5f..9dccba9 100644 --- a/R/massbankParser.R +++ b/R/massbankParser.R @@ -211,3 +211,57 @@ parserMassBank <- function() "block" = .massbankRuleParseBlock ) )} + +#' @title Massbank parser +#' +#' @description +#' +#' Parser for Massbank spectrum files. +#' +#' New objects are supposed to be created with the `MassbankParser` function. +#' +#' @author Michele Stravs, Johannes Rainer +#' +#' @name MassbankParser +#' +#' @exportClass MassbankParser +#' +#' @examples +#' library(MSnio) +#' +#' ## Create and intialize a parser +#' prs <- MassbankParser() +#' +#' ## Spectrum file +#' fl <- system.file("spectra/massbank/EA016614.txt", package="MSnio") +#' res <- importSpectrum(prs, fl) +setClass("MassbankParser", + contains = "SpectrumParser") + +#' @rdname MassbankParser +setMethod("importSpectrum", "MassbankParser", function(object, file, ...) { + if (!file.exists(file)) + stop("File ", file, " not found") + if (length(object@schema) == 0) + stop("Parser does not have any schema") + tkns <- .massbankParseRecord(readLines(file)) + ## map to "standard" fields, return a Spectra/Spectrum/whatever + tkns +}) + +setValidity("MassbankParser", function(object) { + msg <- NULL + ## Would be nice to check the schema for correctness or similar. + if (length(msg)) msg + else TRUE +}) + + +#' @rdname MassbankParser +#' +#' @export MassbankParser +MassbankParser <- function() { + new("MassbankParser", + schema = yaml.load_file(system.file("schemas/schema_massbank_auto.yaml", + package="MSnio"))) +} diff --git a/man/MassbankParser.Rd b/man/MassbankParser.Rd new file mode 100644 index 0000000..83c9bef --- /dev/null +++ b/man/MassbankParser.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/massbankParser.R +\docType{class} +\name{MassbankParser} +\alias{MassbankParser} +\alias{importSpectrum,MassbankParser-method} +\title{Massbank parser} +\usage{ +\S4method{importSpectrum}{MassbankParser}(object, file, ...) + +MassbankParser() +} +\description{ +Parser for Massbank spectrum files. + +New objects are supposed to be created with the `MassbankParser` function. +} +\examples{ +library(MSnio) + +## Create and intialize a parser +prs <- MassbankParser() +} +\author{ +Michele Stravs, Johannes Rainer +} diff --git a/man/SpectrumParser.Rd b/man/SpectrumParser.Rd new file mode 100644 index 0000000..2127942 --- /dev/null +++ b/man/SpectrumParser.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/SpectrumParser.R +\name{SpectrumParser} +\alias{SpectrumParser} +\alias{importSpectrum} +\title{Spectrum parser} +\usage{ +importSpectrum(object, file, ...) +} +\arguments{ +\item{object}{object extending \code{SpectrumParser}.} + +\item{file}{\code{character(1)} with the name of the file from which spectrum +data should be imported.} +} +\value{ +a \code{Spectra} object - TODO needs to be discussed! +} +\description{ +Classes extending the base \code{SpectrumParser} object are supposed to read +spectrum data from a certain input format and return a standardized output +format. + +\code{importSpectrum} reads spectrum data from a file, extracted all fields and +maps them to the corresponding \emph{standard} fields using the parser's schema +definition. +} +\author{ +Johannes Rainer +} diff --git a/man/dot-massbankParseProcessBuffer.Rd b/man/dot-massbankParseProcessBuffer.Rd new file mode 100644 index 0000000..2b9299d --- /dev/null +++ b/man/dot-massbankParseProcessBuffer.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/massbankParser.R +\name{.massbankParseProcessBuffer} +\alias{.massbankParseProcessBuffer} +\title{MassBank parser - line buffer processing} +\usage{ +.massbankParseProcessBuffer(record, lines) +} +\arguments{ +\item{record}{} + +\item{lines}{} +} +\description{ +This receives a single or multiple line entry +(MassBank multiline entries continue the preceding +line with starting whitespace, see record spec.) +} +\details{ +It parses the multiline buffer into tag name and +vector of lines, without further processing, and updates +the record accordingly. +} diff --git a/man/dot-massbankParseRecord.Rd b/man/dot-massbankParseRecord.Rd new file mode 100644 index 0000000..576881d --- /dev/null +++ b/man/dot-massbankParseRecord.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/massbankParser.R +\name{.massbankParseRecord} +\alias{.massbankParseRecord} +\title{MassBank parser - line parser} +\usage{ +.massbankParseRecord(lines) +} +\arguments{ +\item{lines}{Character vector with the record in line-by-line format.} +} +\value{ +List of root-level tag entries (name: tag name, content: unprocessed tag value) +} +\description{ +MassBank parser - line parser +} diff --git a/man/parserMassBank.Rd b/man/parserMassBank.Rd new file mode 100644 index 0000000..7e7930b --- /dev/null +++ b/man/parserMassBank.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/massbankParser.R +\name{parserMassBank} +\alias{parserMassBank} +\title{Generate a MassBank record parser} +\usage{ +parserMassBank() +} +\description{ +Generate a MassBank record parser +}