Merge branch '8784-dir-listings'
[arvados.git] / doc / user / tutorials / writing-cwl-workflow.html.textile.liquid
1 ---
2 layout: default
3 navsection: userguide
4 title: "Writing a CWL workflow"
5 ...
6 {% comment %}
7 Copyright (C) The Arvados Authors. All rights reserved.
8
9 SPDX-License-Identifier: CC-BY-SA-3.0
10 {% endcomment %}
11
12 {% include 'what_is_cwl' %}
13
14 {% include 'tutorial_expectations' %}
15
16 h2. Developing workflows
17
18 For an introduction and and detailed documentation about writing CWL, see the "CWL User Guide":http://commonwl.org/v1.0/UserGuide.html and the "CWL Specification":http://commonwl.org/v1.0 .
19
20 See "Best Practices for writing CWL":{{site.baseurl}}/user/cwl/cwl-style.html and "Arvados CWL Extensions":{{site.baseurl}}/user/cwl/cwl-extensions.html for additional information about using CWL on Arvados.
21
22 h2. Registering a workflow to use in Workbench
23
24 Use @--create-workflow@ to register a CWL workflow with Arvados.  This enables you to share workflows with other Arvados users, and run them by clicking the <span class="btn btn-sm btn-primary"><i class="fa fa-fw fa-gear"></i> Run a process...</span> button on the Workbench Dashboard and on the command line by UUID.
25
26 <notextile>
27 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">arvados-cwl-runner --create-workflow bwa-mem.cwl</span>
28 arvados-cwl-runner 1.0.20160628195002, arvados-python-client 0.1.20160616015107, cwltool 1.0.20160629140624
29 2016-07-01 12:21:01 arvados.arv-run[15796] INFO: Upload local files: "bwa-mem.cwl"
30 2016-07-01 12:21:01 arvados.arv-run[15796] INFO: Uploaded to qr1hi-4zz18-7e0hedrmkuyoei3
31 2016-07-01 12:21:01 arvados.cwl-runner[15796] INFO: Created template qr1hi-p5p6p-rjleou1dwr167v5
32 qr1hi-p5p6p-rjleou1dwr167v5
33 </code></pre>
34 </notextile>
35
36 You can provide a partial input file to set default values for the workflow input parameters.  You can also use the @--name@ option to set the name of the workflow:
37
38 <notextile>
39 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">arvados-cwl-runner --name "My workflow with defaults" --create-workflow bwa-mem.cwl bwa-mem-template.yml</span>
40 arvados-cwl-runner 1.0.20160628195002, arvados-python-client 0.1.20160616015107, cwltool 1.0.20160629140624
41 2016-07-01 14:09:50 arvados.arv-run[3730] INFO: Upload local files: "bwa-mem.cwl"
42 2016-07-01 14:09:50 arvados.arv-run[3730] INFO: Uploaded to qr1hi-4zz18-0f91qkovk4ml18o
43 2016-07-01 14:09:50 arvados.cwl-runner[3730] INFO: Created template qr1hi-p5p6p-0deqe6nuuyqns2i
44 qr1hi-p5p6p-zuniv58hn8d0qd8
45 </code></pre>
46 </notextile>
47
48 h3. Running registered workflows at the command line
49
50 You can run a registered workflow at the command line by its UUID:
51
52 <notextile>
53 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">arvados-cwl-runner qr1hi-p5p6p-zuniv58hn8d0qd8 --help</span>
54 /home/peter/work/scripts/venv/bin/arvados-cwl-runner 0d62edcb9d25bf4dcdb20d8872ea7b438e12fc59 1.0.20161209192028, arvados-python-client 0.1.20161212125425, cwltool 1.0.20161207161158
55 Resolved 'qr1hi-p5p6p-zuniv58hn8d0qd8' to 'keep:655c6cd07550151b210961ed1d3852cf+57/bwa-mem.cwl'
56 usage: qr1hi-p5p6p-zuniv58hn8d0qd8 [-h] [--PL PL] --group_id GROUP_ID
57                                    --read_p1 READ_P1 [--read_p2 READ_P2]
58                                    [--reference REFERENCE] --sample_id
59                                    SAMPLE_ID
60                                    [job_order]
61
62 positional arguments:
63   job_order             Job input json file
64
65 optional arguments:
66   -h, --help            show this help message and exit
67   --PL PL
68   --group_id GROUP_ID
69   --read_p1 READ_P1     The reads, in fastq format.
70   --read_p2 READ_P2     For mate paired reads, the second file (optional).
71   --reference REFERENCE
72                         The index files produced by `bwa index`
73   --sample_id SAMPLE_ID
74 </code></pre>
75 </notextile>
76
77 h2. Using cwltool
78
79 When developing a workflow, it is often helpful to run it on the local host to avoid the overhead of submitting to the cluster.  To execute a workflow only on the local host (without submitting jobs to an Arvados cluster) you can use the @cwltool@ command.  Note that when using @cwltool@ you must have the input data accessible on the local file system using either @arv-mount@ or @arv-get@ to fetch the data from Keep.
80
81 <notextile>
82 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">arv-get 2463fa9efeb75e099685528b3b9071e0+438/ .</span>
83 156 MiB / 156 MiB 100.0%
84 ~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">arv-get ae480c5099b81e17267b7445e35b4bc7+180/ .</span>
85 23 MiB / 23 MiB 100.0%
86 ~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">cwltool bwa-mem-input.yml bwa-mem-input-local.yml</span>
87 cwltool 1.0.20160629140624
88 [job bwa-mem.cwl] /home/example/arvados/doc/user/cwl/bwa-mem$ docker \
89     run \
90     -i \
91     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.ann:/var/lib/cwl/job979368791_bwa-mem/19.fasta.ann:ro \
92     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/HWI-ST1027_129_D0THKACXX.1_1.fastq:/var/lib/cwl/job979368791_bwa-mem/HWI-ST1027_129_D0THKACXX.1_1.fastq:ro \
93     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.sa:/var/lib/cwl/job979368791_bwa-mem/19.fasta.sa:ro \
94     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.amb:/var/lib/cwl/job979368791_bwa-mem/19.fasta.amb:ro \
95     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.pac:/var/lib/cwl/job979368791_bwa-mem/19.fasta.pac:ro \
96     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/HWI-ST1027_129_D0THKACXX.1_2.fastq:/var/lib/cwl/job979368791_bwa-mem/HWI-ST1027_129_D0THKACXX.1_2.fastq:ro \
97     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.bwt:/var/lib/cwl/job979368791_bwa-mem/19.fasta.bwt:ro \
98     --volume=/home/example/arvados/doc/user/cwl/bwa-mem:/var/spool/cwl:rw \
99     --volume=/tmp/tmpgzyou9:/tmp:rw \
100     --workdir=/var/spool/cwl \
101     --read-only=true \
102     --log-driver=none \
103     --user=1001 \
104     --rm \
105     --env=TMPDIR=/tmp \
106     --env=HOME=/var/spool/cwl \
107     biodckr/bwa \
108     bwa \
109     mem \
110     -t \
111     1 \
112     -R \
113     '@RG        ID:arvados_tutorial     PL:illumina     SM:HWI-ST1027_129' \
114     /var/lib/cwl/job979368791_bwa-mem/19.fasta \
115     /var/lib/cwl/job979368791_bwa-mem/HWI-ST1027_129_D0THKACXX.1_1.fastq \
116     /var/lib/cwl/job979368791_bwa-mem/HWI-ST1027_129_D0THKACXX.1_2.fastq > /home/example/arvados/doc/user/cwl/bwa-mem/HWI-ST1027_129_D0THKACXX.1_1.sam
117 [M::bwa_idx_load_from_disk] read 0 ALT contigs
118 [M::process] read 100000 sequences (10000000 bp)...
119 [M::mem_pestat] # candidate unique pairs for (FF, FR, RF, RR): (0, 4745, 1, 0)
120 [M::mem_pestat] skip orientation FF as there are not enough pairs
121 [M::mem_pestat] analyzing insert size distribution for orientation FR...
122 [M::mem_pestat] (25, 50, 75) percentile: (154, 181, 214)
123 [M::mem_pestat] low and high boundaries for computing mean and std.dev: (34, 334)
124 [M::mem_pestat] mean and std.dev: (185.63, 44.88)
125 [M::mem_pestat] low and high boundaries for proper pairs: (1, 394)
126 [M::mem_pestat] skip orientation RF as there are not enough pairs
127 [M::mem_pestat] skip orientation RR as there are not enough pairs
128 [M::mem_process_seqs] Processed 100000 reads in 9.848 CPU sec, 9.864 real sec
129 [main] Version: 0.7.12-r1039
130 [main] CMD: bwa mem -t 1 -R @RG ID:arvados_tutorial     PL:illumina     SM:HWI-ST1027_129 /var/lib/cwl/job979368791_bwa-mem/19.fasta /var/lib/cwl/job979368791_bwa-mem/HWI-ST1027_129_D0THKACXX.1_1.fastq /var/lib/cwl/job979368791_bwa-mem/HWI-ST1027_129_D0THKACXX.1_2.fastq
131 [main] Real time: 10.061 sec; CPU: 10.032 sec
132 Final process status is success
133 {
134     "aligned_sam": {
135         "size": 30738959,
136         "path": "/home/example/arvados/doc/user/cwl/bwa-mem/HWI-ST1027_129_D0THKACXX.1_1.sam",
137         "checksum": "sha1$0c668cca45fef02397bb5302880526d300ee4dac",
138         "class": "File"
139     }
140 }
141 </code></pre>
142 </notextile>
143
144 If you get the error @JavascriptException: Long-running script killed after 20 seconds.@ this may be due to the Dockerized Node.js engine taking too long to start.  You may address this by installing Node.js locally (run @apt-get install nodejs@ on Debian or Ubuntu) or by specifying a longer timeout with the @--eval-timeout@ option.  For example, run the workflow with @cwltool --eval-timeout=40@ for a 40-second timeout.
145
146 h2. Making workflows directly executable
147
148 You can make a workflow file directly executable (@cwl-runner@ should be an alias to @arvados-cwl-runner@) by adding the following line to the top of the file:
149
150 <notextile>
151 <pre><code>#!/usr/bin/env cwl-runner
152 </code></pre>
153 </notextile>
154
155 <notextile>
156 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">./bwa-mem.cwl bwa-mem-input.yml</span>
157 arvados-cwl-runner 1.0.20160628195002, arvados-python-client 0.1.20160616015107, cwltool 1.0.20160629140624
158 2016-06-30 14:56:36 arvados.arv-run[27002] INFO: Upload local files: "bwa-mem.cwl"
159 2016-06-30 14:56:36 arvados.arv-run[27002] INFO: Uploaded to qr1hi-4zz18-h7ljh5u76760ww2
160 2016-06-30 14:56:40 arvados.cwl-runner[27002] INFO: Submitted job qr1hi-8i9sb-fm2n3b1w0l6bskg
161 2016-06-30 14:56:41 arvados.cwl-runner[27002] INFO: Job bwa-mem.cwl (qr1hi-8i9sb-fm2n3b1w0l6bskg) is Running
162 2016-06-30 14:57:12 arvados.cwl-runner[27002] INFO: Job bwa-mem.cwl (qr1hi-8i9sb-fm2n3b1w0l6bskg) is Complete
163 2016-06-30 14:57:12 arvados.cwl-runner[27002] INFO: Overall process status is success
164 {
165     "aligned_sam": {
166         "path": "keep:54325254b226664960de07b3b9482349+154/HWI-ST1027_129_D0THKACXX.1_1.sam",
167         "checksum": "sha1$0dc46a3126d0b5d4ce213b5f0e86e2d05a54755a",
168         "class": "File",
169         "size": 30738986
170     }
171 }
172 </code></pre>
173 </notextile>
174
175 You can even make an input file directly executable the same way with the following two lines at the top:
176
177 <notextile>
178 <pre><code>#!/usr/bin/env cwl-runner
179 cwl:tool: <span class="userinput">bwa-mem.cwl</span>
180 </code></pre>
181 </notextile>
182
183 <notextile>
184 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">./bwa-mem-input.yml</span>
185 arvados-cwl-runner 1.0.20160628195002, arvados-python-client 0.1.20160616015107, cwltool 1.0.20160629140624
186 2016-06-30 14:56:36 arvados.arv-run[27002] INFO: Upload local files: "bwa-mem.cwl"
187 2016-06-30 14:56:36 arvados.arv-run[27002] INFO: Uploaded to qr1hi-4zz18-h7ljh5u76760ww2
188 2016-06-30 14:56:40 arvados.cwl-runner[27002] INFO: Submitted job qr1hi-8i9sb-fm2n3b1w0l6bskg
189 2016-06-30 14:56:41 arvados.cwl-runner[27002] INFO: Job bwa-mem.cwl (qr1hi-8i9sb-fm2n3b1w0l6bskg) is Running
190 2016-06-30 14:57:12 arvados.cwl-runner[27002] INFO: Job bwa-mem.cwl (qr1hi-8i9sb-fm2n3b1w0l6bskg) is Complete
191 2016-06-30 14:57:12 arvados.cwl-runner[27002] INFO: Overall process status is success
192 {
193     "aligned_sam": {
194         "path": "keep:54325254b226664960de07b3b9482349+154/HWI-ST1027_129_D0THKACXX.1_1.sam",
195         "checksum": "sha1$0dc46a3126d0b5d4ce213b5f0e86e2d05a54755a",
196         "class": "File",
197         "size": 30738986
198     }
199 }
200 </code></pre>
201 </notextile>