Mercurial > repos > greg > data_manager_malt_index_builder
comparison data_manager/malt_index_builder.py @ 1:787f1ca9045a draft default tip
Uploaded
author | greg |
---|---|
date | Wed, 13 Oct 2021 20:12:48 +0000 |
parents | d69ebf52c233 |
children |
comparison
equal
deleted
inserted
replaced
0:d69ebf52c233 | 1:787f1ca9045a |
---|---|
3 import json | 3 import json |
4 import optparse | 4 import optparse |
5 import os | 5 import os |
6 import subprocess | 6 import subprocess |
7 import sys | 7 import sys |
8 | |
9 DEFAULT_DATA_TABLE_NAME = "malt_indices" | |
10 | 8 |
11 | 9 |
12 def get_id_name(params, dbkey, fasta_description=None): | 10 def get_id_name(params, dbkey, fasta_description=None): |
13 sequence_id = params['param_dict']['sequence_id'] | 11 sequence_id = params['param_dict']['sequence_id'] |
14 if not sequence_id: | 12 if not sequence_id: |
20 if not sequence_name: | 18 if not sequence_name: |
21 sequence_name = dbkey | 19 sequence_name = dbkey |
22 return sequence_id, sequence_name | 20 return sequence_id, sequence_name |
23 | 21 |
24 | 22 |
25 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, data_table_name=DEFAULT_DATA_TABLE_NAME): | 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. | |
26 fasta_base_name = os.path.split(fasta_filename)[-1] | 27 fasta_base_name = os.path.split(fasta_filename)[-1] |
27 sym_linked_fasta_filename = os.path.join(target_directory, fasta_base_name) | 28 sym_linked_fasta_filename = os.path.join(target_directory, fasta_base_name) |
28 os.symlink(fasta_filename, sym_linked_fasta_filename) | 29 os.symlink(fasta_filename, sym_linked_fasta_filename) |
29 args = ['malt-build', '--input', sym_linked_fasta_filename, '--sequenceType', sequence_type, '--index', 'index'] | 30 args = ['malt-build', '--input', sym_linked_fasta_filename, '--sequenceType', sequence_type, '--index', target_directory] |
30 threads = os.environ.get('GALAXY_SLOTS') | 31 threads = os.environ.get('GALAXY_SLOTS') |
31 if threads: | 32 if threads: |
32 args.extend(['--threads', threads]) | 33 args.extend(['--threads', threads]) |
33 if shapes is not None: | 34 if shapes is not None: |
34 args.extend('--shapes', shapes) | 35 args.extend(['--shapes', shapes]) |
35 if max_hits_per_seed is not None: | 36 if max_hits_per_seed is not None: |
36 args.extend('--maxHitsPerSeed', max_hits_per_seed) | 37 args.extend(['--maxHitsPerSeed', max_hits_per_seed]) |
37 if protein_reduct is not None: | 38 if protein_reduct is not None: |
38 args.extend('--proteinReduct', protein_reduct) | 39 args.extend(['--proteinReduct', protein_reduct]) |
39 proc = subprocess.Popen(args=args, shell=False, cwd=target_directory) | 40 proc = subprocess.Popen(args=args, shell=False, cwd=target_directory) |
40 return_code = proc.wait() | 41 return_code = proc.wait() |
41 if return_code: | 42 if return_code: |
42 sys.exit('Error building index, return_code: %d' % return_code) | 43 sys.exit('Error building index, return_code: %d' % return_code) |
43 data_table_entry = dict(value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name) | 44 # Remove unwanted files from the output directory. |
44 _add_data_table_entry(data_manager_dict, data_table_name, data_table_entry) | 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) | |
45 | 53 |
46 | 54 |
47 def _add_data_table_entry(data_manager_dict, data_table_name, data_table_entry): | 55 def _add_data_table_entry(data_manager_dict, data_table_entry): |
56 data_table_name = "malt_indices" | |
48 data_manager_dict['data_tables'] = data_manager_dict.get('data_tables', {}) | 57 data_manager_dict['data_tables'] = data_manager_dict.get('data_tables', {}) |
49 data_manager_dict['data_tables'][data_table_name] = data_manager_dict['data_tables'].get(data_table_name, []) | 58 data_manager_dict['data_tables'][data_table_name] = data_manager_dict['data_tables'].get(data_table_name, []) |
50 data_manager_dict['data_tables'][data_table_name].append(data_table_entry) | 59 data_manager_dict['data_tables'][data_table_name].append(data_table_entry) |
51 return data_manager_dict | 60 return data_manager_dict |
52 | 61 |
56 parser.add_option('-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", help='fasta filename') | 65 parser.add_option('-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", help='fasta filename') |
57 parser.add_option('-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", help='fasta dbkey') | 66 parser.add_option('-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", help='fasta dbkey') |
58 parser.add_option('-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta description') | 67 parser.add_option('-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta description') |
59 parser.add_option('-e', '--sequence_type', dest='sequence_type', action='store', type="string", help='DNA or Protein sequences') | 68 parser.add_option('-e', '--sequence_type', dest='sequence_type', action='store', type="string", help='DNA or Protein sequences') |
60 parser.add_option('-p', '--shapes', dest='shapes', action='store', type="string", default=None, help='Comma-separated list of seed shapes') | 69 parser.add_option('-p', '--shapes', dest='shapes', action='store', type="string", default=None, help='Comma-separated list of seed shapes') |
61 parser.add_option('-m', '--max_hits_per_seed', dest='max_hits_per_seed', action='store', type="int", default=None, help='Maximum number of hits per seed') | 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') |
62 parser.add_option('-r', '--protein_reduct', dest='protein_reduct', action='store', type="string", default=None, help='Name or definition of protein alphabet reduction') | 71 parser.add_option('-r', '--protein_reduct', dest='protein_reduct', action='store', type="string", default=None, help='Name or definition of protein alphabet reduction') |
63 (options, args) = parser.parse_args() | 72 (options, args) = parser.parse_args() |
64 | 73 |
65 filename = args[0] | 74 filename = args[0] |
66 | 75 |
76 raise Exception('"%s" is not a valid dbkey. You must specify a valid dbkey.' % (dbkey)) | 85 raise Exception('"%s" is not a valid dbkey. You must specify a valid dbkey.' % (dbkey)) |
77 | 86 |
78 sequence_id, sequence_name = get_id_name(params, dbkey=dbkey, fasta_description=options.fasta_description) | 87 sequence_id, sequence_name = get_id_name(params, dbkey=dbkey, fasta_description=options.fasta_description) |
79 | 88 |
80 # Build the index. | 89 # Build the index. |
81 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, data_table_name=DEFAULT_DATA_TABLE_NAME) | 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) |
82 | 91 |
83 # Save info to json file. | 92 # Save info to json file. |
84 with open(filename, 'w') as fh: | 93 with open(filename, 'w') as fh: |
85 json.dump(data_manager_dict, fh, sort_keys=True) | 94 json.dump(data_manager_dict, fh, sort_keys=True) |
86 | 95 |