Project

General

Profile

Port a Pipeline » History » Version 11

Nancy Ouyang, 04/14/2015 02:34 PM

1 7 Tom Clegg
{{>toc}}
2
3 1 Nancy Ouyang
h1. Port a Pipeline
4
5
Like any other tool, Arvados requires time to learn. Thus, we don't encourage using Arvados for initial development of analysis pipelines or exploratory research on small subsets of data, when each quick-and-dirty iteration takes minutes on a single machine. But for any computationally-intense work, Arvados offers a lot of benefits.
6
7 8 Nancy Ouyang
Okay, cool, provenance, reproducibility, easily scaling to gigabytes of data and mucho RAM, evaluating existing pipelines like lobSTR quickly.
8 1 Nancy Ouyang
9 8 Nancy Ouyang
But what about if you want to these benefits when running your own pipelines?
10
In other words, how do you **port a pipeline** to Arvados?
11 1 Nancy Ouyang
12
h2. 1. Quick Way
13
14
First, do you just want to parallelize a single bash script?
15
16 9 Nancy Ouyang
Check if you can use @arv-run@. Take this arv-run example, which searches multiple FASTQ files in parallel, and saves the results to Keep through shell redirection:
17
18
    $ arv-run grep -H -n GCTACCAAGTTT \< *.fa \> output.txt
