summaryrefslogtreecommitdiff
path: root/ccwl/ccwl.scm
blob: 25a9950990d131d9c639ebc94daeb29da1f39a06 (plain)
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
;;; ccwl --- Concise Common Workflow Language
;;; Copyright © 2021 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of ccwl.
;;;
;;; ccwl is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; ccwl is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with ccwl.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This is the main module that presents the public interface to ccwl.

;;; Code:

(define-module (ccwl ccwl)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-9 gnu)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 match)
  #:use-module (ccwl utils)
  #:use-module (ccwl yaml)
  #:export (command
            workflow
            input
            input-with-prefix
            output
            step
            pipeline
            write-cwl))

(define %cwl-version "v1.2")

(define-immutable-record-type <input>
  (make-input id type label default source prefix other)
  input?
  (id input-id)
  (type input-type)
  (label input-label)
  (default input-default)
  (source input-source set-input-source)
  (prefix input-prefix set-input-prefix)
  (other input-other))

(define (input-with-prefix prefix input)
  "Set PREFIX on INPUT object."
  (set-input-prefix input prefix))

(define-immutable-record-type <unspecified-default>
  (make-unspecified-default)
  unspecified-default?)

(define* (input id #:key (type 'File) label (default (make-unspecified-default)) (other '()))
  "Build and return an <input> object."
  ;; The user should never set source, and should not set prefix
  ;; directly during construction of the <input> object. Hence, do not
  ;; expose it as a parameter of this constructor.
  (let ((source #f)
        (prefix #f))
    (make-input id type label default source prefix other)))

(define-immutable-record-type <output>
  (make-output id type binding source other)
  output?
  (id output-id)
  (type output-type)
  (binding output-binding)
  (source output-source set-output-source)
  (other output-other))

(define* (output id #:key (type 'File) binding source (other '()))
  "Build and return an <output> object."
  (make-output id type binding source other))

(define %stdin
  (input "stdin" #:type 'File))

(define %stdout
  (output "stdout" #:type 'stdout))

(define (filter-alist alist)
  "Filter ALIST removing entries with #f as the value."
  (filter (match-lambda
            ((_ . #f) #f)
            (_ #t))
          alist))

(define-immutable-record-type <step>
  (make-step id run in out)
  step?
  (id step-id)
  (run step-run set-step-run)
  (in step-in set-step-in)
  (out step-out set-step-out))

(define (field-appender getter setter)
  (lambda (object element)
    (setter object (cons element (getter object)))))

(define (modify-step-run step proc)
  (set-step-run step (proc (step-run step))))

(define append-step-in
  (field-appender step-in set-step-in))

(define append-step-out
  (field-appender step-out set-step-out))

(define-immutable-record-type <command>
  (make-command additional-inputs outputs args stdin other)
  command?
  (additional-inputs command-additional-inputs)
  (outputs command-outputs set-command-outputs)
  (args command-args)
  (stdin command-stdin set-command-stdin)
  (other command-other))

(define append-command-outputs
  (field-appender command-outputs set-command-outputs))

(define command
  (lambda** (#:key stdin #:key* run (additional-inputs '()) (outputs '()) (other '()))
    (make-command additional-inputs outputs run stdin other)))

(define (input=? input1 input2)
  (eq? (input-id input1)
       (input-id input2)))

(define (invoke-command step-id command . args)
  (make-step step-id
             command
             (plist->alist args)
             (command-outputs command)))

(define* (make-workflow steps inputs outputs #:key (other '()))
  "Build a Workflow class CWL workflow."
  `((cwlVersion . ,%cwl-version)
    (class . Workflow)
    (requirements (SubworkflowFeatureRequirement))
    ,@other
    (inputs . ,(map (lambda (input)
                      `(,(input-id input)
                        ,@(filter-alist
                           `((type . ,(input-type input))
                             (label . ,(input-label input))
                             (default . ,(and (not (unspecified-default? (input-default input)))
                                              (input-default input)))))
                        ,@(input-other input)))
                    inputs))
    (outputs . ,(map (lambda (output)
                       `(,(output-id output)
                         (type . ,(match (output-type output)
                                    ('stdout 'File)
                                    (some-other-type some-other-type)))
                         (outputSource . ,(match (output-source output)
                                            ((? string? source) source)
                                            ((? input? input) (input-id input))))))
                     outputs))
    (steps . ,(map (lambda (step)
                     `(,(step-id step)
                       (in . ,(map (lambda (in)
                                     (match in
                                       ((id . (? string? source))
                                        in)
                                       ((id . (? input? input))
                                        (cons id (input-id input)))))
                                   (step-in step)))
                       (out . ,(list->vector (map output-id (step-out step))))
                       (run . ,(match (step-run step)
                                 ((? command? command)
                                  (command->cwl command))
                                 (tree tree)))))
                   steps))))

(define (output->cwl output)
  `(,(output-id output)
    ,@(filter identity
              (list (and (output-type output)
                         (cons 'type (output-type output)))
                    (and (output-binding output)
                         (cons 'outputBinding (output-binding output)))))
    ,@(output-other output)))

(define-immutable-record-type <cli-element>
  (make-cli-element argument position)
  cli-element?
  (argument cli-element-argument)
  (position cli-element-position))

(define (command->cwl command)
  (let ((elements
         ;; Add a position to all arguments, converting them to
         ;; <cli-element> objects.
         (map (lambda (arg position)
                (make-cli-element
                 ;; If duplicate input, convert it to an expression
                 ;; referring to the input.
                 (if (and (input? arg)
                          (not (= (list-index (lambda (x)
                                                (and (input? x)
                                                     (input=? arg x)))
                                              (command-args command))
                                  position)))
                     (string-append "$(inputs." (symbol->string (input-id arg)) ")")
                     arg)
                 position))
              (command-args command)
              (iota (length (command-args command))))))
    `((cwlVersion . ,%cwl-version)
      (class . CommandLineTool)
      ,@(command-other command)
      (arguments . ,(list->vector
                     ;; Put string arguments into the arguments array.
                     (filter-map (lambda (element)
                                   (and (string? (cli-element-argument element))
                                        `((position . ,(cli-element-position element))
                                          (valueFrom . ,(cli-element-argument element)))))
                                 elements)))
      (inputs . ,(append
                  ;; Put <input> arguments into the inputs array.
                  (filter-map (lambda (element)
                                (let ((input (cli-element-argument element)))
                                  (and (input? input)
                                       `(,(input-id input)
                                         ,@(filter-alist
                                            `((type . ,(input-type input))
                                              (label . ,(input-label input))
                                              (default . ,(and (not (unspecified-default? (input-default input)))
                                                               (input-default input)))
                                              (inputBinding . ,(filter-alist
                                                                `((position . ,(cli-element-position element))
                                                                  (prefix . ,(input-prefix input)))))))
                                         ,@(input-other input)))))
                              elements)
                  (map (lambda (input)
                         `(,(input-id input)
                           ,@(filter-alist
                              `((type . ,(input-type input))
                                (label . ,(input-label input))
                                (default . ,(and (not (unspecified-default? (input-default input)))
                                                 (input-default input)))))
                           ,@(input-other input)))
                       (command-additional-inputs command))
                  (let ((stdin (command-stdin command)))
                    (if stdin
                        (list `(,(input-id stdin)
                                (type . ,(input-type stdin))))
                        (list)))))
      (outputs . ,(map output->cwl (command-outputs command)))
      ,@(if (command-stdin command)
            `((stdin . ,(string-append "$(inputs."
                                       (symbol->string
                                        (input-id (command-stdin command)))
                                       ".path)")))
            '()))))

(define (write-cwl step file)
  (call-with-output-file file
    (cut scm->yaml (let ((run (step-run step)))
                     (if (command? run)
                         (command->cwl run)
                         run))
         <>)))

(define (workflow-steps x)
  (syntax-case x ()
    ((command (step-id) args ...)
     (cons #`(invoke-command
              step-id command
              #,@(append-map (lambda (pair)
                               (syntax-case pair ()
                                 ((key . (_ (id) _ ...))
                                  (list #'key (string-append
                                               (syntax->datum #'id)
                                               "/" (symbol->string
                                                    (keyword->symbol
                                                     (syntax->datum #'key))))))
                                 ((key . atom)
                                  (list #'key #'atom))))
                             (pairify #'(args ...))))
           (append-map workflow-steps #'(args ...))))
    (atom (list))))

(define-syntax workflow
  (lambda (x)
    (syntax-case x ()
      ((_ root)
       #`(make-workflow
          (list #,@(workflow-steps #'root))
          #,(syntax-case x ()
              ((_ (command (step-id) _ ...))
               #'(map (lambda (output)
                        (set-output-source
                         output (string-append step-id "/" (output-id output))))
                      (command-outputs command)))))))))