Mercurial > repos > prog > lcmsmatching
comparison MassbankCompound.R @ 0:e66bb061af06 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 3529b25417f8e1a5836474c9adec4b696d35099d-dirty
| author | prog |
|---|---|
| date | Tue, 12 Jul 2016 12:02:37 -0400 |
| parents | |
| children | 253d531a0193 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:e66bb061af06 |
|---|---|
| 1 if ( ! exists('MassbankCompound')) { # Do not load again if already loaded | |
| 2 | |
| 3 source('BiodbEntry.R') | |
| 4 | |
| 5 ##################### | |
| 6 # CLASS DECLARATION # | |
| 7 ##################### | |
| 8 | |
| 9 MassbankCompound <- setRefClass("MassbankCompound", contains = "BiodbEntry") | |
| 10 | |
| 11 ########### | |
| 12 # FACTORY # | |
| 13 ########### | |
| 14 | |
| 15 createMassbankCompoundFromTxt <- function(contents) { | |
| 16 | |
| 17 library(stringr) | |
| 18 | |
| 19 compounds <- list() | |
| 20 | |
| 21 for (text in contents) { | |
| 22 | |
| 23 # Create instance | |
| 24 compound <- MassbankCompound$new() | |
| 25 | |
| 26 # Read text | |
| 27 lines <- strsplit(text, "\n") | |
| 28 for (s in lines[[1]]) { | |
| 29 | |
| 30 # NAME | |
| 31 if (is.na(compound$getField(RBIODB.NAME))) { | |
| 32 g <- str_match(s, "^CH\\$NAME:\\s+(.+)$") | |
| 33 if ( ! is.na(g[1,1])) | |
| 34 compound$setField(RBIODB.NAME, g[1,2]) | |
| 35 } | |
| 36 | |
| 37 # CHEBI ID | |
| 38 g <- str_match(s, "^CH\\$LINK: CHEBI\\s+(.+)$") | |
| 39 if ( ! is.na(g[1,1])) | |
| 40 compound$setField(RBIODB.CHEBI.ID, g[1,2]) | |
| 41 | |
| 42 # KEGG ID | |
| 43 g <- str_match(s, "^CH\\$LINK: KEGG\\s+(.+)$") | |
| 44 if ( ! is.na(g[1,1])) | |
| 45 compound$setField(RBIODB.KEGG.ID, g[1,2]) | |
| 46 | |
| 47 # PUBCHEM ID | |
| 48 g <- str_match(s, "^CH\\$LINK: PUBCHEM\\s+(.+)$") | |
| 49 if ( ! is.na(g[1,1])) | |
| 50 compound$setField(RBIODB.PUBCHEM.ID, g[1,2]) | |
| 51 | |
| 52 # INCHI | |
| 53 g <- str_match(s, "^CH\\$IUPAC:\\s+(.+)$") | |
| 54 if ( ! is.na(g[1,1])) | |
| 55 compound$setField(RBIODB.INCHI, g[1,2]) | |
| 56 } | |
| 57 | |
| 58 compounds <- c(compounds, compound) | |
| 59 } | |
| 60 | |
| 61 # Replace elements with no accession id by NULL | |
| 62 compounds <- lapply(compounds, function(x) if (is.na(x$getField(RBIODB.NAME))) NULL else x) | |
| 63 | |
| 64 return(compounds) | |
| 65 } | |
| 66 } |
