comparison data_manager/bowtie_index_builder.py @ 5:08a3583826fa draft

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