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