comparison ChemspiderConn.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
children
comparison
equal deleted inserted replaced
1:253d531a0193 2:20d69a062da3
1 #####################
2 # CLASS DECLARATION #
3 #####################
4
5 ChemspiderConn <- methods::setRefClass("ChemspiderConn", contains = "RemotedbConn")
6
7 ##########################
8 # GET ENTRY CONTENT TYPE #
9 ##########################
10
11 ChemspiderConn$methods( getEntryContentType = function() {
12 return(BIODB.XML)
13 })
14
15 #####################
16 # GET ENTRY CONTENT #
17 #####################
18
19 ChemspiderConn$methods( getEntryContent = function(ids) {
20
21 # Debug
22 .self$.print.debug.msg(paste0("Get entry content(s) for ", length(ids)," id(s)..."))
23
24 URL.MAX.LENGTH <- 2083
25
26 # Initialize return values
27 content <- rep(NA_character_, length(ids))
28
29 # Loop on all
30 n <- 0
31 inc <- NA_integer_
32 while (n < length(ids)) {
33
34 # Get list of accession ids to retrieve
35 accessions <- ids[(n + 1):(if (is.na(inc)) length(ids) else (min(n + inc, length(ids))))]
36
37 # Create URL request
38 x <- get.entry.url(class = BIODB.CHEMSPIDER, accession = accessions, content.type = BIODB.XML, max.length = URL.MAX.LENGTH, base.url = .self$.url, token = .self$.token)
39
40 # Debug
41 .self$.print.debug.msg(paste0("Send URL request for ", x$n," id(s)..."))
42
43 # Send request
44 xmlstr <- .self$.get.url(x$url)
45
46 # Error : "Cannot convert WRONG to System.Int32.\r\nParameter name: type ---> Input string was not in a correct format.\r\n"
47 if (grepl('^Cannot convert .* to System\\.Int32\\.', xmlstr)) {
48 # One of the ids is incorrect
49 if (is.na(inc)) {
50 inc <- 1
51 next
52 }
53 else
54 xmlstr <- NA_character_
55 }
56
57 # Increase number of entries retrieved
58 n <- n + x$n
59
60 # Parse XML and get included XML
61 if ( ! is.na(xmlstr)) {
62 xml <- xmlInternalTreeParse(xmlstr, asText = TRUE)
63 ns <- c(csns = "http://www.chemspider.com/")
64 returned.ids <- xpathSApply(xml, "//csns:ExtendedCompoundInfo/csns:CSID", xmlValue, namespaces = ns)
65 content[match(returned.ids, ids)] <- vapply(getNodeSet(xml, "//csns:ExtendedCompoundInfo", namespaces = ns), saveXML, FUN.VALUE = '')
66 }
67
68 # Debug
69 .self$.print.debug.msg(paste0("Now ", length(ids) - n," id(s) left to be retrieved..."))
70 }
71
72 return(content)
73 })
74
75 ################
76 # CREATE ENTRY #
77 ################
78
79 ChemspiderConn$methods( createEntry = function(content, drop = TRUE) {
80 return(createChemspiderEntryFromXml(content, drop = drop))
81 })
82
83 ############################
84 # GET CHEMSPIDER IMAGE URL #
85 ############################
86
87 get.chemspider.image.url <- function(id) {
88
89 url <- paste0('http://www.chemspider.com/ImagesHandler.ashx?w=300&h=300&id=', id)
90
91 return(url)
92 }