Mercurial > repos > prog > lcmsmatching
comparison EnzymeEntry.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 EnzymeEntry <- methods::setRefClass("EnzymeEntry", contains = 'BiodbEntry') | |
| 6 | |
| 7 ########### | |
| 8 # FACTORY # | |
| 9 ########### | |
| 10 | |
| 11 createEnzymeEntryFromTxt <- function(contents, drop = TRUE) { | |
| 12 | |
| 13 entries <- list() | |
| 14 | |
| 15 # Define fields regex | |
| 16 regex <- character() | |
| 17 regex[[BIODB.ACCESSION]] <- "^ID\\s+([0-9.]+)$" | |
| 18 regex[[BIODB.DESCRIPTION]] <- "^DE\\s+(.+)$" | |
| 19 | |
| 20 for (text in contents) { | |
| 21 | |
| 22 # Create instance | |
| 23 entry <- EnzymeEntry$new() | |
| 24 | |
| 25 lines <- strsplit(text, "\n") | |
| 26 for (s in lines[[1]]) { | |
| 27 | |
| 28 # Test generic regex | |
| 29 parsed <- FALSE | |
| 30 for (field in names(regex)) { | |
| 31 g <- stringr::str_match(s, regex[[field]]) | |
| 32 if ( ! is.na(g[1,1])) { | |
| 33 entry$setField(field, g[1,2]) | |
| 34 parsed <- TRUE | |
| 35 break | |
| 36 } | |
| 37 } | |
| 38 if (parsed) | |
| 39 next | |
| 40 } | |
| 41 | |
| 42 entries <- c(entries, entry) | |
| 43 } | |
| 44 | |
| 45 # Replace elements with no accession id by NULL | |
| 46 entries <- lapply(entries, function(x) if (is.na(x$getField(BIODB.ACCESSION))) NULL else x) | |
| 47 | |
| 48 # If the input was a single element, then output a single object | |
| 49 if (drop && length(contents) == 1) | |
| 50 entries <- entries[[1]] | |
| 51 | |
| 52 return(entries) | |
| 53 } |
