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