2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
18 package jalview.ws.rest;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.util.Hashtable;
23 import java.util.Vector;
25 import javax.swing.JMenu;
26 import javax.swing.JMenuItem;
27 import javax.swing.JOptionPane;
28 import javax.swing.event.MenuEvent;
29 import javax.swing.event.MenuListener;
31 import jalview.bin.Cache;
32 import jalview.datamodel.AlignmentView;
33 import jalview.gui.AlignFrame;
34 import jalview.gui.AlignViewport;
35 import jalview.gui.AlignmentPanel;
36 import jalview.gui.Desktop;
37 import jalview.gui.WebserviceInfo;
38 import jalview.io.packed.DataProvider.JvDataType;
39 import jalview.ws.WSClient;
40 import jalview.ws.WSClientI;
41 import jalview.ws.WSMenuEntryProviderI;
47 public class RestClient extends WSClient implements WSClientI,
50 RestServiceDescription service;
52 public RestClient(RestServiceDescription rsd)
58 * parent alignframe for this job
63 * alignment view which provides data for job.
68 * get the alignFrame for the associated input data if it exists.
72 protected AlignFrame recoverAlignFrameForView()
74 return jalview.gui.Desktop.getAlignFrameFor(av);
77 public RestClient(RestServiceDescription service2, AlignFrame alignFrame)
81 av = alignFrame.getViewport();
85 public void setWebserviceInfo(boolean headless)
87 WebServiceJobTitle = service.details.Action + " using "
88 + service.details.Name;
89 WebServiceName = service.details.Name;
90 WebServiceReference = "No reference - go to url for more info";
91 if (service.details.description != null)
93 WebServiceReference = service.details.description;
97 wsInfo = new WebserviceInfo(WebServiceJobTitle, WebServiceName + "\n"
98 + WebServiceReference);
99 wsInfo.setRenderAsHtml(true);
105 public boolean isCancellable()
107 // TODO define process for cancelling rsbws jobs
112 public boolean canMergeResults()
114 // TODO process service definition to identify if the results might be
116 // TODO: change comparison for annotation merge
121 public void cancelJob()
123 System.err.println("Cannot cancel this job type: " + service);
127 public void attachWSMenuEntry(final JMenu wsmenu,
128 final AlignFrame alignFrame)
130 JMenuItem submit = new JMenuItem(service.details.Name);
131 submit.setToolTipText(service.details.Action + " using "
132 + service.details.Name);
133 submit.addActionListener(new ActionListener()
137 public void actionPerformed(ActionEvent e)
139 new RestClient(service, alignFrame);
144 // TODO: menu listener should enable/disable entry depending upon selection
145 // state of the alignment
146 wsmenu.addMenuListener(new MenuListener()
150 public void menuSelected(MenuEvent e)
152 // TODO Auto-generated method stub
157 public void menuDeselected(MenuEvent e)
159 // TODO Auto-generated method stub
164 public void menuCanceled(MenuEvent e)
166 // TODO Auto-generated method stub
175 * record of initial undoredo hash for the alignFrame providing data for this
178 long[] undoredo = null;
181 * Compare the original input data to the data currently presented to the
182 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
183 * any changes in hidden columns as normal) // if different undo/redo then
184 * compare region that was submitted // if same, then merge as before, if
185 * different then prompt user to open a new window.
189 protected boolean isAlignmentModified()
191 if (undoredo == null || av == null || av.getAlignment() == null)
193 // always return modified if we don't have access to live GUI elements
197 if (av.isUndoRedoHashModified(undoredo))
199 // alignment has been modified in some way.
202 // TODO: look deeper into modification of selection state, etc that may
203 // affect RestJobThread.realiseResults(boolean merge);
209 * TODO: combine to form a dataset+alignment+annotation context
211 AlignmentView _input;
216 jalview.io.packed.JalviewDataset jds;
219 * informative name for results
221 public String viewTitle;
223 protected void constructJob()
225 service.setInvolvesFlags();
226 // record all aspects of alignment view so we can merge back or recreate
228 undoredo = av.getUndoRedoHash();
230 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
231 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
232 * new SequenceGroup[0]; }
234 boolean selExists = (av.getSelectionGroup() != null)
235 && (av.getSelectionGroup().getSize() > 1);
236 // TODO: JAL-715: refactor to alignViewport methods and revise to full
237 // focus+context+dataset input data staging model
240 if (service.partitiondata)
242 if (av.getAlignment().getGroups() != null
243 && av.getAlignment().getGroups().size() > 0)
245 // intersect groups with selected region
246 _input = new AlignmentView(av.getAlignment(),
247 av.getColumnSelection(), av.getSelectionGroup(),
248 av.hasHiddenColumns(), true, true);
249 viewTitle = "selected "
250 + (av.hasHiddenColumns() ? "visible" : "")
251 + " region of " + af.getTitle();
255 // use selected region to partition alignment
256 _input = new AlignmentView(av.getAlignment(),
257 av.getColumnSelection(), av.getSelectionGroup(),
258 av.hasHiddenColumns(), false, true);
260 viewTitle = "select and unselected "
261 + (av.hasHiddenColumns() ? "visible" : "")
262 + " regions from " + af.getTitle();
266 // just take selected region intersection
267 _input = new AlignmentView(av.getAlignment(),
268 av.getColumnSelection(), av.getSelectionGroup(),
269 av.hasHiddenColumns(), true, true);
270 viewTitle = "selected " + (av.hasHiddenColumns() ? "visible" : "")
271 + " region of " + af.getTitle();
276 // standard alignment view without selection present
277 _input = new AlignmentView(av.getAlignment(),
278 av.getColumnSelection(), null, av.hasHiddenColumns(), false,
280 viewTitle = "" + (av.hasHiddenColumns() ? "visible region of " : "")
284 RestJobThread jobsthread = new RestJobThread(this);
286 if (jobsthread.isValid())
288 setWebserviceInfo(false);
289 wsInfo.setthisService(this);
290 jobsthread.setWebServiceInfo(wsInfo);
295 // TODO: try to tell the user why the job couldn't be started.
299 (jobsthread.hasWarnings() ? jobsthread.getWarnings()
300 : "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",
302 JOptionPane.WARNING_MESSAGE);
306 public static RestClient makeShmmrRestClient()
308 String action = "Analysis", description = "Sequence Harmony and Multi-Relief (Brandt et al. 2010)", name = "Multi-Harmony";
309 Hashtable<String, InputType> iparams = new Hashtable<String, InputType>();
310 jalview.ws.rest.params.JobConstant toolp;
311 // toolp = new jalview.ws.rest.JobConstant("tool","jalview");
312 // iparams.put(toolp.token, toolp);
313 // toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
314 // iparams.put(toolp.token, toolp);
316 // jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
317 // iparams.put(toolp.token, toolp);
318 // toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
319 // iparams.put(toolp.token, toolp);
320 // toolp = new jalview.ws.rest.params.JobConstant("blast","0");
321 // iparams.put(toolp.token, toolp);
323 jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
324 // SHMR server has a 65K limit for content pasted into the 'ali' parameter,
325 // so we always upload our files.
326 aliinput.token = "ali_file";
327 aliinput.writeAsFile = true;
328 iparams.put(aliinput.token, aliinput);
329 jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
330 sgroups.setMinsize(2);
331 sgroups.min = 2;// need at least two group defined to make a partition
332 iparams.put("groups", sgroups);
333 sgroups.token = "groups";
335 RestServiceDescription shmrService = new RestServiceDescription(
339 "http://zeus.few.vu.nl/programs/shmrwww/index.php?tool=jalview",// ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
340 "?tool=jalview", iparams, true, false, '-');
341 // a priori knowledge of the data returned from the service
342 shmrService.addResultDatatype(JvDataType.ANNOTATION);
343 return new RestClient(shmrService);
346 public AlignmentPanel recoverAlignPanelForView()
348 AlignmentPanel[] aps = Desktop
349 .getAlignmentPanels(av.getSequenceSetId());
350 for (AlignmentPanel alp : aps)
360 public boolean isShowResultsInNewView()
362 // TODO make this a property of the service
366 protected static Vector<String> services = null;
368 public static final String RSBS_SERVICES = "RSBS_SERVICES";
370 public static RestClient[] getRestClients()
372 if (services == null)
374 services = new Vector<String>();
377 for (RestServiceDescription descr : RestServiceDescription
378 .parseDescriptions(jalview.bin.Cache.getDefault(
380 makeShmmrRestClient().service.toString())))
382 services.add(descr.toString());
384 } catch (Exception ex)
387 .println("Serious - RSBS descriptions in user preferences are corrupt!");
388 ex.printStackTrace();
392 RestClient[] lst = new RestClient[services.size()];
394 for (String svc : services)
396 lst[i++] = new RestClient(new RestServiceDescription(svc));
401 public static void main(String args[])
405 RestClient[] clients = getRestClients();
406 System.out.println("Got " + clients.length + " clients.");
408 Vector<String> urls = new Vector<String>();
409 for (RestClient cl : clients)
411 System.out.println("" + (++i) + ": " + cl.service.toString());
412 urls.add(cl.service.toString());
414 setRsbsServices(urls);
415 if (clients.length != getRestClients().length)
418 .println("Failed. Differing numbers of clients when stringified and parsed again.");
421 } catch (Throwable x)
423 System.err.println("Failed. Unexpected exception.");
428 public String getAction()
430 return service.details.Action;
433 public RestServiceDescription getRestDescription()
438 public static Vector<String> getRsbsDescriptions()
440 Vector<String> rsbsDescrs = new Vector<String>();
441 for (RestClient rsbs : getRestClients())
443 rsbsDescrs.add(rsbs.getRestDescription().toString());
448 public static void setRsbsServices(Vector<String> rsbsUrls)
450 if (rsbsUrls != null)
452 // TODO: consider validating services ?
453 services = new Vector<String>(rsbsUrls);
454 StringBuffer sprop = new StringBuffer();
455 for (String s : services)
459 Cache.setProperty(RSBS_SERVICES, sprop.toString());
463 Cache.removeProperty(RSBS_SERVICES);