/
class-app.R
600 lines (531 loc) · 14.2 KB
/
class-app.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
.response_app <- c("href", "id", "name", "project", "revision")
# setClassUnion("SBGWorkflowORToolORNULL", c("SBGWorkflow", "Tool", "NULL"))
# # remove "raw" from default showing methods
#' Class App
#'
#' Class App
#'
#' @field id app id
#' @field project project id
#' @field name app name
#' @field revision app revision
#' @field raw raw cwl list, if doesn't have any, call \code{cwl()} method
#'
#' @export App
#' @return App object.
#' @aliases App
#' @examples
#' \dontrun{
#' a <- Auth(url = "https://api.sbgenomics.com/v2/", token = "your_token")
#' # get a public app
#' app <- a$public_app(id = "admin/sbg-public-data/rna-seq-alignment-star")
#' app$input_matrix()
#' app$output_matrix()
#' # get a public app
#' app <- a$public_app(id = "admin/sbg-public-data/star")
#' app$input_matrix()
#' app$output_matrix()}
App <- setRefClass(
"App",
contains = "Item",
fields = list(
id = "characterORNULL",
project = "characterORNULL",
name = "characterORNULL",
revision = "characterORNULL",
raw = "ANY",
raw_obj = "ANY"
),
methods = list(
initialize = function(id = NULL, project = NULL, name = NULL, revision = NULL, raw = NULL, raw_obj = NULL, ...) {
id <<- id
project <<- project
name <<- name
revision <<- revision
raw <<- raw
raw_obj <<- raw_obj
callSuper(...)
},
copyTo = function(project = NULL, name = NULL) {
auth$copy_app(id, project = project, name = name)
},
copy_to = function(project = NULL, name = NULL) {
copyTo(project = project, name = name)
},
cwl = function(revision = NULL, ...) {
if (!is.null(revision)) {
.id <- .update_revision(id, revision)
} else {
.id <- id
}
if (is.null(auth)) {
stop("auth missing")
}
raw <<- auth$api(
path = paste0("apps/", .id, "/raw"),
methods = "GET", ...
)
raw
},
get_required = function() {
obj <- convert_app(.self)
obj$get_required()
},
input_matrix = function(...) {
if (is.null(raw)) {
message("get cwl raw file")
cwl()
}
sevenbridges:::input_matrix(raw, ...)
},
output_matrix = function(...) {
if (is.null(raw)) {
message("get cwl raw file")
cwl()
}
sevenbridges:::output_matrix(raw, ...)
},
input_type = function(...) {
if (is.null(raw)) {
message("get cwl raw file")
cwl()
}
# obj = convert_app(.self)
# obj$input_type(...)
getInputType(raw)
},
output_type = function(...) {
if (is.null(raw)) {
message("get cwl raw file")
cwl()
}
# obj = convert_app(.self)
# obj$output_type(...)
getOutputType(raw)
},
set_batch = function(input = NULL, criteria = NULL, type = c("ITEM", "CRITERIA")) {
obj <- convert_app(.self)
if (is(obj, "Tool")) {
stop("Tool is not supported for batching yet, only Workflow supports batch")
}
obj$set_batch(input = input, criteria = criteria, type = type)
message("updating app ...")
p <- auth$project(id = project)
pattern <- ".+\\/.+\\/(.+)/.+"
shortname <- str_match(id, pattern)[1, 2]
p$app_add(shortname, obj)
message("done")
},
input_check = function(input, batch = NULL, proj = NULL) {
message("Checking if ID matched...")
in_type <- input_type()
in_id <- names(in_type)
cus_id <- names(input)
idx <- cus_id %in% in_id
if (sum(!idx)) {
stop(
"ID not matched: ", paste(cus_id[!idx], collapse = " "),
".", "\n Inputs ID should be \n", paste(in_id, collapse = " ")
)
}
.type <- in_type[match(cus_id, in_id)]
# conversion for single file trick
id.fl <- which("File" == .type)
id.fls <- which("File..." == .type)
# convert string (id or names) first!
# Based on: is a string a valid file id or not
is_file_id <- function(x) {
nchar(x) == 24 && !grepl("[^0-9a-fA-F]", x)
}
# convert a string to Files/FilesList
# p: Project
as_files <- function(p, x) {
if (!all(sapply(x, is.character))) {
stop("please provide file id(s)")
}
res <- lapply(x, function(f) {
if (is_file_id(f)) {
r <- p$file(id = f)
if (is.null(r)) {
stop(paste("id doesn't exists: ", f))
} else {
message("file id: ", f)
}
r
} else {
r <- p$file(name = f, exact = TRUE)
if (is.null(r)) {
stop(paste("name doesn't exists: ", f))
} else {
message("file name: ", f)
}
r
}
})
if (length(x) == 1) {
res[[1]]
} else {
do.call(FilesList, res)
}
}
for (i in c(id.fl, id.fls)) {
if (is.character(input[[i]])) {
input[[i]] <- as_files(proj, input[[i]])
}
}
# solve edge cases
if (length(id.fl)) {
# solve edge case
for (i in id.fl) {
# input should be File
if (is(input[[i]], "Files")) {
if (!is.null(batch) && cus_id[i] == batch$batch_input) {
# only operate on the batch input node, otherwise will cause error
message("Converting single Files as a list for batch mode: ", cus_id[i])
input[[i]] <- list(input[[i]])
}
}
if (is(input[[i]], "FilesList")) {
if (length(input[[i]]) == 1) {
if (is.null(batch)) {
message("Converting to single Files type: ", names(input[[i]]))
input[[i]] <- input[[i]][[1]]
}
} else {
if (is.null(batch)) {
stop(in_id[i], " only accept single File")
}
}
}
if (is.list(input[[i]])) {
if (length(input[[i]]) == 1 && is(input[[i]][[1]], "Files")) {
if (is.null(batch)) {
message("Converting to single Files type: ", names(input[[i]]))
input[[i]] <- input[[i]][[1]]
}
}
if (length(input[[i]]) > 1) {
if (is.null(batch)) {
stop(in_id[i], " only accept single File")
}
}
}
}
}
if (length(id.fls)) {
# solve edge case
for (i in id.fls) {
if (is(input[[i]], "Files")) {
message("Coverting single File to a FileList...")
input[[i]] <- list(input[[i]])
}
}
}
input
},
show = function() .showFields(.self, "== App ==", .response_app)
)
)
.asApp <- function(x) {
if (!is.null(x$revision)) {
r <- as.character(x$revision)
} else {
r <- x$revision
}
App(
id = x$id,
name = x$name,
project = x$project,
revision = r,
raw = x$raw,
response = response(x)
)
}
AppList <- setListClass("App", contains = "Item0")
.asAppList <- function(x) {
obj <- AppList(lapply(x$items, .asApp))
obj@href <- x$href
obj@response <- response(x)
obj
}
#' Convert App or a CWL JSON file to Tool or Flow object
#'
#' Convert App or a CWL JSON file to Tool or Flow object
#'
#' This function import CWL JSON file, based on its class: CommandLineTool
#' or Worklfow to relevant object in R, Tool object or Flow object.
#'
#' @param from an App object or a CWL JSON
#' @rdname convert_app
#' @export convert_app
#' @aliases convert_app
#' @return Tool or Flow object depends on CWL type.
#' @examples
#' tool.in <- system.file("extdata/app", "tool_star.json", package = "sevenbridges")
#' flow.in <- system.file("extdata/app", "flow_star.json", package = "sevenbridges")
#' # convert to Tool object
#' convert_app(tool.in)
#' # convert to Flow object
#' convert_app(flow.in)
convert_app <- function(from) {
if (is(from, "App")) {
if (is.null(from$raw)) {
message("cannot find raw file, pull raw cwl from internet")
from$cwl()
}
obj <- from$raw
} else if (is.character(from) && file.exists(from)) {
obj <- fromJSON(from, FALSE)
} else {
stop("object to be converted should be either a App object or cwl json file")
}
.convert_app(obj)
}
.convert_app <- function(obj) {
cls <- obj$class
switch(cls,
"CommandLineTool" = .asTool(obj),
"Workflow" = .asFlow(obj)
)
}
.asTool <- function(obj) {
# obj should be raw list
args.inputs <- obj$inputs
args.outputs <- obj$outputs
args.requirements <- obj$requirements
args.hints <- obj$hints
args.stdin <- obj$stdin
args.stdout <- obj$stdout
.diy <- c("inputs", "outputs", "requirements", "hints", "stdin", "stdout")
# inputs
if (length(args.inputs)) {
res.in <- input(args.inputs)
} else {
res.in <- IPList()
}
# outputs
if (length(args.outputs)) {
res.out <- output(args.outputs)
} else {
res.out <- OPList()
}
# hints
if (length(args.hints)) {
res.hints <- requirements(args.hints)
} else {
res.hints <- requirements()
}
# requirements
if (length(args.requirements)) {
res.req <- requirements(args.requirements)
} else {
res.req <- requirements()
}
# stdin
if (length(args.stdin)) {
if (is.character(args.stdin)) {
res.stdin <- args.stdin
} else {
res.stdin <- do.call(Expression, args.stdin)
}
} else {
res.stdin <- NULL
}
# stdout
if (length(args.stdout)) {
if (is.character(args.stdout)) {
res.stdout <- args.stdout
} else {
res.stdout <- do.call(Expression, args.stdout)
}
} else {
res.stdout <- NULL
}
nms <- names(obj)
# extra fields not defined, drop them with warning
.obj.def <- names(Tool$fields())
.obj.extra <- setdiff(nms, .obj.def)
if (length(.obj.extra)) {
warning(
"Extra fields dropped before conversion: ",
paste(.obj.extra, collapse = " ")
)
}
.obj.nms <- setdiff(nms, c(.diy, .obj.extra))
res <- do.call("Tool", obj[.obj.nms])
res$field("inputs", res.in)
res$field("outputs", res.out)
res$field("hints", res.hints)
res$field("requirements", res.req)
res$field("stdin", res.stdin)
res$field("stdout", res.stdout)
# for the reason you convert from App, do not add "#"
res$id <- gsub("^#", "", res$id)
res
}
.asFlow <- function(obj) {
args.inputs <- obj$inputs
args.outputs <- obj$outputs
args.requirements <- obj$requirements
args.hints <- obj$hints
.diy <- c("inputs", "outputs", "requirements", "hints", "steps")
# inputs
if (length(args.inputs)) {
res.in <- input(args.inputs)
} else {
res.in <- IPList()
}
# outputs
if (length(args.outputs)) {
lst <- lapply(args.outputs, function(o) {
.t <- o$type
lst <- lapply(.t, function(x) {
if (("type" %in% names(x)) && x$type == "array") {
do.call(ItemArray, x)
} else {
x
}
})
.type <- do.call(DSCList, lst)
lst <- c(
o[!names(o) %in% c(
"sbg:x",
"sbg:y",
"sbg:includeInPorts",
"type",
"source"
)],
list(
"x" = o$"sbg:x",
"y" = o$"sbg:y",
"type" = .type,
"source" = list(as.character(o$source)),
"includeInPorts" = o$"sbg:includeInPorts"
)
)
do.call(SBGWorkflowOutputParameter, lst)
})
res.out <- do.call(SBGWorkflowOutputParameterList, lst)
} else {
res.out <- SBGWorkflowOutputParameterList()
}
# hints
if (length(args.hints)) {
res.hints <- requirements(args.hints)
} else {
res.hints <- requirements()
}
# requirements
if (length(args.requirements)) {
res.req <- requirements(args.requirements)
} else {
res.req <- requirements()
}
# steps
slst <- get_steplist_item(obj)
# if (length(steplst)) {
# lst <- lapply(steplst, function(x) {
# .convert_app(x$run)
# })
# slst <- lst[[1]]
# for (i in 1:(length(lst) -1)) {
# slst <- slst + lst[[i + 1]]
# }
# } else {
# slst <- SBGStepList()
# }
nms <- names(obj)
# extra fields not defined, drop them with warning
.obj.def <- names(SBGWorkflow$fields())
.obj.extra <- setdiff(nms, .obj.def)
if (length(.obj.extra)) {
warning(
"Extra fields dropped before conversion: ",
paste(.obj.extra, collapse = " ")
)
}
.obj.nms <- setdiff(nms, c(.diy, .obj.extra))
res <- do.call(
"Flow",
c(
obj[.obj.nms],
list(
steps = slst,
inputs = res.in,
outputs = res.out,
hints = res.hints,
requirements = res.req
)
)
)
res
}
#' @rdname convert_app
#' @aliases appType
#' @export appType
#' @param x a App object
#' @section appType:
#' \describe{
#' this function return class of a App object.}
appType <- function(x) {
obj <- x$raw
if (is.null(obj)) {
x$cwl()
obj <- x$raw
}
obj$class
}
get_sbg_item <- function(x) {
lst <- fromJSON(x, FALSE)
nms <- names(lst)
nms[grep("sbg:", nms)]
}
get_nonsbg_item <- function(x, remove = c("inputs", "outputs", "hints", "requirements")) {
lst <- fromJSON(x, FALSE)
nms <- setdiff(names(lst), remove)
nms[!grepl("sbg:", nms)]
}
get_input_item <- function(x) {
lst <- fromJSON(x, FALSE)
input(lst$inputs)
}
get_output_item <- function(x) {
lst <- fromJSON(x, FALSE)
output(lst$outputs)
}
# Step and StepList
get_stepinputlist_item <- function(x) {
# x is a step
lst <- lapply(x$inputs, function(i) {
do.call(WorkflowStepInput, i)
})
WorkflowStepInputList(lst)
}
get_stepoutputlist_item <- function(x) {
# x is a step
lst <- lapply(x$outputs, function(i) {
do.call(WorkflowStepOutput, i)
})
WorkflowStepOutputList(lst)
}
get_step_item <- function(x) {
# x is a step list
.run <- .convert_app(x$run)
SBGStep(
id = x$id,
run = .run,
outputs = get_stepoutputlist_item(x),
inputs = get_stepinputlist_item(x)
)
}
get_steplist_item <- function(input) {
if (is.character(input) && file.exists(input)) {
obj <- fromJSON(input, FALSE)
} else if (is.list(input) && "steps" %in% names(input)) {
obj <- input
} else {
stop("input has to be a json file or steplist parsed from app")
}
ss <- obj$steps
do.call(SBGStepList, lapply(ss, get_step_item))
}