d9452a3c06b0722900df9403660bf26020eace97
[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(
203               "([(a-zA-Z?)(-?)(.?)]+)?[a-zA-Z]([(a-zA-Z?)(-?)(.?)]+)?"))
204       {
205         ++validSeqCount;
206       }
207       if (validSeqCount > 1)
208       {
209         return true;
210       }
211     }
212
213     JOptionPane
214             .showMessageDialog(
215                     alignFrame,
216                     "A minimum of two sequences with at least one non-gap character \nin each sequence is required to perform this operation",
217                     "Invalid selection", JOptionPane.INFORMATION_MESSAGE);
218
219     return false;
220   }
221
222   public static void main(String[] args)
223   {
224     System.out
225             .println("A"
226                     .matches("([(a-zA-Z?)(-?)(.?)]+)?[a-zA-Z]([(a-zA-Z?)(-?)(.?)]+)?"));
227   }
228
229
230
231   protected String getServiceActionKey()
232   {
233     return "MsaWS";
234   }
235
236   protected String getServiceActionDescription()
237   {
238     return "Multiple Sequence Alignment";
239   }
240
241   /**
242    * look at ourselves and work out if we are a service that can take a profile
243    * and align to it
244    * 
245    * @return true if we can send gapped sequences to the alignment service
246    */
247   private boolean canSubmitGaps()
248   {
249     // TODO: query service or extract service handle props to check if we can
250     // realign
251     return (WebServiceName.indexOf("lustal") > -1); // cheat!
252   }
253
254   public void attachWSMenuEntry(JMenu rmsawsmenu,
255           final Jws2Instance service, final AlignFrame alignFrame)
256   {
257     if (registerAAConWSInstance(rmsawsmenu, service, alignFrame))
258     {
259       // Alignment dependent analysis calculation WS gui
260       return;
261     }
262     setWebService(service, true); // headless
263     boolean finished = true, submitGaps = false;
264     JMenu msawsmenu = rmsawsmenu;
265     String svcname = WebServiceName;
266     if (svcname.endsWith("WS"))
267     {
268       svcname = svcname.substring(0, svcname.length() - 2);
269     }
270     String calcName = svcname + " ";
271     if (canSubmitGaps())
272     {
273       msawsmenu = new JMenu(svcname);
274       rmsawsmenu.add(msawsmenu);
275       calcName = "";
276     }
277     boolean hasparams = service.hasParameters();
278     do
279     {
280       String action = "Align ";
281       if (submitGaps == true)
282       {
283         action = "Realign ";
284         msawsmenu = new JMenu(MessageManager.formatMessage(
285                 "label.realign_with_params", new String[]
286                 { svcname }));
287         msawsmenu.setToolTipText(MessageManager
288                 .getString("label.align_sequences_to_existing_alignment"));
289         rmsawsmenu.add(msawsmenu);
290       }
291       final boolean withGaps = submitGaps;
292
293       JMenuItem method = new JMenuItem(MessageManager.formatMessage(
294               "label.calcname_with_default_settings", new String[]
295               { calcName }));
296       method.setToolTipText(MessageManager.formatMessage(
297               "label.action_with_default_settings", new String[]
298               { action }));
299
300       method.addActionListener(new ActionListener()
301       {
302         public void actionPerformed(ActionEvent e)
303         {
304           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
305
306           if (isValidAlignment(msa))
307           {
308           new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
309                   true, alignFrame.getViewport().getAlignment()
310                           .getDataset(), alignFrame);
311           }
312
313         }
314       });
315       msawsmenu.add(method);
316       if (hasparams)
317       {
318         // only add these menu options if the service has user-modifiable
319         // arguments
320         method = new JMenuItem(
321                 MessageManager.getString("label.edit_settings_and_run"));
322         method.setToolTipText(MessageManager
323                 .getString("label.view_and_change_parameters_before_alignment"));
324
325         method.addActionListener(new ActionListener()
326         {
327           public void actionPerformed(ActionEvent e)
328           {
329             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
330
331             if (isValidAlignment(msa))
332             {
333             new MsaWSClient(service, null, null, true, alignFrame
334                     .getTitle(), msa, withGaps, true, alignFrame
335                     .getViewport().getAlignment().getDataset(), alignFrame);
336             }
337
338           }
339         });
340         msawsmenu.add(method);
341         List<WsParamSetI> presets = service.getParamStore().getPresets();
342         if (presets != null && presets.size() > 0)
343         {
344           JMenu presetlist = new JMenu(MessageManager.formatMessage(
345                   "label.run_with_preset_params", new String[]
346                   { calcName }));
347
348           final int showToolTipFor = ToolTipManager.sharedInstance()
349                   .getDismissDelay();
350           for (final WsParamSetI preset : presets)
351           {
352             final JMenuItem methodR = new JMenuItem(preset.getName());
353             final int QUICK_TOOLTIP = 1500;
354             // JAL-1582 shorten tooltip display time in these menu items as
355             // they can obscure other options
356             methodR.addMouseListener(new MouseAdapter()
357             {
358               @Override
359               public void mouseEntered(MouseEvent e)
360               {
361                 ToolTipManager.sharedInstance().setDismissDelay(
362                         QUICK_TOOLTIP);
363               }
364
365               @Override
366               public void mouseExited(MouseEvent e)
367               {
368                 ToolTipManager.sharedInstance().setDismissDelay(showToolTipFor);
369               }
370
371             });
372             methodR.setToolTipText(JvSwingUtils.wrapTooltip(
373                     true,
374                     "<p><strong>"
375                             + (preset.isModifiable() ? MessageManager
376                                     .getString("label.user_preset")
377                                     : MessageManager
378                                             .getString("label.service_preset"))
379                             + "</strong><br/>" + preset.getDescription()
380                             + "</p>"));
381             methodR.addActionListener(new ActionListener()
382             {
383               public void actionPerformed(ActionEvent e)
384               {
385                 AlignmentView msa = alignFrame
386                         .gatherSequencesForAlignment();
387
388                 if (isValidAlignment(msa))
389                 {
390                 new MsaWSClient(service, preset, alignFrame.getTitle(),
391                         msa, false, true, alignFrame.getViewport()
392                                 .getAlignment().getDataset(), 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 }