8ed4a36ad37ea455bf3ba9de93602fd6ca7ef09d
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ws.jws2;
22
23 import java.util.Locale;
24
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.MouseAdapter;
28 import java.awt.event.MouseEvent;
29 import java.util.List;
30
31 import javax.swing.JMenu;
32 import javax.swing.JMenuItem;
33 import javax.swing.ToolTipManager;
34
35 import compbio.data.msa.MsaWS;
36 import compbio.metadata.Argument;
37 import jalview.datamodel.AlignmentI;
38 import jalview.datamodel.AlignmentView;
39 import jalview.gui.AlignFrame;
40 import jalview.gui.Desktop;
41 import jalview.gui.JvOptionPane;
42 import jalview.gui.JvSwingUtils;
43 import jalview.util.MessageManager;
44 import jalview.ws.jws2.jabaws2.Jws2Instance;
45 import jalview.ws.params.WsParamSetI;
46
47 /**
48  * DOCUMENT ME!
49  * 
50  * @author $author$
51  * @version $Revision$
52  */
53 public class MsaWSClient extends Jws2Client
54 {
55   /**
56    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
57    */
58   MsaWS server;
59
60   public MsaWSClient(Jws2Instance sh, String altitle,
61           jalview.datamodel.AlignmentView msa, boolean submitGaps,
62           boolean preserveOrder, AlignmentI seqdataset,
63           AlignFrame _alignFrame)
64   {
65     this(sh, null, null, false, altitle, msa, submitGaps, preserveOrder,
66             seqdataset, _alignFrame);
67     // TODO Auto-generated constructor stub
68   }
69
70   public MsaWSClient(Jws2Instance sh, WsParamSetI preset, String altitle,
71           jalview.datamodel.AlignmentView msa, boolean submitGaps,
72           boolean preserveOrder, AlignmentI seqdataset,
73           AlignFrame _alignFrame)
74   {
75     this(sh, preset, null, false, altitle, msa, submitGaps, preserveOrder,
76             seqdataset, _alignFrame);
77     // TODO Auto-generated constructor stub
78   }
79
80   /**
81    * Creates a new MsaWSClient object that uses a service given by an externally
82    * retrieved ServiceHandle
83    * 
84    * @param sh
85    *          service handle of type AbstractName(MsaWS)
86    * @param altitle
87    *          DOCUMENT ME!
88    * @param msa
89    *          DOCUMENT ME!
90    * @param submitGaps
91    *          DOCUMENT ME!
92    * @param preserveOrder
93    *          DOCUMENT ME!
94    */
95
96   public MsaWSClient(Jws2Instance sh, WsParamSetI preset,
97           List<Argument> arguments, boolean editParams, String altitle,
98           jalview.datamodel.AlignmentView msa, boolean submitGaps,
99           boolean preserveOrder, AlignmentI seqdataset,
100           AlignFrame _alignFrame)
101   {
102     super(_alignFrame, preset, arguments);
103     if (!processParams(sh, editParams))
104     {
105       return;
106     }
107
108     if (!(sh.service instanceof MsaWS))
109     {
110       // redundant at mo - but may change
111       JvOptionPane.showMessageDialog(Desktop.desktop,
112               MessageManager.formatMessage(
113                       "label.service_called_is_not_msa_service",
114                       new String[]
115                       { sh.serviceType }),
116               MessageManager.getString("label.internal_jalview_error"),
117               JvOptionPane.WARNING_MESSAGE);
118
119       return;
120     }
121     server = (MsaWS) sh.service;
122     if ((wsInfo = setWebService(sh, false)) == null)
123     {
124       JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
125               .formatMessage("label.msa_service_is_unknown", new String[]
126               { sh.serviceType }),
127               MessageManager.getString("label.internal_jalview_error"),
128               JvOptionPane.WARNING_MESSAGE);
129
130       return;
131     }
132
133     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
134
135   }
136
137   public MsaWSClient()
138   {
139     super();
140     // add a class reference to the list
141   }
142
143   private void startMsaWSClient(String altitle, AlignmentView msa,
144           boolean submitGaps, boolean preserveOrder, AlignmentI seqdataset)
145   {
146     // if (!locateWebService())
147     // {
148     // return;
149     // }
150
151     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
152             + " of " + altitle + "\nJob details\n");
153     String jobtitle = WebServiceName.toLowerCase(Locale.ROOT);
154     if (jobtitle.endsWith("alignment"))
155     {
156       if (submitGaps && (!jobtitle.endsWith("realignment")
157               || jobtitle.indexOf("profile") == -1))
158       {
159         int pos = jobtitle.indexOf("alignment");
160         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
161                 + altitle;
162       }
163       else
164       {
165         jobtitle = WebServiceName + " of " + altitle;
166       }
167     }
168     else
169     {
170       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
171               + "alignment of " + altitle;
172     }
173
174     MsaWSThread msathread = new MsaWSThread(server, preset, paramset, WsURL,
175             wsInfo, alignFrame, WebServiceName, jobtitle, msa, submitGaps,
176             preserveOrder, seqdataset);
177     if (msathread.hasValidInput())
178     {
179       wsInfo.setthisService(msathread);
180       wsInfo.setVisible(true);
181       msathread.start();
182     }
183     else
184     {
185       wsInfo.setVisible(false);
186       JvOptionPane.showMessageDialog(alignFrame,
187               MessageManager.getString("info.invalid_msa_input_mininfo"),
188               MessageManager.getString("info.invalid_msa_notenough"),
189               JvOptionPane.INFORMATION_MESSAGE);
190     }
191   }
192
193   protected String getServiceActionKey()
194   {
195     return "MsaWS";
196   }
197
198   protected String getServiceActionDescription()
199   {
200     return "Multiple Sequence Alignment";
201   }
202
203   /**
204    * look at ourselves and work out if we are a service that can take a profile
205    * and align to it
206    * 
207    * @return true if we can send gapped sequences to the alignment service
208    */
209   private boolean canSubmitGaps()
210   {
211     // TODO: query service or extract service handle props to check if we can
212     // realign
213     return (WebServiceName.indexOf("lustal") > -1); // cheat!
214   }
215
216   @Override
217   public void attachWSMenuEntry(JMenu rmsawsmenu,
218           final Jws2Instance service, final AlignFrame af)
219   {
220     if (registerAAConWSInstance(rmsawsmenu, service, af))
221     {
222       // Alignment dependent analysis calculation WS gui
223       return;
224     }
225     setWebService(service, true); // headless
226     boolean finished = true, submitGaps = false;
227     JMenu msawsmenu = rmsawsmenu;
228     String svcname = WebServiceName;
229     if (svcname.endsWith("WS"))
230     {
231       svcname = svcname.substring(0, svcname.length() - 2);
232     }
233     String calcName = svcname + " ";
234     if (canSubmitGaps())
235     {
236       msawsmenu = new JMenu(svcname);
237       rmsawsmenu.add(msawsmenu);
238       calcName = "";
239     }
240     boolean hasparams = service.hasParameters();
241     do
242     {
243       String action = "Align ";
244       if (submitGaps == true)
245       {
246         action = "Realign ";
247         msawsmenu = new JMenu(MessageManager
248                 .formatMessage("label.realign_with_params", new String[]
249                 { svcname }));
250         msawsmenu.setToolTipText(MessageManager
251                 .getString("label.align_sequences_to_existing_alignment"));
252         rmsawsmenu.add(msawsmenu);
253       }
254       final boolean withGaps = submitGaps;
255
256       JMenuItem method = new JMenuItem(MessageManager.formatMessage(
257               "label.calcname_with_default_settings", new String[]
258               { calcName }));
259       method.setToolTipText(MessageManager.formatMessage(
260               "label.action_with_default_settings", new String[]
261               { action }));
262
263       method.addActionListener(new ActionListener()
264       {
265         @Override
266         public void actionPerformed(ActionEvent e)
267         {
268           AlignmentView msa = af.gatherSequencesForAlignment();
269
270           if (msa != null)
271           {
272             new MsaWSClient(service, af.getTitle(), msa, withGaps,
273                     true,
274                     af.getViewport().getAlignment().getDataset(),
275                     af);
276           }
277
278         }
279       });
280       msawsmenu.add(method);
281       if (hasparams)
282       {
283         // only add these menu options if the service has user-modifiable
284         // arguments
285         method = new JMenuItem(
286                 MessageManager.getString("label.edit_settings_and_run"));
287         method.setToolTipText(MessageManager.getString(
288                 "label.view_and_change_parameters_before_alignment"));
289
290         method.addActionListener(new ActionListener()
291         {
292           @Override
293           public void actionPerformed(ActionEvent e)
294           {
295             AlignmentView msa = af.gatherSequencesForAlignment();
296             if (msa != null)
297             {
298               startJob(service, af, withGaps, msa);
299             }
300
301           }
302         });
303         msawsmenu.add(method);
304         List<WsParamSetI> presets = service.getParamStore().getPresets();
305         if (presets != null && presets.size() > 0)
306         {
307           JMenu presetlist = new JMenu(MessageManager.formatMessage(
308                   "label.run_with_preset_params", new String[]
309                   { calcName }));
310
311           final int showToolTipFor = ToolTipManager.sharedInstance()
312                   .getDismissDelay();
313           for (final WsParamSetI preSet : presets)
314           {
315             final JMenuItem methodR = new JMenuItem(preSet.getName());
316             final int QUICK_TOOLTIP = 1500;
317             // JAL-1582 shorten tooltip display time in these menu items as
318             // they can obscure other options
319             methodR.addMouseListener(new MouseAdapter()
320             {
321               @Override
322               public void mouseEntered(MouseEvent e)
323               {
324                 ToolTipManager.sharedInstance()
325                         .setDismissDelay(QUICK_TOOLTIP);
326               }
327
328               @Override
329               public void mouseExited(MouseEvent e)
330               {
331                 ToolTipManager.sharedInstance()
332                         .setDismissDelay(showToolTipFor);
333               }
334
335             });
336             String tooltip = JvSwingUtils.wrapTooltip(true, "<strong>"
337                     + (preSet.isModifiable()
338                             ? MessageManager.getString("label.user_preset")
339                             : MessageManager
340                                     .getString("label.service_preset"))
341                     + "</strong><br/>" + preSet.getDescription());
342             methodR.setToolTipText(tooltip);
343             methodR.addActionListener(new ActionListener()
344             {
345               @Override
346               public void actionPerformed(ActionEvent e)
347               {
348                 AlignmentView msa = af
349                         .gatherSequencesForAlignment();
350
351                 if (msa != null)
352                 {
353                   MsaWSClient msac = new MsaWSClient(service, preSet,
354                           af.getTitle(), msa, false, true,
355                           af.getViewport().getAlignment()
356                                   .getDataset(),
357                           af);
358                 }
359
360               }
361
362             });
363             presetlist.add(methodR);
364           }
365           msawsmenu.add(presetlist);
366         }
367       }
368       if (!submitGaps && canSubmitGaps())
369       {
370         submitGaps = true;
371         finished = false;
372       }
373       else
374       {
375         finished = true;
376       }
377     } while (!finished);
378   }
379
380   protected void startJob(final Jws2Instance service, final AlignFrame af,
381           final boolean withGaps, AlignmentView msa)
382   {
383     try {
384     new MsaWSClient(service, null, null, true,
385             af.getTitle(), msa, withGaps, true,
386             af.getViewport().getAlignment().getDataset(),
387             af);
388     } catch (Exception e) {
389       JvOptionPane.showMessageDialog(alignFrame, e.getMessage(),
390               MessageManager.getString("label.state_job_error"),
391               JvOptionPane.WARNING_MESSAGE);
392
393     }
394   }
395 }