JAL-1432 updated copyright notices
[jalview.git] / src / jalview / ws / jws1 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws.jws1;
20
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23
24 import javax.swing.*;
25
26 import ext.vamsas.*;
27 import jalview.datamodel.*;
28 import jalview.gui.*;
29
30 /**
31  * DOCUMENT ME!
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36 public class MsaWSClient extends WS1Client
37 {
38   /**
39    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
40    */
41   ext.vamsas.MuscleWS server;
42
43   AlignFrame alignFrame;
44
45   /**
46    * Creates a new MsaWSClient object that uses a service given by an externally
47    * retrieved ServiceHandle
48    * 
49    * @param sh
50    *          service handle of type AbstractName(MsaWS)
51    * @param altitle
52    *          DOCUMENT ME!
53    * @param msa
54    *          DOCUMENT ME!
55    * @param submitGaps
56    *          DOCUMENT ME!
57    * @param preserveOrder
58    *          DOCUMENT ME!
59    */
60
61   public MsaWSClient(ext.vamsas.ServiceHandle sh, String altitle,
62           jalview.datamodel.AlignmentView msa, boolean submitGaps,
63           boolean preserveOrder, Alignment seqdataset,
64           AlignFrame _alignFrame)
65   {
66     super();
67     alignFrame = _alignFrame;
68     if (!sh.getAbstractName().equals("MsaWS"))
69     {
70       JOptionPane
71               .showMessageDialog(
72                       Desktop.desktop,
73                       "The Service called \n"
74                               + sh.getName()
75                               + "\nis not a \nMultiple Sequence Alignment Service !",
76                       "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
77
78       return;
79     }
80
81     if ((wsInfo = setWebService(sh)) == null)
82     {
83       JOptionPane.showMessageDialog(
84               Desktop.desktop,
85               "The Multiple Sequence Alignment Service named "
86                       + sh.getName() + " is unknown",
87               "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 }