diff MassbankConn.R @ 1:45e985cd8e9e draft

planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit d4048accde6bdfd5b3e14f5394902d38991854f8-dirty
author prog
date Tue, 31 Jan 2017 05:27:24 -0500
parents 3afe41d3e9e7
children
line wrap: on
line diff
--- a/MassbankConn.R	Mon Jul 11 09:12:03 2016 -0400
+++ b/MassbankConn.R	Tue Jan 31 05:27:24 2017 -0500
@@ -1,59 +1,122 @@
-if ( ! exists('MassbankConn')) { # Do not load again if already loaded
+#####################
+# CLASS DECLARATION #
+#####################
+
+MassbankConn <- methods::setRefClass("MassbankConn", contains = c("RemotedbConn", "MassdbConn"), fields = list( .url = "character" ))
 
-	source('BiodbConn.R')
-	source('MassbankSpectrum.R')
+###############
+# 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
 
-	#####################
-	# CLASS DECLARATION #
-	#####################
-	
-	MassbankConn <- setRefClass("MassbankConn", contains = "BiodbConn")
+	callSuper(...)
+})
+
+##########################
+# GET ENTRY CONTENT TYPE #
+##########################
 
-	##########################
-	# GET ENTRY CONTENT TYPE #
-	##########################
+MassbankConn$methods( getEntryContentType = function() {
+	return(BIODB.TXT) 
+})
 
-	MassbankConn$methods( getEntryContentType = function(type) {
-		return(if (type == RBIODB.SPECTRUM) RBIODB.TXT else NULL) 
-	})
+#####################
+# GET ENTRY CONTENT #
+#####################
+
+MassbankConn$methods( getEntryContent = function(ids) {
 
-	#####################
-	# GET ENTRY CONTENT #
-	#####################
-	
-	MassbankConn$methods( getEntryContent = function(type, id) {
+	# Debug
+	.self$.print.debug.msg(paste0("Get entry content(s) for ", length(ids)," id(s)..."))
+
+	URL.MAX.LENGTH <- 2083
 
-		if (type == RBIODB.SPECTRUM) {
+	# Initialize return values
+	content <- rep(NA_character_, length(ids))
 
-			# Initialize return values
-			content <- rep(NA_character_, length(id))
+	# Loop on all
+	n <- 0
+	while (n < length(ids)) {
+
+		# Get list of accession ids to retrieve
+		accessions <- ids[(n + 1):length(ids)]
 
-			# Request
-			xmlstr <- .self$.scheduler$getUrl(get.entry.url(RBIODB.MASSBANK, id, RBIODB.TXT))
+		# 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)
 
-			# Parse XML and get text
-			if ( ! is.na(xmlstr)) {
-				library(XML)
-				xml <-  xmlInternalTreeParse(xmlstr, asText = TRUE)
-				ns <- c(ax21 = "http://api.massbank/xsd")
-				returned.ids <- xpathSApply(xml, "//ax21:id", xmlValue, namespaces = ns)
-				content[match(returned.ids, id)] <- xpathSApply(xml, "//ax21:info", xmlValue, namespaces = ns)
-			}
+		# Increase number of entries retrieved
+		n <- n + x$n
 
-			return(content)
+		# 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)
 		}
 
-		return(NULL)
-	})
-	
-	################
-	# 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(type, content, drop = TRUE) {
-		return(if (type == RBIODB.SPECTRUM) createMassbankSpectrumFromTxt(content, drop = drop) else NULL)
-	})
-}
+		# 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()))
+})