comparison WebServiceExtensionsV1.1/WebServiceToolWorkflow_REST_SOAP/clientGenerator/paramConverter.py @ 0:049760c677de default tip

Galaxy WSExtensions added successfully
author uga-galaxy-group
date Tue, 05 Jul 2011 19:34:18 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:049760c677de
1 '''
2 @author Rui Wnag, Chaitanya Guttula
3 @see LICENSE (MIT style license file).
4 '''
5
6 '''
7 converter galaxy parameter <-----> user input dictionary for web service
8 '|' is the seperator, if the real name contains it, it will cause wrong result in this converter
9 '''
10 from types import *
11 #from galaxy.tools.parameters.basic import *
12
13 __author__="Rui Wang"
14
15 def nested2flatDict(nestedDic):
16 '''
17 nestedDic is dictionary e.g.{a:{a1:None, a2:None}, b:None, c:[{c1:None, c2:None},{c1:None, c2:None}]},
18 converter it into flatDict e.g.
19 {a|a1:TextToolParameter(), a|a2:TextToolParameter(), b:TextToolParameter(),
20 c|1|c1:TextToolParameter(),c|1|c2:TextToolParameter(), c|2|c1:TextToolParameter(),c|2|c2:TextToolParameter()}
21 seperator is |, not /
22 '''
23 if nestedDic is None or len(nestedDic) == 0:
24 return {};
25 flatDic={}
26
27 for key, value in nestedDic.iteritems():
28 if type(value) is DictType:
29 dicRecur(key, value, flatDic);
30 elif type(value) is ListType:
31 listRecur(key, value, flatDic);
32 elif value is None:
33 flatDic[key]=None#TextToolParameter(None, XML( '<param name="' + str(key) + '" type="text" size="10" value="default" />' ) )
34 else:
35 flatDic[key]=str(value)
36 return flatDic
37
38 def dicRecur(prefix, dic, flatDic):
39 '''
40 '''
41 if dic is None or len(dic) == 0:
42 raise ValueError, 'dic is empty'
43 i=0;
44 for key, value in dic.items():
45 if type(value) is DictType:
46 #print key
47 dicRecur(prefix+"|"+key, value, flatDic);
48 elif type(value) is ListType:
49 listRecur(prefix+"|"+key, value, flatDic);
50 elif value is None:
51 #print 'key',key
52 #if (key.find('$')>-1):
53 # vlist = key.split('$')
54 # key = ''.join(vlist)
55
56 #print 'key',key
57 # flatDic[prefix+"|_"+str(i)+"|"+key]=None
58 # i=i+1
59 #else:
60 flatDic[prefix+"|"+key]=None#TextToolParameter(None, XML( '<param name="' + str(prefix) + '|' + str(key) + '" type="text" size="10" value="default" />' ) )
61 else:
62 #print prefix
63 #if (key.find('$')>-1):
64 # vlist = key.split('$')
65 # key = ''.join(vlist)
66
67 # print 'key',key
68 # flatDic[prefix+"|_"+str(i)+"|"+key]=None
69 # i=i+1
70 #else:
71 flatDic[prefix+"|"+key]=str(value)
72
73 def listRecur(prefix, list, flatDic):
74 ''''''
75
76 #You are expecting this list to contain a complexType.
77 #When it doesn't you raise a ValueError.
78 #If the list contains a simpletype then we add |$|
79 # when flatenning.
80 if list is None or len(list)==0:
81 flatDic[prefix+'|$|'] = [];
82 return [];
83 i=0
84 for value in list:
85 if type(value) is DictType:
86 #print prefix
87 dicRecur(prefix+"|"+str(i), value, flatDic);
88 #else:#if type(value) is ListType:
89 # if prefix+'|0|' not in flatDic:
90 # flatDic[prefix+'|0|'] = []
91 # flatDic[prefix+'|0|'].append(value)
92 # return ;
93 i += 1;
94
95 def flat2nestedDict(flatDic):
96 '''the flatDic has value in it
97 converter it to nested dictionary'''
98 if flatDic is None or flatDic=={}:
99 raise ValueError, 'flatDic is empty'
100 nestedDic = {};
101 for key, value in flatDic.iteritems() :
102 key_arr = key.split('|');
103 print key_arr
104 last_idx = len(key_arr) - 1;
105 sub_dic = nestedDic;
106 if last_idx != 0 :
107 i = 0;
108 # Iterates over the split values of a single key
109 while i < last_idx :
110
111 try :
112 key_int = int(key_arr[i]); # checking if the split value of key is number (true only if it )
113 if key_int >= len(sub_dic) :
114 for j in range(key_int - len(sub_dic) + 1) :
115 sub_dic.append({});
116 sub_dic = sub_dic[key_int];
117
118 except ValueError :
119
120 #If the split value is not present in thhe subdic
121 if key_arr[i] == '$':
122 print '$ is there'#,nestedDic
123 else:
124 if key_arr[i] not in sub_dic :
125 try :
126 key_int = int(key_arr[i + 1]); # Ckecking of the next split value in key is number
127
128 # Executed if the key is or array type key
129 sub_list = [];
130 sub_dic[key_arr[i]] = sub_list;
131 for j in range(key_int + 1) :
132 sub_list.append({});
133 sub_dic = sub_list[key_int];
134 i += 1;
135
136 except ValueError : # 2 consicutive split values in key are not numerical value (i.e not array)
137 if key_arr[last_idx-1] == '$' :
138 if i == last_idx-2 :
139 print 'Found the value',key_arr[i]
140 else:
141 temp = {};
142 sub_dic[key_arr[i]] = temp;
143 sub_dic = temp;
144
145 else:
146 temp = {};
147 sub_dic[key_arr[i]] = temp;
148 sub_dic = temp;
149
150 # If the split value is present in the subdir then create a new subdic with
151 else :
152 sub_dic = nestedDic[key_arr[i]];
153
154 i += 1;
155 if value == '' :
156 value = None;
157 if key_arr[last_idx-1] == '$':
158 templist=[];
159 if value != []:
160 if value.find(',')>-1:
161 vlist = value.split(',')
162 for l in vlist:
163 templist.append(l);
164 else:
165 templist.append(value);
166 sub_dic[key_arr[last_idx-2]] = templist;
167 else:
168 sub_dic[key_arr[last_idx]] = value;
169
170 # If the leaf node is basic array type
171 #if key_arr[last_idx] == '':
172 # sub_dic[key_arr[last_idx-2]].append(value);
173 #else:
174 # sub_dic[key_arr[last_idx]] = value;
175
176 return nestedDic
177
178 #testing this module only, before run below test, please comment line 6,30,33,50,53, and uncomment line 29,32,49,52, since they will use other modules of galaxy
179 if __name__=="__main__":
180
181 # nestedDic={'_params':{ '_program' : 'blastp', '_database' :'swissprot', '_email' :'riververy@yahoo.com', '_async': 1}, '_content':[{'_type':'sequence', '_content':'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'},{'_type':'ss', '_content':'bbbbbbbbbbbbbbbb'}]}
182 nestedDic={'_params':{ '_program' : 'blastp', '_database' :{'_string':[]}, '_email' :'riververy@yahoo.com', '_async': '', '_test':[{'_name':'chaithu'},{'_name':'srinivas'}]}, '_content':[{'_type':'sequence', '_content':'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'},{'_type':'ss', '_content':'bbbbbbbbbbbbbbbb'}]}
183 #flatdic = nested2flatDict(nestedDic)
184 #print flatdic
185 flatDic={'_parameters|_program': 'blastp', '_parameters|_stype': 'protein', '_parameters|_sequence': 'MKLSKRYRFWQKVIKALGVLALIATLVLVVYLYKLGILNDSNELKDLVHKYEFWGPMIFIVAQIVQIVFPVIPGGVTTVAGFLIFGPTLGFIYNYIGIIIGSVILFWLVKFYGRKFVLLF', '_email': 'chaitanya.g86@gmail.com', '_parameters|_database|_string|$|': 'uniprotkb,swissprot'}
186 #{'_params|_email': 'riververy@yahoo.com', '_params|_database': 'swissprot', '_params|_async': '1', '_content|0|_type': 'sequence', '_content|1|_type': 'ss','_params|_program|0|': 'hrllo', '_content|1|_content':'bbbbbbbbbbbbbbbb', '_content|0|_content': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'}
187 print flat2nestedDict(flatDic)
188
189
190
191
192
193
194
195
196
197
198