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