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