Mercurial > repos > prog > lcmsmatching
diff MassbankCompound.R @ 0:3afe41d3e9e7 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit bb4d3e23d99828bfee16d31d794c49a17313ec2f
| author | prog |
|---|---|
| date | Mon, 11 Jul 2016 09:12:03 -0400 |
| parents | |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MassbankCompound.R Mon Jul 11 09:12:03 2016 -0400 @@ -0,0 +1,66 @@ +if ( ! exists('MassbankCompound')) { # Do not load again if already loaded + + source('BiodbEntry.R') + + ##################### + # CLASS DECLARATION # + ##################### + + MassbankCompound <- setRefClass("MassbankCompound", contains = "BiodbEntry") + + ########### + # FACTORY # + ########### + + createMassbankCompoundFromTxt <- function(contents) { + + library(stringr) + + compounds <- list() + + for (text in contents) { + + # Create instance + compound <- MassbankCompound$new() + + # Read text + lines <- strsplit(text, "\n") + for (s in lines[[1]]) { + + # NAME + if (is.na(compound$getField(RBIODB.NAME))) { + g <- str_match(s, "^CH\\$NAME:\\s+(.+)$") + if ( ! is.na(g[1,1])) + compound$setField(RBIODB.NAME, g[1,2]) + } + + # CHEBI ID + g <- str_match(s, "^CH\\$LINK: CHEBI\\s+(.+)$") + if ( ! is.na(g[1,1])) + compound$setField(RBIODB.CHEBI.ID, g[1,2]) + + # KEGG ID + g <- str_match(s, "^CH\\$LINK: KEGG\\s+(.+)$") + if ( ! is.na(g[1,1])) + compound$setField(RBIODB.KEGG.ID, g[1,2]) + + # PUBCHEM ID + g <- str_match(s, "^CH\\$LINK: PUBCHEM\\s+(.+)$") + if ( ! is.na(g[1,1])) + compound$setField(RBIODB.PUBCHEM.ID, g[1,2]) + + # INCHI + g <- str_match(s, "^CH\\$IUPAC:\\s+(.+)$") + if ( ! is.na(g[1,1])) + compound$setField(RBIODB.INCHI, g[1,2]) + } + + compounds <- c(compounds, compound) + } + + # Replace elements with no accession id by NULL + compounds <- lapply(compounds, function(x) if (is.na(x$getField(RBIODB.NAME))) NULL else x) + + return(compounds) + } +}
