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