2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: Apache-2.0
11 import arvados_samtools
12 from arvados_ipc import *
14 class InvalidArgumentError(Exception):
17 arvados_samtools.one_task_per_bam_file(if_sequence=0, and_end_task=True)
19 this_job = arvados.current_job()
20 this_task = arvados.current_task()
21 tmpdir = arvados.current_task().tmpdir
22 arvados.util.clear_tmpdir()
24 known_sites_files = arvados.getjobparam(
27 'Mills_and_1000G_gold_standard.indels.b37.vcf',
29 bundle_dir = arvados.util.collection_extract(
30 collection = this_job['script_parameters']['gatk_bundle'],
33 'human_g1k_v37.fasta',
34 'human_g1k_v37.fasta.fai'
35 ] + known_sites_files + [v + '.idx' for v in known_sites_files],
37 ref_fasta_files = [os.path.join(bundle_dir, f)
38 for f in os.listdir(bundle_dir)
39 if re.search(r'\.fasta(\.gz)?$', f)]
41 if 'regions' in this_job['script_parameters']:
42 regions_dir = arvados.util.collection_extract(
43 collection = this_job['script_parameters']['regions'],
45 region_padding = int(this_job['script_parameters']['region_padding'])
46 for f in os.listdir(regions_dir):
47 if re.search(r'\.bed$', f):
49 '--intervals', os.path.join(regions_dir, f),
50 '--interval_padding', str(region_padding)
53 input_collection = this_task['parameters']['input']
54 input_dir = arvados.util.collection_extract(
55 collection = input_collection,
56 path = os.path.join(this_task.tmpdir, 'input'))
58 for f in arvados.util.listdir_recursive(input_dir):
59 if re.search(r'\.bam$', f):
60 input_stream_name, input_file_name = os.path.split(f)
61 input_bam_files += [os.path.join(input_dir, f)]
62 if len(input_bam_files) != 1:
63 raise InvalidArgumentError("Expected exactly one bam file per task.")
66 for f in known_sites_files:
67 known_sites_args += ['-known', os.path.join(bundle_dir, f)]
74 '-nt', arvados_gatk2.cpus_per_task(),
75 '-T', 'RealignerTargetCreator',
76 '-R', ref_fasta_files[0],
77 '-I', input_bam_files[0],
78 '-o', os.path.join(tmpdir, 'intervals.list')
79 ] + known_sites_args + regions_args)
81 pipe_setup(pipes, 'IndelRealigner')
82 if 0 == named_fork(children, 'IndelRealigner'):
83 pipe_closeallbut(pipes, ('IndelRealigner', 'w'))
86 '-T', 'IndelRealigner',
87 '-R', ref_fasta_files[0],
88 '-targetIntervals', os.path.join(tmpdir, 'intervals.list'),
89 '-I', input_bam_files[0],
90 '-o', '/dev/fd/' + str(pipes['IndelRealigner','w']),
91 '--disable_bam_indexing',
92 ] + known_sites_args + regions_args,
95 os.close(pipes.pop(('IndelRealigner','w'), None))
97 pipe_setup(pipes, 'bammanifest')
98 pipe_setup(pipes, 'bam')
99 if 0==named_fork(children, 'bammanifest'):
100 pipe_closeallbut(pipes,
101 ('IndelRealigner', 'r'),
102 ('bammanifest', 'w'),
104 out = arvados.CollectionWriter()
105 out.start_new_stream(input_stream_name)
106 out.start_new_file(input_file_name)
108 buf = os.read(pipes['IndelRealigner','r'], 2**20)
111 os.write(pipes['bam','w'], buf)
113 os.write(pipes['bammanifest','w'], out.manifest_text())
114 os.close(pipes['bammanifest','w'])
117 pipe_setup(pipes, 'index')
118 if 0==named_fork(children, 'index'):
119 pipe_closeallbut(pipes, ('bam', 'r'), ('index', 'w'))
123 'i': '/dev/fd/' + str(pipes['bam','r']),
124 'o': '/dev/fd/' + str(pipes['index','w']),
126 'validation_stringency': 'LENIENT'
131 pipe_setup(pipes, 'indexmanifest')
132 if 0==named_fork(children, 'indexmanifest'):
133 pipe_closeallbut(pipes, ('index', 'r'), ('indexmanifest', 'w'))
134 out = arvados.CollectionWriter()
135 out.start_new_stream(input_stream_name)
136 out.start_new_file(re.sub('\.bam$', '.bai', input_file_name))
138 buf = os.read(pipes['index','r'], 2**20)
142 os.write(pipes['indexmanifest','w'], out.manifest_text())
143 os.close(pipes['indexmanifest','w'])
146 pipe_closeallbut(pipes, ('bammanifest', 'r'), ('indexmanifest', 'r'))
148 for which in ['bammanifest', 'indexmanifest']:
149 with os.fdopen(pipes[which,'r'], 'rb', 2**20) as f:
157 for (childname, pid) in children.items():
158 all_ok = all_ok and waitpid_and_check_exit(pid, childname)
161 this_task.set_output(outmanifest)