Mercurial > repos > prog > lcmsmatching
view PubchemEntry.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 |
line wrap: on
line source
##################### # CLASS DECLARATION # ##################### PubchemEntry <- methods::setRefClass("PubchemEntry", contains = "BiodbEntry") PubchemSubstance <- methods::setRefClass("PubchemSubstance", contains = "BiodbEntry") ##################### # SUBSTANCE FACTORY # ##################### createPubchemSubstanceFromXml <- function(contents, drop = TRUE) { entries <- list() # Define xpath expressions xpath.expr <- character() xpath.expr[[BIODB.ACCESSION]] <- "//PC-ID_id" #xpath.expr[[BIODB.PUBCHEMCOMP.ID]] <- "//PC-CompoundType_id_cid" --> Apparently that can be more than one CID for a substance. for (content in contents) { # Create instance entry <- PubchemEntry$new() if ( ! is.null(content) && ! is.na(content)) { # Parse XML xml <- XML::xmlInternalTreeParse(content, asText = TRUE) # Unknown entry fault <- XML::xpathSApply(xml, "/Fault", XML::xmlValue) if (length(fault) == 0) { # Test generic xpath expressions for (field in names(xpath.expr)) { v <- XML::xpathSApply(xml, xpath.expr[[field]], XML::xmlValue) if (length(v) > 0) entry$setField(field, v) } } } entries <- c(entries, entry) } # Replace elements with no accession id by NULL entries <- lapply(entries, 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) entries <- entries[[1]] return(entries) } #################### # COMPOUND FACTORY # #################### createPubchemEntryFromXml <- function(contents, drop = TRUE) { entries <- list() # Define xpath expressions xpath.expr <- character() xpath.expr[[BIODB.ACCESSION]] <- "//PC-CompoundType_id_cid" xpath.expr[[BIODB.INCHI]] <- "//PC-Urn_label[text()='InChI']/../../..//PC-InfoData_value_sval" xpath.expr[[BIODB.INCHIKEY]] <- "//PC-Urn_label[text()='InChIKey']/../../..//PC-InfoData_value_sval" xpath.expr[[BIODB.FORMULA]] <- "//PC-Urn_label[text()='Molecular Formula']/../../..//PC-InfoData_value_sval" xpath.expr[[BIODB.MASS]] <- "//PC-Urn_label[text()='Mass']/../../..//PC-InfoData_value_fval" xpath.expr[[BIODB.COMP.IUPAC.NAME.SYST]] <- "//PC-Urn_label[text()='IUPAC Name']/../PC-Urn_name[text()='Systematic']/../../..//PC-InfoData_value_sval" for (content in contents) { # Create instance entry <- PubchemEntry$new() if ( ! is.null(content) && ! is.na(content)) { # Parse XML xml <- XML::xmlInternalTreeParse(content, asText = TRUE) # Unknown entry fault <- XML::xpathSApply(xml, "/Fault", XML::xmlValue) if (length(fault) == 0) { # Test generic xpath expressions for (field in names(xpath.expr)) { v <- XML::xpathSApply(xml, xpath.expr[[field]], XML::xmlValue) if (length(v) > 0) entry$setField(field, v) } } } entries <- c(entries, entry) } # Replace elements with no accession id by NULL entries <- lapply(entries, 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) entries <- entries[[1]] return(entries) }