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