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