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