1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 *******************************************************************************/
21 package jalview.ws.rest;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.Collection;
26 import java.util.Hashtable;
27 import java.util.Vector;
29 import javax.swing.JMenu;
30 import javax.swing.JMenuItem;
31 import javax.swing.JOptionPane;
32 import javax.swing.event.MenuEvent;
33 import javax.swing.event.MenuListener;
35 import jalview.bin.Cache;
36 import jalview.datamodel.AlignmentView;
37 import jalview.gui.AlignFrame;
38 import jalview.gui.AlignViewport;
39 import jalview.gui.AlignmentPanel;
40 import jalview.gui.Desktop;
41 import jalview.gui.WebserviceInfo;
42 import jalview.io.packed.DataProvider.JvDataType;
43 import jalview.ws.WSClient;
44 import jalview.ws.WSClientI;
45 import jalview.ws.WSMenuEntryProviderI;
51 public class RestClient extends WSClient implements WSClientI,
54 RestServiceDescription service;
56 public RestClient(RestServiceDescription rsd)
62 * parent alignframe for this job
67 * alignment view which provides data for job.
72 * get the alignFrame for the associated input data if it exists.
76 protected AlignFrame recoverAlignFrameForView()
78 return jalview.gui.Desktop.getAlignFrameFor(av);
81 public RestClient(RestServiceDescription service2, AlignFrame alignFrame)
85 av = alignFrame.getViewport();
89 public void setWebserviceInfo(boolean headless)
91 WebServiceJobTitle = service.details.Action + " using "
92 + service.details.Name;
93 WebServiceName = service.details.Name;
94 WebServiceReference = "No reference - go to url for more info";
95 if (service.details.description != null)
97 WebServiceReference = service.details.description;
101 wsInfo = new WebserviceInfo(WebServiceJobTitle, WebServiceName + "\n"
102 + WebServiceReference);
103 wsInfo.setRenderAsHtml(true);
109 public boolean isCancellable()
111 // TODO define process for cancelling rsbws jobs
116 public boolean canMergeResults()
118 // TODO process service definition to identify if the results might be
120 // TODO: change comparison for annotation merge
125 public void cancelJob()
127 System.err.println("Cannot cancel this job type: " + service);
131 public void attachWSMenuEntry(final JMenu wsmenu,
132 final AlignFrame alignFrame)
134 JMenuItem submit = new JMenuItem(service.details.Name);
135 submit.setToolTipText(service.details.Action + " using "
136 + service.details.Name);
137 submit.addActionListener(new ActionListener()
141 public void actionPerformed(ActionEvent e)
143 new RestClient(service, alignFrame);
148 // TODO: menu listener should enable/disable entry depending upon selection
149 // state of the alignment
150 wsmenu.addMenuListener(new MenuListener()
154 public void menuSelected(MenuEvent e)
156 // TODO Auto-generated method stub
161 public void menuDeselected(MenuEvent e)
163 // TODO Auto-generated method stub
168 public void menuCanceled(MenuEvent e)
170 // TODO Auto-generated method stub
179 * record of initial undoredo hash for the alignFrame providing data for this
182 long[] undoredo = null;
185 * Compare the original input data to the data currently presented to the
186 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
187 * any changes in hidden columns as normal) // if different undo/redo then
188 * compare region that was submitted // if same, then merge as before, if
189 * different then prompt user to open a new window.
193 protected boolean isAlignmentModified()
195 if (undoredo == null || av == null || av.getAlignment() == null)
197 // always return modified if we don't have access to live GUI elements
201 if (av.isUndoRedoHashModified(undoredo))
203 // alignment has been modified in some way.
206 // TODO: look deeper into modification of selection state, etc that may
207 // affect RestJobThread.realiseResults(boolean merge);
213 * TODO: combine to form a dataset+alignment+annotation context
215 AlignmentView _input;
220 jalview.io.packed.JalviewDataset jds;
223 * informative name for results
225 public String viewTitle;
227 protected void constructJob()
229 service.setInvolvesFlags();
230 // record all aspects of alignment view so we can merge back or recreate
232 undoredo = av.getUndoRedoHash();
234 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
235 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
236 * new SequenceGroup[0]; }
238 boolean selExists = (av.getSelectionGroup() != null)
239 && (av.getSelectionGroup().getSize() > 1);
240 // TODO: JAL-715: refactor to alignViewport methods and revise to full
241 // focus+context+dataset input data staging model
244 if (service.partitiondata)
246 if (av.getAlignment().getGroups() != null
247 && av.getAlignment().getGroups().size() > 0)
249 // intersect groups with selected region
250 _input = new AlignmentView(av.getAlignment(),
251 av.getColumnSelection(), av.getSelectionGroup(),
252 av.hasHiddenColumns(), true, true);
253 viewTitle = "selected "
254 + (av.hasHiddenColumns() ? "visible" : "")
255 + " region of " + af.getTitle();
259 // use selected region to partition alignment
260 _input = new AlignmentView(av.getAlignment(),
261 av.getColumnSelection(), av.getSelectionGroup(),
262 av.hasHiddenColumns(), false, true);
264 viewTitle = "select and unselected "
265 + (av.hasHiddenColumns() ? "visible" : "")
266 + " regions from " + af.getTitle();
270 // just take selected region intersection
271 _input = new AlignmentView(av.getAlignment(),
272 av.getColumnSelection(), av.getSelectionGroup(),
273 av.hasHiddenColumns(), true, true);
274 viewTitle = "selected " + (av.hasHiddenColumns() ? "visible" : "")
275 + " region of " + af.getTitle();
280 // standard alignment view without selection present
281 _input = new AlignmentView(av.getAlignment(),
282 av.getColumnSelection(), null, av.hasHiddenColumns(), false,
284 viewTitle = "" + (av.hasHiddenColumns() ? "visible region of " : "")
288 RestJobThread jobsthread = new RestJobThread(this);
290 if (jobsthread.isValid())
292 setWebserviceInfo(false);
293 wsInfo.setthisService(this);
294 jobsthread.setWebServiceInfo(wsInfo);
299 // TODO: try to tell the user why the job couldn't be started.
303 (jobsthread.hasWarnings() ? jobsthread.getWarnings()
304 : "The Job couldn't be started. Please check your input, and the Jalview console for any warning messages."),
305 "Unable to start web service analysis",
306 JOptionPane.WARNING_MESSAGE);
310 public static RestClient makeShmmrRestClient()
312 String action = "Analysis", description = "Sequence Harmony and Multi-Relief (Brandt et al. 2010)", name = "Multi-Harmony";
313 Hashtable<String, InputType> iparams = new Hashtable<String, InputType>();
314 jalview.ws.rest.params.JobConstant toolp;
315 // toolp = new jalview.ws.rest.JobConstant("tool","jalview");
316 // iparams.put(toolp.token, toolp);
317 // toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
318 // iparams.put(toolp.token, toolp);
320 // jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
321 // iparams.put(toolp.token, toolp);
322 // toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
323 // iparams.put(toolp.token, toolp);
324 // toolp = new jalview.ws.rest.params.JobConstant("blast","0");
325 // iparams.put(toolp.token, toolp);
327 jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
328 // SHMR server has a 65K limit for content pasted into the 'ali' parameter,
329 // so we always upload our files.
330 aliinput.token = "ali_file";
331 aliinput.writeAsFile = true;
332 iparams.put(aliinput.token, aliinput);
333 jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
334 sgroups.setMinsize(2);
335 sgroups.min = 2;// need at least two group defined to make a partition
336 iparams.put("groups", sgroups);
337 sgroups.token = "groups";
339 RestServiceDescription shmrService = new RestServiceDescription(
343 "http://zeus.few.vu.nl/programs/shmrwww/index.php?tool=jalview",// ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
344 "?tool=jalview", iparams, true, false, '-');
345 // a priori knowledge of the data returned from the service
346 shmrService.addResultDatatype(JvDataType.ANNOTATION);
347 return new RestClient(shmrService);
350 public AlignmentPanel recoverAlignPanelForView()
352 AlignmentPanel[] aps = Desktop
353 .getAlignmentPanels(av.getSequenceSetId());
354 for (AlignmentPanel alp : aps)
364 public boolean isShowResultsInNewView()
366 // TODO make this a property of the service
370 protected static Vector<String> services = null;
372 public static final String RSBS_SERVICES = "RSBS_SERVICES";
374 public static RestClient[] getRestClients()
376 if (services == null)
378 services = new Vector<String>();
381 for (RestServiceDescription descr : RestServiceDescription
382 .parseDescriptions(jalview.bin.Cache.getDefault(
384 makeShmmrRestClient().service.toString())))
386 services.add(descr.toString());
388 } catch (Exception ex)
391 .println("Serious - RSBS descriptions in user preferences are corrupt!");
392 ex.printStackTrace();
396 RestClient[] lst = new RestClient[services.size()];
398 for (String svc : services)
400 lst[i++] = new RestClient(new RestServiceDescription(svc));
405 public static void main(String args[])
409 RestClient[] clients = getRestClients();
410 System.out.println("Got " + clients.length + " clients.");
412 Vector<String> urls = new Vector<String>();
413 for (RestClient cl : clients)
415 System.out.println("" + (++i) + ": " + cl.service.toString());
416 urls.add(cl.service.toString());
418 setRsbsServices(urls);
419 if (clients.length != getRestClients().length)
422 .println("Failed. Differing numbers of clients when stringified and parsed again.");
425 } catch (Throwable x)
427 System.err.println("Failed. Unexpected exception.");
432 public String getAction()
434 return service.details.Action;
437 public RestServiceDescription getRestDescription()
442 public static Vector<String> getRsbsDescriptions()
444 Vector<String> rsbsDescrs = new Vector<String>();
445 for (RestClient rsbs : getRestClients())
447 rsbsDescrs.add(rsbs.getRestDescription().toString());
452 public static void setRsbsServices(Vector<String> rsbsUrls)
454 if (rsbsUrls != null)
456 // TODO: consider validating services ?
457 services = new Vector<String>(rsbsUrls);
458 StringBuffer sprop = new StringBuffer();
459 for (String s : services)
463 Cache.setProperty(RSBS_SERVICES, sprop.toString());
467 Cache.removeProperty(RSBS_SERVICES);