annotate data_manager/bowtie2_index_builder.py @ 7:224f1598642a draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 47a185ef4aba48923d64570f94a3a7a52d6b41e6"
author iuc
date Wed, 04 Dec 2019 21:11:57 +0000
parents bb050998cee2
children 47432d88b0a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
1 #!/usr/bin/env python
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
2 # Dan Blankenberg
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
3 from __future__ import print_function
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
4
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
5 import optparse
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
6 import os
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
7 import subprocess
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
8 import sys
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
9 from json import dumps, loads
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
10
2
8de13c28d92e planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 1
diff changeset
11 DEFAULT_DATA_TABLE_NAMES = ["bowtie2_indexes"]
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
12
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
13
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
14 def get_id_name( params, dbkey, fasta_description=None):
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
15 # TODO: ensure sequence_id is unique and does not already appear in location file
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
16 sequence_id = params['param_dict']['sequence_id']
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
17 if not sequence_id:
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
18 sequence_id = dbkey
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
19
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
20 sequence_name = params['param_dict']['sequence_name']
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
21 if not sequence_name:
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
22 sequence_name = fasta_description
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
23 if not sequence_name:
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
24 sequence_name = dbkey
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
25 return sequence_id, sequence_name
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
26
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
27
2
8de13c28d92e planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 1
diff changeset
28 def build_bowtie2_index( data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_names=DEFAULT_DATA_TABLE_NAMES ):
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
29 # TODO: allow multiple FASTA input files
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
30 fasta_base_name = os.path.split( fasta_filename )[-1]
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
31 sym_linked_fasta_filename = os.path.join( target_directory, fasta_base_name )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
32 os.symlink( fasta_filename, sym_linked_fasta_filename )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
33 args = [ 'bowtie2-build', sym_linked_fasta_filename, sequence_id ]
6
bb050998cee2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 5dd2e762b5b84cec1c8344ecbeaf903e7dcc292e
iuc
parents: 5
diff changeset
34 threads = os.environ.get('GALAXY_SLOTS')
bb050998cee2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 5dd2e762b5b84cec1c8344ecbeaf903e7dcc292e
iuc
parents: 5
diff changeset
35 if threads:
bb050998cee2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 5dd2e762b5b84cec1c8344ecbeaf903e7dcc292e
iuc
parents: 5
diff changeset
36 args.extend(['--threads', threads])
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
37 proc = subprocess.Popen( args=args, shell=False, cwd=target_directory )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
38 return_code = proc.wait()
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
39 if return_code:
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
40 print("Error building index.", file=sys.stderr)
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
41 sys.exit( return_code )
1
e0839cffd009 Uploaded
devteam
parents: 0
diff changeset
42 data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=sequence_id )
2
8de13c28d92e planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 1
diff changeset
43 for data_table_name in data_table_names:
8de13c28d92e planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 1
diff changeset
44 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry )
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
45
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
46
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
47 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ):
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
48 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
49 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
50 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
51 return data_manager_dict
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
52
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
53
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
54 def main():
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
55 parser = optparse.OptionParser()
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
56 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
57 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
58 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' )
2
8de13c28d92e planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 1
diff changeset
59 parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='append', type="string", default=None, help='data_table_name' )
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
60 (options, args) = parser.parse_args()
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
61
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
62 filename = args[0]
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
63
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
64 params = loads( open( filename ).read() )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
65 target_directory = params[ 'output_data' ][0]['extra_files_path']
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
66 os.mkdir( target_directory )
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
67 data_manager_dict = {}
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
68
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
69 dbkey = options.fasta_dbkey
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
70
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
71 if dbkey in [ None, '', '?' ]:
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
72 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) )
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
73
0
eec64de5f7fb Uploaded
devteam
parents:
diff changeset
74 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description )
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
75
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
76 # build the index
2
8de13c28d92e planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 1
diff changeset
77 build_bowtie2_index( data_manager_dict, options.fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name, data_table_names=options.data_table_name or DEFAULT_DATA_TABLE_NAMES )
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
78
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
79 # save info to json file
6
bb050998cee2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 5dd2e762b5b84cec1c8344ecbeaf903e7dcc292e
iuc
parents: 5
diff changeset
80 with open(filename, 'w') as json_out:
bb050998cee2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 5dd2e762b5b84cec1c8344ecbeaf903e7dcc292e
iuc
parents: 5
diff changeset
81 json_out.write( dumps( data_manager_dict, sort_keys=True ) )
5
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
82
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
83
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
84 if __name__ == "__main__":
8a892275932f planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_bowtie2_index_builder commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents: 2
diff changeset
85 main()