Merge branch '8784-dir-listings'
[arvados.git] / crunch_scripts / arvados-bcbio-nextgen.py
1 #!/usr/bin/python
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: Apache-2.0
5
6 import arvados
7 import subprocess
8 import crunchutil.subst as subst
9 import shutil
10 import os
11 import sys
12 import time
13
14 if len(arvados.current_task()['parameters']) > 0:
15     p = arvados.current_task()['parameters']
16 else:
17     p = arvados.current_job()['script_parameters']
18
19 t = arvados.current_task().tmpdir
20
21 os.unlink("/usr/local/share/bcbio-nextgen/galaxy")
22 os.mkdir("/usr/local/share/bcbio-nextgen/galaxy")
23 shutil.copy("/usr/local/share/bcbio-nextgen/config/bcbio_system.yaml", "/usr/local/share/bcbio-nextgen/galaxy")
24
25 with open("/usr/local/share/bcbio-nextgen/galaxy/tool_data_table_conf.xml", "w") as f:
26     f.write('''<tables>
27     <!-- Locations of indexes in the BWA mapper format -->
28     <table name="bwa_indexes" comment_char="#">
29         <columns>value, dbkey, name, path</columns>
30         <file path="tool-data/bwa_index.loc" />
31     </table>
32     <!-- Locations of indexes in the Bowtie2 mapper format -->
33     <table name="bowtie2_indexes" comment_char="#">
34         <columns>value, dbkey, name, path</columns>
35         <file path="tool-data/bowtie2_indices.loc" />
36     </table>
37     <!-- Locations of indexes in the Bowtie2 mapper format for TopHat2 to use -->
38     <table name="tophat2_indexes" comment_char="#">
39         <columns>value, dbkey, name, path</columns>
40         <file path="tool-data/bowtie2_indices.loc" />
41     </table>
42     <!-- Location of SAMTools indexes and other files -->
43     <table name="sam_fa_indexes" comment_char="#">
44         <columns>index, value, path</columns>
45         <file path="tool-data/sam_fa_indices.loc" />
46     </table>
47     <!-- Location of Picard dict file and other files -->
48     <table name="picard_indexes" comment_char="#">
49         <columns>value, dbkey, name, path</columns>
50         <file path="tool-data/picard_index.loc" />
51     </table>
52     <!-- Location of Picard dict files valid for GATK -->
53     <table name="gatk_picard_indexes" comment_char="#">
54         <columns>value, dbkey, name, path</columns>
55         <file path="tool-data/gatk_sorted_picard_index.loc" />
56     </table>
57 </tables>
58 ''')
59
60 os.mkdir("/usr/local/share/bcbio-nextgen/galaxy/tool-data")
61
62 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/bowtie2_indices.loc", "w") as f:
63     f.write(subst.do_substitution(p, "GRCh37\tGRCh37\tHuman (GRCh37)\t$(dir $(bowtie2_indices))\n"))
64
65 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/bwa_index.loc", "w") as f:
66     f.write(subst.do_substitution(p, "GRCh37\tGRCh37\tHuman (GRCh37)\t$(file $(bwa_index))\n"))
67
68 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/gatk_sorted_picard_index.loc", "w") as f:
69     f.write(subst.do_substitution(p, "GRCh37\tGRCh37\tHuman (GRCh37)\t$(file $(gatk_sorted_picard_index))\n"))
70
71 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/picard_index.loc", "w") as f:
72     f.write(subst.do_substitution(p, "GRCh37\tGRCh37\tHuman (GRCh37)\t$(file $(picard_index))\n"))
73
74 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/sam_fa_indices.loc", "w") as f:
75     f.write(subst.do_substitution(p, "index\tGRCh37\t$(file $(sam_fa_indices))\n"))
76
77 with open("/tmp/crunch-job/freebayes-variant.yaml", "w") as f:
78     f.write('''
79 # Template for whole genome Illumina variant calling with FreeBayes
80 # This is a GATK-free pipeline without post-alignment BAM pre-processing
81 # (recalibration and realignment)
82 ---
83 details:
84   - analysis: variant2
85     genome_build: GRCh37
86     # to do multi-sample variant calling, assign samples the same metadata / batch
87     # metadata:
88     #   batch: your-arbitrary-batch-name
89     algorithm:
90       aligner: bwa
91       mark_duplicates: true
92       recalibrate: false
93       realign: false
94       variantcaller: freebayes
95       platform: illumina
96       quality_format: Standard
97       # for targetted projects, set the region
98       # variant_regions: /path/to/your.bed
99 ''')
100
101 os.unlink("/usr/local/share/bcbio-nextgen/gemini_data")
102 os.symlink(arvados.get_job_param_mount("gemini_data"), "/usr/local/share/bcbio-nextgen/gemini_data")
103
104 os.chdir(arvados.current_task().tmpdir)
105
106 rcode = subprocess.call(["bcbio_nextgen.py", "--workflow", "template", "/tmp/crunch-job/freebayes-variant.yaml", "project1",
107                          subst.do_substitution(p, "$(file $(R1))"),
108                          subst.do_substitution(p, "$(file $(R2))")])
109
110 os.chdir("project1/work")
111
112 os.symlink("/usr/local/share/bcbio-nextgen/galaxy/tool-data", "tool-data")
113
114 rcode = subprocess.call(["bcbio_nextgen.py", "../config/project1.yaml", "-n", os.environ['CRUNCH_NODE_SLOTS']])
115
116 print("run-command: completed with exit code %i (%s)" % (rcode, "success" if rcode == 0 else "failed"))
117
118 if rcode == 0:
119     os.chdir("../final")
120
121     print("arvados-bcbio-nextgen: the follow output files will be saved to keep:")
122
123     subprocess.call(["find", ".", "-type", "f", "-printf", "arvados-bcbio-nextgen: %12.12s %h/%f\\n"])
124
125     print("arvados-bcbio-nextgen: start writing output to keep")
126
127     done = False
128     api = arvados.api('v1')
129     while not done:
130         try:
131             out = arvados.CollectionWriter()
132             out.write_directory_tree(".", max_manifest_depth=0)
133             outuuid = out.finish()
134             api.job_tasks().update(uuid=arvados.current_task()['uuid'],
135                                                  body={
136                                                      'output':outuuid,
137                                                      'success': (rcode == 0),
138                                                      'progress':1.0
139                                                  }).execute()
140             done = True
141         except Exception as e:
142             print("arvados-bcbio-nextgen: caught exception: {}".format(e))
143             time.sleep(5)
144
145 sys.exit(rcode)