JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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.Alignment;
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, Alignment 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, Alignment 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, Alignment 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[]
113                       { sh.serviceType }), MessageManager
114               .getString("label.internal_jalview_error"),
115               JOptionPane.WARNING_MESSAGE);
116
117       return;
118     }
119     server = (MsaWS) sh.service;
120     if ((wsInfo = setWebService(sh, false)) == null)
121     {
122       JOptionPane.showMessageDialog(Desktop.desktop, MessageManager
123               .formatMessage("label.msa_service_is_unknown", new String[]
124               { sh.serviceType }), MessageManager
125               .getString("label.internal_jalview_error"),
126               JOptionPane.WARNING_MESSAGE);
127
128       return;
129     }
130
131     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
132
133
134   }
135
136   public MsaWSClient()
137   {
138     super();
139     // add a class reference to the list
140   }
141
142   private void startMsaWSClient(String altitle, AlignmentView msa,
143           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
144   {
145     // if (!locateWebService())
146     // {
147     // return;
148     // }
149
150     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
151             + " of " + altitle + "\nJob details\n");
152     String jobtitle = WebServiceName.toLowerCase();
153     if (jobtitle.endsWith("alignment"))
154     {
155       if (submitGaps
156               && (!jobtitle.endsWith("realignment") || jobtitle
157                       .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,
175             WsURL, wsInfo, alignFrame, WebServiceName, jobtitle, msa,
176             submitGaps, preserveOrder, seqdataset);
177     if (msathread.hasValidInput())
178     {
179       wsInfo.setthisService(msathread);
180       wsInfo.setVisible(true);
181       msathread.start();
182     }
183     else
184     {
185       JOptionPane.showMessageDialog(alignFrame,
186               MessageManager.getString("info.invalid_msa_input_mininfo"),
187               MessageManager.getString("info.invalid_msa_notenough"),
188               JOptionPane.INFORMATION_MESSAGE);
189       wsInfo.setVisible(false);
190     }
191   }
192
193   public static void main(String[] args)
194   {
195     System.out.println("A".matches("(-*[a-zA-Z]-*){1}[a-zA-Z-]*"));
196   }
197
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   public void attachWSMenuEntry(JMenu rmsawsmenu,
224           final Jws2Instance service, final AlignFrame alignFrame)
225   {
226     if (registerAAConWSInstance(rmsawsmenu, service, alignFrame))
227     {
228       // Alignment dependent analysis calculation WS gui
229       return;
230     }
231     setWebService(service, true); // headless
232     boolean finished = true, submitGaps = false;
233     JMenu msawsmenu = rmsawsmenu;
234     String svcname = WebServiceName;
235     if (svcname.endsWith("WS"))
236     {
237       svcname = svcname.substring(0, svcname.length() - 2);
238     }
239     String calcName = svcname + " ";
240     if (canSubmitGaps())
241     {
242       msawsmenu = new JMenu(svcname);
243       rmsawsmenu.add(msawsmenu);
244       calcName = "";
245     }
246     boolean hasparams = service.hasParameters();
247     do
248     {
249       String action = "Align ";
250       if (submitGaps == true)
251       {
252         action = "Realign ";
253         msawsmenu = new JMenu(MessageManager.formatMessage(
254                 "label.realign_with_params", new String[]
255                 { svcname }));
256         msawsmenu.setToolTipText(MessageManager
257                 .getString("label.align_sequences_to_existing_alignment"));
258         rmsawsmenu.add(msawsmenu);
259       }
260       final boolean withGaps = submitGaps;
261
262       JMenuItem method = new JMenuItem(MessageManager.formatMessage(
263               "label.calcname_with_default_settings", new String[]
264               { calcName }));
265       method.setToolTipText(MessageManager.formatMessage(
266               "label.action_with_default_settings", new String[]
267               { action }));
268
269       method.addActionListener(new ActionListener()
270       {
271         public void actionPerformed(ActionEvent e)
272         {
273           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
274
275           if (msa != null)
276           {
277           new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
278                   true, alignFrame.getViewport().getAlignment()
279                           .getDataset(), alignFrame);
280           }
281
282         }
283       });
284       msawsmenu.add(method);
285       if (hasparams)
286       {
287         // only add these menu options if the service has user-modifiable
288         // arguments
289         method = new JMenuItem(
290                 MessageManager.getString("label.edit_settings_and_run"));
291         method.setToolTipText(MessageManager
292                 .getString("label.view_and_change_parameters_before_alignment"));
293
294         method.addActionListener(new ActionListener()
295         {
296           public void actionPerformed(ActionEvent e)
297           {
298             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
299             if (msa != null)
300             {
301               new MsaWSClient(service, null, null, true, alignFrame
302                     .getTitle(), msa, withGaps, true, alignFrame
303                     .getViewport().getAlignment().getDataset(), alignFrame);
304             }
305
306           }
307         });
308         msawsmenu.add(method);
309         List<WsParamSetI> presets = service.getParamStore().getPresets();
310         if (presets != null && presets.size() > 0)
311         {
312           JMenu presetlist = new JMenu(MessageManager.formatMessage(
313                   "label.run_with_preset_params", new String[]
314                   { 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(showToolTipFor);
337               }
338
339             });
340             methodR.setToolTipText(JvSwingUtils.wrapTooltip(
341                     true,
342                     "<p><strong>"
343                             + (preset.isModifiable() ? MessageManager
344                                     .getString("label.user_preset")
345                                     : MessageManager
346                                             .getString("label.service_preset"))
347                             + "</strong><br/>" + preset.getDescription()
348                             + "</p>"));
349             methodR.addActionListener(new ActionListener()
350             {
351               public void actionPerformed(ActionEvent e)
352               {
353                 AlignmentView msa = alignFrame
354                         .gatherSequencesForAlignment();
355
356                 if (msa != null)
357                 {
358                   MsaWSClient msac = new MsaWSClient(service, preset,
359                           alignFrame.getTitle(),
360                         msa, false, true, alignFrame.getViewport()
361                                 .getAlignment().getDataset(), alignFrame);
362                 }
363
364               }
365
366             });
367             presetlist.add(methodR);
368           }
369           msawsmenu.add(presetlist);
370         }
371       }
372       if (!submitGaps && canSubmitGaps())
373       {
374         submitGaps = true;
375         finished = false;
376       }
377       else
378       {
379         finished = true;
380       }
381     } while (!finished);
382   }
383 }