Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SpectrumParser virtual class #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
50 changes: 50 additions & 0 deletions R/SpectrumParser.R
Original file line number Diff line number Diff line change
@@ -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")
})

54 changes: 54 additions & 0 deletions R/massbankParser.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}
26 changes: 26 additions & 0 deletions man/MassbankParser.Rd

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

30 changes: 30 additions & 0 deletions man/SpectrumParser.Rd

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

23 changes: 23 additions & 0 deletions man/dot-massbankParseProcessBuffer.Rd

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

17 changes: 17 additions & 0 deletions man/dot-massbankParseRecord.Rd

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

11 changes: 11 additions & 0 deletions man/parserMassBank.Rd

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