Mercurial > repos > devteam > data_manager_bwa_mem_index_builder
annotate data_manager/bwa_mem_index_builder.py @ 7:37580590ecfb draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 2cc657cadd9c2e0dee930410d87c23d9cf29aed3"
author | iuc |
---|---|
date | Wed, 04 Dec 2019 16:29:26 -0500 |
parents | 2a386b2383a1 |
children | 63d5652be07a |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
2 # Dan Blankenberg |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
3 from __future__ import print_function |
0 | 4 |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
5 import optparse |
0 | 6 import os |
7 import subprocess | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
8 import sys |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
9 from json import dumps, loads |
0 | 10 |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
11 CHUNK_SIZE = 2**20 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
12 TWO_GB = 2**30 * 2 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
13 DEFAULT_DATA_TABLE_NAME = "bwa_mem_indexes" |
0 | 14 |
15 | |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
16 def get_id_name(params, dbkey, fasta_description=None): |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
17 # TODO: ensure sequence_id is unique and does not already appear in location file |
0 | 18 sequence_id = params['param_dict']['sequence_id'] |
19 if not sequence_id: | |
20 sequence_id = dbkey | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
21 |
0 | 22 sequence_name = params['param_dict']['sequence_name'] |
23 if not sequence_name: | |
24 sequence_name = fasta_description | |
25 if not sequence_name: | |
26 sequence_name = dbkey | |
27 return sequence_id, sequence_name | |
28 | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
29 |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
30 def build_bwa_index(data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_name=DEFAULT_DATA_TABLE_NAME): |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
31 # TODO: allow multiple FASTA input files |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
32 fasta_base_name = os.path.split(fasta_filename)[-1] |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
33 sym_linked_fasta_filename = os.path.join(target_directory, fasta_base_name) |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
34 os.symlink(fasta_filename, sym_linked_fasta_filename) |
0 | 35 if params['param_dict']['index_algorithm'] == 'automatic': |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
36 if os.stat(fasta_filename).st_size < TWO_GB: # use 2 GB as cut off for memory vs. max of 2gb database size; this is somewhat arbitrary |
0 | 37 index_algorithm = 'is' |
38 else: | |
39 index_algorithm = 'bwtsw' | |
40 else: | |
41 index_algorithm = params['param_dict']['index_algorithm'] | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
42 |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
43 args = ['bwa', 'index', '-a', index_algorithm] |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
44 args.append(sym_linked_fasta_filename) |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
45 proc = subprocess.Popen(args=args, shell=False, cwd=target_directory) |
0 | 46 return_code = proc.wait() |
47 if return_code: | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
48 print("Error building index.", file=sys.stderr) |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
49 sys.exit(return_code) |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
50 data_table_entry = dict(value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name) |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
51 _add_data_table_entry(data_manager_dict, data_table_name, data_table_entry) |
0 | 52 |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
53 |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
54 def _add_data_table_entry(data_manager_dict, data_table_name, data_table_entry): |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
55 data_manager_dict['data_tables'] = data_manager_dict.get('data_tables', {}) |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
56 data_manager_dict['data_tables'][data_table_name] = data_manager_dict['data_tables'].get(data_table_name, []) |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
57 data_manager_dict['data_tables'][data_table_name].append(data_table_entry) |
0 | 58 return data_manager_dict |
59 | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
60 |
0 | 61 def main(): |
62 parser = optparse.OptionParser() | |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
63 parser.add_option('-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename') |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
64 parser.add_option('-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey') |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
65 parser.add_option('-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description') |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
66 parser.add_option('-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name') |
0 | 67 (options, args) = parser.parse_args() |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
68 |
0 | 69 filename = args[0] |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
70 |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
71 params = loads(open(filename).read()) |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
72 target_directory = params['output_data'][0]['extra_files_path'] |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
73 os.mkdir(target_directory) |
0 | 74 data_manager_dict = {} |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
75 |
0 | 76 dbkey = options.fasta_dbkey |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
77 |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
78 if dbkey in [None, '', '?']: |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
79 raise Exception('"%s" is not a valid dbkey. You must specify a valid dbkey.' % (dbkey)) |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
80 |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
81 sequence_id, sequence_name = get_id_name(params, dbkey=dbkey, fasta_description=options.fasta_description) |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
82 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
83 # build the index |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
84 build_bwa_index( |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
85 data_manager_dict, |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
86 options.fasta_filename, |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
87 params, |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
88 target_directory, |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
89 dbkey, |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
90 sequence_id, |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
91 sequence_name, |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
92 data_table_name=options.data_table_name or DEFAULT_DATA_TABLE_NAME |
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
93 ) |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
94 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
95 # save info to json file |
6
2a386b2383a1
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit a2c93a8a0afeaecbb43172b44e4a7159bf43d11c"
iuc
parents:
5
diff
changeset
|
96 open(filename, 'w').write(dumps(data_manager_dict, sort_keys=True)) |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
97 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
98 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
99 if __name__ == "__main__": |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
100 main() |