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 jalview.bin.Cache;
24 import jalview.datamodel.AlignmentView;
25 import jalview.gui.AlignFrame;
26 import jalview.gui.AlignViewport;
27 import jalview.gui.AlignmentPanel;
28 import jalview.gui.Desktop;
29 import jalview.gui.WebserviceInfo;
30 import jalview.io.packed.DataProvider.JvDataType;
31 import jalview.util.MessageManager;
32 import jalview.ws.WSClient;
33 import jalview.ws.WSClientI;
34 import jalview.ws.WSMenuEntryProviderI;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.util.Hashtable;
39 import java.util.Vector;
41 import javax.swing.JMenu;
42 import javax.swing.JMenuItem;
43 import javax.swing.JOptionPane;
44 import javax.swing.event.MenuEvent;
45 import javax.swing.event.MenuListener;
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)
83 this(service2, alignFrame, false);
86 boolean headless = false;
88 public RestClient(RestServiceDescription service2, AlignFrame alignFrame,
93 av = alignFrame.getViewport();
98 public void setWebserviceInfo(boolean headless)
100 WebServiceJobTitle = MessageManager.formatMessage("label.webservice_job_title", new String[]{service.details.Action,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, true);
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(MessageManager.formatMessage("label.rest_client_submit", new String[]{service.details.Action,service.details.Name}));
144 submit.addActionListener(new ActionListener()
148 public void actionPerformed(ActionEvent e)
150 new RestClient(service, alignFrame);
155 // TODO: menu listener should enable/disable entry depending upon selection
156 // state of the alignment
157 wsmenu.addMenuListener(new MenuListener()
161 public void menuSelected(MenuEvent e)
163 // TODO Auto-generated method stub
168 public void menuDeselected(MenuEvent e)
170 // TODO Auto-generated method stub
175 public void menuCanceled(MenuEvent e)
177 // TODO Auto-generated method stub
186 * record of initial undoredo hash for the alignFrame providing data for this
189 long[] undoredo = null;
192 * Compare the original input data to the data currently presented to the
193 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
194 * any changes in hidden columns as normal) // if different undo/redo then
195 * compare region that was submitted // if same, then merge as before, if
196 * different then prompt user to open a new window.
200 protected boolean isAlignmentModified()
202 if (undoredo == null || av == null || av.getAlignment() == null)
204 // always return modified if we don't have access to live GUI elements
208 if (av.isUndoRedoHashModified(undoredo))
210 // alignment has been modified in some way.
213 // TODO: look deeper into modification of selection state, etc that may
214 // affect RestJobThread.realiseResults(boolean merge);
220 * TODO: combine to form a dataset+alignment+annotation context
222 AlignmentView _input;
227 jalview.io.packed.JalviewDataset jds;
230 * informative name for results
232 public String viewTitle;
234 protected void constructJob()
236 service.setInvolvesFlags();
237 // record all aspects of alignment view so we can merge back or recreate
239 undoredo = av.getUndoRedoHash();
241 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
242 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
243 * new SequenceGroup[0]; }
245 boolean selExists = (av.getSelectionGroup() != null)
246 && (av.getSelectionGroup().getSize() > 1);
247 // TODO: JAL-715: refactor to alignViewport methods and revise to full
248 // focus+context+dataset input data staging model
251 if (service.partitiondata)
253 if (av.getAlignment().getGroups() != null
254 && av.getAlignment().getGroups().size() > 0)
256 // intersect groups with selected region
257 _input = new AlignmentView(av.getAlignment(),
258 av.getColumnSelection(), av.getSelectionGroup(),
259 av.hasHiddenColumns(), true, true);
260 viewTitle = MessageManager.formatMessage("label.select_visible_region_of", new String[]{(av.hasHiddenColumns() ? MessageManager.getString("label.visible") : ""),af.getTitle()});
264 // use selected region to partition alignment
265 _input = new AlignmentView(av.getAlignment(),
266 av.getColumnSelection(), av.getSelectionGroup(),
267 av.hasHiddenColumns(), false, true);
269 viewTitle = MessageManager.formatMessage("label.select_unselect_visible_regions_from", new String[]{(av.hasHiddenColumns() ? MessageManager.getString("label.visible") : ""),af.getTitle()});
273 // just take selected region intersection
274 _input = new AlignmentView(av.getAlignment(),
275 av.getColumnSelection(), av.getSelectionGroup(),
276 av.hasHiddenColumns(), true, true);
277 viewTitle = MessageManager.formatMessage("label.select_visible_region_of", new String[]{(av.hasHiddenColumns() ? MessageManager.getString("label.visible") : ""),af.getTitle()});
282 // standard alignment view without selection present
283 _input = new AlignmentView(av.getAlignment(),
284 av.getColumnSelection(), null, av.hasHiddenColumns(), false,
286 viewTitle = "" + (av.hasHiddenColumns() ? (new StringBuffer(" ").append(MessageManager.getString("label.visible_region_of")).toString()) : "")
290 RestJobThread jobsthread = new RestJobThread(this);
292 if (jobsthread.isValid())
294 setWebserviceInfo(headless);
297 wsInfo.setthisService(this);
298 jobsthread.setWebServiceInfo(wsInfo);
304 // TODO: try to tell the user why the job couldn't be started.
308 (jobsthread.hasWarnings() ? jobsthread.getWarnings()
309 : MessageManager.getString("label.job_couldnt_be_started_check_input")),
310 MessageManager.getString("label.unable_start_web_service_analysis"),
311 JOptionPane.WARNING_MESSAGE);
315 public static RestClient makeShmmrRestClient()
317 String action = "Analysis", description = "Sequence Harmony and Multi-Relief (Brandt et al. 2010)", name = MessageManager.getString("label.multiharmony");
318 Hashtable<String, InputType> iparams = new Hashtable<String, InputType>();
319 jalview.ws.rest.params.JobConstant toolp;
320 // toolp = new jalview.ws.rest.JobConstant("tool","jalview");
321 // iparams.put(toolp.token, toolp);
322 // toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
323 // iparams.put(toolp.token, toolp);
325 // jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
326 // iparams.put(toolp.token, toolp);
327 // toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
328 // iparams.put(toolp.token, toolp);
329 // toolp = new jalview.ws.rest.params.JobConstant("blast","0");
330 // iparams.put(toolp.token, toolp);
332 jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
333 // SHMR server has a 65K limit for content pasted into the 'ali' parameter,
334 // so we always upload our files.
335 aliinput.token = "ali_file";
336 aliinput.writeAsFile = true;
337 iparams.put(aliinput.token, aliinput);
338 jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
339 sgroups.setMinsize(2);
340 sgroups.min = 2;// need at least two group defined to make a partition
341 iparams.put("groups", sgroups);
342 sgroups.token = "groups";
344 RestServiceDescription shmrService = new RestServiceDescription(
348 "http://zeus.few.vu.nl/programs/shmrwww/index.php?tool=jalview",// ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
349 "?tool=jalview", iparams, true, false, '-');
350 // a priori knowledge of the data returned from the service
351 shmrService.addResultDatatype(JvDataType.ANNOTATION);
352 return new RestClient(shmrService);
355 public AlignmentPanel recoverAlignPanelForView()
357 AlignmentPanel[] aps = Desktop
358 .getAlignmentPanels(av.getSequenceSetId());
359 for (AlignmentPanel alp : aps)
369 public boolean isShowResultsInNewView()
371 // TODO make this a property of the service
375 protected static Vector<String> services = null;
377 public static final String RSBS_SERVICES = "RSBS_SERVICES";
379 public static RestClient[] getRestClients()
381 if (services == null)
383 services = new Vector<String>();
386 for (RestServiceDescription descr : RestServiceDescription
387 .parseDescriptions(jalview.bin.Cache.getDefault(
389 makeShmmrRestClient().service.toString())))
391 services.add(descr.toString());
393 } catch (Exception ex)
396 .println("Serious - RSBS descriptions in user preferences are corrupt!");
397 ex.printStackTrace();
401 RestClient[] lst = new RestClient[services.size()];
403 for (String svc : services)
405 lst[i++] = new RestClient(new RestServiceDescription(svc));
410 public static void main(String args[])
414 RestClient[] clients = getRestClients();
415 System.out.println("Got " + clients.length + " clients.");
417 Vector<String> urls = new Vector<String>();
418 for (RestClient cl : clients)
420 System.out.println("" + (++i) + ": " + cl.service.toString());
421 urls.add(cl.service.toString());
423 setRsbsServices(urls);
424 if (clients.length != getRestClients().length)
427 .println("Failed. Differing numbers of clients when stringified and parsed again.");
430 } catch (Throwable x)
432 System.err.println("Failed. Unexpected exception.");
437 public String getAction()
439 return service.details.Action;
442 public RestServiceDescription getRestDescription()
447 public static Vector<String> getRsbsDescriptions()
449 Vector<String> rsbsDescrs = new Vector<String>();
450 for (RestClient rsbs : getRestClients())
452 rsbsDescrs.add(rsbs.getRestDescription().toString());
457 public static void setRsbsServices(Vector<String> rsbsUrls)
459 if (rsbsUrls != null)
461 // TODO: consider validating services ?
462 services = new Vector<String>(rsbsUrls);
463 StringBuffer sprop = new StringBuffer();
464 for (String s : services)
468 Cache.setProperty(RSBS_SERVICES, sprop.toString());
472 Cache.removeProperty(RSBS_SERVICES);