JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / jws1 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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
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 java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25
26 import javax.swing.*;
27
28 import ext.vamsas.*;
29 import jalview.datamodel.*;
30 import jalview.gui.*;
31 import jalview.util.MessageManager;
32
33 /**
34  * DOCUMENT ME!
35  * 
36  * @author $author$
37  * @version $Revision$
38  */
39 public class MsaWSClient extends WS1Client
40 {
41   /**
42    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
43    */
44   ext.vamsas.MuscleWS server;
45
46   AlignFrame alignFrame;
47
48   /**
49    * Creates a new MsaWSClient object that uses a service given by an externally
50    * retrieved ServiceHandle
51    * 
52    * @param sh
53    *          service handle of type AbstractName(MsaWS)
54    * @param altitle
55    *          DOCUMENT ME!
56    * @param msa
57    *          DOCUMENT ME!
58    * @param submitGaps
59    *          DOCUMENT ME!
60    * @param preserveOrder
61    *          DOCUMENT ME!
62    */
63
64   public MsaWSClient(ext.vamsas.ServiceHandle sh, String altitle,
65           jalview.datamodel.AlignmentView msa, boolean submitGaps,
66           boolean preserveOrder, Alignment seqdataset,
67           AlignFrame _alignFrame)
68   {
69     super();
70     alignFrame = _alignFrame;
71     if (!sh.getAbstractName().equals("MsaWS"))
72     {
73       JOptionPane
74               .showMessageDialog(
75                       Desktop.desktop,
76                       MessageManager.formatMessage("label.service_called_is_not_msa_service", new String[]{sh.getName()}),
77                       MessageManager.getString("label.internal_jalview_error"), JOptionPane.WARNING_MESSAGE);
78
79       return;
80     }
81
82     if ((wsInfo = setWebService(sh)) == null)
83     {
84       JOptionPane.showMessageDialog(
85               Desktop.desktop,
86               MessageManager.formatMessage("label.msa_service_is_unknown", new String[]{sh.getName()}),
87               MessageManager.getString("label.internal_jalview_error"), JOptionPane.WARNING_MESSAGE);
88
89       return;
90     }
91     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
92
93   }
94
95   public MsaWSClient()
96   {
97     super();
98     // add a class reference to the list
99   }
100
101   private void startMsaWSClient(String altitle, AlignmentView msa,
102           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
103   {
104     if (!locateWebService())
105     {
106       return;
107     }
108
109     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
110             + " of " + altitle + "\nJob details\n");
111     String jobtitle = WebServiceName.toLowerCase();
112     if (jobtitle.endsWith("alignment"))
113     {
114       if (submitGaps
115               && (!jobtitle.endsWith("realignment") || jobtitle
116                       .indexOf("profile") == -1))
117       {
118         int pos = jobtitle.indexOf("alignment");
119         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
120                 + altitle;
121       }
122       else
123       {
124         jobtitle = WebServiceName + " of " + altitle;
125       }
126     }
127     else
128     {
129       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
130               + "alignment of " + altitle;
131     }
132
133     MsaWSThread msathread = new MsaWSThread(server, WsURL, wsInfo,
134             alignFrame, WebServiceName, jobtitle, msa, submitGaps,
135             preserveOrder, seqdataset);
136     wsInfo.setthisService(msathread);
137     msathread.start();
138   }
139
140   /**
141    * Initializes the server field with a valid service implementation.
142    * 
143    * @return true if service was located.
144    */
145   private boolean locateWebService()
146   {
147     // TODO: MuscleWS transmuted to generic MsaWS client
148     MuscleWSServiceLocator loc = new MuscleWSServiceLocator(); // Default
149
150     try
151     {
152       this.server = (MuscleWS) loc.getMuscleWS(new java.net.URL(WsURL));
153       ((MuscleWSSoapBindingStub) this.server).setTimeout(60000); // One minute
154       // timeout
155     } catch (Exception ex)
156     {
157       wsInfo.setProgressText("Serious! " + WebServiceName
158               + " Service location failed\nfor URL :" + WsURL + "\n"
159               + ex.getMessage());
160       wsInfo.setStatus(WebserviceInfo.ERROR);
161       ex.printStackTrace();
162
163       return false;
164     }
165
166     loc.getEngine().setOption("axis", "1");
167
168     return true;
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 msawsmenu,
195           final ServiceHandle serviceHandle, final AlignFrame alignFrame)
196   {
197     setWebService(serviceHandle, true); // headless
198     JMenuItem method = new JMenuItem(WebServiceName);
199     method.setToolTipText(WsURL);
200     method.addActionListener(new ActionListener()
201     {
202       public void actionPerformed(ActionEvent e)
203       {
204         AlignmentView msa = alignFrame.gatherSequencesForAlignment();
205         new jalview.ws.jws1.MsaWSClient(serviceHandle, alignFrame
206                 .getTitle(), msa, false, true, alignFrame.getViewport()
207                 .getAlignment().getDataset(), alignFrame);
208
209       }
210
211     });
212     msawsmenu.add(method);
213     if (canSubmitGaps())
214     {
215       // We know that ClustalWS can accept partial alignments for refinement.
216       final JMenuItem methodR = new JMenuItem(serviceHandle.getName()
217               + " Realign");
218       methodR.setToolTipText(WsURL);
219       methodR.addActionListener(new ActionListener()
220       {
221         public void actionPerformed(ActionEvent e)
222         {
223           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
224           new jalview.ws.jws1.MsaWSClient(serviceHandle, alignFrame
225                   .getTitle(), msa, true, true, alignFrame.getViewport()
226                   .getAlignment().getDataset(), alignFrame);
227
228         }
229
230       });
231       msawsmenu.add(methodR);
232
233     }
234
235   }
236 }