comparison PubchemEntry.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 PubchemEntry <- methods::setRefClass("PubchemEntry", contains = "BiodbEntry")
6 PubchemSubstance <- methods::setRefClass("PubchemSubstance", contains = "BiodbEntry")
7
8 #####################
9 # SUBSTANCE FACTORY #
10 #####################
11
12 createPubchemSubstanceFromXml <- function(contents, drop = TRUE) {
13
14 entries <- list()
15
16 # Define xpath expressions
17 xpath.expr <- character()
18 xpath.expr[[BIODB.ACCESSION]] <- "//PC-ID_id"
19 #xpath.expr[[BIODB.PUBCHEMCOMP.ID]] <- "//PC-CompoundType_id_cid" --> Apparently that can be more than one CID for a substance.
20
21 for (content in contents) {
22
23 # Create instance
24 entry <- PubchemEntry$new()
25
26 if ( ! is.null(content) && ! is.na(content)) {
27
28 # Parse XML
29 xml <- XML::xmlInternalTreeParse(content, asText = TRUE)
30
31 # Unknown entry
32 fault <- XML::xpathSApply(xml, "/Fault", XML::xmlValue)
33 if (length(fault) == 0) {
34
35 # Test generic xpath expressions
36 for (field in names(xpath.expr)) {
37 v <- XML::xpathSApply(xml, xpath.expr[[field]], XML::xmlValue)
38 if (length(v) > 0)
39 entry$setField(field, v)
40 }
41 }
42 }
43
44 entries <- c(entries, entry)
45 }
46
47 # Replace elements with no accession id by NULL
48 entries <- lapply(entries, function(x) if (is.na(x$getField(BIODB.ACCESSION))) NULL else x)
49
50 # If the input was a single element, then output a single object
51 if (drop && length(contents) == 1)
52 entries <- entries[[1]]
53
54 return(entries)
55 }
56
57 ####################
58 # COMPOUND FACTORY #
59 ####################
60
61 createPubchemEntryFromXml <- function(contents, drop = TRUE) {
62
63 entries <- list()
64
65 # Define xpath expressions
66 xpath.expr <- character()
67 xpath.expr[[BIODB.ACCESSION]] <- "//PC-CompoundType_id_cid"
68 xpath.expr[[BIODB.INCHI]] <- "//PC-Urn_label[text()='InChI']/../../..//PC-InfoData_value_sval"
69 xpath.expr[[BIODB.INCHIKEY]] <- "//PC-Urn_label[text()='InChIKey']/../../..//PC-InfoData_value_sval"
70 xpath.expr[[BIODB.FORMULA]] <- "//PC-Urn_label[text()='Molecular Formula']/../../..//PC-InfoData_value_sval"
71 xpath.expr[[BIODB.MASS]] <- "//PC-Urn_label[text()='Mass']/../../..//PC-InfoData_value_fval"
72 xpath.expr[[BIODB.COMP.IUPAC.NAME.SYST]] <- "//PC-Urn_label[text()='IUPAC Name']/../PC-Urn_name[text()='Systematic']/../../..//PC-InfoData_value_sval"
73
74 for (content in contents) {
75
76 # Create instance
77 entry <- PubchemEntry$new()
78
79 if ( ! is.null(content) && ! is.na(content)) {
80
81 # Parse XML
82 xml <- XML::xmlInternalTreeParse(content, asText = TRUE)
83
84 # Unknown entry
85 fault <- XML::xpathSApply(xml, "/Fault", XML::xmlValue)
86 if (length(fault) == 0) {
87
88 # Test generic xpath expressions
89 for (field in names(xpath.expr)) {
90 v <- XML::xpathSApply(xml, xpath.expr[[field]], XML::xmlValue)
91 if (length(v) > 0)
92 entry$setField(field, v)
93 }
94 }
95 }
96
97 entries <- c(entries, entry)
98 }
99
100 # Replace elements with no accession id by NULL
101 entries <- lapply(entries, function(x) if (is.na(x$getField(BIODB.ACCESSION))) NULL else x)
102
103 # If the input was a single element, then output a single object
104 if (drop && length(contents) == 1)
105 entries <- entries[[1]]
106
107 return(entries)
108 }