comparison LipidmapsEntry.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
comparison
equal deleted inserted replaced
1:253d531a0193 2:20d69a062da3
1 #####################
2 # CLASS DECLARATION #
3 #####################
4
5 LipidmapsEntry <- methods::setRefClass("LipidmapsEntry", contains = 'BiodbEntry')
6
7 ###########
8 # FACTORY #
9 ###########
10
11 createLipidmapsEntryFromCsv <- function(contents, drop = TRUE) {
12
13 entries <- list()
14
15 # Mapping column names
16 col2field <- list()
17 col2field[[BIODB.NAME]] <- 'COMMON_NAME'
18 col2field[[BIODB.ACCESSION]] <- 'LM_ID'
19 col2field[[BIODB.KEGG.ID]] <- 'KEGG_ID'
20 col2field[[BIODB.HMDB.ID]] <- 'HMDBID'
21 col2field[[BIODB.MASS]] <- 'MASS'
22 col2field[[BIODB.FORMULA]] <- 'FORMULA'
23
24 for (text in contents) {
25
26 # Create instance
27 entry <- LipidmapsEntry$new()
28
29 # Split text in lines
30 lines <- split.str(text, sep = "\n", unlist = TRUE)
31
32 # An error occured
33 if ( ! grepl("No record found", lines[[2]])) {
34
35 # Keys on first line
36 keys <- split.str(lines[[1]], unlist = TRUE)
37
38 # Values on second line
39 values <- split.str(lines[[2]], unlist = TRUE)
40 names(values) <- keys[seq(values)]
41
42 # Get field values
43 for (field in names(col2field))
44 if (values[[col2field[[field]]]] != '-')
45 entry$setField(field, values[[col2field[[field]]]])
46
47 # Set names
48 if (values[['SYNONYMS']] != '-') {
49 # TODO
50 }
51 }
52
53 entries <- c(entries, entry)
54 }
55
56 # Replace elements with no accession id by NULL
57 entries <- lapply(entries, function(x) if (is.na(x$getField(BIODB.ACCESSION))) NULL else x)
58
59 # If the input was a single element, then output a single object
60 if (drop && length(contents) == 1)
61 entries <- entries[[1]]
62
63 return(entries)
64 }