Mercurial > repos > prog > lcmsmatching
comparison LipidmapsCompound.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('LipidmapsCompound')) { # Do not load again if already loaded | |
| 2 | |
| 3 source('BiodbEntry.R') | |
| 4 source('strhlp.R', chdir = TRUE) | |
| 5 | |
| 6 ##################### | |
| 7 # CLASS DECLARATION # | |
| 8 ##################### | |
| 9 | |
| 10 LipidmapsCompound <- setRefClass("LipidmapsCompound", contains = 'BiodbEntry') | |
| 11 | |
| 12 ########### | |
| 13 # FACTORY # | |
| 14 ########### | |
| 15 | |
| 16 createLipidmapsCompoundFromCsv <- function(contents, drop = TRUE) { | |
| 17 | |
| 18 compounds <- list() | |
| 19 | |
| 20 # Mapping column names | |
| 21 col2field <- list() | |
| 22 col2field[[RBIODB.NAME]] <- 'COMMON_NAME' | |
| 23 col2field[[RBIODB.ACCESSION]] <- 'LM_ID' | |
| 24 col2field[[RBIODB.KEGG.ID]] <- 'KEGG_ID' | |
| 25 col2field[[RBIODB.HMDB.ID]] <- 'HMDBID' | |
| 26 col2field[[RBIODB.MASS]] <- 'MASS' | |
| 27 col2field[[RBIODB.FORMULA]] <- 'FORMULA' | |
| 28 | |
| 29 for (text in contents) { | |
| 30 | |
| 31 # Create instance | |
| 32 compound <- LipidmapsCompound$new() | |
| 33 | |
| 34 # Split text in lines | |
| 35 lines <- split.str(text, sep = "\n", unlist = TRUE) | |
| 36 | |
| 37 # An error occured | |
| 38 if ( ! grepl("No record found", lines[[2]])) { | |
| 39 | |
| 40 # Keys on first line | |
| 41 keys <- split.str(lines[[1]], unlist = TRUE) | |
| 42 | |
| 43 # Values on second line | |
| 44 values <- split.str(lines[[2]], unlist = TRUE) | |
| 45 names(values) <- keys[seq(values)] | |
| 46 | |
| 47 # Get field values | |
| 48 for (field in names(col2field)) | |
| 49 if (values[[col2field[[field]]]] != '-') | |
| 50 compound$setField(field, values[[col2field[[field]]]]) | |
| 51 | |
| 52 # Set names | |
| 53 if (values[['SYNONYMS']] != '-') { | |
| 54 # TODO | |
| 55 } | |
| 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.ACCESSION))) NULL else x) | |
| 63 | |
| 64 # If the input was a single element, then output a single object | |
| 65 if (drop && length(contents) == 1) | |
| 66 compounds <- compounds[[1]] | |
| 67 | |
| 68 return(compounds) | |
| 69 } | |
| 70 } |
