JAL-1604 Entire sequence submision for MSA job bugfix
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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.datamodel.SequenceI;
26 import jalview.gui.AlignFrame;
27 import jalview.gui.Desktop;
28 import jalview.gui.JvSwingUtils;
29 import jalview.util.MessageManager;
30 import jalview.ws.jws2.jabaws2.Jws2Instance;
31 import jalview.ws.params.WsParamSetI;
32
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseAdapter;
36 import java.awt.event.MouseEvent;
37 import java.util.List;
38
39 import javax.swing.JMenu;
40 import javax.swing.JMenuItem;
41 import javax.swing.JOptionPane;
42 import javax.swing.ToolTipManager;
43
44 import compbio.data.msa.MsaWS;
45 import compbio.metadata.Argument;
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, Alignment 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, Alignment 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, Alignment 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       JOptionPane.showMessageDialog(Desktop.desktop, MessageManager
112               .formatMessage("label.service_called_is_not_msa_service",
113                       new String[]
114                       { sh.serviceType }), MessageManager
115               .getString("label.internal_jalview_error"),
116               JOptionPane.WARNING_MESSAGE);
117
118       return;
119     }
120     server = (MsaWS) sh.service;
121     if ((wsInfo = setWebService(sh, false)) == null)
122     {
123       JOptionPane.showMessageDialog(Desktop.desktop, MessageManager
124               .formatMessage("label.msa_service_is_unknown", new String[]
125               { sh.serviceType }), MessageManager
126               .getString("label.internal_jalview_error"),
127               JOptionPane.WARNING_MESSAGE);
128
129       return;
130     }
131
132     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
133
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, Alignment 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();
154     if (jobtitle.endsWith("alignment"))
155     {
156       if (submitGaps
157               && (!jobtitle.endsWith("realignment") || jobtitle
158                       .indexOf("profile") == -1))
159       {
160         int pos = jobtitle.indexOf("alignment");
161         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
162                 + altitle;
163       }
164       else
165       {
166         jobtitle = WebServiceName + " of " + altitle;
167       }
168     }
169     else
170     {
171       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
172               + "alignment of " + altitle;
173     }
174
175     MsaWSThread msathread = new MsaWSThread(server, preset, paramset,
176             WsURL, wsInfo, alignFrame, WebServiceName, jobtitle, msa,
177             submitGaps, preserveOrder, seqdataset);
178     wsInfo.setthisService(msathread);
179
180     msathread.start();
181
182   }
183
184   private boolean isValidAlignment(AlignmentView av)
185   {
186     int validSeqCount = 0;
187     List<SequenceI> seqs = av.getVisibleAlignment('c').getSequences(); // .getSequences();
188     if (seqs.size() < 2)
189     {
190       JOptionPane
191               .showMessageDialog(
192                       alignFrame,
193                       "A minimum of two sequences is required to perform this operation",
194                       "Invalid selection", JOptionPane.INFORMATION_MESSAGE);
195
196       return false;
197     }
198
199     for (SequenceI seq : seqs)
200     {
201
202       if (seq.getSequenceAsString().matches("(-*[a-zA-Z]-*){3}[a-zA-Z-]*"))
203       {
204         ++validSeqCount;
205       }
206       if (validSeqCount > 1)
207       {
208         return true;
209       }
210     }
211
212     JOptionPane
213             .showMessageDialog(
214                     alignFrame,
215                     "All selected sequence for this job must have a  minimum of \nthree non-gap character to perform this operation",
216                     "Invalid selection", JOptionPane.INFORMATION_MESSAGE);
217
218     return false;
219   }
220
221   public static void main(String[] args)
222   {
223     System.out.println("A".matches("(-*[a-zA-Z]-*){1}[a-zA-Z-]*"));
224   }
225
226
227
228   protected String getServiceActionKey()
229   {
230     return "MsaWS";
231   }
232
233   protected String getServiceActionDescription()
234   {
235     return "Multiple Sequence Alignment";
236   }
237
238   /**
239    * look at ourselves and work out if we are a service that can take a profile
240    * and align to it
241    * 
242    * @return true if we can send gapped sequences to the alignment service
243    */
244   private boolean canSubmitGaps()
245   {
246     // TODO: query service or extract service handle props to check if we can
247     // realign
248     return (WebServiceName.indexOf("lustal") > -1); // cheat!
249   }
250
251   public void attachWSMenuEntry(JMenu rmsawsmenu,
252           final Jws2Instance service, final AlignFrame alignFrame)
253   {
254     if (registerAAConWSInstance(rmsawsmenu, service, alignFrame))
255     {
256       // Alignment dependent analysis calculation WS gui
257       return;
258     }
259     setWebService(service, true); // headless
260     boolean finished = true, submitGaps = false;
261     JMenu msawsmenu = rmsawsmenu;
262     String svcname = WebServiceName;
263     if (svcname.endsWith("WS"))
264     {
265       svcname = svcname.substring(0, svcname.length() - 2);
266     }
267     String calcName = svcname + " ";
268     if (canSubmitGaps())
269     {
270       msawsmenu = new JMenu(svcname);
271       rmsawsmenu.add(msawsmenu);
272       calcName = "";
273     }
274     boolean hasparams = service.hasParameters();
275     do
276     {
277       String action = "Align ";
278       if (submitGaps == true)
279       {
280         action = "Realign ";
281         msawsmenu = new JMenu(MessageManager.formatMessage(
282                 "label.realign_with_params", new String[]
283                 { svcname }));
284         msawsmenu.setToolTipText(MessageManager
285                 .getString("label.align_sequences_to_existing_alignment"));
286         rmsawsmenu.add(msawsmenu);
287       }
288       final boolean withGaps = submitGaps;
289
290       JMenuItem method = new JMenuItem(MessageManager.formatMessage(
291               "label.calcname_with_default_settings", new String[]
292               { calcName }));
293       method.setToolTipText(MessageManager.formatMessage(
294               "label.action_with_default_settings", new String[]
295               { action }));
296
297       method.addActionListener(new ActionListener()
298       {
299         public void actionPerformed(ActionEvent e)
300         {
301           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
302
303           if (msa != null && isValidAlignment(msa))
304           {
305           new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
306                   true, alignFrame.getViewport().getAlignment()
307                           .getDataset(), 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
320                 .getString("label.view_and_change_parameters_before_alignment"));
321
322         method.addActionListener(new ActionListener()
323         {
324           public void actionPerformed(ActionEvent e)
325           {
326             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
327
328             if (msa != null && isValidAlignment(msa))
329             {
330             new MsaWSClient(service, null, null, true, alignFrame
331                     .getTitle(), msa, withGaps, true, alignFrame
332                     .getViewport().getAlignment().getDataset(), alignFrame);
333             }
334
335           }
336         });
337         msawsmenu.add(method);
338         List<WsParamSetI> presets = service.getParamStore().getPresets();
339         if (presets != null && presets.size() > 0)
340         {
341           JMenu presetlist = new JMenu(MessageManager.formatMessage(
342                   "label.run_with_preset_params", new String[]
343                   { calcName }));
344
345           final int showToolTipFor = ToolTipManager.sharedInstance()
346                   .getDismissDelay();
347           for (final WsParamSetI preset : presets)
348           {
349             final JMenuItem methodR = new JMenuItem(preset.getName());
350             final int QUICK_TOOLTIP = 1500;
351             // JAL-1582 shorten tooltip display time in these menu items as
352             // they can obscure other options
353             methodR.addMouseListener(new MouseAdapter()
354             {
355               @Override
356               public void mouseEntered(MouseEvent e)
357               {
358                 ToolTipManager.sharedInstance().setDismissDelay(
359                         QUICK_TOOLTIP);
360               }
361
362               @Override
363               public void mouseExited(MouseEvent e)
364               {
365                 ToolTipManager.sharedInstance().setDismissDelay(showToolTipFor);
366               }
367
368             });
369             methodR.setToolTipText(JvSwingUtils.wrapTooltip(
370                     true,
371                     "<p><strong>"
372                             + (preset.isModifiable() ? MessageManager
373                                     .getString("label.user_preset")
374                                     : MessageManager
375                                             .getString("label.service_preset"))
376                             + "</strong><br/>" + preset.getDescription()
377                             + "</p>"));
378             methodR.addActionListener(new ActionListener()
379             {
380               public void actionPerformed(ActionEvent e)
381               {
382                 AlignmentView msa = alignFrame
383                         .gatherSequencesForAlignment();
384
385                 if (msa != null && isValidAlignment(msa))
386                 {
387                 new MsaWSClient(service, preset, alignFrame.getTitle(),
388                         msa, false, true, alignFrame.getViewport()
389                                 .getAlignment().getDataset(), alignFrame);
390                 }
391
392               }
393
394             });
395             presetlist.add(methodR);
396           }
397           msawsmenu.add(presetlist);
398         }
399       }
400       if (!submitGaps && canSubmitGaps())
401       {
402         submitGaps = true;
403         finished = false;
404       }
405       else
406       {
407         finished = true;
408       }
409     } while (!finished);
410   }
411 }