2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.rest;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.Hashtable;
26 import java.util.Vector;
28 import javax.swing.JMenu;
29 import javax.swing.JMenuItem;
30 import javax.swing.JOptionPane;
31 import javax.swing.event.MenuEvent;
32 import javax.swing.event.MenuListener;
34 import jalview.bin.Cache;
35 import jalview.datamodel.AlignmentView;
36 import jalview.gui.AlignFrame;
37 import jalview.gui.AlignViewport;
38 import jalview.gui.AlignmentPanel;
39 import jalview.gui.Desktop;
40 import jalview.gui.WebserviceInfo;
41 import jalview.io.packed.DataProvider.JvDataType;
42 import jalview.ws.WSClient;
43 import jalview.ws.WSClientI;
44 import jalview.ws.WSMenuEntryProviderI;
50 public class RestClient extends WSClient implements WSClientI,
53 RestServiceDescription service;
55 public RestClient(RestServiceDescription rsd)
61 * parent alignframe for this job
66 * alignment view which provides data for job.
71 * get the alignFrame for the associated input data if it exists.
75 protected AlignFrame recoverAlignFrameForView()
77 return jalview.gui.Desktop.getAlignFrameFor(av);
80 public RestClient(RestServiceDescription service2, AlignFrame alignFrame)
82 this(service2, alignFrame, false);
85 boolean headless = false;
87 public RestClient(RestServiceDescription service2, AlignFrame alignFrame,
92 av = alignFrame.getViewport();
97 public void setWebserviceInfo(boolean headless)
99 WebServiceJobTitle = service.details.Action + " using "
100 + service.details.Name;
101 WebServiceName = service.details.Name;
102 WebServiceReference = "No reference - go to url for more info";
103 if (service.details.description != null)
105 WebServiceReference = service.details.description;
109 wsInfo = new WebserviceInfo(WebServiceJobTitle, WebServiceName + "\n"
110 + WebServiceReference);
111 wsInfo.setRenderAsHtml(true);
117 public boolean isCancellable()
119 // TODO define process for cancelling rsbws jobs
124 public boolean canMergeResults()
126 // TODO process service definition to identify if the results might be
128 // TODO: change comparison for annotation merge
133 public void cancelJob()
135 System.err.println("Cannot cancel this job type: " + service);
139 public void attachWSMenuEntry(final JMenu wsmenu,
140 final AlignFrame alignFrame)
142 JMenuItem submit = new JMenuItem(service.details.Name);
143 submit.setToolTipText(service.details.Action + " using "
144 + service.details.Name);
145 submit.addActionListener(new ActionListener()
149 public void actionPerformed(ActionEvent e)
151 new RestClient(service, alignFrame);
156 // TODO: menu listener should enable/disable entry depending upon selection
157 // state of the alignment
158 wsmenu.addMenuListener(new MenuListener()
162 public void menuSelected(MenuEvent e)
164 // TODO Auto-generated method stub
169 public void menuDeselected(MenuEvent e)
171 // TODO Auto-generated method stub
176 public void menuCanceled(MenuEvent e)
178 // TODO Auto-generated method stub
187 * record of initial undoredo hash for the alignFrame providing data for this
190 long[] undoredo = null;
193 * Compare the original input data to the data currently presented to the
194 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
195 * any changes in hidden columns as normal) // if different undo/redo then
196 * compare region that was submitted // if same, then merge as before, if
197 * different then prompt user to open a new window.
201 protected boolean isAlignmentModified()
203 if (undoredo == null || av == null || av.getAlignment() == null)
205 // always return modified if we don't have access to live GUI elements
209 if (av.isUndoRedoHashModified(undoredo))
211 // alignment has been modified in some way.
214 // TODO: look deeper into modification of selection state, etc that may
215 // affect RestJobThread.realiseResults(boolean merge);
221 * TODO: combine to form a dataset+alignment+annotation context
223 AlignmentView _input;
228 jalview.io.packed.JalviewDataset jds;
231 * informative name for results
233 public String viewTitle;
235 protected void constructJob()
237 service.setInvolvesFlags();
238 // record all aspects of alignment view so we can merge back or recreate
240 undoredo = av.getUndoRedoHash();
242 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
243 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
244 * new SequenceGroup[0]; }
246 boolean selExists = (av.getSelectionGroup() != null)
247 && (av.getSelectionGroup().getSize() > 1);
248 // TODO: JAL-715: refactor to alignViewport methods and revise to full
249 // focus+context+dataset input data staging model
252 if (service.partitiondata)
254 if (av.getAlignment().getGroups() != null
255 && av.getAlignment().getGroups().size() > 0)
257 // intersect groups with selected region
258 _input = new AlignmentView(av.getAlignment(),
259 av.getColumnSelection(), av.getSelectionGroup(),
260 av.hasHiddenColumns(), true, true);
261 viewTitle = "selected "
262 + (av.hasHiddenColumns() ? "visible" : "")
263 + " region of " + af.getTitle();
267 // use selected region to partition alignment
268 _input = new AlignmentView(av.getAlignment(),
269 av.getColumnSelection(), av.getSelectionGroup(),
270 av.hasHiddenColumns(), false, true);
272 viewTitle = "select and unselected "
273 + (av.hasHiddenColumns() ? "visible" : "")
274 + " regions from " + af.getTitle();
278 // just take selected region intersection
279 _input = new AlignmentView(av.getAlignment(),
280 av.getColumnSelection(), av.getSelectionGroup(),
281 av.hasHiddenColumns(), true, true);
282 viewTitle = "selected " + (av.hasHiddenColumns() ? "visible" : "")
283 + " region of " + af.getTitle();
288 // standard alignment view without selection present
289 _input = new AlignmentView(av.getAlignment(),
290 av.getColumnSelection(), null, av.hasHiddenColumns(), false,
292 viewTitle = "" + (av.hasHiddenColumns() ? "visible region of " : "")
296 RestJobThread jobsthread = new RestJobThread(this);
298 if (jobsthread.isValid())
300 setWebserviceInfo(headless);
303 wsInfo.setthisService(this);
304 jobsthread.setWebServiceInfo(wsInfo);
310 // TODO: try to tell the user why the job couldn't be started.
314 (jobsthread.hasWarnings() ? jobsthread.getWarnings()
315 : "The Job couldn't be started. Please check your input, and the Jalview console for any warning messages."),
316 "Unable to start web service analysis",
317 JOptionPane.WARNING_MESSAGE);
321 public static RestClient makeShmmrRestClient()
323 String action = "Analysis", description = "Sequence Harmony and Multi-Relief (Brandt et al. 2010)", name = "Multi-Harmony";
324 Hashtable<String, InputType> iparams = new Hashtable<String, InputType>();
325 jalview.ws.rest.params.JobConstant toolp;
326 // toolp = new jalview.ws.rest.JobConstant("tool","jalview");
327 // iparams.put(toolp.token, toolp);
328 // toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
329 // iparams.put(toolp.token, toolp);
331 // jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
332 // iparams.put(toolp.token, toolp);
333 // toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
334 // iparams.put(toolp.token, toolp);
335 // toolp = new jalview.ws.rest.params.JobConstant("blast","0");
336 // iparams.put(toolp.token, toolp);
338 jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
339 // SHMR server has a 65K limit for content pasted into the 'ali' parameter,
340 // so we always upload our files.
341 aliinput.token = "ali_file";
342 aliinput.writeAsFile = true;
343 iparams.put(aliinput.token, aliinput);
344 jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
345 sgroups.setMinsize(2);
346 sgroups.min = 2;// need at least two group defined to make a partition
347 iparams.put("groups", sgroups);
348 sgroups.token = "groups";
350 RestServiceDescription shmrService = new RestServiceDescription(
354 "http://zeus.few.vu.nl/programs/shmrwww/index.php?tool=jalview",// ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
355 "?tool=jalview", iparams, true, false, '-');
356 // a priori knowledge of the data returned from the service
357 shmrService.addResultDatatype(JvDataType.ANNOTATION);
358 return new RestClient(shmrService);
361 public AlignmentPanel recoverAlignPanelForView()
363 AlignmentPanel[] aps = Desktop
364 .getAlignmentPanels(av.getSequenceSetId());
365 for (AlignmentPanel alp : aps)
375 public boolean isShowResultsInNewView()
377 // TODO make this a property of the service
381 protected static Vector<String> services = null;
383 public static final String RSBS_SERVICES = "RSBS_SERVICES";
385 public static RestClient[] getRestClients()
387 if (services == null)
389 services = new Vector<String>();
392 for (RestServiceDescription descr : RestServiceDescription
393 .parseDescriptions(jalview.bin.Cache.getDefault(
395 makeShmmrRestClient().service.toString())))
397 services.add(descr.toString());
399 } catch (Exception ex)
402 .println("Serious - RSBS descriptions in user preferences are corrupt!");
403 ex.printStackTrace();
407 RestClient[] lst = new RestClient[services.size()];
409 for (String svc : services)
411 lst[i++] = new RestClient(new RestServiceDescription(svc));
416 public static void main(String args[])
420 RestClient[] clients = getRestClients();
421 System.out.println("Got " + clients.length + " clients.");
423 Vector<String> urls = new Vector<String>();
424 for (RestClient cl : clients)
426 System.out.println("" + (++i) + ": " + cl.service.toString());
427 urls.add(cl.service.toString());
429 setRsbsServices(urls);
430 if (clients.length != getRestClients().length)
433 .println("Failed. Differing numbers of clients when stringified and parsed again.");
436 } catch (Throwable x)
438 System.err.println("Failed. Unexpected exception.");
443 public String getAction()
445 return service.details.Action;
448 public RestServiceDescription getRestDescription()
453 public static Vector<String> getRsbsDescriptions()
455 Vector<String> rsbsDescrs = new Vector<String>();
456 for (RestClient rsbs : getRestClients())
458 rsbsDescrs.add(rsbs.getRestDescription().toString());
463 public static void setRsbsServices(Vector<String> rsbsUrls)
465 if (rsbsUrls != null)
467 // TODO: consider validating services ?
468 services = new Vector<String>(rsbsUrls);
469 StringBuffer sprop = new StringBuffer();
470 for (String s : services)
474 Cache.setProperty(RSBS_SERVICES, sprop.toString());
478 Cache.removeProperty(RSBS_SERVICES);