Mercurial > repos > prog > lcmsmatching
diff MassbankConn.R @ 6:f86fec07f392 draft default tip
planemo upload commit c397cd8a93953798d733fd62653f7098caac30ce
author | prog |
---|---|
date | Fri, 22 Feb 2019 16:04:22 -0500 |
parents | fb9c0409d85c |
children |
line wrap: on
line diff
--- a/MassbankConn.R Wed Apr 19 10:00:05 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,122 +0,0 @@ -##################### -# CLASS DECLARATION # -##################### - -MassbankConn <- methods::setRefClass("MassbankConn", contains = c("RemotedbConn", "MassdbConn"), fields = list( .url = "character" )) - -############### -# CONSTRUCTOR # -############### - -MassbankConn$methods( initialize = function(url = NA_character_, ...) { - - # Set URL - .url <<- if (is.null(url) || is.na(url)) BIODB.MASSBANK.EU.WS.URL else url - - callSuper(...) -}) - -########################## -# GET ENTRY CONTENT TYPE # -########################## - -MassbankConn$methods( getEntryContentType = function() { - return(BIODB.TXT) -}) - -##################### -# GET ENTRY CONTENT # -##################### - -MassbankConn$methods( getEntryContent = function(ids) { - - # Debug - .self$.print.debug.msg(paste0("Get entry content(s) for ", length(ids)," id(s)...")) - - URL.MAX.LENGTH <- 2083 - - # Initialize return values - content <- rep(NA_character_, length(ids)) - - # Loop on all - n <- 0 - while (n < length(ids)) { - - # Get list of accession ids to retrieve - accessions <- ids[(n + 1):length(ids)] - - # Create URL request - x <- get.entry.url(class = BIODB.MASSBANK, accession = accessions, content.type = BIODB.TXT, max.length = URL.MAX.LENGTH, base.url = .self$.url) - - # Debug - .self$.print.debug.msg(paste0("Send URL request for ", x$n," id(s)...")) - - # Send request - xmlstr <- .self$.get.url(x$url) - - # Increase number of entries retrieved - n <- n + x$n - - # Parse XML and get text - if ( ! is.na(xmlstr)) { - xml <- xmlInternalTreeParse(xmlstr, asText = TRUE) - ns <- c(ax21 = "http://api.massbank/xsd") - returned.ids <- xpathSApply(xml, "//ax21:id", xmlValue, namespaces = ns) - if (length(returned.ids) > 0) - content[match(returned.ids, ids)] <- xpathSApply(xml, "//ax21:info", xmlValue, namespaces = ns) - } - - # Debug - .self$.print.debug.msg(paste0("Now ", length(ids) - n," id(s) left to be retrieved...")) - } - - return(content) -}) - -################ -# CREATE ENTRY # -################ - -# Creates a Spectrum instance from file content. -# content A file content, downloaded from the public database. -# RETURN A spectrum instance. -MassbankConn$methods( createEntry = function(content, drop = TRUE) { - return(createMassbankEntryFromTxt(content, drop = drop)) -}) - -################# -# GET MZ VALUES # -################# - -MassbankConn$methods( getMzValues = function(mode = NULL, max.results = NA_integer_) { -}) - -################# -# GET ENTRY IDS # -################# - -MassbankConn$methods( getEntryIds = function(max.results = NA_integer_) { - - # Set URL - url <- paste0(.self$.url, 'searchPeak?mzs=1000&relativeIntensity=100&tolerance=1000&instrumentTypes=all&ionMode=Both') - url <- paste0(url, '&maxNumResults=', (if (is.na(max.results)) 0 else max.results)) - - # Send request - xmlstr <- .self$.get.url(url) - - # Parse XML and get text - if ( ! is.na(xmlstr)) { - xml <- xmlInternalTreeParse(xmlstr, asText = TRUE) - ns <- c(ax21 = "http://api.massbank/xsd") - returned.ids <- xpathSApply(xml, "//ax21:id", xmlValue, namespaces = ns) - return(returned.ids) - } -}) - -################## -# GET NB ENTRIES # -################## - -MassbankConn$methods( getNbEntries = function() { - return(length(.self$getEntryIds())) -})