Mercurial > repos > prog > lcmsmatching
comparison BiodbEntry.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('BiodbEntry')) { # Do not load again if already loaded | |
| 2 | |
| 3 source('biodb-common.R') | |
| 4 | |
| 5 ######################## | |
| 6 # ENTRY ABSTRACT CLASS # | |
| 7 ######################## | |
| 8 | |
| 9 BiodbEntry <- setRefClass("BiodbEntry", fields = list(.fields ='list', .factory = "ANY")) | |
| 10 | |
| 11 ############### | |
| 12 # CONSTRUCTOR # | |
| 13 ############### | |
| 14 | |
| 15 BiodbEntry$methods( initialize = function(...) { | |
| 16 | |
| 17 .fields <<- list() | |
| 18 .factory <<- NULL | |
| 19 | |
| 20 callSuper(...) # calls super-class initializer with remaining parameters | |
| 21 }) | |
| 22 | |
| 23 ############# | |
| 24 # SET FIELD # | |
| 25 ############# | |
| 26 | |
| 27 BiodbEntry$methods( setField = function(field, value) { | |
| 28 | |
| 29 class = .self$getFieldClass(field) | |
| 30 | |
| 31 # Check cardinality | |
| 32 if (class != 'data.frame' && .self$getFieldCardinality(field) == RBIODB.CARD.ONE && length(value) > 1) | |
| 33 stop(paste0('Cannot set more that one value to single value field "', field, '" in BiodEntry.')) | |
| 34 | |
| 35 # Check value class | |
| 36 value <- switch(class, | |
| 37 'character' = as.character(value), | |
| 38 'double' = as.double(value), | |
| 39 'integer' = as.integer(value), | |
| 40 'logical' = as.logical(value), | |
| 41 value) | |
| 42 # TODO check value class | |
| 43 | |
| 44 .self$.fields[[field]] <- value | |
| 45 }) | |
| 46 | |
| 47 ################### | |
| 48 # GET FIELD CLASS # | |
| 49 ################### | |
| 50 | |
| 51 BiodbEntry$methods( getFieldClass = function(field) { | |
| 52 | |
| 53 if ( ! field %in% RBIODB.FIELDS[['name']]) | |
| 54 stop(paste0('Unknown field "', field, '" in BiodEntry.')) | |
| 55 | |
| 56 field.class <- RBIODB.FIELDS[which(field == RBIODB.FIELDS[['name']]), 'class'] | |
| 57 | |
| 58 return(field.class) | |
| 59 }) | |
| 60 | |
| 61 ######################### | |
| 62 # GET FIELD CARDINALITY # | |
| 63 ######################### | |
| 64 | |
| 65 BiodbEntry$methods( getFieldCardinality = function(field) { | |
| 66 | |
| 67 if ( ! field %in% RBIODB.FIELDS[['name']]) | |
| 68 stop(paste0('Unknown field "', field, '" in BiodEntry.')) | |
| 69 | |
| 70 field.card <- RBIODB.FIELDS[which(field == RBIODB.FIELDS[['name']]), 'cardinality'] | |
| 71 | |
| 72 return(field.card) | |
| 73 }) | |
| 74 | |
| 75 ############# | |
| 76 # GET FIELD # | |
| 77 ############# | |
| 78 | |
| 79 BiodbEntry$methods( getField = function(field) { | |
| 80 | |
| 81 if ( ! field %in% RBIODB.FIELDS[['name']]) | |
| 82 stop(paste0('Unknown field "', field, '" in BiodEntry.')) | |
| 83 | |
| 84 if (field %in% names(.self$.fields)) | |
| 85 return(.self$.fields[[field]]) | |
| 86 else if (.self$.compute.field(field)) | |
| 87 return(.self$.fields[[field]]) | |
| 88 | |
| 89 # Return NULL or NA | |
| 90 class = .self$getFieldClass(field) | |
| 91 return(if (class %in% c('character', 'integer', 'double', 'logical')) as.vector(NA, mode = class) else NULL) | |
| 92 }) | |
| 93 | |
| 94 ################# | |
| 95 # COMPUTE FIELD # | |
| 96 ################## | |
| 97 | |
| 98 BiodbEntry$methods( .compute.field = function(field) { | |
| 99 | |
| 100 if ( ! is.null(.self$.factory) && field %in% names(RBIODB.FIELD.COMPUTING)) { | |
| 101 for (db in RBIODB.FIELD.COMPUTING[[field]]) { | |
| 102 db.id <- .self$getField(paste0(db, 'id')) | |
| 103 if ( ! is.na(db.id)) { | |
| 104 db.compound <- .self$.factory$createEntry(db, type = RBIODB.COMPOUND, id = db.id) | |
| 105 if ( ! is.null(db.compound)) { | |
| 106 .self$setField(field, db.compound$getField(field)) | |
| 107 return(TRUE) | |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 return(FALSE) | |
| 114 }) | |
| 115 | |
| 116 ########### | |
| 117 # FACTORY # | |
| 118 ########### | |
| 119 | |
| 120 BiodbEntry$methods( setFactory = function(factory) { | |
| 121 | |
| 122 is.null(factory) || inherits(factory, "BiodbFactory") || stop("The factory instance must inherit from BiodbFactory class.") | |
| 123 .factory <<- factory | |
| 124 }) | |
| 125 } |
