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