comparison data_manager/malt_index_builder.py @ 0:5f9d6aee2256 draft default tip

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_malt_index_builder commit 710e56e0e522b0ed060dab2fecf05ed1c79c928f"
author iuc
date Wed, 17 Nov 2021 08:22:56 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5f9d6aee2256
1 #!/usr/bin/env python
2
3 import json
4 import optparse
5 import os
6 import subprocess
7 import sys
8
9
10 def get_id_name(params, dbkey, fasta_description=None):
11 sequence_id = params['param_dict']['sequence_id']
12 if not sequence_id:
13 sequence_id = dbkey
14
15 sequence_name = params['param_dict']['sequence_name']
16 if not sequence_name:
17 sequence_name = fasta_description
18 if not sequence_name:
19 sequence_name = dbkey
20 return sequence_id, sequence_name
21
22
23 def build_malt_index(data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, sequence_type, shapes, max_hits_per_seed, protein_reduct):
24 # The malt-build program produces a directory of files,
25 # so the data table path entry will be a directory and
26 # not an index file.
27 fasta_base_name = os.path.split(fasta_filename)[-1]
28 sym_linked_fasta_filename = os.path.join(target_directory, fasta_base_name)
29 os.symlink(fasta_filename, sym_linked_fasta_filename)
30 args = ['malt-build', '--input', sym_linked_fasta_filename, '--sequenceType', sequence_type, '--index', target_directory]
31 threads = os.environ.get('GALAXY_SLOTS')
32 if threads:
33 args.extend(['--threads', threads])
34 if shapes is not None:
35 args.extend(['--shapes', shapes])
36 if max_hits_per_seed is not None:
37 args.extend(['--maxHitsPerSeed', max_hits_per_seed])
38 if protein_reduct is not None:
39 args.extend(['--proteinReduct', protein_reduct])
40 proc = subprocess.Popen(args=args, shell=False, cwd=target_directory)
41 return_code = proc.wait()
42 if return_code:
43 sys.exit('Error building index, return_code: %d' % return_code)
44 # Remove unwanted files from the output directory.
45 os.remove(sym_linked_fasta_filename)
46 # The path entry here is the directory
47 # where the index files will be located,
48 # not a single index file (malt-build
49 # produces a directory if files, which
50 # is considered an index..
51 data_table_entry = dict(value=sequence_id, dbkey=dbkey, name=sequence_name, path=None)
52 _add_data_table_entry(data_manager_dict, data_table_entry)
53
54
55 def _add_data_table_entry(data_manager_dict, data_table_entry):
56 data_table_name = "malt_indices"
57 data_manager_dict['data_tables'] = data_manager_dict.get('data_tables', {})
58 data_manager_dict['data_tables'][data_table_name] = data_manager_dict['data_tables'].get(data_table_name, [])
59 data_manager_dict['data_tables'][data_table_name].append(data_table_entry)
60 return data_manager_dict
61
62
63 def main():
64 parser = optparse.OptionParser()
65 parser.add_option('-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", help='fasta filename')
66 parser.add_option('-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", help='fasta dbkey')
67 parser.add_option('-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta description')
68 parser.add_option('-e', '--sequence_type', dest='sequence_type', action='store', type="string", help='DNA or Protein sequences')
69 parser.add_option('-p', '--shapes', dest='shapes', action='store', type="string", default=None, help='Comma-separated list of seed shapes')
70 parser.add_option('-m', '--max_hits_per_seed', dest='max_hits_per_seed', action='store', type="string", default=None, help='Maximum number of hits per seed')
71 parser.add_option('-r', '--protein_reduct', dest='protein_reduct', action='store', type="string", default=None, help='Name or definition of protein alphabet reduction')
72 (options, args) = parser.parse_args()
73
74 filename = args[0]
75
76 with open(filename) as fh:
77 params = json.load(fh)
78 target_directory = params['output_data'][0]['extra_files_path']
79 os.mkdir(target_directory)
80 data_manager_dict = {}
81
82 dbkey = options.fasta_dbkey
83
84 if dbkey in [None, '', '?']:
85 raise Exception('"%s" is not a valid dbkey. You must specify a valid dbkey.' % (dbkey))
86
87 sequence_id, sequence_name = get_id_name(params, dbkey=dbkey, fasta_description=options.fasta_description)
88
89 # Build the index.
90 build_malt_index(data_manager_dict, options.fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, options.sequence_type, options.shapes, options.max_hits_per_seed, options.protein_reduct)
91
92 # Save info to json file.
93 with open(filename, 'w') as fh:
94 json.dump(data_manager_dict, fh, sort_keys=True)
95
96
97 if __name__ == "__main__":
98 main()