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