Mercurial > repos > rhpvorderman > data_manager_select_index_by_path
comparison data_manager/path_name_value_key_manager.py @ 0:5f8d9309058b draft
planemo upload for repository https://github.com/LUMC/lumc-galaxy-tools/tree/master/data_manager_select_index_by_path commit b3f86a0c89c2956f40ee0d462cb31a60eb91724a
author | rhpvorderman |
---|---|
date | Mon, 25 Sep 2017 03:35:26 -0400 |
parents | |
children | 8495c49cd056 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:5f8d9309058b |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import json | |
4 import argparse | |
5 import os | |
6 import yaml | |
7 | |
8 def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ): | |
9 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) | |
10 data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] ) | |
11 data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry ) | |
12 return data_manager_dict | |
13 | |
14 | |
15 def check_param(name, value, default=None, check_tab=True): | |
16 if value in [ None, '', '?' ]: | |
17 if default: | |
18 print "Using {0} for {1} as no value provided".format( default, name ) | |
19 value = default | |
20 else: | |
21 raise Exception( '{0} is not a valid {1}. You must specify a valid {1}.'.format( value, name ) ) | |
22 if check_tab and "\t" in value: | |
23 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 ) ) | |
24 return value | |
25 | |
26 def prefix_exists(directory, prefix): | |
27 '''checks if files exist with prefix in a directory. Returns Boolean''' | |
28 matched_files = [] | |
29 directory_files = os.listdir(directory) | |
30 for directory_file in directory_files: | |
31 if directory_file.startswith(prefix): | |
32 matched_files.append(directory_file) | |
33 # Empty list should return False | |
34 return bool(matched_files) | |
35 | |
36 def prefix_plus_extension_exists(directory, prefix, extension): | |
37 '''checks if files exist with prefix in a directory. Returns Boolean''' | |
38 matched_files = [] | |
39 directory_files = os.listdir(directory) | |
40 for directory_file in directory_files: | |
41 if directory_file.startswith(prefix) and directory_file.endswith(extension): | |
42 matched_files.append(directory_file) | |
43 # Empty list should return False | |
44 return bool(matched_files) | |
45 | |
46 def main(): | |
47 | |
48 #value = "test_value" | |
49 #name = "test_name" | |
50 #print '{0} other {1} more{0}'.format(value, name ) | |
51 #print '{0} is not a valid {1}. It may not contain a tab.'.format( value, name ) | |
52 | |
53 #Parse Command Line | |
54 parser = argparse.ArgumentParser() | |
55 parser.add_argument( '--value', action='store', type=str, default=None, help='value' ) | |
56 parser.add_argument( '--dbkey', action='store', type=str, default=None, help='dbkey' ) | |
57 parser.add_argument( '--name', action='store', type=str, default=None, help='name' ) | |
58 parser.add_argument( '--path', action='store', type=str, default=None, help='path' ) | |
59 parser.add_argument( '--data_table_name', action='store', type=str, default=None, help='path' ) | |
60 parser.add_argument( '--json_output_file', action='store', type=str, default=None, help='path' ) | |
61 options = parser.parse_args() | |
62 | |
63 path = check_param("path", options.path) | |
64 basename = os.path.basename(path) | |
65 filename = os.path.splitext(basename)[0] | |
66 name = check_param("name", options.name, default=filename) | |
67 value = check_param("value", options.value, default=name) | |
68 dbkey = check_param("dbkey", options.dbkey, default=value) | |
69 data_table_name = check_param("data_table_name", options.data_table_name) | |
70 json_output_file = check_param("json_output_file", options.json_output_file, check_tab=False) | |
71 | |
72 # Check if file or prefix exists | |
73 indexes = yaml.load(file(os.path.join(os.path.dirname(__file__), 'indexes.yml'))) | |
74 index_dict = indexes.get(data_table_name,{}) | |
75 index_name = index_dict.get('name','index') | |
76 index_extensions = index_dict.get('extensions', ['']) | |
77 no_prefix = index_dict.get('no_prefix', False) | |
78 if not no_prefix: | |
79 dirname = os.path.dirname(path) | |
80 prefix = basename | |
81 for extension in index_extensions: | |
82 if not prefix_plus_extension_exists(dirname,prefix,extension): | |
83 raise Exception( 'Unable to find files with prefix "{0}" and extension "{1}" in {2}. Is this a valid {3}?'.format( prefix, extension, dirname, index_name ) ) | |
84 else: | |
85 if not os.path.exists(path): | |
86 raise Exception( 'Unable to find path {0}.'.format( path ) ) | |
87 | |
88 if os.path.exists(json_output_file): | |
89 params = json.loads( open( json_output_file ).read() ) | |
90 print "params", params | |
91 else: | |
92 params = {} | |
93 | |
94 data_manager_dict = {} | |
95 data_table_entry = dict( value=value, dbkey=dbkey, name=name, path=path ) | |
96 _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ) | |
97 | |
98 #save info to json file | |
99 with open( json_output_file, 'wb' ) as output_file: | |
100 output_file.write( json.dumps( data_manager_dict ) ) | |
101 output_file.write( "\n" ) | |
102 | |
103 if __name__ == "__main__": | |
104 main() |