4 package jalview.ws.rest;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.util.Collection;
9 import java.util.Hashtable;
10 import java.util.Vector;
12 import javax.swing.JMenu;
13 import javax.swing.JMenuItem;
14 import javax.swing.JOptionPane;
15 import javax.swing.event.MenuEvent;
16 import javax.swing.event.MenuListener;
18 import jalview.bin.Cache;
19 import jalview.datamodel.AlignmentView;
20 import jalview.gui.AlignFrame;
21 import jalview.gui.AlignViewport;
22 import jalview.gui.AlignmentPanel;
23 import jalview.gui.Desktop;
24 import jalview.gui.WebserviceInfo;
25 import jalview.io.packed.DataProvider.JvDataType;
26 import jalview.ws.WSClient;
27 import jalview.ws.WSClientI;
28 import jalview.ws.WSMenuEntryProviderI;
34 public class RestClient extends WSClient implements WSClientI,
37 RestServiceDescription service;
39 public RestClient(RestServiceDescription rsd)
45 * parent alignframe for this job
50 * alignment view which provides data for job.
55 * get the alignFrame for the associated input data if it exists.
59 protected AlignFrame recoverAlignFrameForView()
61 return jalview.gui.Desktop.getAlignFrameFor(av);
64 public RestClient(RestServiceDescription service2, AlignFrame alignFrame)
68 av = alignFrame.getViewport();
72 public void setWebserviceInfo(boolean headless)
74 WebServiceJobTitle = service.details.Action + " using "
75 + service.details.Name;
76 WebServiceName = service.details.Name;
77 WebServiceReference = "No reference - go to url for more info";
78 if (service.details.description != null)
80 WebServiceReference = service.details.description;
84 wsInfo = new WebserviceInfo(WebServiceJobTitle, WebServiceName + "\n"
85 + WebServiceReference);
86 wsInfo.setRenderAsHtml(true);
92 public boolean isCancellable()
94 // TODO define process for cancelling rsbws jobs
99 public boolean canMergeResults()
101 // TODO process service definition to identify if the results might be
103 // TODO: change comparison for annotation merge
108 public void cancelJob()
110 System.err.println("Cannot cancel this job type: " + service);
114 public void attachWSMenuEntry(final JMenu wsmenu,
115 final AlignFrame alignFrame)
117 JMenuItem submit = new JMenuItem(service.details.Name);
118 submit.setToolTipText(service.details.Action+" using "+service.details.Name);
119 submit.addActionListener(new ActionListener()
123 public void actionPerformed(ActionEvent e)
125 new RestClient(service, alignFrame);
130 // TODO: menu listener should enable/disable entry depending upon selection
131 // state of the alignment
132 wsmenu.addMenuListener(new MenuListener()
136 public void menuSelected(MenuEvent e)
138 // TODO Auto-generated method stub
143 public void menuDeselected(MenuEvent e)
145 // TODO Auto-generated method stub
150 public void menuCanceled(MenuEvent e)
152 // TODO Auto-generated method stub
161 * record of initial undoredo hash for the alignFrame providing data for this
164 long[] undoredo = null;
167 * Compare the original input data to the data currently presented to the
168 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
169 * any changes in hidden columns as normal) // if different undo/redo then
170 * compare region that was submitted // if same, then merge as before, if
171 * different then prompt user to open a new window.
175 protected boolean isAlignmentModified()
177 if (undoredo == null || av==null || av.getAlignment()==null)
179 // always return modified if we don't have access to live GUI elements anymore.
182 if (av.isUndoRedoHashModified(undoredo))
184 // alignment has been modified in some way.
187 // TODO: look deeper into modification of selection state, etc that may affect RestJobThread.realiseResults(boolean merge);
193 * TODO: combine to form a dataset+alignment+annotation context
195 AlignmentView _input;
200 jalview.io.packed.JalviewDataset jds;
203 * informative name for results
205 public String viewTitle;
207 protected void constructJob()
209 service.setInvolvesFlags();
210 // record all aspects of alignment view so we can merge back or recreate
212 undoredo = av.getUndoRedoHash();
214 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
215 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
216 * new SequenceGroup[0]; }
218 boolean selExists = (av.getSelectionGroup() != null)
219 && (av.getSelectionGroup().getSize() > 1);
220 // TODO: JAL-715: refactor to alignViewport methods and revise to full focus+context+dataset input data staging model
223 if (service.partitiondata)
225 if (av.getAlignment().getGroups()!=null && av.getAlignment().getGroups().size() > 0)
227 // intersect groups with selected region
228 _input = new AlignmentView(av.getAlignment(),
229 av.getColumnSelection(),
230 av.getSelectionGroup(),
231 av.hasHiddenColumns(),
234 viewTitle = "selected "+(av.hasHiddenColumns() ? "visible" : "") + " region of "+af.getTitle();
238 // use selected region to partition alignment
239 _input = new AlignmentView(av.getAlignment(),
240 av.getColumnSelection(),
241 av.getSelectionGroup(),
242 av.hasHiddenColumns(),
246 viewTitle = "select and unselected "+(av.hasHiddenColumns() ? "visible" : "") + " regions from "+af.getTitle();
250 // just take selected region intersection
251 _input = new AlignmentView(av.getAlignment(),
252 av.getColumnSelection(),
253 av.getSelectionGroup(),
254 av.hasHiddenColumns(),
257 viewTitle = "selected "+(av.hasHiddenColumns() ? "visible" : "") + " region of "+af.getTitle();
260 // standard alignment view without selection present
261 _input = new AlignmentView(av.getAlignment(),
262 av.getColumnSelection(),
264 av.hasHiddenColumns(),
267 viewTitle = ""+(av.hasHiddenColumns() ? "visible region of " : "") + af.getTitle();
270 RestJobThread jobsthread = new RestJobThread(this);
272 if (jobsthread.isValid())
274 setWebserviceInfo(false);
275 wsInfo.setthisService(this);
276 jobsthread.setWebServiceInfo(wsInfo);
281 // TODO: try to tell the user why the job couldn't be started.
282 JOptionPane.showMessageDialog(Desktop.desktop,
283 (jobsthread.hasWarnings() ? jobsthread.getWarnings() : "The Job couldn't be started. Please check your input, and the Jalview console for any warning messages."),
284 "Unable to start web service analysis", JOptionPane.WARNING_MESSAGE);
288 public static RestClient makeShmmrRestClient()
290 String action = "Analysis", description = "Sequence Harmony and Multi-Relief (UNSTABLE!)", name = "Sequence Harmony";
291 Hashtable<String, InputType> iparams = new Hashtable<String, InputType>();
292 jalview.ws.rest.params.JobConstant toolp;
293 //toolp = new jalview.ws.rest.JobConstant("tool","jalview");
294 //iparams.put(toolp.token, toolp);
295 toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
296 iparams.put(toolp.token, toolp);
297 toolp = new jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
298 iparams.put(toolp.token, toolp);
299 toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
300 iparams.put(toolp.token, toolp);
301 toolp = new jalview.ws.rest.params.JobConstant("blast","0");
302 iparams.put(toolp.token, toolp);
304 jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
305 aliinput.token = "ali";//_file";
306 aliinput.writeAsFile=false;//true;
307 //aliinput.token = "ali_file";
308 //aliinput.writeAsFile=true;
309 iparams.put(aliinput.token, aliinput);
310 jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
311 sgroups.setMinsize(2);
312 sgroups.min=2;// need at least two group defined to make a partition
313 iparams.put("groups", sgroups);
314 sgroups.token = "groups";
316 RestServiceDescription shmrService = new RestServiceDescription(
320 "http://www.ibi.vu.nl/programs/shmrwww/index.php?tool=jalview",// ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
321 "?tool=jalview", iparams, true, false, '-');
322 // a priori knowledge of the data returned from the service
323 shmrService.addResultDatatype(JvDataType.ANNOTATION);
324 return new RestClient(shmrService);
327 public AlignmentPanel recoverAlignPanelForView()
329 AlignmentPanel[] aps = Desktop.getAlignmentPanels(av.getSequenceSetId());
330 for (AlignmentPanel alp:aps)
340 public boolean isShowResultsInNewView()
342 // TODO make this a property of the service
346 protected static Vector<String> services=null;
347 public static final String RSBS_SERVICES="RSBS_SERVICES";
348 public static RestClient[] getRestClients()
352 services = new Vector<String>();
354 for (RestServiceDescription descr: RestServiceDescription.parseDescriptions(jalview.bin.Cache.getDefault(RSBS_SERVICES,makeShmmrRestClient().service.toString())))
356 services.add(descr.toString());
359 catch (Exception ex) {
360 System.err.println("Serious - RSBS descriptions in user preferences are corrupt!");
361 ex.printStackTrace();
365 RestClient[] lst = new RestClient[services.size()];
367 for (String svc:services) {
368 lst[i++] = new RestClient(new RestServiceDescription(svc));
372 public static void main(String args[])
375 RestClient[] clients = getRestClients();
376 System.out.println("Got "+clients.length+" clients.");
378 Vector<String> urls=new Vector<String>();
379 for (RestClient cl:clients) {
380 System.out.println(""+(++i)+": "+cl.service.toString());
381 urls.add(cl.service.toString());
383 setRsbsServices(urls);
384 if (clients.length!=getRestClients().length)
386 System.err.println("Failed. Differing numbers of clients when stringified and parsed again.");
389 } catch (Throwable x)
391 System.err.println("Failed. Unexpected exception.");
395 public String getAction()
397 return service.details.Action;
400 public RestServiceDescription getRestDescription()
405 public static Vector<String> getRsbsDescriptions()
407 Vector<String> rsbsDescrs = new Vector<String>();
408 for (RestClient rsbs:getRestClients())
410 rsbsDescrs.add(rsbs.getRestDescription().toString());
415 public static void setRsbsServices(Vector<String> rsbsUrls)
419 // TODO: consider validating services ?
420 services = new Vector<String>(rsbsUrls);
421 StringBuffer sprop = new StringBuffer();
422 for (String s:services)
426 Cache.setProperty(RSBS_SERVICES, sprop.toString());
430 Cache.removeProperty(RSBS_SERVICES);