Mercurial > repos > devteam > data_manager_twobit_builder
comparison data_manager/twobit_builder.py @ 0:74b09c8e5f6e draft
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_twobit_builder commit 130cb0c08ad3c5b858ba46b1024dcdccc3cb68c6-dirty
author | devteam |
---|---|
date | Thu, 11 Jun 2015 16:14:27 -0400 |
parents | |
children | 9946bc39c834 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:74b09c8e5f6e |
---|---|
1 #!/usr/bin/env python | |
2 #Dan Blankenberg | |
3 | |
4 import sys, os, tempfile, optparse, uuid, subprocess | |
5 | |
6 from json import loads, dumps | |
7 | |
8 | |
9 CHUNK_SIZE = 2**20 #1mb | |
10 | |
11 def get_id_name( params, dbkey, fasta_description=None): | |
12 #TODO: ensure sequence_id is unique and does not already appear in location file | |
13 sequence_id = params['param_dict']['sequence_id'] | |
14 if not sequence_id: | |
15 sequence_id = dbkey #uuid.uuid4() generate and use an uuid | |
16 | |
17 sequence_name = params['param_dict']['sequence_name'] | |
18 if not sequence_name: | |
19 sequence_name = fasta_description | |
20 if not sequence_name: | |
21 sequence_name = dbkey | |
22 return sequence_id, sequence_name | |
23 | |
24 def build_twobit( data_manager_dict, fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name ): | |
25 twobit_base_name = "%s.2bit" % ( sequence_id ) | |
26 twobit_filename = os.path.join( target_directory, twobit_base_name ) | |
27 | |
28 args = [ 'faToTwoBit', fasta_filename, twobit_filename ] | |
29 tmp_stderr = tempfile.NamedTemporaryFile( prefix = "tmp-data-manager-twobit-builder-stderr" ) | |
30 proc = subprocess.Popen( args=args, shell=False, cwd=target_directory, stderr=tmp_stderr.fileno() ) | |
31 return_code = proc.wait() | |
32 if return_code: | |
33 tmp_stderr.flush() | |
34 tmp_stderr.seek(0) | |
35 print >> sys.stderr, "Error building index:" | |
36 while True: | |
37 chunk = tmp_stderr.read( CHUNK_SIZE ) | |
38 if not chunk: | |
39 break | |
40 sys.stderr.write( chunk ) | |
41 sys.exit( return_code ) | |
42 tmp_stderr.close() | |
43 #lastz_seqs | |
44 data_table_entry = dict( value=sequence_id, name=sequence_name, path=twobit_base_name ) | |
45 | |
46 _add_data_table_entry( data_manager_dict, "lastz_seqs", data_table_entry ) | |
47 #twobit.loc | |
48 data_table_entry = dict( value=sequence_id, path=twobit_base_name ) | |
49 | |
50 _add_data_table_entry( data_manager_dict, "twobit", data_table_entry ) | |
51 #alignseq | |
52 data_table_entry = dict( type="seq", value=sequence_id, path=twobit_base_name ) | |
53 | |
54 _add_data_table_entry( data_manager_dict, "alignseq_seq", data_table_entry ) | |
55 | |
56 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ): | |
57 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) | |
58 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] ) | |
59 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry ) | |
60 return data_manager_dict | |
61 | |
62 def main(): | |
63 #Parse Command Line | |
64 parser = optparse.OptionParser() | |
65 parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' ) | |
66 parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' ) | |
67 parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' ) | |
68 (options, args) = parser.parse_args() | |
69 | |
70 filename = args[0] | |
71 | |
72 params = loads( open( filename ).read() ) | |
73 | |
74 target_directory = params[ 'output_data' ][0]['extra_files_path'] | |
75 os.mkdir( target_directory ) | |
76 data_manager_dict = {} | |
77 | |
78 dbkey = options.fasta_dbkey | |
79 | |
80 if dbkey in [ None, '', '?' ]: | |
81 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) ) | |
82 | |
83 sequence_id, sequence_name = get_id_name( params, dbkey=dbkey, fasta_description=options.fasta_description ) | |
84 | |
85 #build the index | |
86 build_twobit( data_manager_dict, options.fasta_filename, params, target_directory, dbkey, sequence_id, sequence_name ) | |
87 | |
88 #save info to json file | |
89 open( filename, 'wb' ).write( dumps( data_manager_dict ) ) | |
90 | |
91 if __name__ == "__main__": main() | |
92 |