comparison LipidmapsCompound.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
comparison
equal deleted inserted replaced
-1:000000000000 0:3afe41d3e9e7
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 }