annotate toolfactory/testclass.py @ 6:efefe43f23c8 draft default tip

Uploaded
author fubar
date Fri, 30 Apr 2021 02:10:32 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
1 import argparse
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
2 import json
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
3 import os
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
4 import requests
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
5
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
6
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
7 from bioblend import galaxy
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
8
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
9 class ToolTester():
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
10 # test a newly installed tool using bioblend
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
11 """
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
12
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
13 https://github.com/nsoranzo/bioblend-tutorial/blob/master/historical_exercises/api-scripts.exercises/run_tool.py
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
14 import sys
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
15 import json
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
16 import requests
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
17 import output
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
18
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
19 BASE_URL = 'http://localhost:8080'
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
20
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
21 # -----------------------------------------------------------------------------
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
22 def run_tool( tool_id, history_id, **kwargs ):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
23 full_url = BASE_URL + '/api/tools'
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
24
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
25 #EXERCISE: POST...
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
26
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
27 # -----------------------------------------------------------------------------
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
28 if __name__ == '__main__':
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
29 # e.g. ./run_tool.py Filter1 ebfb8f50c6abde6d '{ "input" : { "src": "hda", "id": "77f74776fd03cbc5" }, "cond" : "c6>=100.0" }'
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
30 # e.g. ./run_tool.py sort1 f597429621d6eb2b '{ "input": { "src": "hda", "id": "b472e2eb553fa0d1" }, "column": "c6", "style": "alpha", "column_set_0|other_column" : "c2", "column_set_0|other_style": "num" }'
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
31 tool_id, history_id = sys.argv[1:3]
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
32 params = json.loads( sys.argv[3] ) if len( sys.argv ) >= 4 else {}
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
33 response = run_tool( tool_id, history_id, **params )
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
34 output.output_response( response )
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
35
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
36
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
37 def get_testdata(self,urlin,fout):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
38 '''
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
39 grab a test file
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
40 GET /api/tools/{tool_id}/test_data_download?tool_version={tool_version}&filename={filename}
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
41 http://localhost:8080/api/tools/tacrev/test_data_download?tool_version=2.00&filename=in
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
42 '''
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
43 """
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
44 def __init__(self, args):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
45 self.galaxy = args.galaxy
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
46 self.key = args.key
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
47 self.tool_id = args.tool_id
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
48
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
49 def run_test(self):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
50 """
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
51 GET /api/tools/{tool_id}/test_data_download?tool_version={tool_version}&filename={filename}
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
52 http://localhost:8080/api/tools/tacrev/test_data_download?tool_version=2.00&filename=input1
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
53 """
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
54 inputs = {}
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
55 gi = galaxy.GalaxyInstance(url=self.galaxy, key=self.key, verify=False)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
56 chistory = gi.histories.get_most_recently_used_history()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
57 chistory_id = chistory['id']
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
58 #contents = gi.histories.show_history(chistory_id, contents=True)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
59 #print('####chistory',chistory,'\n#### contents=',contents)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
60 #history = gi.histories.create_history(name=f"{self.tool_id}_test_history")
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
61 #new_hist_id = history['id']
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
62 fapi = ''.join([self.galaxy, '/api/tools/', self.tool_id, '/build'])
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
63 build = gi.make_get_request(url=fapi,params={"history_id":chistory_id}).json()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
64 fapi = ''.join([self.galaxy, '/api/tools/', self.tool_id, '/test_data'])
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
65 test_data = requests.get(fapi, params={'key':self.key, 'history_id':chistory_id})# gi.make_get_request(url=fapi,params={"history_id":chistory_id,'key':self.key}).json()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
66 print(test_data)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
67 testinputs = test_data.json()[0].get('inputs',None)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
68 print('testinputs',testinputs)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
69 stateinputs = build.get('state_inputs',None) # 'input1': {'values': [{'id': '7b326180327c3fcc', 'src': 'hda'}]}}
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
70 if testinputs:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
71 for k in testinputs.keys():
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
72 v = testinputs[k]
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
73 if '|' in k:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
74 nk = k.split('|')[-1]
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
75 inputs[nk] = v
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
76 else:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
77 inputs[k] = v
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
78 if stateinputs:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
79 print('stateinputs',stateinputs)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
80 for k in stateinputs.keys():
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
81 inp = stateinputs[k]
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
82 if isinstance(inp,dict):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
83 if inp.get('values',None):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
84 for anin in inp['values']:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
85 if anin.get('id', None) and anin.get('src', None):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
86 gi.histories.copy_dataset(chistory_id, anin['id'], source=anin['src'])
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
87 print('******copied id', anin['id'])
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
88 up = {k:anin}
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
89 print(up)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
90 inputs.update(up) # replace the input def
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
91 print('after state inputs', inputs)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
92 fapi = ''.join([self.galaxy, '/api/tools'])
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
93 r = gi.tools.run_tool(chistory_id, self.tool_id, inputs, input_format='legacy')
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
94 print(f"Called test on {self.tool_id} - got {r}")
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
95
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
96 def _parser():
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
97 parser = argparse.ArgumentParser()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
98 parser.add_argument("-g", "--galaxy", help='URL of target galaxy',default="http://localhost:8080")
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
99 parser.add_argument("-a", "--key", help='Galaxy admin key', default="13073fde17d06591ce36e596e3c29904")
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
100 parser.add_argument("-t", "--tool_id", help='Tool id to test', default="plotter")
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
101 return parser
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
102
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
103
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
104 if __name__ == "__main__":
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
105 args = _parser().parse_args()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
106 tt = ToolTester(args)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
107 tt.run_test()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
108