comparison data_manager/picard_index_builder.py @ 1:dc28cd6a1d44 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_picard_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
author iuc
date Tue, 04 Apr 2017 18:12:10 -0400
parents 00491eabe22b
children b99040168706
comparison
equal deleted inserted replaced
0:00491eabe22b 1:dc28cd6a1d44
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 #Dan Blankenberg 2 # Dan Blankenberg
3 from __future__ import print_function
3 4
5 import optparse
6 import os
7 import subprocess
4 import sys 8 import sys
5 import os 9 from json import dumps, loads
6 import optparse
7 import subprocess
8
9 from json import loads, dumps
10
11 10
12 DEFAULT_DATA_TABLE_NAME = "picard_indexes" 11 DEFAULT_DATA_TABLE_NAME = "picard_indexes"
13 12
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:
18 sequence_id = dbkey 18 sequence_id = dbkey
19 19
20 sequence_name = params['param_dict']['sequence_name'] 20 sequence_name = params['param_dict']['sequence_name']
21 if not sequence_name: 21 if not sequence_name:
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
27 def build_picard_index( data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_name=DEFAULT_DATA_TABLE_NAME ): 28 def build_picard_index( data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_name=DEFAULT_DATA_TABLE_NAME ):
28 #TODO: allow multiple FASTA input files 29 # TODO: allow multiple FASTA input files
29 fasta_base_name = os.path.split( fasta_filename )[-1] 30 fasta_base_name = os.path.split( fasta_filename )[-1]
30 prefix = os.path.splitext( fasta_base_name )[0] 31 prefix = os.path.splitext( fasta_base_name )[0]
31 sym_linked_fasta_filename = os.path.join( target_directory, fasta_base_name ) 32 sym_linked_fasta_filename = os.path.join( target_directory, fasta_base_name )
32 os.symlink( fasta_filename, sym_linked_fasta_filename ) 33 os.symlink( fasta_filename, sym_linked_fasta_filename )
33 args = [ 'java', '-Xmx4G', '-jar', os.path.join( os.environ[ 'JAVA_JAR_PATH' ], 'CreateSequenceDictionary.jar' ), 'R=%s' % sym_linked_fasta_filename, 'O=%s.dict' % prefix ] 34 args = [ 'java', '-Xmx4G', '-jar', os.path.join( os.environ[ 'JAVA_JAR_PATH' ], 'CreateSequenceDictionary.jar' ), 'R=%s' % sym_linked_fasta_filename, 'O=%s.dict' % prefix ]
34 proc = subprocess.Popen( args=args, shell=False, cwd=target_directory ) 35 proc = subprocess.Popen( args=args, shell=False, cwd=target_directory )
35 return_code = proc.wait() 36 return_code = proc.wait()
36 if return_code: 37 if return_code:
37 print >> sys.stderr, "Error building index." 38 print("Error building index.", file=sys.stderr)
38 sys.exit( return_code ) 39 sys.exit( return_code )
39 data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name ) 40 data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name )
40 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ) 41 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry )
42
41 43
42 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ): 44 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ):
43 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) 45 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
44 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] ) 46 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] )
45 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry ) 47 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry )
46 return data_manager_dict 48 return data_manager_dict
47 49
50
48 def main(): 51 def main():
49 #Parse Command Line
50 parser = optparse.OptionParser() 52 parser = optparse.OptionParser()
51 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' ) 53 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' )
52 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' ) 54 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' )
53 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' ) 55 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' )
54 parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name' ) 56 parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name' )
55 (options, args) = parser.parse_args() 57 (options, args) = parser.parse_args()
56 58
57 filename = args[0] 59 filename = args[0]
58 60
59 params = loads( open( filename ).read() ) 61 params = loads( open( filename ).read() )
60 target_directory = params[ 'output_data' ][0]['extra_files_path'] 62 target_directory = params[ 'output_data' ][0]['extra_files_path']
61 os.mkdir( target_directory ) 63 os.mkdir( target_directory )
62 data_manager_dict = {} 64 data_manager_dict = {}
63 65
64 dbkey = options.fasta_dbkey 66 dbkey = options.fasta_dbkey
65 67
66 if dbkey in [ None, '', '?' ]: 68 if dbkey in [ None, '', '?' ]:
67 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) ) 69 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) )
68 70
69 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description ) 71 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description )
70 72
71 #build the index 73 # build the index
72 build_picard_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 ) 74 build_picard_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 )
73 75
74 #save info to json file 76 # save info to json file
75 open( filename, 'wb' ).write( dumps( data_manager_dict ) ) 77 open( filename, 'wb' ).write( dumps( data_manager_dict ) )
76 78
77 if __name__ == "__main__": main() 79
80 if __name__ == "__main__":
81 main()