annotate data_manager/salmon_index_builder.py @ 6:d22f69a2e80e draft

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