1
|
1 #!/usr/bin/env python
|
|
2 # Based heavily on the kallisto data manager wrapper script by iuc
|
|
3 from __future__ import print_function
|
|
4
|
|
5 import argparse
|
|
6 import os
|
|
7 import subprocess
|
|
8 import sys
|
|
9 from json import dumps, loads
|
|
10
|
|
11 DEFAULT_DATA_TABLE_NAME = "salmon_indexes"
|
|
12
|
|
13
|
|
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
|
|
16 sequence_id = params['param_dict']['sequence_id']
|
|
17 if not sequence_id:
|
|
18 sequence_id = dbkey
|
|
19
|
|
20 sequence_name = params['param_dict']['sequence_name']
|
|
21 if not sequence_name:
|
|
22 sequence_name = fasta_description
|
|
23 if not sequence_name:
|
|
24 sequence_name = dbkey
|
|
25 return sequence_id, sequence_name
|
|
26
|
|
27
|
|
28 def build_salmon_index( data_manager_dict, options, params, sequence_id, sequence_name ):
|
|
29 data_table_name = options.data_table_name or DEFAULT_DATA_TABLE_NAME
|
|
30 target_directory = params[ 'output_data' ][0]['extra_files_path']
|
|
31 if not os.path.exists( target_directory ):
|
|
32 os.mkdir( target_directory )
|
|
33 if options.kmer_size != '':
|
|
34 args.append('-k')
|
|
35 args.append(options.kmer_size)
|
|
36 args.extend( [ '-t' , options.fasta_filename, '-i', sequence_id ] )
|
|
37 proc = subprocess.Popen( args=args, shell=False, cwd=target_directory )
|
|
38 return_code = proc.wait()
|
|
39 if return_code:
|
|
40 print("Error building index.", file=sys.stderr)
|
|
41 sys.exit( return_code )
|
|
42 data_table_entry = dict( value=sequence_id, dbkey=options.fasta_dbkey, name=sequence_name, path=sequence_id )
|
|
43 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry )
|
|
44
|
|
45
|
|
46 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ):
|
|
47 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
|
|
48 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] )
|
|
49 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry )
|
|
50 return data_manager_dict
|
|
51
|
|
52
|
|
53 def main():
|
|
54 # Parse Command Line
|
|
55 parser = argparse.ArgumentParser()
|
|
56 parser.add_argument( '--output', dest='output', action='store', type=str, default=None )
|
|
57 parser.add_argument( '--fasta_filename', dest='fasta_filename', action='store', type=str, default=None )
|
|
58 parser.add_argument( '--fasta_dbkey', dest='fasta_dbkey', action='store', type=str, default=None )
|
|
59 parser.add_argument( '--fasta_description', dest='fasta_description', action='store', type=str, default=None )
|
|
60 parser.add_argument( '--data_table_name', dest='data_table_name', action='store', type=str, default='salmon_indexes' )
|
|
61 parser.add_argument( '-k', '--kmer_size', dest='kmer_size', action='store', type=str, help='kmer_size' )
|
|
62 options = parser.parse_args()
|
|
63
|
|
64 filename = options.output
|
|
65
|
|
66 params = loads( open( filename ).read() )
|
|
67 data_manager_dict = {}
|
|
68
|
|
69 if options.fasta_dbkey in [ None, '', '?' ]:
|
|
70 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( options.fasta_dbkey ) )
|
|
71
|
|
72 sequence_id, sequence_name = get_id_name( params, dbkey=options.fasta_dbkey, fasta_description=options.fasta_description )
|
|
73
|
|
74 # build the index
|
|
75 build_salmon_index( data_manager_dict, options, params, sequence_id, sequence_name )
|
|
76
|
|
77 # save info to json file
|
|
78 open( filename, 'w' ).write( dumps( data_manager_dict ) )
|
|
79
|
|
80
|
|
81 if __name__ == "__main__":
|
|
82 main()
|