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