Mercurial > repos > iuc > data_manager_bowtie_index_builder
comparison data_manager/bowtie_index_builder.py @ 0:0a0c648498e2 draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie_index_builder commit 418f745190d77983c3fb09badb6298493bccf4e0
author | iuc |
---|---|
date | Tue, 05 Jan 2016 12:57:11 -0500 |
parents | |
children | 35c1e3785c90 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0a0c648498e2 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import sys | |
4 import os | |
5 import tempfile | |
6 import optparse | |
7 import subprocess | |
8 | |
9 from galaxy.util.json import from_json_string, to_json_string | |
10 | |
11 | |
12 CHUNK_SIZE = 2**20 | |
13 | |
14 DEFAULT_DATA_TABLE_NAME = "bowtie_indexes" | |
15 | |
16 def get_id_name( params, dbkey, fasta_description=None): | |
17 #TODO: ensure sequence_id is unique and does not already appear in location file | |
18 sequence_id = params['param_dict']['sequence_id'] | |
19 if not sequence_id: | |
20 sequence_id = dbkey | |
21 | |
22 sequence_name = params['param_dict']['sequence_name'] | |
23 if not sequence_name: | |
24 sequence_name = fasta_description | |
25 if not sequence_name: | |
26 sequence_name = dbkey | |
27 return sequence_id, sequence_name | |
28 | |
29 | |
30 def build_bowtie_index( data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_name=DEFAULT_DATA_TABLE_NAME, color_space = False ): | |
31 #TODO: allow multiple FASTA input files | |
32 #tmp_dir = tempfile.mkdtemp( prefix='tmp-data-manager-bowtie-index-builder-' ) | |
33 fasta_base_name = os.path.split( fasta_filename )[-1] | |
34 sym_linked_fasta_filename = os.path.join( target_directory, fasta_base_name ) | |
35 os.symlink( fasta_filename, sym_linked_fasta_filename ) | |
36 args = [ 'bowtie-build' ] | |
37 if color_space: | |
38 args.append( '-c' ) | |
39 args.append( sym_linked_fasta_filename) | |
40 args.append( fasta_base_name ) | |
41 args.append( sym_linked_fasta_filename ) | |
42 tmp_stderr = tempfile.NamedTemporaryFile( prefix = "tmp-data-manager-bowtie-index-builder-stderr" ) | |
43 proc = subprocess.Popen( args=args, shell=False, cwd=target_directory, stderr=tmp_stderr.fileno() ) | |
44 return_code = proc.wait() | |
45 if return_code: | |
46 tmp_stderr.flush() | |
47 tmp_stderr.seek(0) | |
48 print >> sys.stderr, "Error building index:" | |
49 while True: | |
50 chunk = tmp_stderr.read( CHUNK_SIZE ) | |
51 if not chunk: | |
52 break | |
53 sys.stderr.write( chunk ) | |
54 sys.exit( return_code ) | |
55 tmp_stderr.close() | |
56 data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name ) | |
57 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ) | |
58 | |
59 | |
60 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ): | |
61 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) | |
62 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] ) | |
63 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry ) | |
64 return data_manager_dict | |
65 | |
66 | |
67 def main(): | |
68 #Parse Command Line | |
69 parser = optparse.OptionParser() | |
70 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' ) | |
71 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' ) | |
72 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' ) | |
73 parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name' ) | |
74 parser.add_option( '-c', '--color_space', dest='color_space', action='store_true', default=False, help='color_space' ) | |
75 (options, args) = parser.parse_args() | |
76 | |
77 filename = args[0] | |
78 | |
79 params = from_json_string( open( filename ).read() ) | |
80 target_directory = params[ 'output_data' ][0]['extra_files_path'] | |
81 os.mkdir( target_directory ) | |
82 data_manager_dict = {} | |
83 | |
84 dbkey = options.fasta_dbkey | |
85 | |
86 if dbkey in [ None, '', '?' ]: | |
87 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) ) | |
88 | |
89 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description ) | |
90 | |
91 #build the index | |
92 build_bowtie_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 ) | |
93 | |
94 #save info to json file | |
95 open( filename, 'wb' ).write( to_json_string( data_manager_dict ) ) | |
96 | |
97 if __name__ == "__main__": | |
98 main() |