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