comparison HmdbCompound.R @ 0:e66bb061af06 draft

planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 3529b25417f8e1a5836474c9adec4b696d35099d-dirty
author prog
date Tue, 12 Jul 2016 12:02:37 -0400
parents
children 253d531a0193
comparison
equal deleted inserted replaced
-1:000000000000 0:e66bb061af06
1 if ( ! exists('HmdbCompound')) { # Do not load again if already loaded
2
3 source('BiodbEntry.R')
4
5 #####################
6 # CLASS DECLARATION #
7 #####################
8
9 HmdbCompound <- setRefClass("HmdbCompound", contains = "BiodbEntry")
10
11 ###########
12 # FACTORY #
13 ###########
14
15 createHmdbCompoundFromXml <- function(contents, drop = FALSE) {
16
17 library(XML)
18
19 compounds <- list()
20
21 # Define xpath expressions
22 xpath.expr <- character()
23 xpath.expr[[RBIODB.ACCESSION]] <- "/metabolite/accession"
24 xpath.expr[[RBIODB.KEGG.ID]] <- "//kegg_id"
25 xpath.expr[[RBIODB.NAME]] <- "/metabolite/name"
26 xpath.expr[[RBIODB.FORMULA]] <- "/metabolite/chemical_formula"
27 xpath.expr[[RBIODB.SUPER.CLASS]] <- "//super_class"
28 xpath.expr[[RBIODB.AVERAGE.MASS]] <- "//average_molecular_weight"
29 xpath.expr[[RBIODB.MONOISOTOPIC.MASS]] <- "//monisotopic_moleculate_weight"
30
31 for (content in contents) {
32
33 # Create instance
34 compound <- HmdbCompound$new()
35
36 # Parse XML
37 xml <- xmlInternalTreeParse(content, asText = TRUE)
38
39 # An error occured
40 if (length(getNodeSet(xml, "//error")) == 0) {
41
42 # Test generic xpath expressions
43 for (field in names(xpath.expr)) {
44 v <- xpathSApply(xml, xpath.expr[[field]], xmlValue)
45 if (length(v) > 0)
46 compound$setField(field, v)
47 }
48
49 }
50
51 compounds <- c(compounds, compound)
52 }
53
54 # Replace elements with no accession id by NULL
55 compounds <- lapply(compounds, function(x) if (is.na(x$getField(RBIODB.ACCESSION))) NULL else x)
56
57 # If the input was a single element, then output a single object
58 if (drop && length(contents) == 1)
59 compounds <- compounds[[1]]
60
61 return(compounds)
62 }
63 }