19 1 Nancy Ouyang
20
h3. 1.1 Install arv-run
21
22
See: http://doc.arvados.org/sdk/python/sdk-python.html and http://doc.arvados.org/user/reference/api-tokens.html, or in short below:
23
<pre>
24
$ sudo apt-get install python-pip python-dev libattr1-dev libfuse-dev pkg-config python-yaml
25
$ sudo pip install --pre arvados-python-client
26
</pre>
27
(Lost? See http://doc.arvados.org/sdk/python/sdk-python.html )
28
29
If you try to use arv run right now, it will complain about some settings your missing. To fix that,
30
31
# Go to su92l.arvadosapi.com
32
# Login with any Google account (you may need to click login a few times if you hit multiple redirects from Google)
33
# Click in the upper right on your account name -> Manage Account
34
# Optional: While you're here, click "send request for shell access", since that will give you shell access to a VM with all of the Arvados tools pre-installed.
35
# Copy the lines of text, something like
36
<pre>
37
HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
38
export ARVADOS_API_TOKEN=sekritlongthing
39
export ARVADOS_API_HOST=su92l.arvadosapi.com
40
unset ARVADOS_API_HOST_INSECURE
41
</pre>
42
# If you want this to persist across reboot, add this to ~/.bashrc or your ~/.bash_profile
43
44
(Lost? See http://doc.arvados.org/user/reference/api-tokens.html )
45
46
h3. 1.2 Submit job to Arvados
47
48
First, check: Does your command work locally?
49
50
    $ grep -H -n GCTACCAAGTTT *.fa
51
52
If so, then submit it to arvados using @arv run@
53
54
    $ arv-run grep -H -n GCTACCAAGTTT \< *.fa \> output.txt
55
56
* This bash command stores the results as output.txt
57
* Note that due to the particulars of grep, Arvados will report this pipeline as failed if grep does not find anything, and no output will appear on Arvados
58
59
Your dataset is uploaded to Arvados if it wasn't on there already (which may take a while if it's a large dataset), your "grep" job is submitted to run on the Arvados cluster, and you get the results in a few minutes (stored inside output.txt in Arvados). If you go to Workbench at su92l, you will see the pipeline running. It may take a few minutes for Arvados to spool up a node, provision it, and run your job. The robots are working hard for you, grab a cup of coffee.
60
61
(Lost? See http://doc.arvados.org/user/topics/arv-run.html )
62
63
h3. 1.3 However
64
65 10 Nancy Ouyang
If your pipeline looks more like
66
67 11 Nancy Ouyang
!{width: 50%}https://arvados.org/attachments/download/428/provenance_graph_full.png!:https://arvados.org/attachments/download/428/provenance_graph_full.png
68 10 Nancy Ouyang
69
@arv-run@ is not powerful enough. Here we gooooo.
70 1 Nancy Ouyang
71
h2. 2. In Short
72
73
**Estimated reading time: 1 hour.**
74
75
You must write a **pipeline template** that describes your pipeline to Arvados.
76
77
h3. 2.1 VM (Virtual Machine) Access
78
79
Note: You'll need the Arvados set of command-line tools to follow along. The easiest way to get started is to get access to a Virtual Machine (VM) with all the tools pre-installed.
80
81
# Go to su92l.arvadosapi.com
82
# Login with google account (you may need to click login a few times, our redirects are not working well)
83
# Click in the upper right on your account name -> Manage Account4. Hit the "Request shell access" button under Manage Account in workbench.
84
85
h3. 2.2 Pipeline Template Example
86
87
Here is what a simple pipeline template looks like, where the output of program A is provided as input to program B. We'll explain what it all means shortly, but first, don't worry -- you'll never be creating a pipeline template from scratch. You'll always copy and modify an existing boilerplate one (yes, a template for the pipeline template! :])
88
89
90
    **pipelinetemplate.json**
91
    {
92
    "name": "Tiny Bash Script",
93
      "components": {
94
        "Create Two Files": {
95
          "script": "run-command",
96
          "script_version": "master",
97
          "repository": "nancy",
98
          "script_parameters": {
99
            "command": [
100
              "$(job.srcdir)/crunch_scripts/createtwofiles.sh"
101
            ]
102
          ,
103
          "runtime_constraints": {
104
            "docker_image": "nancy/cgatools-wormtable"
105
        ,
106
        "Merge Files": {
107
          "script": "run-command",
108
          "script_version": "master",
109
          "repository": "nancy",
110
          "script_parameters": {
111
            "command": [
112
              "$(job.srcdir)/crunch_scripts/mergefiles.sh",
113
              "$(input)"
114
            ],
115
            "input": {
116
              "output_of": "Create Two Files"
117
          ,
118
          "runtime_constraints": {
119
            "docker_image": "nancy/cgatools-wormtable"
120
          
121
h2. 3. simple and sweet port-a-pipeline example
122
123
Okay, let's dig into what's going on.
124
125
h3. 3.1 the setup
126
127
We'll port an artificially simple pipeline which involves just two short bash scripts, described as "A" and "B" below:
128
129
**Script A. Create two files**
130
Input: nothing
131
Output: two files (out1.txt and out2.txt)
132
133
**Script B. Merge two files into a single file**
134
Input: output of step A
135
Output: a single file (output.txt)
136
137
Visually, this looks like [!graph image] (ignore the long strings of gibberish in the rectangles for now).
138
139
Here's what we've explained so far in the pipeline template:
140
141
142
    **pipelinetemplate.json**
143
    {
144
    **"name": "Tiny Bash Script",**
145
      "components": {
146
       **"Create Two Files": {**
147
          "script": "run-command",
148
          "script_version": "master",
149
          "repository": "arvados",
150
          "script_parameters": {
151
            "command": [
152
              "$(job.srcdir)/crunch_scripts/ *createtwofiles.sh* "
153
            ]
154
          ,
155
          "runtime_constraints": {
156
            "docker_image": "nancy/cgatools-wormtable"
157
        ,
158
        **"Merge Files": {**
159
          "script": "run-command",
160
          "script_version": "master",
161
          "repository": "arvados",
162
          "script_parameters": {
163
            "command": [
164
              "$(job.srcdir)/crunch_scripts/ *mergefiles.sh* ",
165
              "$(input)"
166
            ],
167
           **"input": {**
168
              **"output_of": "Create Two Files"**
169
          ,
170
          "runtime_constraints": {
171
            "docker_image": "nancy/cgatools-wormtable"
172
173
174
h3. **3.2 arv-what?**
175
176
Before we go further, let's take a quick step back. Arvados consists of two parts
177
178
**Part 1. Keep** - I have all your files in the cloud!
179
180
You can access your files through your browser, using **Workbench**, or using the Arvados command line (CLI) tools (link: http://doc.arvados.org/sdk/cli/index.html ).
181
182
Visually, this looks like
183
[!image 1: workbench]
184
[!image 2: shell session, arv mount]
185
186
**Part 2. Crunch** - I run all your scripts in the cloud!
187
188
Crunch both dispatches jobs and provides version control for your pipelines.
189
190
You describe your workflow to Crunch using **pipeline templates**. Pipeline templates describe a pipeline ("workflow"), the type of inputs each step in the pipeline requires, and . You provide a high-level description of how data flows through the pipeline—for example, the outputs of programs A and B are provided as input to program C—and let Crunch take care of the details of starting the individual programs at the right time with the inputs you specified.
191
192
[!image 2: complex pipeline]
193
194
Once you save a pipeline template in Arvados, you run it by creating a pipeline instance that lists the specific inputs you’d like to use. The pipeline’s final output(s) will be saved in a project you specify.
195
196
Concretely, a pipeline template describes
197
198
* **data inputs** - specified as Keep content addresses
199
* **job scripts** - stored in a Git version control repository and referenced by a commit hash
200
* **parameters** - which, along with the data inputs, can have default values or can be filled in later when the pipeline is actually run
201
* **the execution environment** - stored in Docker images and referenced by Docker image name
202
203
**What is Docker**? Docker allows Arvados to replicate the execution environment your tools need. You install whatever bioinformatics tools (bwa-mem, vcftools, etc.) you are using inside a Docker image, upload it to Arvados, and Arvados will recreate your environment for computers in the cloud.
204
205
**Protip:** Install stable external tools in Docker. Put your own scripts in a Git repository. This is because each docker image is about 500 GB, so each new docker image takes a while to upload (30 minutes) if you are not using Arvados on a local cluster. In the future, we hope to use small diff files describing just the changes made to Docker image instead of the full Docker image. [Last updated 19 Feb 2015]
206
207
h3. 3.3 In More Detail
208
209
Alright, let's put that all together.
210
211
    **pipelinetemplate.json**
212
    {
213
    "name": "Tiny Bash Script",
214
      "components": {
215
        "Create Two Files": {
216
          "script": "run-command",
217
          "script_version": "master",
218
          "repository": "nancy",
219
          "script_parameters": {
220
            "command": [
221
              "$(job.srcdir)/crunch_scripts/createtwofiles.sh" **#[1]**
222
            ]
223
          ,
224
          "runtime_constraints": {
225
            "docker_image": "nancy/cgatools-wormtable"
226
        ,
227
        "Merge Files": {
228
          "script": "run-command",
229
          "script_version": "master",
230
          "repository": "nancy",
231
          "script_parameters": {
232
            "command": [
233
              "$(job.srcdir)/crunch_scripts/mergefiles.sh", **#[2]**
234
              "$(input)"
235
            ],
236
            "input": {
237
              "output_of": "Create Two Files" **#[3]**
238
          ,
239
          "runtime_constraints": {
240
            "docker_image": "nancy/cgatools-wormtable"    
241
    
242
**Explanation**
243
    
244
[1] **$(job.srcdir)** references the git repository "in the cloud". Even though **run-command** is in nancy/crunch_scripts/ and is "magically found" by Arvados, INSIDE run-command you can't reference other files in the same repo as run-command without this magic variable.
245
246
Any output files as a result of this run-command will be automagically stored to keep as an auto-named collection (which you can think of as a folder for now).
247
248
[2] Okay, so how does the next script know where to find the output of the previous job? run-command will keep track of the collections it's created, so we can feed that in as an argument to our next script. In this "command" section under "run-command", you can think of the commas as spaces. Thus, what this line is saying is "run mergefile.sh on the previous input", or
249
250
  $ mergefiles.sh [directory with output of previous command]
251
252
[3] Here we set the variable "input" to point to the directory with the output of the previous command "Create Two Files".
253
254
(Lost? Try the hands-on example in the next section, or read more detailed documentation on the Arvados website: 
255
256
* http://doc.arvados.org/user/tutorials/running-external-program.html
257
* http://doc.arvados.org/user/topics/run-command.html
258
* http://doc.arvados.org/api/schema/PipelineTemplate.html )
259
260
h3. 3.4 All hands on deck!
261
262
Okay, now that we know the rough shape of what's going on, let's get our hands dirty.
263
264 2 Nancy Ouyang
*From your local machine, login to Arvados virtual machine*
265 1 Nancy Ouyang
266 4 Nancy Ouyang
Single step:
267 1 Nancy Ouyang
268 4 Nancy Ouyang
  nrw@ *@nrw-local@* $ ssh nancy@lightning-dev4.shell.arvados
269
270 1 Nancy Ouyang
(Lost? See "SSH access to machine with Arvados commandline tools installed" http://doc.arvados.org/user/getting_started/ssh-access-unix.html )
271
272
**In VM, create pipeline template**
273
274
A few steps:
275
276
  nancy@ *@lightning-dev4.su92l@* :~$ arv create pipeline_template
277
Created object qr1hi-p5p6p-3p6uweo7omeq9e7
278
$ arv edit qr1hi-p5p6p-3p6uweo7omeq9e7 #Create the pipeline template as described above! [[Todo: concrete thing]]
279
280
(Lost? See "Writing a pipeline template" http://doc.arvados.org/user/tutorials/running-external-program.html )
281
282
*In VM, set up git repository with run_command and our scripts*
283
284 2 Nancy Ouyang
A few steps: 
285 1 Nancy Ouyang
286 2 Nancy Ouyang
  $ mkdir @~@/projects
287
$ cd @~@/projects
288
~/projects $ git clone git@git.qr1hi.arvadosapi.com:nancy.git 
289 1 Nancy Ouyang
290 2 Nancy Ouyang
(Lost? Find your own git URL by going to https://workbench.su92l.arvadosapi.com/manage_account )
291 1 Nancy Ouyang
292 2 Nancy Ouyang
    ⤷Copy run_command & its dependencies into this crunch_scripts
293
  $ git clone https://github.com/curoverse/arvados.git 
294 1 Nancy Ouyang
295 2 Nancy Ouyang
(Lost? Visit https://github.com/curoverse/arvados )
296
297
  @  @$ cd ./nancy
298
  *@~/projects/nancy@* $ mkdir crunch_scripts
299
  *@~/projects/nancy@* $ cd crunch_scripts
300
  *@~/projects/nancy/crunch_scripts@* $ cp @~@/projects/arvados/crunch_scripts/run_command . #trailing dot!
301
  ~/projects/nancy/crunch_scripts$ cp ~/projects/arvados/crunch_scripts/crunchutil . #trailing dot!
302
303
  @  @$ cd ~/projects/nancy/crunch_scripts
304
305
  @  @$ vi createtwofiles.sh
306
    ⤷ $cat createtwofiles.sh
307
    #!/bin/bash
308
    echo "Hello " > out1.txt
309
    echo "Arvados!" > out2.txt
310
311
  @  @$ vi mergefiles.sh
312 1 Nancy Ouyang
    ⤷$cat mergefiles.sh
313 5 Nancy Ouyang
      #!/bin/bash *#[1]*
314
      PREVOUTDIR=$1 *#[2]*
315
      echo $TASK_KEEPMOUNT/by_id/$PREVOUTDIR *#[3]*
316 1 Nancy Ouyang
      cat $TASK_KEEPMOUNT/by_id/$PREVOUTDIR/*.txt > output.txt
317
    
318 5 Nancy Ouyang
⤷ *Explanations*
319 6 Nancy Ouyang
*[1]* We use the @#!@ syntax to let bash know what to execute this file with
320 5 Nancy Ouyang
321
  ⤷To find the location of any particular tool, try using **which**
322
    $ which python
323
    /usr/bin/python
324
    $ which bash
325
    /bin/bash
326
    
327
*[2]* [[TODO: $1]] Here we give a human-readable name, @PREVOUTDIR@, to the first argument given to @mergefiles.sh@, which (referring back to the pipeline template) we defined as the directory containing the output of the previous job (which ran @createtwofiles.sh@).
328
    
329
*[3]* Using the environmental variable @TASK_KEEPMOUNT@ allows us to not make assumptions about where **keep** is mounted. @TASK_KEEPMOUNT@ will be replaced by Arvados automatically with the name of the location to which **keep** is mounted on each worker node. (Lost? Visit http://doc.arvados.org/user/tutorials/tutorial-keep-mount.html )
330 1 Nancy Ouyang
    
331 6 Nancy Ouyang
<pre>$ chmod +x createtwofiles.sh mergefiles.sh # make these files executable</pre>
332 1 Nancy Ouyang
333
**Commit changes and push to remote**
334
335 2 Nancy Ouyang
A few steps: 
336 1 Nancy Ouyang
337 2 Nancy Ouyang
  $ git status #check that everything looks ok
338
$ git add *
339
$ git commit -m “hello world-of-arvados scripts!”
340
$ git push
341
342 1 Nancy Ouyang
**Install Docker**
343
344 2 Nancy Ouyang
A few steps: 
345 1 Nancy Ouyang
346 2 Nancy Ouyang
  $ sudo apt-get install docker.io
347
$ sudo groupadd docker
348
$ sudo gpasswd -a $USER docker #in my case, I replace $USER with “nancy”
349
$ sudo service docker restart
350
$ exec su -l $USER   #if you don’t want to login+out or spawn a new shell, this will restart your shell
351
352 1 Nancy Ouyang
**Make docker less sad about running out of space on the VM**
353
354 2 Nancy Ouyang
A few steps:
355
356
  $ sudo mkdir /data/docker
357
$ sudo vi /etc/default/docker
358
@  @⤷$ cat /etc/default/docker
359
      DOCKER_OPTS="--graph='/data/docker'"
360
      export TMPDIR="/data/docker"
361 1 Nancy Ouyang
     
362
**Make Arvados less sad about running out of space on the VM**
363 2 Nancy Ouyang
364
A few steps: 
365
366 1 Nancy Ouyang
    $ sudo mkdir /data/docker-cache
367 3 Nancy Ouyang
$ sudo chown nancy:nancy /data/docker-cache
368
$ ln -s /data/docker-cache docker
369 1 Nancy Ouyang
370
**Create Docker image with Arvados command-line tools and other tools we want**
371 2 Nancy Ouyang
372
A few steps: 
373
374 3 Nancy Ouyang
    $ docker pull arvados/jobs
375 1 Nancy Ouyang
$ docker run -ti arvados/jobs /bin/bash
376 2 Nancy Ouyang
377 4 Nancy Ouyang
Now we are in the docker image.
378 2 Nancy Ouyang
379 1 Nancy Ouyang
    root@4fa648c759f3:/# apt-get update 
380
381 2 Nancy Ouyang
    @  @⤷In the docker image, install external tools that you don't expect to need to update often. 
382 1 Nancy Ouyang
    For instance, we can install the wormtable python tool in this docker image
383 2 Nancy Ouyang
    @  @# apt-get install libdb-dev
384 3 Nancy Ouyang
    @  @# pip install wormtable
385 1 Nancy Ouyang
386
    @  @  ⤷ Note: If you're installing from binaries, you should either
387 6 Nancy Ouyang
        1) Install in existing places where bash looks for programs (e.g. install in /usr/local/bin/cgatools). 
388
        To see where bash looks, inspect the PATH variable.
389 1 Nancy Ouyang
          #echo $PATH
390
          /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
391 6 Nancy Ouyang
        2) If you put them in a custom directory, remember them to reference them as such in your scripts
392
        (e.g. spell out /home/nrw/local/bin/cgatools).
393
        Arvados will not respect modifyng the $PATH variable by using the ~/.bashrc configuration file in the docker image.
394 2 Nancy Ouyang
395 1 Nancy Ouyang
(Lost? See http://doc.arvados.org/user/topics/arv-docker.html )
396
    
397
  root@4fa648c759f3:/# exit
398 2 Nancy Ouyang
399 1 Nancy Ouyang
*Commit Docker image*
400 2 Nancy Ouyang
<pre>
401 6 Nancy Ouyang
$ docker commit 4fa648c759f3 nancy/cgatools-wormtable #Label the image thoughtfully
402
$ #For instance here I used the name of key tools I installed: cgatools & wormtable
403 2 Nancy Ouyang
</pre>
404 1 Nancy Ouyang
405 2 Nancy Ouyang
*Upload Docker image from your VM to Keep*
406
<pre>
407
$ arv keep docker nancy/cgatools-wormtable #this takes a few minutes
408
$ arv keep docker #lists docker images in the cloud, so you can double-check what was uploaded </pre>
409 1 Nancy Ouyang
410
**Run this pipeline!**
411
Go to Workbench and hit **Run**.
412 2 Nancy Ouyang
<pre>$ firefox http://su92l.arvadosapi.com</pre>
413 1 Nancy Ouyang
[!image: workbench with 'tiny bash script']
414
415 2 Nancy Ouyang
*Note: If no worker nodes are already provisioned, your job may take up to 10 minutes to queue up and start.* Behind-the-scenes, Arvados is requesting compute nodes for you and installing your Docker image and otherwise setting up the environment on those nodes. Then Arvados will be ready to run your job. Be patient -- the wait time may seem frustrating for a trivial pipeline like this, but Arvados really excels at handling long and complicated pipelines with built-in data provenance and pipeline reproducibility.
416 1 Nancy Ouyang
417
h3. 3.5 Celebrate
418
419
Whew! Congratulations on porting your first pipeline to Arvados! Check out http://doc.arvados.org/user/topics/crunch-tools-overview.html to learn more about the different ways to port pipelines to Arvados and how to take full advantage of Arvados's features, like restarting pipelines from where they failed instead of from the beginning. 
420
421
h2. 4. Debugging Tips and Pro-Tips
422
423
h3. **4.1 Pro-tips**
424
425
**Keep mounts are read-only right now. [19 March 2015]**
426
Need to 1) make some temporary directories or 2) change directories away from wherever you started out in but still upload the results to keep?
427
428
For 1, Explicitly use the $HOME directory and make the temporary files there
429
For 2, Use present working directory, $(pwd) at the beginning of your script to write down the directory where run-command will look for files to upload to keep.
430
431
Here's an example:
432
<pre>
433
$ cat mergefiles.sh
434
  TMPDIR=$HOME #directory to make temporary files in
435
  OUTDIR=$(pwd) #directory to put output files in
436
  mkdir $TMPDIR
437
  touch $TMPDIR/sometemporaryfile.txt #this file is deleted when the worker node is stopped
438
  touch $OUTDIR/someoutputfile.txt #this file will be uploaded to keep by run-command
439
</pre>
440
441
* make sure you point to the right repository, your own or arvados.
442
* make sure you pin the script versions of your python sdk, docker image, and script version or you will not get reproducibiltiy.
443
* if you have a file you want to use as a crunch script, make sure its in a crunch_scripts directory. otherwise, arvados will not find it. i.e. ~/path/to/git/repo/crunch_scripts/foo.py
444
445
h3. 4.2 Common log errors and reasons for pipelines to fail
446
447
Todo.
448
449
h3. 4.3 Miscellaneous Notes
450
451
Other ways to avoid the read-only keep mount problem is to use task.vwd which uses symlinks from the output directory which is writable to the colelction in keep. If you can change your working directory to the output directory and do all your work there, you'll avoid the keep read only issue.  (lost? see http://doc.arvados.org/user/topics/run-command.html )
452
    
453
When indexing, i.e. tabix, bwa index, etc. The index file tends to be created in the same directory as your fastq file. In order to avoid this, use ^. There is no way to send the index file to another directory. If you figure out a way, please tell me.
454
455
"bash" "-c" could be your friend, it works sometimes, sometimes it doesnt. I don't have a good handle on why this works sometimes.
456
457
if you're trying to iterate over >1 files using the task.foreach, its important to know that run-command uses a m x n method of making groups. I dont think I can explain it right now, but it may not be exactly what you want and you can trip over it. (lost? see http://doc.arvados.org/user/topics/run-command.html )
458
459
When trying to pair up reads, its hard to use run-command. You have to manipulate basename and hope your file names are foo.1 foo.2. base name will treat the group as foo (because you'll regex the groups as foo) and you can glob for foo.1 and foo.2. but if the file names are foo_1 and foo_2, you cant regex search them for foo becuase you'll get both names into a group and you'll be iterating through both of them twice, because of m x n. 
460
    
461
Your scripts need to point to the right place where the file is. Its currently hard to figure out how to grep the file names, you have to do some magic through the collection api.
462
463
h2. 5. Learn More
464
465
To learn more, head over to the Arvados User Guide documentation online: http://doc.arvados.org/user/