Mercurial > repos > devteam > data_manager_bwa_mem_index_builder
annotate data_manager/bwa_mem_index_builder.py @ 5:46066df8813d draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
author | iuc |
---|---|
date | Tue, 04 Apr 2017 18:07:04 -0400 |
parents | 36447bb36384 |
children | 2a386b2383a1 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
2 # Dan Blankenberg |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
3 from __future__ import print_function |
0 | 4 |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
5 import optparse |
0 | 6 import os |
7 import subprocess | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
8 import sys |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
9 from json import dumps, loads |
0 | 10 |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
11 CHUNK_SIZE = 2**20 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
12 TWO_GB = 2**30 * 2 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
13 DEFAULT_DATA_TABLE_NAME = "bwa_mem_indexes" |
0 | 14 |
15 | |
16 def get_id_name( params, dbkey, fasta_description=None): | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
17 # TODO: ensure sequence_id is unique and does not already appear in location file |
0 | 18 sequence_id = params['param_dict']['sequence_id'] |
19 if not sequence_id: | |
20 sequence_id = dbkey | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
21 |
0 | 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 | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
29 |
0 | 30 def build_bwa_index( data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_name=DEFAULT_DATA_TABLE_NAME ): |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
31 # TODO: allow multiple FASTA input files |
0 | 32 fasta_base_name = os.path.split( fasta_filename )[-1] |
33 sym_linked_fasta_filename = os.path.join( target_directory, fasta_base_name ) | |
34 os.symlink( fasta_filename, sym_linked_fasta_filename ) | |
35 if params['param_dict']['index_algorithm'] == 'automatic': | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
36 if os.stat( fasta_filename ).st_size < TWO_GB: # use 2 GB as cut off for memory vs. max of 2gb database size; this is somewhat arbitrary |
0 | 37 index_algorithm = 'is' |
38 else: | |
39 index_algorithm = 'bwtsw' | |
40 else: | |
41 index_algorithm = params['param_dict']['index_algorithm'] | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
42 |
0 | 43 args = [ 'bwa', 'index', '-a', index_algorithm ] |
44 args.append( sym_linked_fasta_filename ) | |
45 proc = subprocess.Popen( args=args, shell=False, cwd=target_directory ) | |
46 return_code = proc.wait() | |
47 if return_code: | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
48 print("Error building index.", file=sys.stderr) |
0 | 49 sys.exit( return_code ) |
50 data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name ) | |
51 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ) | |
52 | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
53 |
0 | 54 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ): |
55 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) | |
56 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] ) | |
57 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry ) | |
58 return data_manager_dict | |
59 | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
60 |
0 | 61 def main(): |
62 parser = optparse.OptionParser() | |
63 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' ) | |
64 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' ) | |
65 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' ) | |
66 parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name' ) | |
67 (options, args) = parser.parse_args() | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
68 |
0 | 69 filename = args[0] |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
70 |
0 | 71 params = loads( open( filename ).read() ) |
72 target_directory = params[ 'output_data' ][0]['extra_files_path'] | |
73 os.mkdir( target_directory ) | |
74 data_manager_dict = {} | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
75 |
0 | 76 dbkey = options.fasta_dbkey |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
77 |
0 | 78 if dbkey in [ None, '', '?' ]: |
79 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) ) | |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
80 |
0 | 81 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description ) |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
82 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
83 # build the index |
0 | 84 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 ) |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
85 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
86 # save info to json file |
0 | 87 open( filename, 'wb' ).write( dumps( data_manager_dict ) ) |
5
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
88 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
89 |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
90 if __name__ == "__main__": |
46066df8813d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bwa_mem_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
91 main() |