Mercurial > repos > prog > lcmsmatching
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 0:3afe41d3e9e7 | 1:45e985cd8e9e |
|---|---|
| 1 if ( ! exists('MassbankConn')) { # Do not load again if already loaded | 1 ##################### |
| 2 # CLASS DECLARATION # | |
| 3 ##################### | |
| 2 | 4 |
| 3 source('BiodbConn.R') | 5 MassbankConn <- methods::setRefClass("MassbankConn", contains = c("RemotedbConn", "MassdbConn"), fields = list( .url = "character" )) |
| 4 source('MassbankSpectrum.R') | |
| 5 | 6 |
| 6 ##################### | 7 ############### |
| 7 # CLASS DECLARATION # | 8 # CONSTRUCTOR # |
| 8 ##################### | 9 ############### |
| 9 | |
| 10 MassbankConn <- setRefClass("MassbankConn", contains = "BiodbConn") | |
| 11 | 10 |
| 12 ########################## | 11 MassbankConn$methods( initialize = function(url = NA_character_, ...) { |
| 13 # GET ENTRY CONTENT TYPE # | |
| 14 ########################## | |
| 15 | 12 |
| 16 MassbankConn$methods( getEntryContentType = function(type) { | 13 # Set URL |
| 17 return(if (type == RBIODB.SPECTRUM) RBIODB.TXT else NULL) | 14 .url <<- if (is.null(url) || is.na(url)) BIODB.MASSBANK.EU.WS.URL else url |
| 18 }) | |
| 19 | 15 |
| 20 ##################### | 16 callSuper(...) |
| 21 # GET ENTRY CONTENT # | 17 }) |
| 22 ##################### | |
| 23 | |
| 24 MassbankConn$methods( getEntryContent = function(type, id) { | |
| 25 | 18 |
| 26 if (type == RBIODB.SPECTRUM) { | 19 ########################## |
| 20 # GET ENTRY CONTENT TYPE # | |
| 21 ########################## | |
| 27 | 22 |
| 28 # Initialize return values | 23 MassbankConn$methods( getEntryContentType = function() { |
| 29 content <- rep(NA_character_, length(id)) | 24 return(BIODB.TXT) |
| 25 }) | |
| 30 | 26 |
| 31 # Request | 27 ##################### |
| 32 xmlstr <- .self$.scheduler$getUrl(get.entry.url(RBIODB.MASSBANK, id, RBIODB.TXT)) | 28 # GET ENTRY CONTENT # |
| 29 ##################### | |
| 33 | 30 |
| 34 # Parse XML and get text | 31 MassbankConn$methods( getEntryContent = function(ids) { |
| 35 if ( ! is.na(xmlstr)) { | |
| 36 library(XML) | |
| 37 xml <- xmlInternalTreeParse(xmlstr, asText = TRUE) | |
| 38 ns <- c(ax21 = "http://api.massbank/xsd") | |
| 39 returned.ids <- xpathSApply(xml, "//ax21:id", xmlValue, namespaces = ns) | |
| 40 content[match(returned.ids, id)] <- xpathSApply(xml, "//ax21:info", xmlValue, namespaces = ns) | |
| 41 } | |
| 42 | 32 |
| 43 return(content) | 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) | |
| 44 } | 67 } |
| 45 | 68 |
| 46 return(NULL) | 69 # Debug |
| 47 }) | 70 .self$.print.debug.msg(paste0("Now ", length(ids) - n," id(s) left to be retrieved...")) |
| 48 | 71 } |
| 49 ################ | 72 |
| 50 # CREATE ENTRY # | 73 return(content) |
| 51 ################ | 74 }) |
| 52 | 75 |
| 53 # Creates a Spectrum instance from file content. | 76 ################ |
| 54 # content A file content, downloaded from the public database. | 77 # CREATE ENTRY # |
| 55 # RETURN A spectrum instance. | 78 ################ |
| 56 MassbankConn$methods( createEntry = function(type, content, drop = TRUE) { | 79 |
| 57 return(if (type == RBIODB.SPECTRUM) createMassbankSpectrumFromTxt(content, drop = drop) else NULL) | 80 # Creates a Spectrum instance from file content. |
| 58 }) | 81 # content A file content, downloaded from the public database. |
| 59 } | 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 }) |
