Mercurial > repos > devteam > data_manager_picard_index_builder
annotate data_manager/picard_index_builder.py @ 6:fa8c605bf385 draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 096286097ed5cdf189a1b68c3fc34d10f4142e54
author | iuc |
---|---|
date | Sun, 16 Apr 2023 08:28:16 +0000 |
parents | acaa6719a478 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
2 # Dan Blankenberg |
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
3 from __future__ import print_function |
0 | 4 |
6
fa8c605bf385
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 096286097ed5cdf189a1b68c3fc34d10f4142e54
iuc
parents:
5
diff
changeset
|
5 import json |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
6 import optparse |
0 | 7 import os |
8 import subprocess | |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
9 import sys |
0 | 10 |
11 DEFAULT_DATA_TABLE_NAME = "picard_indexes" | |
12 | |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
13 |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
14 def get_id_name(params, dbkey, fasta_description=None): |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
15 # TODO: ensure sequence_id is unique and does not already appear in location file |
0 | 16 sequence_id = params['param_dict']['sequence_id'] |
17 if not sequence_id: | |
18 sequence_id = dbkey | |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
19 |
0 | 20 sequence_name = params['param_dict']['sequence_name'] |
21 if not sequence_name: | |
22 sequence_name = fasta_description | |
23 if not sequence_name: | |
24 sequence_name = dbkey | |
25 return sequence_id, sequence_name | |
26 | |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
27 |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
28 def build_picard_index(data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_name=DEFAULT_DATA_TABLE_NAME): |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
29 # TODO: allow multiple FASTA input files |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
30 fasta_base_name = os.path.split(fasta_filename)[-1] |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
31 prefix = os.path.splitext(fasta_base_name)[0] |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
32 sym_linked_fasta_filename = os.path.join(target_directory, fasta_base_name) |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
33 os.symlink(fasta_filename, sym_linked_fasta_filename) |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
34 args = ['picard', 'CreateSequenceDictionary', 'R=%s' % sym_linked_fasta_filename, 'O=%s.dict' % prefix] |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
35 proc = subprocess.Popen(args=args, shell=False, cwd=target_directory) |
0 | 36 return_code = proc.wait() |
37 if return_code: | |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
38 print("Error building index.", file=sys.stderr) |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
39 sys.exit(return_code) |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
40 data_table_entry = dict(value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name) |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
41 _add_data_table_entry(data_manager_dict, data_table_name, data_table_entry) |
0 | 42 |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
43 |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
44 def _add_data_table_entry(data_manager_dict, data_table_name, data_table_entry): |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
45 data_manager_dict['data_tables'] = data_manager_dict.get('data_tables', {}) |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
46 data_manager_dict['data_tables'][data_table_name] = data_manager_dict['data_tables'].get(data_table_name, []) |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
47 data_manager_dict['data_tables'][data_table_name].append(data_table_entry) |
0 | 48 return data_manager_dict |
49 | |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
50 |
0 | 51 def main(): |
52 parser = optparse.OptionParser() | |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
53 parser.add_option('-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename') |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
54 parser.add_option('-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey') |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
55 parser.add_option('-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description') |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
56 parser.add_option('-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name') |
0 | 57 (options, args) = parser.parse_args() |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
58 |
0 | 59 filename = args[0] |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
60 |
6
fa8c605bf385
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 096286097ed5cdf189a1b68c3fc34d10f4142e54
iuc
parents:
5
diff
changeset
|
61 with open(filename) as fh: |
fa8c605bf385
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 096286097ed5cdf189a1b68c3fc34d10f4142e54
iuc
parents:
5
diff
changeset
|
62 params = json.load(fh) |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
63 target_directory = params['output_data'][0]['extra_files_path'] |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
64 os.mkdir(target_directory) |
0 | 65 data_manager_dict = {} |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
66 |
0 | 67 dbkey = options.fasta_dbkey |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
68 |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
69 if dbkey in [None, '', '?']: |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
70 raise Exception('"%s" is not a valid dbkey. You must specify a valid dbkey.' % (dbkey)) |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
71 |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
72 sequence_id, sequence_name = get_id_name(params, dbkey=dbkey, fasta_description=options.fasta_description) |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
73 |
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
74 # build the index |
5
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
75 build_picard_index(data_manager_dict, options.fasta_filename, params, target_directory, dbkey, sequence_id, |
acaa6719a478
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 31fc4022a8c2fef50a3aff9210091be21e8450ef"
iuc
parents:
4
diff
changeset
|
76 sequence_name, data_table_name=options.data_table_name or DEFAULT_DATA_TABLE_NAME) |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
77 |
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
78 # save info to json file |
6
fa8c605bf385
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 096286097ed5cdf189a1b68c3fc34d10f4142e54
iuc
parents:
5
diff
changeset
|
79 with open(filename, 'w') as fh: |
fa8c605bf385
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 096286097ed5cdf189a1b68c3fc34d10f4142e54
iuc
parents:
5
diff
changeset
|
80 json.dump(data_manager_dict, fh, sort_keys=True) |
3
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
81 |
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
82 |
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
83 if __name__ == "__main__": |
0663d25e619c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
84 main() |