comparison data_manager/bwa_index_builder.py @ 0:eae04baa35a6 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_sailfish_index_builder commit 418f745190d77983c3fb09badb6298493bccf4e0
author iuc
date Tue, 05 Jan 2016 12:58:16 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:eae04baa35a6
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 galaxy.util.json import from_json_string, to_json_string
11
12
13 CHUNK_SIZE = 2**20
14 ONE_GB = 2**30
15
16 DEFAULT_DATA_TABLE_NAME = "sailfish_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 tmp_stderr = tempfile.NamedTemporaryFile( prefix = "tmp-data-manager-bwa-index-builder-stderr" )
50 proc = subprocess.Popen( args=args, shell=False, cwd=target_directory, stderr=tmp_stderr.fileno() )
51 return_code = proc.wait()
52 if return_code:
53 tmp_stderr.flush()
54 tmp_stderr.seek(0)
55 print >> sys.stderr, "Error building index:"
56 while True:
57 chunk = tmp_stderr.read( CHUNK_SIZE )
58 if not chunk:
59 break
60 sys.stderr.write( chunk )
61 sys.exit( return_code )
62 tmp_stderr.close()
63 data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name )
64 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry )
65
66 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ):
67 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
68 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] )
69 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry )
70 return data_manager_dict
71
72 def main():
73 #Parse Command Line
74 parser = optparse.OptionParser()
75 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' )
76 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' )
77 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' )
78 parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name' )
79 parser.add_option( '-c', '--color_space', dest='color_space', action='store_true', default=False, help='color_space' )
80 (options, args) = parser.parse_args()
81
82 filename = args[0]
83
84 params = from_json_string( open( filename ).read() )
85 target_directory = params[ 'output_data' ][0]['extra_files_path']
86 os.mkdir( target_directory )
87 data_manager_dict = {}
88
89 dbkey = options.fasta_dbkey
90
91 if dbkey in [ None, '', '?' ]:
92 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) )
93
94 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description )
95
96 #build the index
97 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 )
98
99 #save info to json file
100 open( filename, 'wb' ).write( to_json_string( data_manager_dict ) )
101
102 if __name__ == "__main__": main()