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