Merge branch 'Jalview-JS/develop' into merge_js_develop
[jalview.git] / src / jalview / ws / jws1 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.jws1;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.AlignmentView;
25 import jalview.gui.AlignFrame;
26 import jalview.gui.Desktop;
27 import jalview.gui.JvOptionPane;
28 import jalview.gui.WebserviceInfo;
29 import jalview.util.MessageManager;
30
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33
34 import javax.swing.JMenu;
35 import javax.swing.JMenuItem;
36
37 import ext.vamsas.MuscleWSServiceLocator;
38 import ext.vamsas.MuscleWSSoapBindingStub;
39 import ext.vamsas.ServiceHandle;
40
41 /**
42  * DOCUMENT ME!
43  * 
44  * @author $author$
45  * @version $Revision$
46  */
47 public class MsaWSClient extends WS1Client
48 {
49   /**
50    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
51    */
52   ext.vamsas.MuscleWS server;
53
54   /**
55    * Creates a new MsaWSClient object that uses a service given by an externally
56    * retrieved ServiceHandle
57    * 
58    * @param sh
59    *          service handle of type AbstractName(MsaWS)
60    * @param altitle
61    *          DOCUMENT ME!
62    * @param msa
63    *          DOCUMENT ME!
64    * @param submitGaps
65    *          DOCUMENT ME!
66    * @param preserveOrder
67    *          DOCUMENT ME!
68    */
69
70   public MsaWSClient(ext.vamsas.ServiceHandle sh, String altitle,
71           jalview.datamodel.AlignmentView msa, boolean submitGaps,
72           boolean preserveOrder, AlignmentI seqdataset,
73           AlignFrame _alignFrame)
74   {
75     super();
76     alignFrame = _alignFrame;
77     if (!sh.getAbstractName().equals("MsaWS"))
78     {
79       JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
80               MessageManager.formatMessage(
81                       "label.service_called_is_not_msa_service",
82                       new String[]
83                       { sh.getName() }),
84               MessageManager.getString("label.internal_jalview_error"),
85               JvOptionPane.WARNING_MESSAGE);
86
87       return;
88     }
89
90     if ((wsInfo = setWebService(sh)) == null)
91     {
92       JvOptionPane.showMessageDialog(Desktop.getDesktopPane(), MessageManager
93               .formatMessage("label.msa_service_is_unknown", new String[]
94               { sh.getName() }),
95               MessageManager.getString("label.internal_jalview_error"),
96               JvOptionPane.WARNING_MESSAGE);
97
98       return;
99     }
100     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
101
102   }
103
104   public MsaWSClient()
105   {
106     super();
107     // add a class reference to the list
108   }
109
110   private void startMsaWSClient(String altitle, AlignmentView msa,
111           boolean submitGaps, boolean preserveOrder, AlignmentI seqdataset)
112   {
113     if (!locateWebService())
114     {
115       return;
116     }
117
118     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
119             + " of " + altitle + "\nJob details\n");
120     String jobtitle = WebServiceName.toLowerCase();
121     if (jobtitle.endsWith("alignment"))
122     {
123       if (submitGaps && (!jobtitle.endsWith("realignment")
124               || jobtitle.indexOf("profile") == -1))
125       {
126         int pos = jobtitle.indexOf("alignment");
127         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
128                 + altitle;
129       }
130       else
131       {
132         jobtitle = WebServiceName + " of " + altitle;
133       }
134     }
135     else
136     {
137       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
138               + "alignment of " + altitle;
139     }
140
141     MsaWSThread msathread = new MsaWSThread(server, WsURL, wsInfo,
142             alignFrame, WebServiceName, jobtitle, msa, submitGaps,
143             preserveOrder, seqdataset);
144     wsInfo.setthisService(msathread);
145     msathread.start();
146   }
147
148   /**
149    * Initializes the server field with a valid service implementation.
150    * 
151    * @return true if service was located.
152    */
153   private boolean locateWebService()
154   {
155     // TODO: MuscleWS transmuted to generic MsaWS client
156     MuscleWSServiceLocator loc = new MuscleWSServiceLocator(); // Default
157
158     try
159     {
160       this.server = loc.getMuscleWS(new java.net.URL(WsURL));
161       ((MuscleWSSoapBindingStub) this.server).setTimeout(60000); // One minute
162       // timeout
163     } catch (Exception ex)
164     {
165       wsInfo.setProgressText("Serious! " + WebServiceName
166               + " Service location failed\nfor URL :" + WsURL + "\n"
167               + ex.getMessage());
168       wsInfo.setStatus(WebserviceInfo.ERROR);
169       ex.printStackTrace();
170
171       return false;
172     }
173
174     loc.getEngine().setOption("axis", "1");
175
176     return true;
177   }
178
179   protected String getServiceActionKey()
180   {
181     return "MsaWS";
182   }
183
184   protected String getServiceActionDescription()
185   {
186     return "Multiple Sequence Alignment";
187   }
188
189   /**
190    * look at ourselves and work out if we are a service that can take a profile
191    * and align to it
192    * 
193    * @return true if we can send gapped sequences to the alignment service
194    */
195   private boolean canSubmitGaps()
196   {
197     // TODO: query service or extract service handle props to check if we can
198     // realign
199     return (WebServiceName.indexOf("lustal") > -1); // cheat!
200   }
201
202   @Override
203   public void attachWSMenuEntry(JMenu msawsmenu,
204           final ServiceHandle serviceHandle, final AlignFrame alignFrame)
205   {
206     setWebService(serviceHandle, true); // headless
207     JMenuItem method = new JMenuItem(WebServiceName);
208     method.setToolTipText(WsURL);
209     method.addActionListener(new ActionListener()
210     {
211       @Override
212       public void actionPerformed(ActionEvent e)
213       {
214         AlignmentView msa = alignFrame.gatherSequencesForAlignment();
215         new jalview.ws.jws1.MsaWSClient(serviceHandle,
216                 alignFrame.getTitle(), msa, false, true,
217                 alignFrame.getViewport().getAlignment().getDataset(),
218                 alignFrame);
219
220       }
221
222     });
223     msawsmenu.add(method);
224     if (canSubmitGaps())
225     {
226       // We know that ClustalWS can accept partial alignments for refinement.
227       final JMenuItem methodR = new JMenuItem(
228               serviceHandle.getName() + " Realign");
229       methodR.setToolTipText(WsURL);
230       methodR.addActionListener(new ActionListener()
231       {
232         @Override
233         public void actionPerformed(ActionEvent e)
234         {
235           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
236           new jalview.ws.jws1.MsaWSClient(serviceHandle,
237                   alignFrame.getTitle(), msa, true, true,
238                   alignFrame.getViewport().getAlignment().getDataset(),
239                   alignFrame);
240
241         }
242
243       });
244       msawsmenu.add(methodR);
245
246     }
247
248   }
249 }