comparison 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
comparison
equal deleted inserted replaced
5:fb9c0409d85c 6:f86fec07f392
1 #####################
2 # CLASS DECLARATION #
3 #####################
4
5 MassbankConn <- methods::setRefClass("MassbankConn", contains = c("RemotedbConn", "MassdbConn"), fields = list( .url = "character" ))
6
7 ###############
8 # CONSTRUCTOR #
9 ###############
10
11 MassbankConn$methods( initialize = function(url = NA_character_, ...) {
12
13 # Set URL
14 .url <<- if (is.null(url) || is.na(url)) BIODB.MASSBANK.EU.WS.URL else url
15
16 callSuper(...)
17 })
18
19 ##########################
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)
50
51 # Debug
52 .self$.print.debug.msg(paste0("Send URL request for ", x$n," id(s)..."))
53
54 # Send request
55 xmlstr <- .self$.get.url(x$url)
56
57 # Increase number of entries retrieved
58 n <- n + x$n
59
60 # Parse XML and get text
61 if ( ! is.na(xmlstr)) {
62 xml <- xmlInternalTreeParse(xmlstr, asText = TRUE)
63 ns <- c(ax21 = "http://api.massbank/xsd")
64 returned.ids <- xpathSApply(xml, "//ax21:id", xmlValue, namespaces = ns)
65 if (length(returned.ids) > 0)
66 content[match(returned.ids, ids)] <- xpathSApply(xml, "//ax21:info", xmlValue, namespaces = ns)
67 }
68
69 # Debug
70 .self$.print.debug.msg(paste0("Now ", length(ids) - n," id(s) left to be retrieved..."))
71 }
72
73 return(content)
74 })
75
76 ################
77 # CREATE ENTRY #
78 ################
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 })