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