63e60d7c2bcf997cec1729cb93caad2520ea3ee4
[jalview.git] / src / jalview / ws / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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
37     extends WSClient
38 {
39   /**
40    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
41    */
42   ext.vamsas.MuscleWS server;
43   AlignFrame alignFrame;
44
45   /**
46    * Creates a new MsaWSClient object that uses a service
47    * given by an externally retrieved ServiceHandle
48    *
49    * @param sh service handle of type AbstractName(MsaWS)
50    * @param altitle DOCUMENT ME!
51    * @param msa DOCUMENT ME!
52    * @param submitGaps DOCUMENT ME!
53    * @param preserveOrder DOCUMENT ME!
54    */
55
56   public MsaWSClient(ext.vamsas.ServiceHandle sh, String altitle,
57                      jalview.datamodel.AlignmentView msa,
58                      boolean submitGaps, boolean preserveOrder,
59                      Alignment seqdataset,
60                      AlignFrame _alignFrame)
61   {
62     super();
63     alignFrame = _alignFrame;
64     if (!sh.getAbstractName().equals("MsaWS"))
65     {
66       JOptionPane.showMessageDialog(Desktop.desktop,
67                                     "The Service called \n" + sh.getName() +
68                                     "\nis not a \nMultiple Sequence Alignment Service !",
69                                     "Internal Jalview Error",
70                                     JOptionPane.WARNING_MESSAGE);
71
72       return;
73     }
74
75     if ( (wsInfo = setWebService(sh)) == null)
76     {
77       JOptionPane.showMessageDialog(Desktop.desktop,
78                                     "The Multiple Sequence Alignment Service named " +
79                                     sh.getName() +
80                                     " is unknown", "Internal Jalview Error",
81                                     JOptionPane.WARNING_MESSAGE);
82
83       return;
84     }
85     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
86
87   }
88
89   public MsaWSClient()
90   {
91     super();
92     // add a class reference to the list
93   }
94
95   private void startMsaWSClient(String altitle, AlignmentView msa,
96                                 boolean submitGaps, boolean preserveOrder,
97                                 Alignment seqdataset)
98   {
99     if (!locateWebService())
100     {
101       return;
102     }
103
104     wsInfo.setProgressText( ( (submitGaps) ? "Re-alignment" : "Alignment") +
105                            " of " + altitle + "\nJob details\n");
106     String jobtitle = WebServiceName.toLowerCase();
107     if (jobtitle.endsWith("alignment"))
108     {
109       if (submitGaps
110           && (!jobtitle.endsWith("realignment")
111               || jobtitle.indexOf("profile") == -1))
112       {
113         int pos = jobtitle.indexOf("alignment");
114         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of " +
115             altitle;
116       }
117       else
118       {
119         jobtitle = WebServiceName + " of " + altitle;
120       }
121     }
122     else
123     {
124       jobtitle = WebServiceName + (submitGaps ? " re" : " ") + "alignment of " +
125           altitle;
126     }
127
128     MsaWSThread msathread = new MsaWSThread(server, WsURL, wsInfo, alignFrame,
129                                             WebServiceName,
130                                             jobtitle,
131                                             msa,
132                                             submitGaps, preserveOrder,
133                                             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 timeout
152     }
153     catch (Exception ex)
154     {
155       wsInfo.setProgressText("Serious! " + WebServiceName +
156                              " Service location failed\nfor URL :" + WsURL +
157                              "\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    * look at ourselves and work out if we are a service that can take a profile and align to it 
181    * @return true if we can send gapped sequences to the alignment service
182    */
183   private boolean canSubmitGaps()
184   {
185     // TODO: query service or extract service handle props to check if we can realign
186     return (WebServiceName.indexOf("lustal") > -1); // cheat!
187   }
188   public void attachWSMenuEntry(JMenu msawsmenu, final ServiceHandle serviceHandle, final AlignFrame alignFrame)
189   {
190     setWebService(serviceHandle, true); // headless
191     JMenuItem method = new JMenuItem(WebServiceName);
192     method.setToolTipText(WsURL);
193     method.addActionListener(new ActionListener()
194     {
195       public void actionPerformed(ActionEvent e)
196       {
197         AlignmentView msa = alignFrame.gatherSequencesForAlignment();
198         new jalview.ws.MsaWSClient(serviceHandle, alignFrame.getTitle(), msa,
199                                    false, true,
200                                    alignFrame.getViewport().getAlignment().getDataset(),
201                                    alignFrame);
202
203       }
204
205     });
206     msawsmenu.add(method);    
207     if (canSubmitGaps())
208     {
209       // We know that ClustalWS can accept partial alignments for refinement.
210       final JMenuItem methodR = new JMenuItem(serviceHandle.getName()+" Realign");
211       methodR.setToolTipText(WsURL);
212         methodR.addActionListener(new ActionListener()
213       {
214         public void actionPerformed(ActionEvent e)
215         {
216           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
217           new jalview.ws.MsaWSClient(serviceHandle, alignFrame.getTitle(), msa,
218                                      true, true,
219                                      alignFrame.getViewport().getAlignment().getDataset(),
220                                      alignFrame);
221
222         }
223
224       });
225       msawsmenu.add(methodR);
226
227     }
228
229   }
230 }