comparison data_manager/twobit_builder.py @ 3:5c77d4091880 draft

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