Mercurial > repos > prog > lcmsmatching
comparison UniProtCompound.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('UniprotCompound')) { # Do not load again if already loaded | |
2 | |
3 source('BiodbEntry.R') | |
4 | |
5 ##################### | |
6 # CLASS DECLARATION # | |
7 ##################### | |
8 | |
9 UniprotCompound <- setRefClass("UniprotCompound", contains = "BiodbEntry") | |
10 | |
11 ########### | |
12 # FACTORY # | |
13 ########### | |
14 | |
15 createUniprotCompoundFromXml <- function(contents, drop = FALSE) { | |
16 | |
17 library(XML) | |
18 | |
19 # Set XML namespace | |
20 ns <- c(uniprot = "http://uniprot.org/uniprot") | |
21 | |
22 compounds <- list() | |
23 | |
24 # Define xpath expressions | |
25 xpath.values <- character() | |
26 xpath.values[[RBIODB.NAME]] <- "/uniprot:uniprot/uniprot:compound/uniprot:name" | |
27 xpath.values[[RBIODB.GENE.SYMBOLS]] <- "//uniprot:gene/uniprot:name" | |
28 xpath.values[[RBIODB.FULLNAMES]] <- "//uniprot:protein//uniprot:fullName" | |
29 xpath.values[[RBIODB.SEQUENCE]] <- "//uniprot:entry/uniprot:sequence" | |
30 xpath.values[[RBIODB.ACCESSION]] <- "//uniprot:accession[1]" | |
31 xpath.attr <- list() | |
32 xpath.attr[[RBIODB.KEGG.ID]] <- list(path = "//uniprot:dbReference[@type='KEGG']", attr = 'id') | |
33 xpath.attr[[RBIODB.NCBI.GENE.ID]] <- list(path = "//uniprot:dbReference[@type='GeneID']", attr = 'id') | |
34 xpath.attr[[RBIODB.ENZYME.ID]] <- list(path = "//uniprot:dbReference[@type='EC']", attr = 'id') | |
35 xpath.attr[[RBIODB.MASS]] <- list(path = "//uniprot:entry/uniprot:sequence", attr = 'mass') | |
36 xpath.attr[[RBIODB.LENGTH]] <- list(path = "//uniprot:entry/uniprot:sequence", attr = 'length') | |
37 | |
38 for (content in contents) { | |
39 | |
40 # Create instance | |
41 compound <- HmdbCompound$new() | |
42 | |
43 # If the entity doesn't exist (i.e.: no <id>.xml page), then it returns an HTML page | |
44 if ( ! grepl("^<!DOCTYPE html ", content, perl = TRUE)) { | |
45 | |
46 # Parse XML | |
47 xml <- xmlInternalTreeParse(content, asText = TRUE) | |
48 | |
49 # Test value xpath | |
50 for (field in names(xpath.values)) { | |
51 v <- xpathSApply(xml, xpath.values[[field]], xmlValue, namespaces = ns) | |
52 if (length(v) > 0) | |
53 compound$setField(field, v) | |
54 } | |
55 | |
56 # Test attribute xpath | |
57 for (field in names(xpath.attr)) { | |
58 v <- xpathSApply(xml, xpath.attr[[field]]$path, xmlGetAttr, xpath.attr[[field]]$attr, namespaces = ns) | |
59 if (length(v) > 0) | |
60 compound$setField(field, v) | |
61 } | |
62 | |
63 # Remove new lines from sequence string | |
64 seq <- compound$getField(RBIODB.SEQUENCE) | |
65 if ( ! is.na(seq)) | |
66 compound$setField(RBIODB.SEQUENCE, gsub("\\n", "", seq)) | |
67 } | |
68 | |
69 compounds <- c(compounds, compound) | |
70 } | |
71 | |
72 # Replace elements with no accession id by NULL | |
73 compounds <- lapply(compounds, function(x) if (is.na(x$getField(RBIODB.ACCESSION))) NULL else x) | |
74 | |
75 # If the input was a single element, then output a single object | |
76 if (drop && length(contents) == 1) | |
77 compounds <- compounds[[1]] | |
78 | |
79 return(compounds) | |
80 } | |
81 } |