annotate data_manager/picard_index_builder.py @ 2:a8a705f6be94 draft

planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
author devteam
date Tue, 13 Oct 2015 10:18:49 -0400
parents 9a024421602a
children 0663d25e619c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9a024421602a Uploaded
devteam
parents:
diff changeset
1 #!/usr/bin/env python
9a024421602a Uploaded
devteam
parents:
diff changeset
2 #Dan Blankenberg
9a024421602a Uploaded
devteam
parents:
diff changeset
3
9a024421602a Uploaded
devteam
parents:
diff changeset
4 import sys
9a024421602a Uploaded
devteam
parents:
diff changeset
5 import os
9a024421602a Uploaded
devteam
parents:
diff changeset
6 import optparse
9a024421602a Uploaded
devteam
parents:
diff changeset
7 import subprocess
9a024421602a Uploaded
devteam
parents:
diff changeset
8
9a024421602a Uploaded
devteam
parents:
diff changeset
9 from json import loads, dumps
9a024421602a Uploaded
devteam
parents:
diff changeset
10
9a024421602a Uploaded
devteam
parents:
diff changeset
11
9a024421602a Uploaded
devteam
parents:
diff changeset
12 DEFAULT_DATA_TABLE_NAME = "picard_indexes"
9a024421602a Uploaded
devteam
parents:
diff changeset
13
9a024421602a Uploaded
devteam
parents:
diff changeset
14 def get_id_name( params, dbkey, fasta_description=None):
9a024421602a Uploaded
devteam
parents:
diff changeset
15 #TODO: ensure sequence_id is unique and does not already appear in location file
9a024421602a Uploaded
devteam
parents:
diff changeset
16 sequence_id = params['param_dict']['sequence_id']
9a024421602a Uploaded
devteam
parents:
diff changeset
17 if not sequence_id:
9a024421602a Uploaded
devteam
parents:
diff changeset
18 sequence_id = dbkey
9a024421602a Uploaded
devteam
parents:
diff changeset
19
9a024421602a Uploaded
devteam
parents:
diff changeset
20 sequence_name = params['param_dict']['sequence_name']
9a024421602a Uploaded
devteam
parents:
diff changeset
21 if not sequence_name:
9a024421602a Uploaded
devteam
parents:
diff changeset
22 sequence_name = fasta_description
9a024421602a Uploaded
devteam
parents:
diff changeset
23 if not sequence_name:
9a024421602a Uploaded
devteam
parents:
diff changeset
24 sequence_name = dbkey
9a024421602a Uploaded
devteam
parents:
diff changeset
25 return sequence_id, sequence_name
9a024421602a Uploaded
devteam
parents:
diff changeset
26
9a024421602a Uploaded
devteam
parents:
diff changeset
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 ):
9a024421602a Uploaded
devteam
parents:
diff changeset
28 #TODO: allow multiple FASTA input files
9a024421602a Uploaded
devteam
parents:
diff changeset
29 fasta_base_name = os.path.split( fasta_filename )[-1]
9a024421602a Uploaded
devteam
parents:
diff changeset
30 prefix = os.path.splitext( fasta_base_name )[0]
9a024421602a Uploaded
devteam
parents:
diff changeset
31 sym_linked_fasta_filename = os.path.join( target_directory, fasta_base_name )
9a024421602a Uploaded
devteam
parents:
diff changeset
32 os.symlink( fasta_filename, sym_linked_fasta_filename )
9a024421602a Uploaded
devteam
parents:
diff changeset
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 ]
9a024421602a Uploaded
devteam
parents:
diff changeset
34 proc = subprocess.Popen( args=args, shell=False, cwd=target_directory )
9a024421602a Uploaded
devteam
parents:
diff changeset
35 return_code = proc.wait()
9a024421602a Uploaded
devteam
parents:
diff changeset
36 if return_code:
9a024421602a Uploaded
devteam
parents:
diff changeset
37 print >> sys.stderr, "Error building index."
9a024421602a Uploaded
devteam
parents:
diff changeset
38 sys.exit( return_code )
9a024421602a Uploaded
devteam
parents:
diff changeset
39 data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name )
9a024421602a Uploaded
devteam
parents:
diff changeset
40 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry )
9a024421602a Uploaded
devteam
parents:
diff changeset
41
9a024421602a Uploaded
devteam
parents:
diff changeset
42 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ):
9a024421602a Uploaded
devteam
parents:
diff changeset
43 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
9a024421602a Uploaded
devteam
parents:
diff changeset
44 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] )
9a024421602a Uploaded
devteam
parents:
diff changeset
45 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry )
9a024421602a Uploaded
devteam
parents:
diff changeset
46 return data_manager_dict
9a024421602a Uploaded
devteam
parents:
diff changeset
47
9a024421602a Uploaded
devteam
parents:
diff changeset
48 def main():
9a024421602a Uploaded
devteam
parents:
diff changeset
49 #Parse Command Line
9a024421602a Uploaded
devteam
parents:
diff changeset
50 parser = optparse.OptionParser()
9a024421602a Uploaded
devteam
parents:
diff changeset
51 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' )
9a024421602a Uploaded
devteam
parents:
diff changeset
52 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' )
9a024421602a Uploaded
devteam
parents:
diff changeset
53 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' )
9a024421602a Uploaded
devteam
parents:
diff changeset
54 parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name' )
9a024421602a Uploaded
devteam
parents:
diff changeset
55 (options, args) = parser.parse_args()
9a024421602a Uploaded
devteam
parents:
diff changeset
56
9a024421602a Uploaded
devteam
parents:
diff changeset
57 filename = args[0]
9a024421602a Uploaded
devteam
parents:
diff changeset
58
9a024421602a Uploaded
devteam
parents:
diff changeset
59 params = loads( open( filename ).read() )
9a024421602a Uploaded
devteam
parents:
diff changeset
60 target_directory = params[ 'output_data' ][0]['extra_files_path']
9a024421602a Uploaded
devteam
parents:
diff changeset
61 os.mkdir( target_directory )
9a024421602a Uploaded
devteam
parents:
diff changeset
62 data_manager_dict = {}
9a024421602a Uploaded
devteam
parents:
diff changeset
63
9a024421602a Uploaded
devteam
parents:
diff changeset
64 dbkey = options.fasta_dbkey
9a024421602a Uploaded
devteam
parents:
diff changeset
65
9a024421602a Uploaded
devteam
parents:
diff changeset
66 if dbkey in [ None, '', '?' ]:
9a024421602a Uploaded
devteam
parents:
diff changeset
67 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) )
9a024421602a Uploaded
devteam
parents:
diff changeset
68
9a024421602a Uploaded
devteam
parents:
diff changeset
69 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description )
9a024421602a Uploaded
devteam
parents:
diff changeset
70
9a024421602a Uploaded
devteam
parents:
diff changeset
71 #build the index
9a024421602a Uploaded
devteam
parents:
diff changeset
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 )
9a024421602a Uploaded
devteam
parents:
diff changeset
73
9a024421602a Uploaded
devteam
parents:
diff changeset
74 #save info to json file
9a024421602a Uploaded
devteam
parents:
diff changeset
75 open( filename, 'wb' ).write( dumps( data_manager_dict ) )
9a024421602a Uploaded
devteam
parents:
diff changeset
76
9a024421602a Uploaded
devteam
parents:
diff changeset
77 if __name__ == "__main__": main()