Mercurial > repos > prog > lcmsmatching
view ChemspiderCompound.R @ 1:253d531a0193 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 36c9d8099c20a1ae848f1337c16564335dd8fb2b
author | prog |
---|---|
date | Sat, 03 Sep 2016 17:02:01 -0400 |
parents | e66bb061af06 |
children |
line wrap: on
line source
if ( ! exists('ChemspiderCompound')) { # Do not load again if already loaded source('BiodbEntry.R') ##################### # CLASS DECLARATION # ##################### ChemspiderCompound <- setRefClass("ChemspiderCompound", contains = "BiodbEntry") ########### # FACTORY # ########### createChemspiderCompoundFromHtml <- function(contents, drop = TRUE) { library(XML) compounds <- list() # Define xpath expressions xpath.expr <- character() for (html in contents) { # Create instance compound <- ChemspiderCompound$new() # Parse HTML xml <- htmlTreeParse(html, asText = TRUE, useInternalNodes = TRUE) # Test generic xpath expressions for (field in names(xpath.expr)) { v <- xpathSApply(xml, xpath.expr[[field]], xmlValue) if (length(v) > 0) compound$setField(field, v) } # Get accession accession <- xpathSApply(xml, "//li[starts-with(., 'ChemSpider ID')]", xmlValue) if (length(accession) > 0) { accession <- sub('^ChemSpider ID([0-9]+)$', '\\1', accession, perl = TRUE) compound$setField(BIODB.ACCESSION, accession) } compounds <- c(compounds, compound) } # Replace elements with no accession id by NULL compounds <- lapply(compounds, function(x) if (is.na(x$getField(BIODB.ACCESSION))) NULL else x) # If the input was a single element, then output a single object if (drop && length(contents) == 1) compounds <- compounds[[1]] return(compounds) } }