Mercurial > repos > prog > lcmsmatching
comparison BiodbFactory.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('BiodbFactory')) { # Do not load again if already loaded | |
2 | |
3 library(methods) | |
4 source('ChebiConn.R') | |
5 source('KeggConn.R') | |
6 source('PubchemConn.R') | |
7 source('HmdbConn.R') | |
8 source('ChemspiderConn.R') | |
9 source('EnzymeConn.R') | |
10 source('LipidmapsConn.R') | |
11 source('MirbaseConn.R') | |
12 source('NcbigeneConn.R') | |
13 source('NcbiccdsConn.R') | |
14 source('UniprotConn.R') | |
15 source('MassbankConn.R') | |
16 | |
17 ##################### | |
18 # CLASS DECLARATION # | |
19 ##################### | |
20 | |
21 BiodbFactory <- setRefClass("BiodbFactory", fields = list(.useragent = "character", .conn = "list", .cache.dir = "character")) | |
22 | |
23 ############### | |
24 # CONSTRUCTOR # | |
25 ############### | |
26 | |
27 BiodbFactory$methods( initialize = function(useragent = NA_character_, cache.dir = NA_character_, ...) { | |
28 | |
29 ( ! is.null(useragent) && ! is.na(useragent)) || stop("You must provide a user agent string (e.g.: \"myapp ; my.email@address\").") | |
30 .useragent <<- useragent | |
31 .conn <<- list() | |
32 .cache.dir <<- cache.dir | |
33 | |
34 callSuper(...) # calls super-class initializer with remaining parameters | |
35 }) | |
36 | |
37 ################## | |
38 # GET USER AGENT # | |
39 ################## | |
40 | |
41 BiodbFactory$methods( getUserAgent = function() { | |
42 return(.self$.useragent) | |
43 }) | |
44 | |
45 ############ | |
46 # GET CONN # | |
47 ############ | |
48 | |
49 BiodbFactory$methods( getConn = function(class) { | |
50 | |
51 if ( ! class %in% names(.self$.conn)) { | |
52 | |
53 # Create connection instance | |
54 conn <- switch(class, | |
55 chebi = ChebiConn$new(useragent = .self$.useragent), | |
56 kegg = KeggConn$new(useragent = .self$.useragent), | |
57 pubchem = PubchemConn$new(useragent = .self$.useragent), | |
58 hmdb = HmdbConn$new(useragent = .self$.useragent), | |
59 chemspider = ChemspiderConn$new(useragent = .self$.useragent), | |
60 enzyme = EnzymeConn$new(useragent = .self$.useragent), | |
61 lipidmaps = LipidmapsConn$new(useragent = .self$.useragent), | |
62 mirbase = MirbaseConn$new(useragent = .self$.useragent), | |
63 ncbigene = NcbigeneConn$new(useragent = .self$.useragent), | |
64 ncbiccds = NcbiccdsConn$new(useragent = .self$.useragent), | |
65 uniprot = UniprotConn$new(useragent = .self$.useragent), | |
66 massbank = MassbankConn$new(useragent = .self$.useragent), | |
67 NULL) | |
68 | |
69 # Unknown class | |
70 if (is.null(conn)) | |
71 stop(paste0("Unknown r-biodb class \"", class,"\".")) | |
72 | |
73 .self$.conn[[class]] <- conn | |
74 } | |
75 | |
76 return (.self$.conn[[class]]) | |
77 }) | |
78 | |
79 ################ | |
80 # CREATE ENTRY # | |
81 ################ | |
82 | |
83 BiodbFactory$methods( createEntry = function(class, type, id = NULL, content = NULL, drop = TRUE) { | |
84 | |
85 is.null(id) && is.null(content) && stop("One of id or content must be set.") | |
86 ! is.null(id) && ! is.null(content) && stop("id and content cannot be both set.") | |
87 | |
88 # Get content | |
89 if ( ! is.null(id)) | |
90 content <- .self$getEntryContent(class, type, id) | |
91 | |
92 conn <- .self$getConn(class) | |
93 entry <- conn$createEntry(type = type, content = content, drop = drop) | |
94 | |
95 # Set factory | |
96 for (e in c(entry)) | |
97 e$setFactory(.self) | |
98 | |
99 return(entry) | |
100 }) | |
101 | |
102 ######################## | |
103 # GET CACHE FILE PATHS # | |
104 ######################## | |
105 | |
106 BiodbFactory$methods( .get.cache.file.paths = function(class, type, id) { | |
107 | |
108 # Get extension | |
109 ext <- .self$getConn(class)$getEntryContentType(type) | |
110 | |
111 # Set filenames | |
112 filenames <- vapply(id, function(x) paste0(class, '-', type, '-', x, '.', ext), FUN.VALUE = '') | |
113 | |
114 # set file paths | |
115 file.paths <- vapply(filenames, function(x) file.path(.self$.cache.dir, x), FUN.VALUE = '') | |
116 | |
117 # Create cache dir if needed | |
118 if ( ! is.na(.self$.cache.dir) && ! file.exists(.self$.cache.dir)) | |
119 dir.create(.self$.cache.dir) | |
120 | |
121 return(file.paths) | |
122 }) | |
123 | |
124 ########################### | |
125 # LOAD CONTENT FROM CACHE # | |
126 ########################### | |
127 | |
128 BiodbFactory$methods( .load.content.from.cache = function(class, type, id) { | |
129 | |
130 content <- NULL | |
131 | |
132 # Read contents from files | |
133 file.paths <- .self$.get.cache.file.paths(class, type, id) | |
134 content <- lapply(file.paths, function(x) { if (file.exists(x)) paste(readLines(x), collapse = "\n") else NULL }) | |
135 | |
136 return(content) | |
137 }) | |
138 | |
139 ######################### | |
140 # SAVE CONTENT TO CACHE # | |
141 ######################### | |
142 | |
143 BiodbFactory$methods( .save.content.to.cache = function(class, type, id, content) { | |
144 | |
145 # Write contents into files | |
146 file.paths <- .self$.get.cache.file.paths(class, type, id) | |
147 mapply(function(c, f) { if ( ! is.null(c)) writeLines(c, f) }, content, file.paths) | |
148 }) | |
149 | |
150 ##################### | |
151 # GET ENTRY CONTENT # | |
152 ##################### | |
153 | |
154 BiodbFactory$methods( getEntryContent = function(class, type, id) { | |
155 | |
156 content <- NULL | |
157 # Load from cache | |
158 if ( ! is.na(.self$.cache.dir)) | |
159 content <- .self$.load.content.from.cache(class, type, id) | |
160 | |
161 # Get contents | |
162 missing.content.indexes <- vapply(content, is.null, FUN.VALUE = TRUE) | |
163 missing.ids <- if (is.null(content)) id else id[missing.content.indexes] | |
164 if (length(missing.ids) > 0) { | |
165 | |
166 # Use connector to get missing contents | |
167 conn <- .self$getConn(class) | |
168 missing.contents <- conn$getEntryContent(type, missing.ids) | |
169 | |
170 # Save to cache | |
171 if ( ! is.null(missing.contents) && ! is.na(.self$.cache.dir)) | |
172 .self$.save.content.to.cache(class, type, missing.ids, missing.contents) | |
173 | |
174 # Merge content and missing.contents | |
175 if (is.null(content)) | |
176 content <- missing.contents | |
177 else | |
178 content[missing.content.indexes] <- missing.contents | |
179 } | |
180 | |
181 return(content) | |
182 }) | |
183 } |