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 "+service.details.Name);
136 submit.addActionListener(new ActionListener()
140 public void actionPerformed(ActionEvent e)
142 new RestClient(service, alignFrame);
147 // TODO: menu listener should enable/disable entry depending upon selection
148 // state of the alignment
149 wsmenu.addMenuListener(new MenuListener()
153 public void menuSelected(MenuEvent e)
155 // TODO Auto-generated method stub
160 public void menuDeselected(MenuEvent e)
162 // TODO Auto-generated method stub
167 public void menuCanceled(MenuEvent e)
169 // TODO Auto-generated method stub
178 * record of initial undoredo hash for the alignFrame providing data for this
181 long[] undoredo = null;
184 * Compare the original input data to the data currently presented to the
185 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
186 * any changes in hidden columns as normal) // if different undo/redo then
187 * compare region that was submitted // if same, then merge as before, if
188 * different then prompt user to open a new window.
192 protected boolean isAlignmentModified()
194 if (undoredo == null || av==null || av.getAlignment()==null)
196 // always return modified if we don't have access to live GUI elements anymore.
199 if (av.isUndoRedoHashModified(undoredo))
201 // alignment has been modified in some way.
204 // TODO: look deeper into modification of selection state, etc that may affect RestJobThread.realiseResults(boolean merge);
210 * TODO: combine to form a dataset+alignment+annotation context
212 AlignmentView _input;
217 jalview.io.packed.JalviewDataset jds;
220 * informative name for results
222 public String viewTitle;
224 protected void constructJob()
226 service.setInvolvesFlags();
227 // record all aspects of alignment view so we can merge back or recreate
229 undoredo = av.getUndoRedoHash();
231 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
232 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
233 * new SequenceGroup[0]; }
235 boolean selExists = (av.getSelectionGroup() != null)
236 && (av.getSelectionGroup().getSize() > 1);
237 // TODO: JAL-715: refactor to alignViewport methods and revise to full focus+context+dataset input data staging model
240 if (service.partitiondata)
242 if (av.getAlignment().getGroups()!=null && av.getAlignment().getGroups().size() > 0)
244 // intersect groups with selected region
245 _input = new AlignmentView(av.getAlignment(),
246 av.getColumnSelection(),
247 av.getSelectionGroup(),
248 av.hasHiddenColumns(),
251 viewTitle = "selected "+(av.hasHiddenColumns() ? "visible" : "") + " region of "+af.getTitle();
255 // use selected region to partition alignment
256 _input = new AlignmentView(av.getAlignment(),
257 av.getColumnSelection(),
258 av.getSelectionGroup(),
259 av.hasHiddenColumns(),
263 viewTitle = "select and unselected "+(av.hasHiddenColumns() ? "visible" : "") + " regions from "+af.getTitle();
267 // just take selected region intersection
268 _input = new AlignmentView(av.getAlignment(),
269 av.getColumnSelection(),
270 av.getSelectionGroup(),
271 av.hasHiddenColumns(),
274 viewTitle = "selected "+(av.hasHiddenColumns() ? "visible" : "") + " region of "+af.getTitle();
277 // standard alignment view without selection present
278 _input = new AlignmentView(av.getAlignment(),
279 av.getColumnSelection(),
281 av.hasHiddenColumns(),
284 viewTitle = ""+(av.hasHiddenColumns() ? "visible region of " : "") + af.getTitle();
287 RestJobThread jobsthread = new RestJobThread(this);
289 if (jobsthread.isValid())
291 setWebserviceInfo(false);
292 wsInfo.setthisService(this);
293 jobsthread.setWebServiceInfo(wsInfo);
298 // TODO: try to tell the user why the job couldn't be started.
299 JOptionPane.showMessageDialog(Desktop.desktop,
300 (jobsthread.hasWarnings() ? jobsthread.getWarnings() : "The Job couldn't be started. Please check your input, and the Jalview console for any warning messages."),
301 "Unable to start web service analysis", JOptionPane.WARNING_MESSAGE);
305 public static RestClient makeShmmrRestClient()
307 String action = "Analysis", description = "Sequence Harmony and Multi-Relief (Brandt et al. 2010)", name = "Multi-Harmony";
308 Hashtable<String, InputType> iparams = new Hashtable<String, InputType>();
309 jalview.ws.rest.params.JobConstant toolp;
310 //toolp = new jalview.ws.rest.JobConstant("tool","jalview");
311 //iparams.put(toolp.token, toolp);
312 //toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
313 //iparams.put(toolp.token, toolp);
314 //toolp = new jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
315 //iparams.put(toolp.token, toolp);
316 //toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
317 //iparams.put(toolp.token, toolp);
318 //toolp = new jalview.ws.rest.params.JobConstant("blast","0");
319 //iparams.put(toolp.token, toolp);
321 jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
322 // SHMR server has a 65K limit for content pasted into the 'ali' parameter, so we always upload our files.
323 aliinput.token = "ali_file";//_file";
324 aliinput.writeAsFile=true;//true;
325 //aliinput.token = "ali_file";
326 //aliinput.writeAsFile=true;
327 iparams.put(aliinput.token, aliinput);
328 jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
329 sgroups.setMinsize(2);
330 sgroups.min=2;// need at least two group defined to make a partition
331 iparams.put("groups", sgroups);
332 sgroups.token = "groups";
334 RestServiceDescription shmrService = new RestServiceDescription(
338 "http://zeus.few.vu.nl/programs/shmrwww/index.php?tool=jalview",// ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
339 "?tool=jalview", iparams, true, false, '-');
340 // a priori knowledge of the data returned from the service
341 shmrService.addResultDatatype(JvDataType.ANNOTATION);
342 return new RestClient(shmrService);
345 public AlignmentPanel recoverAlignPanelForView()
347 AlignmentPanel[] aps = Desktop.getAlignmentPanels(av.getSequenceSetId());
348 for (AlignmentPanel alp:aps)
358 public boolean isShowResultsInNewView()
360 // TODO make this a property of the service
364 protected static Vector<String> services=null;
365 public static final String RSBS_SERVICES="RSBS_SERVICES";
366 public static RestClient[] getRestClients()
370 services = new Vector<String>();
372 for (RestServiceDescription descr: RestServiceDescription.parseDescriptions(jalview.bin.Cache.getDefault(RSBS_SERVICES,makeShmmrRestClient().service.toString())))
374 services.add(descr.toString());
377 catch (Exception ex) {
378 System.err.println("Serious - RSBS descriptions in user preferences are corrupt!");
379 ex.printStackTrace();
383 RestClient[] lst = new RestClient[services.size()];
385 for (String svc:services) {
386 lst[i++] = new RestClient(new RestServiceDescription(svc));
390 public static void main(String args[])
393 RestClient[] clients = getRestClients();
394 System.out.println("Got "+clients.length+" clients.");
396 Vector<String> urls=new Vector<String>();
397 for (RestClient cl:clients) {
398 System.out.println(""+(++i)+": "+cl.service.toString());
399 urls.add(cl.service.toString());
401 setRsbsServices(urls);
402 if (clients.length!=getRestClients().length)
404 System.err.println("Failed. Differing numbers of clients when stringified and parsed again.");
407 } catch (Throwable x)
409 System.err.println("Failed. Unexpected exception.");
413 public String getAction()
415 return service.details.Action;
418 public RestServiceDescription getRestDescription()
423 public static Vector<String> getRsbsDescriptions()
425 Vector<String> rsbsDescrs = new Vector<String>();
426 for (RestClient rsbs:getRestClients())
428 rsbsDescrs.add(rsbs.getRestDescription().toString());
433 public static void setRsbsServices(Vector<String> rsbsUrls)
437 // TODO: consider validating services ?
438 services = new Vector<String>(rsbsUrls);
439 StringBuffer sprop = new StringBuffer();
440 for (String s:services)
444 Cache.setProperty(RSBS_SERVICES, sprop.toString());
448 Cache.removeProperty(RSBS_SERVICES);