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