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.ApplicationSingletonProvider;
24 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
25 import jalview.bin.Cache;
26 import jalview.datamodel.AlignmentView;
27 import jalview.gui.AlignFrame;
28 import jalview.gui.AlignViewport;
29 import jalview.gui.AlignmentPanel;
30 import jalview.gui.Desktop;
31 import jalview.gui.JvOptionPane;
32 import jalview.gui.WebserviceInfo;
33 import jalview.io.packed.DataProvider.JvDataType;
34 import jalview.util.MessageManager;
35 import jalview.ws.WSClient;
36 import jalview.ws.WSClientI;
37 import jalview.ws.WSMenuEntryProviderI;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.util.Hashtable;
42 import java.util.Vector;
44 import javax.swing.JMenu;
45 import javax.swing.JMenuItem;
46 import javax.swing.event.MenuEvent;
47 import javax.swing.event.MenuListener;
53 public class RestClient extends WSClient
54 implements WSClientI, WSMenuEntryProviderI, ApplicationSingletonI
57 private static RestClient getInstance()
59 return (RestClient) ApplicationSingletonProvider.getInstance(RestClient.class);
62 public static final String RSBS_SERVICES = "RSBS_SERVICES";
65 protected Vector<String> services = null;
68 RestServiceDescription service;
70 public RestClient(RestServiceDescription rsd)
76 * parent alignframe for this job
81 * alignment view which provides data for job.
86 * get the alignFrame for the associated input data if it exists.
90 protected AlignFrame recoverAlignFrameForView()
92 return jalview.gui.Desktop.getAlignFrameFor(av);
95 public RestClient(RestServiceDescription service2, AlignFrame alignFrame)
97 this(service2, alignFrame, false);
100 boolean headless = false;
102 public RestClient(RestServiceDescription service2, AlignFrame alignFrame,
107 av = alignFrame.getViewport();
112 public void setWebserviceInfo(boolean headless)
114 WebServiceJobTitle = MessageManager
115 .formatMessage("label.webservice_job_title", new String[]
116 { service.details.Action, service.details.Name });
117 WebServiceName = service.details.Name;
118 WebServiceReference = "No reference - go to url for more info";
119 if (service.details.description != null)
121 WebServiceReference = service.details.description;
125 wsInfo = new WebserviceInfo(WebServiceJobTitle,
126 WebServiceName + "\n" + WebServiceReference, true);
127 wsInfo.setRenderAsHtml(true);
133 public boolean isCancellable()
135 // TODO define process for cancelling rsbws jobs
140 public boolean canMergeResults()
142 // TODO process service definition to identify if the results might be
144 // TODO: change comparison for annotation merge
149 public void cancelJob()
151 System.err.println("Cannot cancel this job type: " + service);
155 public void attachWSMenuEntry(final JMenu wsmenu,
156 final AlignFrame alignFrame)
158 JMenuItem submit = new JMenuItem(service.details.Name);
159 submit.setToolTipText(MessageManager
160 .formatMessage("label.rest_client_submit", new String[]
161 { service.details.Action, service.details.Name }));
162 submit.addActionListener(new ActionListener()
166 public void actionPerformed(ActionEvent e)
168 new RestClient(service, alignFrame);
173 // TODO: menu listener should enable/disable entry depending upon selection
174 // state of the alignment
175 wsmenu.addMenuListener(new MenuListener()
179 public void menuSelected(MenuEvent e)
181 // TODO Auto-generated method stub
186 public void menuDeselected(MenuEvent e)
188 // TODO Auto-generated method stub
193 public void menuCanceled(MenuEvent e)
195 // TODO Auto-generated method stub
204 * record of initial undoredo hash for the alignFrame providing data for this
207 long[] undoredo = null;
210 * Compare the original input data to the data currently presented to the
211 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
212 * any changes in hidden columns as normal) // if different undo/redo then
213 * compare region that was submitted // if same, then merge as before, if
214 * different then prompt user to open a new window.
218 protected boolean isAlignmentModified()
220 if (undoredo == null || av == null || av.getAlignment() == null)
222 // always return modified if we don't have access to live GUI elements
226 if (av.isUndoRedoHashModified(undoredo))
228 // alignment has been modified in some way.
231 // TODO: look deeper into modification of selection state, etc that may
232 // affect RestJobThread.realiseResults(boolean merge);
238 * TODO: combine to form a dataset+alignment+annotation context
240 AlignmentView _input;
245 jalview.io.packed.JalviewDataset jds;
248 * informative name for results
250 public String viewTitle;
252 protected void constructJob()
254 service.setInvolvesFlags();
255 // record all aspects of alignment view so we can merge back or recreate
257 undoredo = av.getUndoRedoHash();
259 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
260 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
261 * new SequenceGroup[0]; }
263 boolean selExists = (av.getSelectionGroup() != null)
264 && (av.getSelectionGroup().getSize() > 1);
265 // TODO: JAL-715: refactor to alignViewport methods and revise to full
266 // focus+context+dataset input data staging model
269 if (service.partitiondata)
271 if (av.getAlignment().getGroups() != null
272 && av.getAlignment().getGroups().size() > 0)
274 // intersect groups with selected region
275 _input = new AlignmentView(av.getAlignment(),
276 av.getAlignment().getHiddenColumns(),
277 av.getSelectionGroup(), av.hasHiddenColumns(), true,
279 viewTitle = MessageManager.formatMessage(
280 "label.select_visible_region_of", new String[]
281 { (av.hasHiddenColumns()
282 ? MessageManager.getString("label.visible")
288 // use selected region to partition alignment
289 _input = new AlignmentView(av.getAlignment(),
290 av.getAlignment().getHiddenColumns(),
291 av.getSelectionGroup(), av.hasHiddenColumns(), false,
294 viewTitle = MessageManager.formatMessage(
295 "label.select_unselect_visible_regions_from", new String[]
296 { (av.hasHiddenColumns()
297 ? MessageManager.getString("label.visible")
303 // just take selected region intersection
304 _input = new AlignmentView(av.getAlignment(),
305 av.getAlignment().getHiddenColumns(),
306 av.getSelectionGroup(), av.hasHiddenColumns(), true, true);
307 viewTitle = MessageManager.formatMessage(
308 "label.select_visible_region_of", new String[]
309 { (av.hasHiddenColumns()
310 ? MessageManager.getString("label.visible")
317 // standard alignment view without selection present
318 _input = new AlignmentView(av.getAlignment(),
319 av.getAlignment().getHiddenColumns(), null,
320 av.hasHiddenColumns(), false, true);
322 + (av.hasHiddenColumns()
323 ? (new StringBuffer(" ")
324 .append(MessageManager
325 .getString("label.visible_region_of"))
331 RestJobThread jobsthread = new RestJobThread(this);
333 if (jobsthread.isValid())
335 setWebserviceInfo(headless);
338 wsInfo.setthisService(this);
339 jobsthread.setWebServiceInfo(wsInfo);
345 // TODO: try to tell the user why the job couldn't be started.
346 JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
347 (jobsthread.hasWarnings() ? jobsthread.getWarnings()
348 : MessageManager.getString(
349 "label.job_couldnt_be_started_check_input")),
351 .getString("label.unable_start_web_service_analysis"),
352 JvOptionPane.WARNING_MESSAGE);
356 public static RestClient makeShmmrRestClient()
358 String action = "Analysis",
359 description = "Sequence Harmony and Multi-Relief (Brandt et al. 2010)",
360 name = MessageManager.getString("label.multiharmony");
361 Hashtable<String, InputType> iparams = new Hashtable<String, InputType>();
362 jalview.ws.rest.params.JobConstant toolp;
363 // toolp = new jalview.ws.rest.JobConstant("tool","jalview");
364 // iparams.put(toolp.token, toolp);
365 // toolp = new jalview.ws.rest.params.JobConstant("mbjob[method]","shmr");
366 // iparams.put(toolp.token, toolp);
368 // jalview.ws.rest.params.JobConstant("mbjob[description]","step 1");
369 // iparams.put(toolp.token, toolp);
370 // toolp = new jalview.ws.rest.params.JobConstant("start_search","1");
371 // iparams.put(toolp.token, toolp);
372 // toolp = new jalview.ws.rest.params.JobConstant("blast","0");
373 // iparams.put(toolp.token, toolp);
375 jalview.ws.rest.params.Alignment aliinput = new jalview.ws.rest.params.Alignment();
376 // SHMR server has a 65K limit for content pasted into the 'ali' parameter,
377 // so we always upload our files.
378 aliinput.token = "ali_file";
379 aliinput.writeAsFile = true;
380 iparams.put(aliinput.token, aliinput);
381 jalview.ws.rest.params.SeqGroupIndexVector sgroups = new jalview.ws.rest.params.SeqGroupIndexVector();
382 sgroups.setMinsize(2);
383 sgroups.min = 2;// need at least two group defined to make a partition
384 iparams.put("groups", sgroups);
385 sgroups.token = "groups";
387 RestServiceDescription shmrService = new RestServiceDescription(action,
389 "http://zeus.few.vu.nl/programs/shmrwww/index.php?tool=jalview", // ?tool=jalview&mbjob[method]=shmr&mbjob[description]=step1",
390 "?tool=jalview", iparams, true, false, '-');
391 // a priori knowledge of the data returned from the service
392 shmrService.addResultDatatype(JvDataType.ANNOTATION);
393 return new RestClient(shmrService);
396 public AlignmentPanel recoverAlignPanelForView()
398 AlignmentPanel[] aps = Desktop
399 .getAlignmentPanels(av.getSequenceSetId());
400 for (AlignmentPanel alp : aps)
410 public boolean isShowResultsInNewView()
412 // TODO make this a property of the service
416 public static RestClient[] getRestClients()
418 return getInstance().getClients();
421 private RestClient[] getClients()
423 if (services == null)
425 services = new Vector<String>();
428 for (RestServiceDescription descr : RestServiceDescription
430 jalview.bin.Cache.getDefault(RSBS_SERVICES,
431 makeShmmrRestClient().service.toString())))
433 services.add(descr.toString());
435 } catch (Exception ex)
438 "Serious - RSBS descriptions in user preferences are corrupt!");
439 ex.printStackTrace();
443 RestClient[] lst = new RestClient[services.size()];
445 for (String svc : services)
447 lst[i++] = new RestClient(new RestServiceDescription(svc));
452 public String getAction()
454 return service.details.Action;
457 public RestServiceDescription getRestDescription()
462 public static Vector<String> getRsbsDescriptions()
464 Vector<String> rsbsDescrs = new Vector<String>();
465 for (RestClient rsbs : getRestClients())
467 rsbsDescrs.add(rsbs.getRestDescription().toString());
472 public static void setRsbsServices(Vector<String> rsbsUrls)
474 if (rsbsUrls != null)
477 // TODO: consider validating services ?
478 getInstance().services = new Vector<String>(rsbsUrls);
479 StringBuffer sprop = new StringBuffer();
480 for (String s : getInstance().services)
484 Cache.setProperty(RSBS_SERVICES, sprop.toString());
488 Cache.removeProperty(RSBS_SERVICES);