2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.JvOptionPane;
30 import jalview.gui.WebserviceInfo;
31 import jalview.io.packed.DataProvider.JvDataType;
32 import jalview.util.MessageManager;
33 import jalview.ws.WSClient;
34 import jalview.ws.WSClientI;
35 import jalview.ws.WSMenuEntryProviderI;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.util.Hashtable;
40 import java.util.Vector;
42 import javax.swing.JMenu;
43 import javax.swing.JMenuItem;
44 import javax.swing.event.MenuEvent;
45 import javax.swing.event.MenuListener;
51 public class RestClient extends WSClient
52 implements WSClientI, WSMenuEntryProviderI
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
101 .formatMessage("label.webservice_job_title", new String[]
102 { service.details.Action, service.details.Name });
103 WebServiceName = service.details.Name;
104 WebServiceReference = "No reference - go to url for more info";
105 if (service.details.description != null)
107 WebServiceReference = service.details.description;
111 wsInfo = new WebserviceInfo(WebServiceJobTitle,
112 WebServiceName + "\n" + WebServiceReference, true);
113 wsInfo.setRenderAsHtml(true);
119 public boolean isCancellable()
121 // TODO define process for cancelling rsbws jobs
126 public boolean canMergeResults()
128 // TODO process service definition to identify if the results might be
130 // TODO: change comparison for annotation merge
135 public void cancelJob()
137 System.err.println("Cannot cancel this job type: " + service);
141 public void attachWSMenuEntry(final JMenu wsmenu,
142 final AlignFrame alignFrame)
144 JMenuItem submit = new JMenuItem(service.details.Name);
145 submit.setToolTipText(MessageManager
146 .formatMessage("label.rest_client_submit", new String[]
147 { service.details.Action, service.details.Name }));
148 submit.addActionListener(new ActionListener()
152 public void actionPerformed(ActionEvent e)
154 new RestClient(service, alignFrame);
159 // TODO: menu listener should enable/disable entry depending upon selection
160 // state of the alignment
161 wsmenu.addMenuListener(new MenuListener()
165 public void menuSelected(MenuEvent e)
167 // TODO Auto-generated method stub
172 public void menuDeselected(MenuEvent e)
174 // TODO Auto-generated method stub
179 public void menuCanceled(MenuEvent e)
181 // TODO Auto-generated method stub
190 * record of initial undoredo hash for the alignFrame providing data for this
193 long[] undoredo = null;
196 * Compare the original input data to the data currently presented to the
197 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
198 * any changes in hidden columns as normal) // if different undo/redo then
199 * compare region that was submitted // if same, then merge as before, if
200 * different then prompt user to open a new window.
204 protected boolean isAlignmentModified()
206 if (undoredo == null || av == null || av.getAlignment() == null)
208 // always return modified if we don't have access to live GUI elements
212 if (av.isUndoRedoHashModified(undoredo))
214 // alignment has been modified in some way.
217 // TODO: look deeper into modification of selection state, etc that may
218 // affect RestJobThread.realiseResults(boolean merge);
224 * TODO: combine to form a dataset+alignment+annotation context
226 AlignmentView _input;
231 jalview.io.packed.JalviewDataset jds;
234 * informative name for results
236 public String viewTitle;
238 protected void constructJob()
240 service.setInvolvesFlags();
241 // record all aspects of alignment view so we can merge back or recreate
243 undoredo = av.getUndoRedoHash();
245 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
246 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
247 * new SequenceGroup[0]; }
249 boolean selExists = (av.getSelectionGroup() != null)
250 && (av.getSelectionGroup().getSize() > 1);
251 // TODO: JAL-715: refactor to alignViewport methods and revise to full
252 // focus+context+dataset input data staging model
255 if (service.partitiondata)
257 if (av.getAlignment().getGroups() != null
258 && av.getAlignment().getGroups().size() > 0)
260 // intersect groups with selected region
261 _input = new AlignmentView(av.getAlignment(),
262 av.getAlignment().getHiddenColumns(),
263 av.getSelectionGroup(), av.hasHiddenColumns(), true,
265 viewTitle = MessageManager.formatMessage(
266 "label.select_visible_region_of", new String[]
267 { (av.hasHiddenColumns()
268 ? MessageManager.getString("label.visible")
274 // use selected region to partition alignment
275 _input = new AlignmentView(av.getAlignment(),
276 av.getAlignment().getHiddenColumns(),
277 av.getSelectionGroup(), av.hasHiddenColumns(), false,
280 viewTitle = MessageManager.formatMessage(
281 "label.select_unselect_visible_regions_from", new String[]
282 { (av.hasHiddenColumns()
283 ? MessageManager.getString("label.visible")
289 // just take selected region intersection
290 _input = new AlignmentView(av.getAlignment(),
291 av.getAlignment().getHiddenColumns(),
292 av.getSelectionGroup(), av.hasHiddenColumns(), true, true);
293 viewTitle = MessageManager.formatMessage(
294 "label.select_visible_region_of", new String[]
295 { (av.hasHiddenColumns()
296 ? MessageManager.getString("label.visible")
303 // standard alignment view without selection present
304 _input = new AlignmentView(av.getAlignment(),
305 av.getAlignment().getHiddenColumns(), null,
306 av.hasHiddenColumns(), false, true);
308 + (av.hasHiddenColumns()
309 ? (new StringBuffer(" ")
310 .append(MessageManager
311 .getString("label.visible_region_of"))
317 RestJobThread jobsthread = new RestJobThread(this);
319 if (jobsthread.isValid())
321 setWebserviceInfo(headless);
324 wsInfo.setthisService(this);
325 jobsthread.setWebServiceInfo(wsInfo);
331 // TODO: try to tell the user why the job couldn't be started.
332 JvOptionPane.showMessageDialog(Desktop.desktop,
333 (jobsthread.hasWarnings() ? jobsthread.getWarnings()
334 : MessageManager.getString(
335 "label.job_couldnt_be_started_check_input")),
337 .getString("label.unable_start_web_service_analysis"),
338 JvOptionPane.WARNING_MESSAGE);
342 public static RestClient makeShmmrRestClient()
344 String action = "Analysis",
345 description = "Sequence Harmony and Multi-Relief (Brandt et al. 2010)",
346 name = MessageManager.getString("label.multiharmony");
347 Hashtable<String, InputType> iparams = new Hashtable<String, InputType>();
348 jalview.ws.rest.params.JobConstant toolp;
349 // toolp = new jalview.ws.rest.JobConstant("tool","jalview");
350 // iparams.put(toolp.token, toolp);
351 // toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
352 // iparams.put(toolp.token, toolp);
354 // jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
355 // iparams.put(toolp.token, toolp);
356 // toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
357 // iparams.put(toolp.token, toolp);
358 // toolp = new jalview.ws.rest.params.JobConstant("blast","0");
359 // iparams.put(toolp.token, toolp);
361 jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
362 // SHMR server has a 65K limit for content pasted into the 'ali' parameter,
363 // so we always upload our files.
364 aliinput.token = "ali_file";
365 aliinput.writeAsFile = true;
366 iparams.put(aliinput.token, aliinput);
367 jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
368 sgroups.setMinsize(2);
369 sgroups.min = 2;// need at least two group defined to make a partition
370 iparams.put("groups", sgroups);
371 sgroups.token = "groups";
373 RestServiceDescription shmrService = new RestServiceDescription(action,
375 "http://zeus.few.vu.nl/programs/shmrwww/index.php?tool=jalview", // ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
376 "?tool=jalview", iparams, true, false, '-');
377 // a priori knowledge of the data returned from the service
378 shmrService.addResultDatatype(JvDataType.ANNOTATION);
379 return new RestClient(shmrService);
382 public AlignmentPanel recoverAlignPanelForView()
384 AlignmentPanel[] aps = Desktop
385 .getAlignmentPanels(av.getSequenceSetId());
386 for (AlignmentPanel alp : aps)
396 public boolean isShowResultsInNewView()
398 // TODO make this a property of the service
402 protected static Vector<String> services = null;
404 public static final String RSBS_SERVICES = "RSBS_SERVICES";
406 public static RestClient[] getRestClients()
408 if (services == null)
410 services = new Vector<String>();
413 for (RestServiceDescription descr : RestServiceDescription
415 jalview.bin.Cache.getDefault(RSBS_SERVICES,
416 makeShmmrRestClient().service.toString())))
418 services.add(descr.toString());
420 } catch (Exception ex)
423 "Serious - RSBS descriptions in user preferences are corrupt!");
424 ex.printStackTrace();
428 RestClient[] lst = new RestClient[services.size()];
430 for (String svc : services)
432 lst[i++] = new RestClient(new RestServiceDescription(svc));
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);