diff MassbankConn.R @ 2:20d69a062da3 draft

planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit d4048accde6bdfd5b3e14f5394902d38991854f8
author prog
date Thu, 02 Mar 2017 08:55:00 -0500
parents 253d531a0193
children
line wrap: on
line diff
--- a/MassbankConn.R	Sat Sep 03 17:02:01 2016 -0400
+++ b/MassbankConn.R	Thu Mar 02 08:55:00 2017 -0500
@@ -1,92 +1,122 @@
-if ( ! exists('MassbankConn')) { # Do not load again if already loaded
+#####################
+# CLASS DECLARATION #
+#####################
 
-	source('RemotedbConn.R')
-	source('MassdbConn.R')
-	source('MassbankSpectrum.R')
+MassbankConn <- methods::setRefClass("MassbankConn", contains = c("RemotedbConn", "MassdbConn"), fields = list( .url = "character" ))
+
+###############
+# CONSTRUCTOR #
+###############
 
-	#####################
-	# CLASS DECLARATION #
-	#####################
-	
-	MassbankConn <- setRefClass("MassbankConn", contains = c("RemotedbConn", "MassdbConn"))
+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 #
+##########################
 
-	##########################
-	# 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)..."))
 
-	MassbankConn$methods( getEntryContentType = function(type) {
-		return(if (type == BIODB.SPECTRUM) BIODB.TXT else NULL) 
-	})
+	URL.MAX.LENGTH <- 2083
+
+	# Initialize return values
+	content <- rep(NA_character_, length(ids))
 
-	#####################
-	# GET ENTRY CONTENT #
-	#####################
-	
-	MassbankConn$methods( getEntryContent = function(type, 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("Get entry content(s) for ", length(ids)," id(s)..."))
-
-		if (type == BIODB.SPECTRUM) {
-
-			URL.MAX.LENGTH <- 2083
-
-			# Initialize return values
-			content <- rep(NA_character_, length(ids))
+		.self$.print.debug.msg(paste0("Send URL request for ", x$n," id(s)..."))
 
-			# Loop on all
-			n <- 0
-			while (n < length(ids)) {
+		# Send request
+		xmlstr <- .self$.get.url(x$url)
 
-				# 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)
+		# Increase number of entries retrieved
+		n <- n + x$n
 
-				# Debug
-				.self$.print.debug.msg(paste0("Send URL request for ", x$n," id(s)..."))
-
-				# Send request
-				xmlstr <- .self$.scheduler$getUrl(x$url)
-
-				# Increase number of entries retrieved
-				n <- n + x$n
-
-				# 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, 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)
+		# 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 == BIODB.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 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()))
+})