Mercurial > repos > prog > lcmsmatching
comparison MirbaseEntry.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 MirbaseEntry <- methods::setRefClass("MirbaseEntry", contains = "BiodbEntry") | |
| 6 | |
| 7 ########### | |
| 8 # FACTORY # | |
| 9 ########### | |
| 10 | |
| 11 createMirbaseEntryFromHtml <- function(contents, drop = TRUE) { | |
| 12 | |
| 13 entries <- list() | |
| 14 | |
| 15 # Define fields regex | |
| 16 xpath.expr <- character() | |
| 17 xpath.expr[[BIODB.ACCESSION]] <- "//td[text()='Accession number']/../td[2]" | |
| 18 xpath.expr[[BIODB.NAME]] <- "//td[text()='ID']/../td[2]" | |
| 19 xpath.expr[[BIODB.SEQUENCE]] <- "//td[text()='Sequence']/..//pre" | |
| 20 | |
| 21 for (html in contents) { | |
| 22 | |
| 23 # Create instance | |
| 24 entry <- MirbaseEntry$new() | |
| 25 | |
| 26 # Parse HTML | |
| 27 xml <- XML::htmlTreeParse(html, asText = TRUE, useInternalNodes = TRUE) | |
| 28 | |
| 29 # Test generic xpath expressions | |
| 30 for (field in names(xpath.expr)) { | |
| 31 v <- XML::xpathSApply(xml, xpath.expr[[field]], XML::xmlValue) | |
| 32 if (length(v) > 0) | |
| 33 entry$setField(field, v) | |
| 34 } | |
| 35 | |
| 36 entries <- c(entries, entry) | |
| 37 } | |
| 38 | |
| 39 # Replace elements with no accession id by NULL | |
| 40 entries <- lapply(entries, function(x) if (is.na(x$getField(BIODB.ACCESSION))) NULL else x) | |
| 41 | |
| 42 # If the input was a single element, then output a single object | |
| 43 if (drop && length(contents) == 1) | |
| 44 entries <- entries[[1]] | |
| 45 | |
| 46 return(entries) | |
| 47 } |
