comparison 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
comparison
equal deleted inserted replaced
1:253d531a0193 2:20d69a062da3
1 if ( ! exists('MassbankConn')) { # Do not load again if already loaded 1 #####################
2 # CLASS DECLARATION #
3 #####################
2 4
3 source('RemotedbConn.R') 5 MassbankConn <- methods::setRefClass("MassbankConn", contains = c("RemotedbConn", "MassdbConn"), fields = list( .url = "character" ))
4 source('MassdbConn.R')
5 source('MassbankSpectrum.R')
6 6
7 ##################### 7 ###############
8 # CLASS DECLARATION # 8 # CONSTRUCTOR #
9 ##################### 9 ###############
10
11 MassbankConn <- setRefClass("MassbankConn", contains = c("RemotedbConn", "MassdbConn"))
12 10
13 ########################## 11 MassbankConn$methods( initialize = function(url = NA_character_, ...) {
14 # GET ENTRY CONTENT TYPE #
15 ##########################
16 12
17 MassbankConn$methods( getEntryContentType = function(type) { 13 # Set URL
18 return(if (type == BIODB.SPECTRUM) BIODB.TXT else NULL) 14 .url <<- if (is.null(url) || is.na(url)) BIODB.MASSBANK.EU.WS.URL else url
19 })
20 15
21 ##################### 16 callSuper(...)
22 # GET ENTRY CONTENT # 17 })
23 ##################### 18
24 19 ##########################
25 MassbankConn$methods( getEntryContent = function(type, ids) { 20 # GET ENTRY CONTENT TYPE #
21 ##########################
22
23 MassbankConn$methods( getEntryContentType = function() {
24 return(BIODB.TXT)
25 })
26
27 #####################
28 # GET ENTRY CONTENT #
29 #####################
30
31 MassbankConn$methods( getEntryContent = function(ids) {
32
33 # Debug
34 .self$.print.debug.msg(paste0("Get entry content(s) for ", length(ids)," id(s)..."))
35
36 URL.MAX.LENGTH <- 2083
37
38 # Initialize return values
39 content <- rep(NA_character_, length(ids))
40
41 # Loop on all
42 n <- 0
43 while (n < length(ids)) {
44
45 # Get list of accession ids to retrieve
46 accessions <- ids[(n + 1):length(ids)]
47
48 # Create URL request
49 x <- get.entry.url(class = BIODB.MASSBANK, accession = accessions, content.type = BIODB.TXT, max.length = URL.MAX.LENGTH, base.url = .self$.url)
26 50
27 # Debug 51 # Debug
28 .self$.print.debug.msg(paste0("Get entry content(s) for ", length(ids)," id(s)...")) 52 .self$.print.debug.msg(paste0("Send URL request for ", x$n," id(s)..."))
29 53
30 if (type == BIODB.SPECTRUM) { 54 # Send request
55 xmlstr <- .self$.get.url(x$url)
31 56
32 URL.MAX.LENGTH <- 2083 57 # Increase number of entries retrieved
58 n <- n + x$n
33 59
34 # Initialize return values 60 # Parse XML and get text
35 content <- rep(NA_character_, length(ids)) 61 if ( ! is.na(xmlstr)) {
36 62 xml <- xmlInternalTreeParse(xmlstr, asText = TRUE)
37 # Loop on all 63 ns <- c(ax21 = "http://api.massbank/xsd")
38 n <- 0 64 returned.ids <- xpathSApply(xml, "//ax21:id", xmlValue, namespaces = ns)
39 while (n < length(ids)) { 65 if (length(returned.ids) > 0)
40 66 content[match(returned.ids, ids)] <- xpathSApply(xml, "//ax21:info", xmlValue, namespaces = ns)
41 # Get list of accession ids to retrieve
42 accessions <- ids[(n + 1):length(ids)]
43
44 # Create URL request
45 x <- get.entry.url(class = BIODB.MASSBANK, accession = accessions, content.type = BIODB.TXT, max.length = URL.MAX.LENGTH)
46
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..."))
67 }
68
69 return(content)
70 } 67 }
71 68
72 return(NULL) 69 # Debug
73 }) 70 .self$.print.debug.msg(paste0("Now ", length(ids) - n," id(s) left to be retrieved..."))
74 71 }
75 ################
76 # CREATE ENTRY #
77 ################
78
79 # Creates a Spectrum instance from file content.
80 # content A file content, downloaded from the public database.
81 # RETURN A spectrum instance.
82 MassbankConn$methods( createEntry = function(type, content, drop = TRUE) {
83 return(if (type == BIODB.SPECTRUM) createMassbankSpectrumFromTxt(content, drop = drop) else NULL)
84 })
85 72
86 ################# 73 return(content)
87 # GET MZ VALUES # 74 })
88 ################# 75
89 76 ################
90 MassbankConn$methods( getMzValues = function(mode = NULL, max.results = NA_integer_) { 77 # CREATE ENTRY #
91 }) 78 ################
92 } 79
80 # Creates a Spectrum instance from file content.
81 # content A file content, downloaded from the public database.
82 # RETURN A spectrum instance.
83 MassbankConn$methods( createEntry = function(content, drop = TRUE) {
84 return(createMassbankEntryFromTxt(content, drop = drop))
85 })
86
87 #################
88 # GET MZ VALUES #
89 #################
90
91 MassbankConn$methods( getMzValues = function(mode = NULL, max.results = NA_integer_) {
92 })
93
94 #################
95 # GET ENTRY IDS #
96 #################
97
98 MassbankConn$methods( getEntryIds = function(max.results = NA_integer_) {
99
100 # Set URL
101 url <- paste0(.self$.url, 'searchPeak?mzs=1000&relativeIntensity=100&tolerance=1000&instrumentTypes=all&ionMode=Both')
102 url <- paste0(url, '&maxNumResults=', (if (is.na(max.results)) 0 else max.results))
103
104 # Send request
105 xmlstr <- .self$.get.url(url)
106
107 # Parse XML and get text
108 if ( ! is.na(xmlstr)) {
109 xml <- xmlInternalTreeParse(xmlstr, asText = TRUE)
110 ns <- c(ax21 = "http://api.massbank/xsd")
111 returned.ids <- xpathSApply(xml, "//ax21:id", xmlValue, namespaces = ns)
112 return(returned.ids)
113 }
114 })
115
116 ##################
117 # GET NB ENTRIES #
118 ##################
119
120 MassbankConn$methods( getNbEntries = function() {
121 return(length(.self$getEntryIds()))
122 })