Bugfix os.environ() -> os.environ[] no issue #
[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
9 if len(arvados.current_task()['parameters']) > 0:
10     p = arvados.current_task()['parameters']
11 else:
12     p = arvados.current_job()['script_parameters']
13
14 t = arvados.current_task().tmpdir
15
16 os.unlink("/usr/local/share/bcbio-nextgen/galaxy")
17 os.mkdir("/usr/local/share/bcbio-nextgen/galaxy")
18 shutil.copy("/usr/local/share/bcbio-nextgen/config/bcbio_system.yaml", "/usr/local/share/bcbio-nextgen/galaxy")
19
20 with open("/usr/local/share/bcbio-nextgen/galaxy/tool_data_table_conf.xml", "w") as f:
21     f.write('''<tables>
22     <!-- Locations of indexes in the BWA mapper format -->
23     <table name="bwa_indexes" comment_char="#">
24         <columns>value, dbkey, name, path</columns>
25         <file path="tool-data/bwa_index.loc" />
26     </table>
27     <!-- Locations of indexes in the Bowtie2 mapper format -->
28     <table name="bowtie2_indexes" comment_char="#">
29         <columns>value, dbkey, name, path</columns>
30         <file path="tool-data/bowtie2_indices.loc" />
31     </table>
32     <!-- Locations of indexes in the Bowtie2 mapper format for TopHat2 to use -->
33     <table name="tophat2_indexes" comment_char="#">
34         <columns>value, dbkey, name, path</columns>
35         <file path="tool-data/bowtie2_indices.loc" />
36     </table>
37     <!-- Location of SAMTools indexes and other files -->
38     <table name="sam_fa_indexes" comment_char="#">
39         <columns>index, value, path</columns>
40         <file path="tool-data/sam_fa_indices.loc" />
41     </table>
42     <!-- Location of Picard dict file and other files -->
43     <table name="picard_indexes" comment_char="#">
44         <columns>value, dbkey, name, path</columns>
45         <file path="tool-data/picard_index.loc" />
46     </table>
47     <!-- Location of Picard dict files valid for GATK -->
48     <table name="gatk_picard_indexes" comment_char="#">
49         <columns>value, dbkey, name, path</columns>
50         <file path="tool-data/gatk_sorted_picard_index.loc" />
51     </table>
52 </tables>
53 ''')
54
55 os.mkdir("/usr/local/share/bcbio-nextgen/galaxy/tool-data")
56
57 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/bowtie2_indices.loc", "w") as f:
58     f.write(subst.do_substitution(p, "GRCh37\tGRCh37\tHuman (GRCh37)\t$(dir $(bowtie2_indices))\n"))
59
60 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/bwa_index.loc", "w") as f:
61     f.write(subst.do_substitution(p, "GRCh37\tGRCh37\tHuman (GRCh37)\t$(file $(bwa_index))\n"))
62
63 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/gatk_sorted_picard_index.loc", "w") as f:
64     f.write(subst.do_substitution(p, "GRCh37\tGRCh37\tHuman (GRCh37)\t$(file $(gatk_sorted_picard_index))\n"))
65
66 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/picard_index.loc", "w") as f:
67     f.write(subst.do_substitution(p, "GRCh37\tGRCh37\tHuman (GRCh37)\t$(file $(picard_index))\n"))
68
69 with open("/usr/local/share/bcbio-nextgen/galaxy/tool-data/sam_fa_indices.loc", "w") as f:
70     f.write(subst.do_substitution(p, "index\tGRCh37\t$(file $(sam_fa_indices))\n"))
71
72 with open("/tmp/crunch-job/gatk-variant.yaml", "w") as f:
73     f.write('''
74 # Template for whole genome Illumina variant calling with GATK pipeline
75 ---
76 details:
77   - analysis: variant2
78     genome_build: GRCh37
79     # to do multi-sample variant calling, assign samples the same metadata / batch
80     # metadata:
81     #   batch: your-arbitrary-batch-name
82     algorithm:
83       aligner: bwa
84       mark_duplicates: picard
85       recalibrate: gatk
86       realign: gatk
87       variantcaller: gatk-haplotype
88       platform: illumina
89       quality_format: Standard
90       coverage_interval: genome
91       # for targetted projects, set the region
92       # variant_regions: /path/to/your.bed
93 ''')
94
95 os.chdir(arvados.current_task().tmpdir)
96
97 rcode = subprocess.call(["bcbio_nextgen.py", "--workflow", "template", "/tmp/crunch-job/gatk-variant.yaml", "project1",
98                          subst.do_substitution(p, "$(file $(R1))"),
99                          subst.do_substitution(p, "$(file $(R2))")])
100
101 os.chdir("project1/work")
102
103 os.symlink("/usr/local/share/bcbio-nextgen/galaxy/tool-data", "tool-data")
104
105 rcode = subprocess.call(["bcbio_nextgen.py", "../config/project1.yaml", "-n", os.environ['CRUNCH_NODE_SLOTS']])