comparison MassbankConn.R @ 1:253d531a0193 draft

planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 36c9d8099c20a1ae848f1337c16564335dd8fb2b
author prog
date Sat, 03 Sep 2016 17:02:01 -0400
parents e66bb061af06
children 20d69a062da3
comparison
equal deleted inserted replaced
0:e66bb061af06 1:253d531a0193
1 if ( ! exists('MassbankConn')) { # Do not load again if already loaded 1 if ( ! exists('MassbankConn')) { # Do not load again if already loaded
2 2
3 source('BiodbConn.R') 3 source('RemotedbConn.R')
4 source('MassdbConn.R')
4 source('MassbankSpectrum.R') 5 source('MassbankSpectrum.R')
5 6
6 ##################### 7 #####################
7 # CLASS DECLARATION # 8 # CLASS DECLARATION #
8 ##################### 9 #####################
9 10
10 MassbankConn <- setRefClass("MassbankConn", contains = "BiodbConn") 11 MassbankConn <- setRefClass("MassbankConn", contains = c("RemotedbConn", "MassdbConn"))
11 12
12 ########################## 13 ##########################
13 # GET ENTRY CONTENT TYPE # 14 # GET ENTRY CONTENT TYPE #
14 ########################## 15 ##########################
15 16
16 MassbankConn$methods( getEntryContentType = function(type) { 17 MassbankConn$methods( getEntryContentType = function(type) {
17 return(if (type == RBIODB.SPECTRUM) RBIODB.TXT else NULL) 18 return(if (type == BIODB.SPECTRUM) BIODB.TXT else NULL)
18 }) 19 })
19 20
20 ##################### 21 #####################
21 # GET ENTRY CONTENT # 22 # GET ENTRY CONTENT #
22 ##################### 23 #####################
23 24
24 MassbankConn$methods( getEntryContent = function(type, id) { 25 MassbankConn$methods( getEntryContent = function(type, ids) {
25 26
26 if (type == RBIODB.SPECTRUM) { 27 # Debug
28 .self$.print.debug.msg(paste0("Get entry content(s) for ", length(ids)," id(s)..."))
29
30 if (type == BIODB.SPECTRUM) {
31
32 URL.MAX.LENGTH <- 2083
27 33
28 # Initialize return values 34 # Initialize return values
29 content <- rep(NA_character_, length(id)) 35 content <- rep(NA_character_, length(ids))
30 36
31 # Request 37 # Loop on all
32 xmlstr <- .self$.scheduler$getUrl(get.entry.url(RBIODB.MASSBANK, id, RBIODB.TXT)) 38 n <- 0
39 while (n < length(ids)) {
33 40
34 # Parse XML and get text 41 # Get list of accession ids to retrieve
35 if ( ! is.na(xmlstr)) { 42 accessions <- ids[(n + 1):length(ids)]
36 library(XML) 43
37 xml <- xmlInternalTreeParse(xmlstr, asText = TRUE) 44 # Create URL request
38 ns <- c(ax21 = "http://api.massbank/xsd") 45 x <- get.entry.url(class = BIODB.MASSBANK, accession = accessions, content.type = BIODB.TXT, max.length = URL.MAX.LENGTH)
39 returned.ids <- xpathSApply(xml, "//ax21:id", xmlValue, namespaces = ns) 46
40 content[match(returned.ids, id)] <- xpathSApply(xml, "//ax21:info", xmlValue, namespaces = ns) 47 # Debug
48 .self$.print.debug.msg(paste0("Send URL request for ", x$n," id(s)..."))
49
50 # Send request
51 xmlstr <- .self$.scheduler$getUrl(x$url)
52
53 # Increase number of entries retrieved
54 n <- n + x$n
55
56 # Parse XML and get text
57 if ( ! is.na(xmlstr)) {
58 library(XML)
59 xml <- xmlInternalTreeParse(xmlstr, asText = TRUE)
60 ns <- c(ax21 = "http://api.massbank/xsd")
61 returned.ids <- xpathSApply(xml, "//ax21:id", xmlValue, namespaces = ns)
62 content[match(returned.ids, ids)] <- xpathSApply(xml, "//ax21:info", xmlValue, namespaces = ns)
63 }
64
65 # Debug
66 .self$.print.debug.msg(paste0("Now ", length(ids) - n," id(s) left to be retrieved..."))
41 } 67 }
42 68
43 return(content) 69 return(content)
44 } 70 }
45 71
52 78
53 # Creates a Spectrum instance from file content. 79 # Creates a Spectrum instance from file content.
54 # content A file content, downloaded from the public database. 80 # content A file content, downloaded from the public database.
55 # RETURN A spectrum instance. 81 # RETURN A spectrum instance.
56 MassbankConn$methods( createEntry = function(type, content, drop = TRUE) { 82 MassbankConn$methods( createEntry = function(type, content, drop = TRUE) {
57 return(if (type == RBIODB.SPECTRUM) createMassbankSpectrumFromTxt(content, drop = drop) else NULL) 83 return(if (type == BIODB.SPECTRUM) createMassbankSpectrumFromTxt(content, drop = drop) else NULL)
84 })
85
86 #################
87 # GET MZ VALUES #
88 #################
89
90 MassbankConn$methods( getMzValues = function(mode = NULL, max.results = NA_integer_) {
58 }) 91 })
59 } 92 }