comparison data_manager/bowtie_index_builder.py @ 1:35c1e3785c90 draft

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