Mercurial > repos > prog > lcmsmatching
comparison HmdbEntry.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 HmdbEntry <- methods::setRefClass("HmdbEntry", contains = "BiodbEntry") | |
6 | |
7 ########### | |
8 # FACTORY # | |
9 ########### | |
10 | |
11 createHmdbEntryFromXml <- function(contents, drop = FALSE) { | |
12 | |
13 entries <- list() | |
14 | |
15 # Define xpath expressions | |
16 xpath.expr <- character() | |
17 xpath.expr[[BIODB.ACCESSION]] <- "/metabolite/accession" | |
18 xpath.expr[[BIODB.KEGG.ID]] <- "//kegg_id" | |
19 xpath.expr[[BIODB.NAME]] <- "/metabolite/name" | |
20 xpath.expr[[BIODB.FORMULA]] <- "/metabolite/chemical_formula" | |
21 xpath.expr[[BIODB.SUPER.CLASS]] <- "//super_class" | |
22 xpath.expr[[BIODB.AVERAGE.MASS]] <- "//average_molecular_weight" | |
23 xpath.expr[[BIODB.MONOISOTOPIC.MASS]] <- "//monisotopic_moleculate_weight" | |
24 | |
25 for (content in contents) { | |
26 | |
27 # Create instance | |
28 entry <- HmdbEntry$new() | |
29 | |
30 if ( ! is.null(content) && ! is.na(content)) { | |
31 | |
32 # Parse XML | |
33 xml <- XML::xmlInternalTreeParse(content, asText = TRUE) | |
34 | |
35 # An error occured | |
36 if (length(XML::getNodeSet(xml, "//error")) == 0) { | |
37 | |
38 # Test generic xpath expressions | |
39 for (field in names(xpath.expr)) { | |
40 v <- XML::xpathSApply(xml, xpath.expr[[field]], XML::xmlValue) | |
41 if (length(v) > 0) | |
42 entry$setField(field, v) | |
43 } | |
44 | |
45 } | |
46 } | |
47 | |
48 entries <- c(entries, entry) | |
49 } | |
50 | |
51 # Replace elements with no accession id by NULL | |
52 entries <- lapply(entries, function(x) if (is.na(x$getField(BIODB.ACCESSION))) NULL else x) | |
53 | |
54 # If the input was a single element, then output a single object | |
55 if (drop && length(contents) == 1) | |
56 entries <- entries[[1]] | |
57 | |
58 return(entries) | |
59 } |