forked from TNO/PhenotypeDatabase-RClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbnp.functions.R
241 lines (212 loc) · 7.35 KB
/
dbnp.functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# Copyright 2012 Thomas Kelder
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
library(RJSONIO)
library(RCurl)
library(digest)
.vars = new.env()
.set = function(name, value) {
assign(name, value, env=.vars)
}
.get = function(name) {
v = get(name, .vars)
v
}
.set("deviceID", digest(paste(Sys.info(), collapse="."), algo="md5", serialize = F)) ##TODO: replace with real unique machine id
.set("defaultBase", "http://studies.dbnp.org/api/")
.set("authSequence", 0)
.set("authKey", "")
.set("authToken", "")
.set("verbose", F)
.getValidation = function() {
.set('authSequence', .get('authSequence') + 1)
str = paste(.get('authToken'), .get('authSequence'), .get('authKey'), sep="")
if(.get("verbose")) {
message("Sequence: ", .get('authSequence'))
message("Validation string: ", str)
}
digest(str, algo = "md5", serialize = F)
}
.createUrl = function(base, cmd, pars = list()) {
url = paste(base, cmd, "?", sep="")
for(p in names(pars)) {
v = pars[[p]]
if(is.null(v) || v != "") url = paste(url, p, "=", v, "&", sep="")
}
gsub("[\\?&]{1}$", "", url)
}
.getUrlErr = function(...) {
if(.get("verbose")) print(url)
err= ""
r = getURL(..., verbose = .get("verbose"))
if(!isValidJSON(r, asText = T)) {
stop(r)
}
r
}
setGscfBaseUrl = function(baseUrl = "http://studies.dbnp.org/api/") {
.set("defaultBase", baseUrl)
}
getGscfBaseUrl = function() .get("defaultBase")
setGscfDeviceId = function(deviceId) {
.set("deviceID", deviceId)
}
getGscfDeviceId = function() .get("deviceID")
setGscfVerbose = function(verbose = F) {
.set("verbose", verbose)
}
authenticate = function
### Authenticate with GSCF
(user, ##<< The username
pass, ##<< The password
shared.key ##<< The shared key (can be found at your profile page in the GSCF web interface)
) {
url = .createUrl(.get("defaultBase"), "authenticate", list(deviceID = .get("deviceID")))
resp = .getUrlErr(url, .opts = curlOptions(userpwd = paste(user, pass, sep=":")))
auth = fromJSON(resp)
.set("authSequence", auth$sequence)
.set("authKey", shared.key)
.set("authToken", auth$token)
T
}
.fieldAsName = function(x, f = "token") {
names(x) = as.character(sapply(x, function(y) y[f]))
x
}
getStudies = function
### Get a list of the available studies
() {
url = .createUrl(getGscfBaseUrl(), "getStudies", list(validation = .getValidation(), deviceID = .get("deviceID")))
resp = .getUrlErr(url)
r = fromJSON(resp)
.fieldAsName(r$studies)
### A named list with the available studies
}
getSubjectsForStudy = function
### Get subjects for a given study
(studyToken ##<< The token of the study to get the subjects for
) {
url = .createUrl(getGscfBaseUrl(), "getSubjectsForStudy", list(studyToken = studyToken, deviceID = .get("deviceID"), validation = .getValidation()))
resp = .getUrlErr(url)
r = fromJSON(resp)
.fieldAsName(r$subjects)
### A named list with the subjects
}
getEventGroupsForStudy = function
### Get event groups for a given study
(studyToken ##<< The token of the study to get the event groups for
) {
url = .createUrl(getGscfBaseUrl(), "getEventGroupsForStudy", list(studyToken = studyToken, deviceID = .get("deviceID"), validation = .getValidation()))
resp = .getUrlErr(url)
r = fromJSON(resp)
.fieldAsName(r$eventGroups)
### A named list with the event groups
}
getEventsForStudy = function
### Get events for a given study
(studyToken ##<< The token of the study to get the events for
) {
url = .createUrl(getGscfBaseUrl(), "getEventsForStudy", list(studyToken = studyToken, deviceID = .get("deviceID"), validation = .getValidation()))
resp = .getUrlErr(url)
r = fromJSON(resp)
.fieldAsName(r$events)
### A named list with the events
}
getSamplingEventsForStudy = function
### Get sampling events for a given study
(studyToken ##<< The token of the study to get the sampling events for
) {
url = .createUrl(getGscfBaseUrl(), "getSamplingEventsForStudy", list(studyToken = studyToken, deviceID = .get("deviceID"), validation = .getValidation()))
resp = .getUrlErr(url)
r = fromJSON(resp)
.fieldAsName(r$samplingEvents)
### A named list with the sampling events
}
getSamplesForStudy = function
### Get samples for a given study
(studyToken ##<< The token of the study to get the samples for
) {
url = .createUrl(getGscfBaseUrl(), "getSamplesForStudy", list(studyToken = studyToken, deviceID = .get("deviceID"), validation = .getValidation()))
resp = .getUrlErr(url)
r = fromJSON(resp)
.fieldAsName(r$samples)
### A named list with the samples
}
getAssaysForStudy = function
### Get the available assays for a given study
(studyToken ##<< The token of the study to get the assays for
) {
url = .createUrl(getGscfBaseUrl(), "getAssaysForStudy", list(studyToken = studyToken, deviceID = .get("deviceID"), validation = .getValidation()))
resp = .getUrlErr(url)
r = fromJSON(resp)
.fieldAsName(r$assays)
### A named list of the assays
}
getSamplesForAssay = function
### Get samples for a given assay
(assayToken ##<< The token of the assay to get the samples for
) {
url = .createUrl(getGscfBaseUrl(), "getSamplesForAssay", list(assayToken = assayToken, deviceID = .get("deviceID"), validation = .getValidation()))
resp = .getUrlErr(url)
r = fromJSON(resp)
.fieldAsName(r$samples)
### A named list of the available samples
}
getMeasurementDataForAssay = function
### Get data for a given assay
(assayToken ##<< The token of the assay to get the data for
) {
url = .createUrl(getGscfBaseUrl(), "getMeasurementDataForAssay", list(assayToken = assayToken, deviceID = .get("deviceID"), validation = .getValidation()))
resp = .getUrlErr(url)
r = fromJSON(resp)
r$measurements
### Named list with available measurement data
}
assayDataAsMatrix = function
### Convenience function to load all data and samples for given assays.
(assayTokens ##<< The tokens of the assays to get the data for
) {
assayData = lapply(assayTokens, function(a) {
if(.get("verbose")) message("Getting data for assay: ", a)
samples = getSamplesForAssay(a)
data = getMeasurementDataForAssay(a)
list(samples = samples, data = data)
})
names(assayData) = assayTokens
data = c("sampleToken", "sampleName", "measurement", "value")
for(a in names(assayData)) {
ad = assayData[[a]]$data
samples = assayData[[a]]$samples
sampleNames = lapply(samples, function(x) x['name'])
for(s in names(ad)) {
row = ad[[s]]
row[sapply(row, is.null)] = NA
for(m in names(row)) {
rd = as.character(c(s, sampleNames[s], m, row[m]))
data = rbind(data, rd)
}
}
}
if(!is.null(dim(data))) {
colnames(data) = data[1,]
data = data[2:nrow(data),]
rownames(data) = NULL
data = as.data.frame(data, stringsAsFactors = F)
list(data = data, raw = assayData)
} else {
if(.get("verbose")) print(data)
warning("No data!")
}
### Returns a named list with the following items $data (data as pivot table),
### $raw (data in list form as returned by getSamplesForAssay and getMeasurementDataForAssay)
}