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