licencing and format applied (eclipse)
[jalview.git] / src / jalview / ws / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.ws;
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 WSClient
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(Desktop.desktop,
84               "The Multiple Sequence Alignment Service named "
85                       + sh.getName() + " is unknown",
86               "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
87
88       return;
89     }
90     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
91
92   }
93
94   public MsaWSClient()
95   {
96     super();
97     // add a class reference to the list
98   }
99
100   private void startMsaWSClient(String altitle, AlignmentView msa,
101           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
102   {
103     if (!locateWebService())
104     {
105       return;
106     }
107
108     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
109             + " of " + altitle + "\nJob details\n");
110     String jobtitle = WebServiceName.toLowerCase();
111     if (jobtitle.endsWith("alignment"))
112     {
113       if (submitGaps
114               && (!jobtitle.endsWith("realignment") || jobtitle
115                       .indexOf("profile") == -1))
116       {
117         int pos = jobtitle.indexOf("alignment");
118         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
119                 + altitle;
120       }
121       else
122       {
123         jobtitle = WebServiceName + " of " + altitle;
124       }
125     }
126     else
127     {
128       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
129               + "alignment of " + altitle;
130     }
131
132     MsaWSThread msathread = new MsaWSThread(server, WsURL, wsInfo,
133             alignFrame, WebServiceName, jobtitle, msa, submitGaps,
134             preserveOrder, seqdataset);
135     wsInfo.setthisService(msathread);
136     msathread.start();
137   }
138
139   /**
140    * Initializes the server field with a valid service implementation.
141    * 
142    * @return true if service was located.
143    */
144   private boolean locateWebService()
145   {
146     // TODO: MuscleWS transmuted to generic MsaWS client
147     MuscleWSServiceLocator loc = new MuscleWSServiceLocator(); // Default
148
149     try
150     {
151       this.server = (MuscleWS) loc.getMuscleWS(new java.net.URL(WsURL));
152       ((MuscleWSSoapBindingStub) this.server).setTimeout(60000); // One minute
153                                                                   // timeout
154     } catch (Exception ex)
155     {
156       wsInfo.setProgressText("Serious! " + WebServiceName
157               + " Service location failed\nfor URL :" + WsURL + "\n"
158               + ex.getMessage());
159       wsInfo.setStatus(WebserviceInfo.ERROR);
160       ex.printStackTrace();
161
162       return false;
163     }
164
165     loc.getEngine().setOption("axis", "1");
166
167     return true;
168   }
169
170   protected String getServiceActionKey()
171   {
172     return "MsaWS";
173   }
174
175   protected String getServiceActionDescription()
176   {
177     return "Multiple Sequence Alignment";
178   }
179
180   /**
181    * look at ourselves and work out if we are a service that can take a profile
182    * and align to it
183    * 
184    * @return true if we can send gapped sequences to the alignment service
185    */
186   private boolean canSubmitGaps()
187   {
188     // TODO: query service or extract service handle props to check if we can
189     // realign
190     return (WebServiceName.indexOf("lustal") > -1); // cheat!
191   }
192
193   public void attachWSMenuEntry(JMenu msawsmenu,
194           final ServiceHandle serviceHandle, final AlignFrame alignFrame)
195   {
196     setWebService(serviceHandle, true); // headless
197     JMenuItem method = new JMenuItem(WebServiceName);
198     method.setToolTipText(WsURL);
199     method.addActionListener(new ActionListener()
200     {
201       public void actionPerformed(ActionEvent e)
202       {
203         AlignmentView msa = alignFrame.gatherSequencesForAlignment();
204         new jalview.ws.MsaWSClient(serviceHandle, alignFrame.getTitle(),
205                 msa, false, true, alignFrame.getViewport().getAlignment()
206                         .getDataset(), alignFrame);
207
208       }
209
210     });
211     msawsmenu.add(method);
212     if (canSubmitGaps())
213     {
214       // We know that ClustalWS can accept partial alignments for refinement.
215       final JMenuItem methodR = new JMenuItem(serviceHandle.getName()
216               + " Realign");
217       methodR.setToolTipText(WsURL);
218       methodR.addActionListener(new ActionListener()
219       {
220         public void actionPerformed(ActionEvent e)
221         {
222           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
223           new jalview.ws.MsaWSClient(serviceHandle, alignFrame.getTitle(),
224                   msa, true, true, alignFrame.getViewport().getAlignment()
225                           .getDataset(), alignFrame);
226
227         }
228
229       });
230       msawsmenu.add(methodR);
231
232     }
233
234   }
235 }