update author list in license for (JAL-826)
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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  */
18 package jalview.ws.jws2;
19
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.util.ArrayList;
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 compbio.metadata.Option;
32 import compbio.metadata.Preset;
33 import compbio.metadata.PresetManager;
34 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
35 import jalview.ws.jws2.dm.JabaWsParamSet;
36 import jalview.ws.params.WsParamSetI;
37
38 /**
39  * DOCUMENT ME!
40  * 
41  * @author $author$
42  * @version $Revision$
43  */
44 public class MsaWSClient extends Jws2Client
45 {
46   /**
47    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
48    */
49   MsaWS server;
50
51   AlignFrame alignFrame;
52
53   private WsParamSetI preset;
54
55   private List<Argument> paramset;
56
57   public MsaWSClient(Jws2Discoverer.Jws2Instance sh, String altitle,
58           jalview.datamodel.AlignmentView msa, boolean submitGaps,
59           boolean preserveOrder, Alignment seqdataset,
60           AlignFrame _alignFrame)
61   {
62     this(sh, null, null, false, altitle, msa, submitGaps, preserveOrder,
63             seqdataset, _alignFrame);
64     // TODO Auto-generated constructor stub
65   }
66
67   public MsaWSClient(Jws2Discoverer.Jws2Instance sh, WsParamSetI preset,
68           String altitle, jalview.datamodel.AlignmentView msa,
69           boolean submitGaps, boolean preserveOrder, Alignment seqdataset,
70           AlignFrame _alignFrame)
71   {
72     this(sh, preset, null, false, altitle, msa, submitGaps, preserveOrder,
73             seqdataset, _alignFrame);
74     // TODO Auto-generated constructor stub
75   }
76
77   /**
78    * Creates a new MsaWSClient object that uses a service given by an externally
79    * retrieved ServiceHandle
80    * 
81    * @param sh
82    *          service handle of type AbstractName(MsaWS)
83    * @param altitle
84    *          DOCUMENT ME!
85    * @param msa
86    *          DOCUMENT ME!
87    * @param submitGaps
88    *          DOCUMENT ME!
89    * @param preserveOrder
90    *          DOCUMENT ME!
91    */
92
93   public MsaWSClient(Jws2Discoverer.Jws2Instance sh, WsParamSetI preset,
94           List<Argument> arguments, boolean editParams, String altitle,
95           jalview.datamodel.AlignmentView msa, boolean submitGaps,
96           boolean preserveOrder, Alignment seqdataset,
97           AlignFrame _alignFrame)
98   {
99     super();
100     alignFrame = _alignFrame;
101     if (!(sh.service instanceof MsaWS))
102     {
103       // redundant at mo - but may change
104       JOptionPane
105               .showMessageDialog(
106                       Desktop.desktop,
107                       "The Service called \n"
108                               + sh.serviceType
109                               + "\nis not a \nMultiple Sequence Alignment Service !",
110                       "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
111
112       return;
113     }
114     server = sh.service;
115     this.preset=preset;
116     if (preset != null)
117     {
118       if (!((preset instanceof JabaPreset) || preset instanceof JabaWsParamSet)) {
119       /*{
120         this.preset = ((JabaPreset) preset).p;
121       }
122       else if (preset instanceof JabaWsParamSet)
123       {
124         List<Argument> newargs = new ArrayList<Argument>();
125         JabaWsParamSet pset = ((JabaWsParamSet) preset);
126         for (Option opt : pset.getjabaArguments())
127         {
128           newargs.add(opt);
129         }
130         if (arguments != null && arguments.size() > 0)
131         {
132           // merge arguments with preset's own arguments.
133           for (Argument opt : arguments)
134           {
135             newargs.add(opt);
136           }
137         }
138         paramset = newargs;
139       }
140       else
141       {*/
142         throw new Error(
143                 "Implementation error: Can only instantiate Jaba parameter sets.");
144       }
145     }
146     else
147     {
148       // just provided with a bunch of arguments
149       this.paramset = arguments;
150     }
151     if (editParams)
152     {
153       if (sh.paramStore == null)
154       {
155         sh.paramStore = new JabaParamStore(sh,
156                 Desktop.getUserParameterStore());
157       }
158       WsJobParameters jobParams = new WsJobParameters(sh, preset);
159       if (!jobParams.showRunDialog())
160       {
161         return;
162       }
163       WsParamSetI prset = jobParams.getPreset();
164       if (prset == null)
165       {
166         paramset = JabaParamStore.getJabafromJwsArgs(jobParams
167                 .getJobParams());
168       }
169       else
170       {
171         this.preset = prset; // ((JabaPreset) prset).p;
172         paramset = null; // no user supplied parameters.
173       }
174     }
175
176     if ((wsInfo = setWebService(sh, false)) == null)
177     {
178       JOptionPane.showMessageDialog(Desktop.desktop,
179               "The Multiple Sequence Alignment Service named "
180                       + sh.serviceType + " is unknown",
181               "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
182
183       return;
184     }
185     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
186
187   }
188
189   public MsaWSClient()
190   {
191     super();
192     // add a class reference to the list
193   }
194
195   private void startMsaWSClient(String altitle, AlignmentView msa,
196           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
197   {
198     // if (!locateWebService())
199     // {
200     // return;
201     // }
202
203     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
204             + " of " + altitle + "\nJob details\n");
205     String jobtitle = WebServiceName.toLowerCase();
206     if (jobtitle.endsWith("alignment"))
207     {
208       if (submitGaps
209               && (!jobtitle.endsWith("realignment") || jobtitle
210                       .indexOf("profile") == -1))
211       {
212         int pos = jobtitle.indexOf("alignment");
213         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
214                 + altitle;
215       }
216       else
217       {
218         jobtitle = WebServiceName + " of " + altitle;
219       }
220     }
221     else
222     {
223       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
224               + "alignment of " + altitle;
225     }
226
227     MsaWSThread msathread = new MsaWSThread(server, preset, paramset,
228             WsURL, wsInfo, alignFrame, WebServiceName, jobtitle, msa,
229             submitGaps, preserveOrder, seqdataset);
230     wsInfo.setthisService(msathread);
231     msathread.start();
232   }
233
234   protected String getServiceActionKey()
235   {
236     return "MsaWS";
237   }
238
239   protected String getServiceActionDescription()
240   {
241     return "Multiple Sequence Alignment";
242   }
243
244   /**
245    * look at ourselves and work out if we are a service that can take a profile
246    * and align to it
247    * 
248    * @return true if we can send gapped sequences to the alignment service
249    */
250   private boolean canSubmitGaps()
251   {
252     // TODO: query service or extract service handle props to check if we can
253     // realign
254     return (WebServiceName.indexOf("lustal") > -1); // cheat!
255   }
256
257   public void attachWSMenuEntry(JMenu rmsawsmenu,
258           final Jws2Instance service, final AlignFrame alignFrame)
259   {
260     setWebService(service, true); // headless
261     boolean finished = true, submitGaps = false;
262     JMenu msawsmenu = rmsawsmenu;
263     String svcname = WebServiceName;
264     if (svcname.endsWith("WS"))
265     {
266       svcname = svcname.substring(0, svcname.length() - 2);
267     }
268     String calcName = svcname + " ";
269     if (canSubmitGaps())
270     {
271       msawsmenu = new JMenu(svcname);
272       rmsawsmenu.add(msawsmenu);
273       calcName = "";
274     }
275     boolean hasparams = service.hasParameters();
276     do
277     {
278       String action = "Align ";
279       if (submitGaps == true)
280       {
281         action = "Realign ";
282         msawsmenu = new JMenu("Realign with " + svcname);
283         msawsmenu
284                 .setToolTipText("Align sequences to an existing alignment");
285         rmsawsmenu.add(msawsmenu);
286       }
287       final boolean withGaps = submitGaps;
288
289       JMenuItem method = new JMenuItem(calcName + "with Defaults");
290       method.setToolTipText(action + "with default settings");
291
292       method.addActionListener(new ActionListener()
293       {
294         public void actionPerformed(ActionEvent e)
295         {
296           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
297           new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
298                   true, alignFrame.getViewport().getAlignment()
299                           .getDataset(), alignFrame);
300
301         }
302       });
303       msawsmenu.add(method);
304       if (hasparams)
305       {
306         // only add these menu options if the service has user-modifiable
307         // arguments
308         method = new JMenuItem("Edit settings and run ...");
309         method.setToolTipText("View and change the parameters before alignment.");
310
311         method.addActionListener(new ActionListener()
312         {
313           public void actionPerformed(ActionEvent e)
314           {
315             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
316             new MsaWSClient(service, null, null, true, alignFrame
317                     .getTitle(), msa, withGaps, true, alignFrame
318                     .getViewport().getAlignment().getDataset(), alignFrame);
319
320           }
321         });
322         msawsmenu.add(method);
323         List<WsParamSetI> presets = service.getParamStore().getPresets();
324         if (presets != null && presets.size() > 0)
325         {
326           JMenu presetlist = new JMenu("Run "+calcName + "with preset");
327
328           for (final WsParamSetI preset : presets)
329           {
330             final JMenuItem methodR = new JMenuItem(preset.getName());
331             methodR.setToolTipText("<html><p>"
332                     + JvSwingUtils.wrapTooltip("<strong>"
333                             + (preset.isModifiable() ? "User Preset"
334                                     : "Service Preset") + "</strong><br/>"
335                             + preset.getDescription() + "</p>") + "</html>");
336             methodR.addActionListener(new ActionListener()
337             {
338               public void actionPerformed(ActionEvent e)
339               {
340                 AlignmentView msa = alignFrame
341                         .gatherSequencesForAlignment();
342                 new MsaWSClient(service, preset, alignFrame.getTitle(),
343                         msa, false, true, alignFrame.getViewport()
344                                 .getAlignment().getDataset(), alignFrame);
345
346               }
347
348             });
349             presetlist.add(methodR);
350           }
351           msawsmenu.add(presetlist);
352         }
353       }
354       if (!submitGaps && canSubmitGaps())
355       {
356         submitGaps = true;
357         finished = false;
358       }
359       else
360       {
361         finished = true;
362       }
363     } while (!finished);
364   }
365 }