comparison data_manager/tagdust_architecture_data_manager.py @ 0:e3b3261e5498 draft default tip

Uploaded
author brenninc
date Sun, 08 May 2016 04:44:17 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e3b3261e5498
1 #!/usr/bin/env python
2
3 import json
4 import optparse
5 import os.path
6
7 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ):
8 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
9 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] )
10 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry )
11 return data_manager_dict
12
13
14 def get_param(name, params, default=None, check_tab=True):
15 value = params.get(name)
16 print name, value
17 return check_param(name, value, default=default, check_tab=check_tab)
18
19
20 def check_param(name, value, default=None, check_tab=True):
21 if value in [ None, '', '?' ]:
22 if default:
23 print "Using {0} for {1} as no value provided".format( default, name )
24 value = default
25 else:
26 raise Exception( '{0} is not a valid {1}. You must specify a valid {1}.'.format( value, name ) )
27 if check_tab and "\t" in value:
28 raise Exception( '{0} is not a valid {1}. It may not contain a tab because these are used as seperators by galaxy .'.format( value, name ) )
29 return value
30
31
32 def createFileBasedOnHmm(hmms):
33 file_name = ""
34 bar_code = "no"
35 for hmm in hmms:
36 block = hmm["block"].strip()
37 if not (block[0] in ['R','O','G','B','F','S','P']):
38 raise Exception( "hmm block {0} is not a valid. It must start with one of ['R','O','G','B','F','S','P'].".format( block ) )
39 if block[0] == 'B':
40 bar_code = "yes"
41 if block[1] != ':':
42 raise Exception( "hmm block {0} is not a valid. The second character must be ':'".format( block ) )
43 if "\t" in hmm:
44 raise Exception( "hmm block {0} is not a valid. It may not contain a tab, due to galaxy using tabs as seperators".format( block ) )
45 file_name = file_name + block + "_"
46 file_name = file_name[:-1] + ".txt"
47 return bar_code, file_name
48
49
50 def get_path(galaxy_tool_dir, file_name):
51 file_path = os.path.join(galaxy_tool_dir, "tagdust_architecture")
52 if os.path.exists(file_path):
53 if os.path.isfile(file_path):
54 raise Exception( "Found a file at {0}, but expecting a directory there".format( file_path ) )
55 else:
56 os.mkdir(file_path)
57 return os.path.join(file_path, file_name)
58
59
60 def writeHmm(hmms, file_path):
61 with open( file_path, 'w' ) as output_file:
62 output_file.write("./tagdust")
63 for i, hmm in enumerate( hmms, 1 ):
64 output_file.write(" ")
65 output_file.write(str(-i))
66 output_file.write(" ")
67 output_file.write(hmm["block"])
68 output_file.write("\n")
69
70 def main():
71
72 #Parse Command Line
73 parser = optparse.OptionParser()
74 parser.add_option( '--data_table_name', action='store', type="string", default=None, help='path' )
75 parser.add_option( '--json_output_file', action='store', type="string", default=None, help='path' )
76 (options, args) = parser.parse_args()
77
78 data_table_name = check_param("data_table_name", options.data_table_name)
79 json_output_file = check_param("json_output_file", options.json_output_file, check_tab=False)
80
81 param_dict = json.loads( open( json_output_file ).read() )
82 params = param_dict.get("param_dict")
83 print "input params:"
84 print params
85
86 hmms = get_param("hmms", params)
87 galaxy_tool_dir = get_param("GALAXY_DATA_INDEX_DIR", params)
88
89 data_table_entry = {}
90
91 data_table_entry["barcode"], file_name = createFileBasedOnHmm(hmms)
92 data_table_entry["path"] = get_path(galaxy_tool_dir, file_name)
93 writeHmm(hmms, data_table_entry["path"])
94
95 basename = os.path.basename(data_table_entry["path"])
96 filename = os.path.splitext(basename)[0]
97 data_table_entry["name"] = get_param("name", params, default=filename)
98 data_table_entry["value"] = get_param("value", params, default=data_table_entry["name"])
99 data_table_entry["dbkey"] = get_param("dbkey", params, default=data_table_entry["value"])
100
101 data_manager_dict = {}
102 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry )
103
104 print "output:"
105 print data_manager_dict
106 # save info to json file
107 with open( json_output_file, 'wb' ) as output_file:
108 output_file.write( json.dumps( data_manager_dict ) )
109 output_file.write( "\n" )
110
111
112 if __name__ == "__main__":
113 main()