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