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