285dcc01d9224398c3ad6c98e6e9a4d6dcc5bcd1
[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 java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.List;
26
27 import javax.swing.*;
28
29 import jalview.datamodel.*;
30 import jalview.gui.*;
31 import compbio.data.msa.MsaWS;
32 import compbio.metadata.Argument;
33 import jalview.util.MessageManager;
34 import jalview.ws.jws2.jabaws2.Jws2Instance;
35 import jalview.ws.params.WsParamSetI;
36
37 /**
38  * DOCUMENT ME!
39  * 
40  * @author $author$
41  * @version $Revision$
42  */
43 public class MsaWSClient extends Jws2Client
44 {
45   /**
46    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
47    */
48   MsaWS server;
49
50   public MsaWSClient(Jws2Instance sh, String altitle,
51           jalview.datamodel.AlignmentView msa, boolean submitGaps,
52           boolean preserveOrder, Alignment seqdataset,
53           AlignFrame _alignFrame)
54   {
55     this(sh, null, null, false, altitle, msa, submitGaps, preserveOrder,
56             seqdataset, _alignFrame);
57     // TODO Auto-generated constructor stub
58   }
59
60   public MsaWSClient(Jws2Instance sh, WsParamSetI preset, String altitle,
61           jalview.datamodel.AlignmentView msa, boolean submitGaps,
62           boolean preserveOrder, Alignment seqdataset,
63           AlignFrame _alignFrame)
64   {
65     this(sh, preset, null, false, altitle, msa, submitGaps, preserveOrder,
66             seqdataset, _alignFrame);
67     // TODO Auto-generated constructor stub
68   }
69
70   /**
71    * Creates a new MsaWSClient object that uses a service given by an externally
72    * retrieved ServiceHandle
73    * 
74    * @param sh
75    *          service handle of type AbstractName(MsaWS)
76    * @param altitle
77    *          DOCUMENT ME!
78    * @param msa
79    *          DOCUMENT ME!
80    * @param submitGaps
81    *          DOCUMENT ME!
82    * @param preserveOrder
83    *          DOCUMENT ME!
84    */
85
86   public MsaWSClient(Jws2Instance sh, WsParamSetI preset,
87           List<Argument> arguments, boolean editParams, String altitle,
88           jalview.datamodel.AlignmentView msa, boolean submitGaps,
89           boolean preserveOrder, Alignment seqdataset,
90           AlignFrame _alignFrame)
91   {
92     super(_alignFrame, preset, arguments);
93     if (!processParams(sh, editParams))
94     {
95       return;
96     }
97
98     if (!(sh.service instanceof MsaWS))
99     {
100       // redundant at mo - but may change
101       JOptionPane
102               .showMessageDialog(
103                       Desktop.desktop,
104                       "The Service called \n"
105                               + sh.serviceType
106                               + "\nis not a \nMultiple Sequence Alignment Service !",
107                       "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
108
109       return;
110     }
111     server = (MsaWS) sh.service;
112     if ((wsInfo = setWebService(sh, false)) == null)
113     {
114       JOptionPane.showMessageDialog(Desktop.desktop,
115               "The Multiple Sequence Alignment Service named "
116                       + sh.serviceType + " is unknown",
117               "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
118
119       return;
120     }
121     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
122
123   }
124
125   public MsaWSClient()
126   {
127     super();
128     // add a class reference to the list
129   }
130
131   private void startMsaWSClient(String altitle, AlignmentView msa,
132           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
133   {
134     // if (!locateWebService())
135     // {
136     // return;
137     // }
138
139     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
140             + " of " + altitle + "\nJob details\n");
141     String jobtitle = WebServiceName.toLowerCase();
142     if (jobtitle.endsWith("alignment"))
143     {
144       if (submitGaps
145               && (!jobtitle.endsWith("realignment") || jobtitle
146                       .indexOf("profile") == -1))
147       {
148         int pos = jobtitle.indexOf("alignment");
149         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
150                 + altitle;
151       }
152       else
153       {
154         jobtitle = WebServiceName + " of " + altitle;
155       }
156     }
157     else
158     {
159       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
160               + "alignment of " + altitle;
161     }
162
163     MsaWSThread msathread = new MsaWSThread(server, preset, paramset,
164             WsURL, wsInfo, alignFrame, WebServiceName, jobtitle, msa,
165             submitGaps, preserveOrder, seqdataset);
166     wsInfo.setthisService(msathread);
167     msathread.start();
168   }
169
170   protected String getServiceActionKey()
171   {
172     return "MsaWS";
173   }
174
175   protected String getServiceActionDescription()
176   {
177     return "Multiple Sequence Alignment";
178   }
179
180   /**
181    * look at ourselves and work out if we are a service that can take a profile
182    * and align to it
183    * 
184    * @return true if we can send gapped sequences to the alignment service
185    */
186   private boolean canSubmitGaps()
187   {
188     // TODO: query service or extract service handle props to check if we can
189     // realign
190     return (WebServiceName.indexOf("lustal") > -1); // cheat!
191   }
192
193   public void attachWSMenuEntry(JMenu rmsawsmenu,
194           final Jws2Instance service, final AlignFrame alignFrame)
195   {
196     if (registerAAConWSInstance(rmsawsmenu, service, alignFrame))
197     {
198       // Alignment dependent analysis calculation WS gui
199       return;
200     }
201     setWebService(service, true); // headless
202     boolean finished = true, submitGaps = false;
203     JMenu msawsmenu = rmsawsmenu;
204     String svcname = WebServiceName;
205     if (svcname.endsWith("WS"))
206     {
207       svcname = svcname.substring(0, svcname.length() - 2);
208     }
209     String calcName = svcname + " ";
210     if (canSubmitGaps())
211     {
212       msawsmenu = new JMenu(svcname);
213       rmsawsmenu.add(msawsmenu);
214       calcName = "";
215     }
216     boolean hasparams = service.hasParameters();
217     do
218     {
219       String action = "Align ";
220       if (submitGaps == true)
221       {
222         action = "Realign ";
223         msawsmenu = new JMenu(MessageManager.formatMessage(
224                 "label.realign_with_params", new String[]
225                 { svcname }));
226         msawsmenu.setToolTipText(MessageManager
227                 .getString("label.align_sequences_to_existing_alignment"));
228         rmsawsmenu.add(msawsmenu);
229       }
230       final boolean withGaps = submitGaps;
231
232       JMenuItem method = new JMenuItem(MessageManager.formatMessage(
233               "label.calcname_with_default_settings", new String[]
234               { calcName }));
235       method.setToolTipText(MessageManager.formatMessage(
236               "label.action_with_default_settings", new String[]
237               { action }));
238
239       method.addActionListener(new ActionListener()
240       {
241         public void actionPerformed(ActionEvent e)
242         {
243           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
244           new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
245                   true, alignFrame.getViewport().getAlignment()
246                           .getDataset(), alignFrame);
247
248         }
249       });
250       msawsmenu.add(method);
251       if (hasparams)
252       {
253         // only add these menu options if the service has user-modifiable
254         // arguments
255         method = new JMenuItem(
256                 MessageManager.getString("label.edit_settings_and_run"));
257         method.setToolTipText(MessageManager
258                 .getString("label.view_and_change_parameters_before_alignment"));
259
260         method.addActionListener(new ActionListener()
261         {
262           public void actionPerformed(ActionEvent e)
263           {
264             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
265             new MsaWSClient(service, null, null, true, alignFrame
266                     .getTitle(), msa, withGaps, true, alignFrame
267                     .getViewport().getAlignment().getDataset(), alignFrame);
268
269           }
270         });
271         msawsmenu.add(method);
272         List<WsParamSetI> presets = service.getParamStore().getPresets();
273         if (presets != null && presets.size() > 0)
274         {
275           JMenu presetlist = new JMenu(MessageManager.formatMessage(
276                   "label.run_with_preset_params", new String[]
277                   { calcName }));
278
279           for (final WsParamSetI preset : presets)
280           {
281             final JMenuItem methodR = new JMenuItem(preset.getName());
282             methodR.setToolTipText("<html><p>"
283                     + JvSwingUtils.wrapTooltip("<strong>"
284                             + (preset.isModifiable() ? "User Preset"
285                                     : "Service Preset") + "</strong><br/>"
286                             + preset.getDescription() + "</p>") + "</html>");
287             methodR.addActionListener(new ActionListener()
288             {
289               public void actionPerformed(ActionEvent e)
290               {
291                 AlignmentView msa = alignFrame
292                         .gatherSequencesForAlignment();
293                 new MsaWSClient(service, preset, alignFrame.getTitle(),
294                         msa, false, true, alignFrame.getViewport()
295                                 .getAlignment().getDataset(), alignFrame);
296
297               }
298
299             });
300             presetlist.add(methodR);
301           }
302           msawsmenu.add(presetlist);
303         }
304       }
305       if (!submitGaps && canSubmitGaps())
306       {
307         submitGaps = true;
308         finished = false;
309       }
310       else
311       {
312         finished = true;
313       }
314     } while (!finished);
315   }
316 }