Mercurial > repos > devteam > data_manager_bowtie2_index_builder
comparison data_manager/bowtie2_index_builder.py @ 1:9bf4eb559ed5 draft
planemo upload commit 4381f4022eca6f3c58ae1d01096795cd28c15062
author | devteam |
---|---|
date | Mon, 10 Aug 2015 16:44:10 -0400 |
parents | 9458af1ab90b |
children | 83da94c0e4a6 |
comparison
equal
deleted
inserted
replaced
0:9458af1ab90b | 1:9bf4eb559ed5 |
---|---|
7 import subprocess | 7 import subprocess |
8 | 8 |
9 from json import loads, dumps | 9 from json import loads, dumps |
10 | 10 |
11 | 11 |
12 DEFAULT_DATA_TABLE_NAME = "bowtie2_indexes" | 12 DEFAULT_DATA_TABLE_NAMES = ["bowtie2_indexes"] |
13 | 13 |
14 def get_id_name( params, dbkey, fasta_description=None): | 14 def get_id_name( params, dbkey, fasta_description=None): |
15 #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 |
16 sequence_id = params['param_dict']['sequence_id'] | 16 sequence_id = params['param_dict']['sequence_id'] |
17 if not sequence_id: | 17 if not sequence_id: |
22 sequence_name = fasta_description | 22 sequence_name = fasta_description |
23 if not sequence_name: | 23 if not sequence_name: |
24 sequence_name = dbkey | 24 sequence_name = dbkey |
25 return sequence_id, sequence_name | 25 return sequence_id, sequence_name |
26 | 26 |
27 def build_bowtie2_index( data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_name=DEFAULT_DATA_TABLE_NAME ): | 27 def build_bowtie2_index( data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_names=DEFAULT_DATA_TABLE_NAMES ): |
28 #TODO: allow multiple FASTA input files | 28 #TODO: allow multiple FASTA input files |
29 fasta_base_name = os.path.split( fasta_filename )[-1] | 29 fasta_base_name = os.path.split( fasta_filename )[-1] |
30 sym_linked_fasta_filename = os.path.join( target_directory, fasta_base_name ) | 30 sym_linked_fasta_filename = os.path.join( target_directory, fasta_base_name ) |
31 os.symlink( fasta_filename, sym_linked_fasta_filename ) | 31 os.symlink( fasta_filename, sym_linked_fasta_filename ) |
32 args = [ 'bowtie2-build', sym_linked_fasta_filename, sequence_id ] | 32 args = [ 'bowtie2-build', sym_linked_fasta_filename, sequence_id ] |
34 return_code = proc.wait() | 34 return_code = proc.wait() |
35 if return_code: | 35 if return_code: |
36 print >> sys.stderr, "Error building index." | 36 print >> sys.stderr, "Error building index." |
37 sys.exit( return_code ) | 37 sys.exit( return_code ) |
38 data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=sequence_id ) | 38 data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=sequence_id ) |
39 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ) | 39 for data_table_name in data_table_names: |
40 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ) | |
40 | 41 |
41 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ): | 42 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ): |
42 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) | 43 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) |
43 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] ) | 44 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] ) |
44 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry ) | 45 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry ) |
48 #Parse Command Line | 49 #Parse Command Line |
49 parser = optparse.OptionParser() | 50 parser = optparse.OptionParser() |
50 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' ) | 51 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' ) |
51 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' ) | 52 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' ) |
52 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' ) | 53 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' ) |
53 parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name' ) | 54 parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='append', type="string", default=None, help='data_table_name' ) |
54 (options, args) = parser.parse_args() | 55 (options, args) = parser.parse_args() |
55 | 56 |
56 filename = args[0] | 57 filename = args[0] |
57 | 58 |
58 params = loads( open( filename ).read() ) | 59 params = loads( open( filename ).read() ) |
66 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) ) | 67 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) ) |
67 | 68 |
68 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description ) | 69 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description ) |
69 | 70 |
70 #build the index | 71 #build the index |
71 build_bowtie2_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 ) | 72 build_bowtie2_index( data_manager_dict, options.fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_names=options.data_table_name or DEFAULT_DATA_TABLE_NAMES ) |
72 | 73 |
73 #save info to json file | 74 #save info to json file |
74 open( filename, 'wb' ).write( dumps( data_manager_dict ) ) | 75 open( filename, 'wb' ).write( dumps( data_manager_dict ) ) |
75 | 76 |
76 if __name__ == "__main__": main() | 77 if __name__ == "__main__": main() |