13558: Merge branch 'master' into wtsi-hgi-13558-debug-log-tag-req-id
[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":https://www.commonwl.org/user_guide 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. Using Composer
23
24 You can create new workflows in the browser using "Arvados Composer":{{site.baseurl}}/user/composer/composer.html
25
26 h2. Registering a workflow to use in Workbench
27
28 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.
29
30 <notextile>
31 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">arvados-cwl-runner --create-workflow bwa-mem.cwl</span>
32 arvados-cwl-runner 1.0.20160628195002, arvados-python-client 0.1.20160616015107, cwltool 1.0.20160629140624
33 2016-07-01 12:21:01 arvados.arv-run[15796] INFO: Upload local files: "bwa-mem.cwl"
34 2016-07-01 12:21:01 arvados.arv-run[15796] INFO: Uploaded to qr1hi-4zz18-7e0hedrmkuyoei3
35 2016-07-01 12:21:01 arvados.cwl-runner[15796] INFO: Created template qr1hi-p5p6p-rjleou1dwr167v5
36 qr1hi-p5p6p-rjleou1dwr167v5
37 </code></pre>
38 </notextile>
39
40 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:
41
42 <notextile>
43 <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>
44 arvados-cwl-runner 1.0.20160628195002, arvados-python-client 0.1.20160616015107, cwltool 1.0.20160629140624
45 2016-07-01 14:09:50 arvados.arv-run[3730] INFO: Upload local files: "bwa-mem.cwl"
46 2016-07-01 14:09:50 arvados.arv-run[3730] INFO: Uploaded to qr1hi-4zz18-0f91qkovk4ml18o
47 2016-07-01 14:09:50 arvados.cwl-runner[3730] INFO: Created template qr1hi-p5p6p-0deqe6nuuyqns2i
48 qr1hi-p5p6p-zuniv58hn8d0qd8
49 </code></pre>
50 </notextile>
51
52 h3. Running registered workflows at the command line
53
54 You can run a registered workflow at the command line by its UUID:
55
56 <notextile>
57 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">arvados-cwl-runner qr1hi-p5p6p-zuniv58hn8d0qd8 --help</span>
58 /home/peter/work/scripts/venv/bin/arvados-cwl-runner 0d62edcb9d25bf4dcdb20d8872ea7b438e12fc59 1.0.20161209192028, arvados-python-client 0.1.20161212125425, cwltool 1.0.20161207161158
59 Resolved 'qr1hi-p5p6p-zuniv58hn8d0qd8' to 'keep:655c6cd07550151b210961ed1d3852cf+57/bwa-mem.cwl'
60 usage: qr1hi-p5p6p-zuniv58hn8d0qd8 [-h] [--PL PL] --group_id GROUP_ID
61                                    --read_p1 READ_P1 [--read_p2 READ_P2]
62                                    [--reference REFERENCE] --sample_id
63                                    SAMPLE_ID
64                                    [job_order]
65
66 positional arguments:
67   job_order             Job input json file
68
69 optional arguments:
70   -h, --help            show this help message and exit
71   --PL PL
72   --group_id GROUP_ID
73   --read_p1 READ_P1     The reads, in fastq format.
74   --read_p2 READ_P2     For mate paired reads, the second file (optional).
75   --reference REFERENCE
76                         The index files produced by `bwa index`
77   --sample_id SAMPLE_ID
78 </code></pre>
79 </notextile>
80
81 h2. Using cwltool
82
83 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.
84
85 <notextile>
86 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">arv-get 2463fa9efeb75e099685528b3b9071e0+438/ .</span>
87 156 MiB / 156 MiB 100.0%
88 ~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">arv-get ae480c5099b81e17267b7445e35b4bc7+180/ .</span>
89 23 MiB / 23 MiB 100.0%
90 ~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">cwltool bwa-mem-input.yml bwa-mem-input-local.yml</span>
91 cwltool 1.0.20160629140624
92 [job bwa-mem.cwl] /home/example/arvados/doc/user/cwl/bwa-mem$ docker \
93     run \
94     -i \
95     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.ann:/var/lib/cwl/job979368791_bwa-mem/19.fasta.ann:ro \
96     --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 \
97     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.sa:/var/lib/cwl/job979368791_bwa-mem/19.fasta.sa:ro \
98     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.amb:/var/lib/cwl/job979368791_bwa-mem/19.fasta.amb:ro \
99     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.pac:/var/lib/cwl/job979368791_bwa-mem/19.fasta.pac:ro \
100     --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 \
101     --volume=/home/example/arvados/doc/user/cwl/bwa-mem/19.fasta.bwt:/var/lib/cwl/job979368791_bwa-mem/19.fasta.bwt:ro \
102     --volume=/home/example/arvados/doc/user/cwl/bwa-mem:/var/spool/cwl:rw \
103     --volume=/tmp/tmpgzyou9:/tmp:rw \
104     --workdir=/var/spool/cwl \
105     --read-only=true \
106     --log-driver=none \
107     --user=1001 \
108     --rm \
109     --env=TMPDIR=/tmp \
110     --env=HOME=/var/spool/cwl \
111     biodckr/bwa \
112     bwa \
113     mem \
114     -t \
115     1 \
116     -R \
117     '@RG        ID:arvados_tutorial     PL:illumina     SM:HWI-ST1027_129' \
118     /var/lib/cwl/job979368791_bwa-mem/19.fasta \
119     /var/lib/cwl/job979368791_bwa-mem/HWI-ST1027_129_D0THKACXX.1_1.fastq \
120     /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
121 [M::bwa_idx_load_from_disk] read 0 ALT contigs
122 [M::process] read 100000 sequences (10000000 bp)...
123 [M::mem_pestat] # candidate unique pairs for (FF, FR, RF, RR): (0, 4745, 1, 0)
124 [M::mem_pestat] skip orientation FF as there are not enough pairs
125 [M::mem_pestat] analyzing insert size distribution for orientation FR...
126 [M::mem_pestat] (25, 50, 75) percentile: (154, 181, 214)
127 [M::mem_pestat] low and high boundaries for computing mean and std.dev: (34, 334)
128 [M::mem_pestat] mean and std.dev: (185.63, 44.88)
129 [M::mem_pestat] low and high boundaries for proper pairs: (1, 394)
130 [M::mem_pestat] skip orientation RF as there are not enough pairs
131 [M::mem_pestat] skip orientation RR as there are not enough pairs
132 [M::mem_process_seqs] Processed 100000 reads in 9.848 CPU sec, 9.864 real sec
133 [main] Version: 0.7.12-r1039
134 [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
135 [main] Real time: 10.061 sec; CPU: 10.032 sec
136 Final process status is success
137 {
138     "aligned_sam": {
139         "size": 30738959,
140         "path": "/home/example/arvados/doc/user/cwl/bwa-mem/HWI-ST1027_129_D0THKACXX.1_1.sam",
141         "checksum": "sha1$0c668cca45fef02397bb5302880526d300ee4dac",
142         "class": "File"
143     }
144 }
145 </code></pre>
146 </notextile>
147
148 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.
149
150 h2. Making workflows directly executable
151
152 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:
153
154 <notextile>
155 <pre><code>#!/usr/bin/env cwl-runner
156 </code></pre>
157 </notextile>
158
159 <notextile>
160 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">./bwa-mem.cwl bwa-mem-input.yml</span>
161 arvados-cwl-runner 1.0.20160628195002, arvados-python-client 0.1.20160616015107, cwltool 1.0.20160629140624
162 2016-06-30 14:56:36 arvados.arv-run[27002] INFO: Upload local files: "bwa-mem.cwl"
163 2016-06-30 14:56:36 arvados.arv-run[27002] INFO: Uploaded to qr1hi-4zz18-h7ljh5u76760ww2
164 2016-06-30 14:56:40 arvados.cwl-runner[27002] INFO: Submitted job qr1hi-8i9sb-fm2n3b1w0l6bskg
165 2016-06-30 14:56:41 arvados.cwl-runner[27002] INFO: Job bwa-mem.cwl (qr1hi-8i9sb-fm2n3b1w0l6bskg) is Running
166 2016-06-30 14:57:12 arvados.cwl-runner[27002] INFO: Job bwa-mem.cwl (qr1hi-8i9sb-fm2n3b1w0l6bskg) is Complete
167 2016-06-30 14:57:12 arvados.cwl-runner[27002] INFO: Overall process status is success
168 {
169     "aligned_sam": {
170         "path": "keep:54325254b226664960de07b3b9482349+154/HWI-ST1027_129_D0THKACXX.1_1.sam",
171         "checksum": "sha1$0dc46a3126d0b5d4ce213b5f0e86e2d05a54755a",
172         "class": "File",
173         "size": 30738986
174     }
175 }
176 </code></pre>
177 </notextile>
178
179 You can even make an input file directly executable the same way with the following two lines at the top:
180
181 <notextile>
182 <pre><code>#!/usr/bin/env cwl-runner
183 cwl:tool: <span class="userinput">bwa-mem.cwl</span>
184 </code></pre>
185 </notextile>
186
187 <notextile>
188 <pre><code>~/arvados/doc/user/cwl/bwa-mem$ <span class="userinput">./bwa-mem-input.yml</span>
189 arvados-cwl-runner 1.0.20160628195002, arvados-python-client 0.1.20160616015107, cwltool 1.0.20160629140624
190 2016-06-30 14:56:36 arvados.arv-run[27002] INFO: Upload local files: "bwa-mem.cwl"
191 2016-06-30 14:56:36 arvados.arv-run[27002] INFO: Uploaded to qr1hi-4zz18-h7ljh5u76760ww2
192 2016-06-30 14:56:40 arvados.cwl-runner[27002] INFO: Submitted job qr1hi-8i9sb-fm2n3b1w0l6bskg
193 2016-06-30 14:56:41 arvados.cwl-runner[27002] INFO: Job bwa-mem.cwl (qr1hi-8i9sb-fm2n3b1w0l6bskg) is Running
194 2016-06-30 14:57:12 arvados.cwl-runner[27002] INFO: Job bwa-mem.cwl (qr1hi-8i9sb-fm2n3b1w0l6bskg) is Complete
195 2016-06-30 14:57:12 arvados.cwl-runner[27002] INFO: Overall process status is success
196 {
197     "aligned_sam": {
198         "path": "keep:54325254b226664960de07b3b9482349+154/HWI-ST1027_129_D0THKACXX.1_1.sam",
199         "checksum": "sha1$0dc46a3126d0b5d4ce213b5f0e86e2d05a54755a",
200         "class": "File",
201         "size": 30738986
202     }
203 }
204 </code></pre>
205 </notextile>