-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfive_safes_mapping.R
More file actions
executable file
·377 lines (316 loc) · 11.7 KB
/
five_safes_mapping.R
File metadata and controls
executable file
·377 lines (316 loc) · 11.7 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# The plan here is to map as much of the information we can get from opal and
# DataSHIELD to the five safes framework.
################################################################################
# Create RO-Crate ----
################################################################################
## Setup ----
# install.packages("pak")
pak::pkg_install("ResearchObject/ro-crate-r@dev")
pak::pkg_install("datashield/dsBaseClient@v6.3.5-dev")
## Global variables
USERNAME <- "demo_user"
USERPASS <- "Demo_password1!"
PROJECT <- "DEMO"
TABLES <- c("CNSIM1")
SERVER <- "https://localhost:8843" # "https://opal-demo.obiba.org"
PROFILE <- "demo"
# Create base RO-Crate with 5 Safes profile
dsROCrate <- rocrateR::rocrate_5s()
# To start us off we will use the opal demo server, with `USERNAME` and `PROJECT`.
o <- opalr::opal.login(
username = "administrator",
password = "password",
url = SERVER#, profile = PROFILE
)
################################################################################
# Safe people ----
################################################################################
# Only administrator users can run this
opalr::oadmin.user_profiles(o)
opalr::dsadmin.perm(o)
opalr::opal.perms(o, USERNAME)
# create entities for users
safe_people_entities <- opalr::oadmin.users(o) |>
tibble::as_tibble() |>
dplyr::filter(enabled) |>
purrr::pmap(function(name, ...) {
rocrateR::entity(
x = digest::digest(name),
type = "Person",
name = name
)
})
# add user to RO-Crate
## find index for the entity of the current user, `USERNAME`
idx <- sapply(safe_people_entities, "[[", "name") == USERNAME
## add user
dsROCrate <- dsROCrate |>
rocrateR::add_entity(safe_people_entities[idx][[1]], overwrite = TRUE) |>
rocrateR::add_entity_value("./", "author", getElement(safe_people_entities[idx][[1]], "@id"))
print(dsROCrate)
################################################################################
# Safe data ----
################################################################################
# Table names, update times etc.
project_tbl <- opalr::opal.project(o, PROJECT)
project_tbl
opalr::opal.tables(o, PROJECT)
# inspect table access rights
TABLES |>
purrr::walk(function(t) {
message("Table: ", t)
opalr::opal.table_perm(o, PROJECT, t) |>
glue::glue_data("[{type}] {subject} \t|\t Permissions: {permission}\n\n") |>
message()
})
# Retrieve all projects and add entities to the `dsROCrate` object
opalr::opal.projects(o) |>
tibble::as_tibble() |>
dplyr::filter(name == PROJECT) |> # filter specific project, set by PROJECT
purrr::pwalk(function(name, tittle, created, lastUpdate, ...) {
# Table names, update times etc.
project_details_tbl <- opalr::opal.project(o, name)
project_tables <- tryCatch({
project_details_tbl |>
purrr::pluck("datasource") |>
purrr::pluck("table") |>
purrr::list_c()
}, error = function(e) {
list()
})
# add table entities to the `dsROCrate` object
project_details_entities <- tibble::tibble(
datasource = name,
table = project_tables
) |>
dplyr::filter(table %in% TABLES) |> # filter specific tables, set by TABLES
purrr::pmap(function(datasource, table) {
table_details <- opalr::opal.table(o, datasource, table)
timestamps <- getElement(table_details, "timestamps")
new_dataset_entity <- rocrateR::entity(
x = digest::digest(paste0(datasource, "_", table)),
type = "Dataset",
dateCreated = getElement(timestamps, "created"),
dateModified = getElement(timestamps, "lastUpdate"),
path = getElement(table_details, "link")
)
# add new dataset entity to the RO-Crate
dsROCrate <<- dsROCrate |>
rocrateR::add_entity(new_dataset_entity, overwrite = TRUE)
return(new_dataset_entity)
})
project_entity <- rocrateR::entity(
x = digest::digest(name),
type = "Project",
name = name,
dateCreated = created,
dateModified = lastUpdate,
hasPart = project_details_entities |>
sapply("[[", "@id") |>
lapply(function(id) {
list(`@id` = id)
})
)
# if no tables are associated to this project, then drop `hasPart`
if (length(project_details_entities) == 0) {
project_entity$hasPart <- NULL
}
# add new project entity to the RO-Crate
dsROCrate <<- dsROCrate |>
rocrateR::add_entity(project_entity, overwrite = TRUE)
}, .progress = TRUE)
print(dsROCrate)
################################################################################
# Safe setting ----
# There is a lot of information here. Could just have e.g. list of package names
# instead of full descriptions etc.
################################################################################
# Disclosure settings
opalr::dsadmin.activity(o)
opalr::dsadmin.get_options(o) # must login as administrator user
## Add DataSHIELD options as part of the Safe setting
'
{
"@id": "_:localid:tre72:project81",
"@type": "PropertyValue",
"name": "tre72",
"value": "project81"
}
'
disc_setting_entities <- opalr::dsadmin.get_options(o) |>
tibble::as_tibble() |>
purrr::pmap(function(name, value, ...) {
rocrateR::entity(
x = paste0("_:localid:", name, ":", value),
type = "PropertyValue",
name = name,
value = value
)
})
# Installed packages
opalr::dsadmin.package_descriptions(o) |>
tibble::as_tibble() |>
purrr::pwalk(function(Package, Version, Description, Author, ...) {
author_entities <- Author |>
stringr::str_split(pattern = ",\\n", simplify = TRUE) |>
purrr::map(function(str) {
# check if current author is a physical person (has aut or cre roles)
is_person <- stringr::str_detect(str, "\\[.*(aut|cre).*\\]")
if(!is_person)
return(NULL)
# create tibble with author name and ORCiD (if available)
tibble::tibble(
author = str |>
stringr::str_extract("(.*)(?= \\[aut)") |>
stringr::str_squish(),
orcid = str |>
stringr::str_extract("(?<=https:\\/\\/orcid.org\\/)(.*)(?=>)")
)
}) |>
purrr::list_c() |>
purrr::pmap(function(author, orcid) {
author_entity <- rocrateR::entity(
x = ifelse(is.na(orcid) | nchar(orcid) == 0, digest::digest(author),
paste0("https://orcid.org/", orcid)),
type = "Person",
name = author
)
# # add author entity to the RO-Crate
# dsROCrate <<- dsROCrate |>
# rocrateR::add_entity(author_entity, overwrite = TRUE)
return(author_entity)
})
package_entity <- rocrateR::entity(
x = digest::digest(paste0(Package, "_", Version)),
type = "SoftwareApplication",
name = Package,
version = Version,
description = Description,
# author = author_entities |>
# sapply("[[", "@id") |>
# lapply(function(id) {
# list(`@id` = id)
# })
)
# if no authors are associated to this package, then drop `author`
if (length(author_entities) == 0) {
package_entity$author <- NULL
}
# add new package entity to the RO-Crate
dsROCrate <<- dsROCrate |>
rocrateR::add_entity(package_entity, overwrite = TRUE)
})
print(dsROCrate)
# Methods
opalr::dsadmin.get_methods(o, type = "aggregate")
opalr::dsadmin.get_methods(o, type = "assign")
# Available profiles
opalr::dsadmin.profiles(o)
opalr::dsadmin.profile_perm(o, "default")
# Opal installed packages
opalr::oadmin.installed_packages(o) |>
tibble::as_tibble()
# Opal version
opalr::opal.get(o, 'system', 'version')
opalr::dsadmin.get_methods(o, type = "aggregate", profile = PROFILE) |>
tibble::as_tibble()
# add custom method
opalr::dsadmin.set_method(o, "hello", func = function(x) { paste0("Hello ", x, "!") }, type = "aggregate")#, profile = PROFILE)
# verify custom method
opalr::dsadmin.get_method(o, "hello", type = "aggregate")#, profile = PROFILE)
opalr::dsadmin.profile_enable(o, PROFILE, enabled = TRUE)
opalr::dsadmin.profiles(o)
# library(DSOpal)
# DSOpal::Opal()
builder <- DSI::newDSLoginBuilder()
builder$append(server = "study1",
url = SERVER, # "https://opal-demo.obiba.org",
user = "administrator",
password = "password",
driver = "OpalDriver") #, profile = PROFILE)
logindata <- builder$build()
conns <- DSI::datashield.login(logins = logindata)
# call the hello() function on the R server
DSI::datashield.aggregate(conns, expr = quote(hello('friends')))
DSI::datashield.errors()
DSI::datashield.logout(conns)
################################################################################
# Safe project ----
# I expect a lot of this will come from upstream - e.g. data access committee
# sign off etc. I don't know how we will get this into here.
################################################################################
# opalr::opal.execute(o)
# Project details
project_tbl <- opalr::opal.project(o, PROJECT)
project_tbl
opalr::opal.tables(o, PROJECT)
# Project details were added to the RO-Crate in the 'Safe Data' section
################################################################################
# Safe output ----
# We will likely have to parse the log files to show which methods have been
# run. Later on we can add the stat barn tags.
################################################################################
# Test analysis (to generate some logs) ----
builder <- DSI::newDSLoginBuilder()
builder$append(server = "study1",
url = SERVER,
user = USERNAME, #"administrator",
password = USERPASS, #"password",
driver = "OpalDriver")
logindata <- builder$build()
conns <- DSI::datashield.login(logins = logindata)#, assign = TRUE, symbol = "D")
## assign data
DSI::datashield.tables(conns["study1"])
DSI::datashield.assign.table(conns["study1"], "cnsim", "DEMO.CNSIM1", errors.print = TRUE)
dsBaseClient::ds.ls(datasources = conns["study1"])
dsBaseClient::ds.summary("cnsim")
dsBaseClient::ds.table("cnsim$DIS_DIAB")
# DSI::datashield.errors()
DSI::datashield.workspace_save(conns, "datashield_workspace")
DSI::datashield.profiles(conns)
DSI::datashield.logout(conns)
## Check-out logs
o <- opalr::opal.login(
username = "administrator",
password = "password",
url = SERVER
)
opalr::dsadmin.activity(o)
opalr::dsadmin.activity_summary(o)
opalr::dsadmin.log(o) |>
tibble::as_tibble() |>
View()
parse_timestamp <- function(x, format = "%Y-%m-%dT%H:%M:%S") {
x |>
sub(pattern = "\\..*Z$", replacement = "") |>
as.POSIXct(format = format, tz = "UTC") |>
format(format)
}
# Parse logs
userlogs <- opalr::dsadmin.log(o) |>
tibble::as_tibble() |>
dplyr::filter(logger_name == "datashield.user") |>
dplyr::filter(username == USERNAME) |>
glue::glue_data(
"[{level}][{parse_timestamp(`@timestamp`)}]{stringr::str_pad(paste0('[', ds_action, ']'), 12, 'right', ' ')}{message}"
)
log_filename <- paste0("./", Sys.Date(), "-dslogs-", USERNAME, ".log")
userlogs |>
readr::write_lines(log_filename)
opalr::opal.logout(o)
# create new data entity for log file
log_entity <- rocrateR::entity(
x = basename(log_filename), #digest::digest(basename(log_filename)),
type = "File",
dateModified = Sys.time(),
name = basename(log_filename)
)
# add entity to the RO-Crate
dsROCrate <- dsROCrate |>
rocrateR::add_entity(log_entity) |>
rocrateR::add_entity_value(id = "./", key = "hasPart", value = list(`@id` = log_entity$`@id`))
#### Write RO-Crate to file
dsROCrate |>
rocrateR::write_rocrate("./ro-crate-metadata.json")
#### Zip all the files in the crate
zip("./demo_5safes_rocrate.zip", c("./ro-crate-metadata.json", log_filename))