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
|
6
|
11 DEFAULT_DATA_TABLE_NAME = "salmon_indexes_versioned"
|
1
|
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 )
|
2
|
33 args = [ 'salmon', 'index' ]
|
1
|
34 if options.kmer_size != '':
|
|
35 args.append('-k')
|
|
36 args.append(options.kmer_size)
|
6
|
37 args.extend( [ '-t', options.fasta_filename, '-i', target_directory ] )
|
4
|
38 proc = subprocess.Popen( args=args, shell=False)
|
1
|
39 return_code = proc.wait()
|
|
40 if return_code:
|
|
41 print("Error building index.", file=sys.stderr)
|
|
42 sys.exit( return_code )
|
6
|
43 data_table_entry = dict( value=sequence_id, dbkey=options.fasta_dbkey, name=sequence_name, path=sequence_id, version=options.index_version )
|
1
|
44 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry )
|
|
45
|
|
46
|
|
47 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ):
|
|
48 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
|
|
49 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] )
|
|
50 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry )
|
|
51 return data_manager_dict
|
|
52
|
|
53
|
|
54 def main():
|
|
55 # Parse Command Line
|
|
56 parser = argparse.ArgumentParser()
|
|
57 parser.add_argument( '--output', dest='output', action='store', type=str, default=None )
|
|
58 parser.add_argument( '--fasta_filename', dest='fasta_filename', action='store', type=str, default=None )
|
|
59 parser.add_argument( '--fasta_dbkey', dest='fasta_dbkey', action='store', type=str, default=None )
|
|
60 parser.add_argument( '--fasta_description', dest='fasta_description', action='store', type=str, default=None )
|
|
61 parser.add_argument( '--data_table_name', dest='data_table_name', action='store', type=str, default='salmon_indexes' )
|
|
62 parser.add_argument( '-k', '--kmer_size', dest='kmer_size', action='store', type=str, help='kmer_size' )
|
|
63 options = parser.parse_args()
|
|
64
|
|
65 filename = options.output
|
|
66
|
|
67 params = loads( open( filename ).read() )
|
|
68 data_manager_dict = {}
|
|
69
|
|
70 if options.fasta_dbkey in [ None, '', '?' ]:
|
|
71 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( options.fasta_dbkey ) )
|
|
72
|
|
73 sequence_id, sequence_name = get_id_name( params, dbkey=options.fasta_dbkey, fasta_description=options.fasta_description )
|
|
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()
|