JAL-1583 MSA webservice client side validation
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.jws2;
22
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.AlignmentView;
26 import jalview.datamodel.SequenceI;
27 import jalview.gui.AlignFrame;
28 import jalview.gui.Desktop;
29 import jalview.gui.JvSwingUtils;
30 import jalview.util.MessageManager;
31 import jalview.ws.jws2.jabaws2.Jws2Instance;
32 import jalview.ws.params.WsParamSetI;
33
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.util.List;
37
38 import javax.swing.JMenu;
39 import javax.swing.JMenuItem;
40 import javax.swing.JOptionPane;
41
42 import compbio.data.msa.MsaWS;
43 import compbio.metadata.Argument;
44
45 /**
46  * DOCUMENT ME!
47  * 
48  * @author $author$
49  * @version $Revision$
50  */
51 public class MsaWSClient extends Jws2Client
52 {
53   /**
54    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
55    */
56   MsaWS server;
57
58   public MsaWSClient(Jws2Instance sh, String altitle,
59           jalview.datamodel.AlignmentView msa, boolean submitGaps,
60           boolean preserveOrder, Alignment seqdataset,
61           AlignFrame _alignFrame)
62   {
63     this(sh, null, null, false, altitle, msa, submitGaps, preserveOrder,
64             seqdataset, _alignFrame);
65     // TODO Auto-generated constructor stub
66   }
67
68   public MsaWSClient(Jws2Instance sh, WsParamSetI preset, String altitle,
69           jalview.datamodel.AlignmentView msa, boolean submitGaps,
70           boolean preserveOrder, Alignment seqdataset,
71           AlignFrame _alignFrame)
72   {
73     this(sh, preset, null, false, altitle, msa, submitGaps, preserveOrder,
74             seqdataset, _alignFrame);
75     // TODO Auto-generated constructor stub
76   }
77
78   /**
79    * Creates a new MsaWSClient object that uses a service given by an externally
80    * retrieved ServiceHandle
81    * 
82    * @param sh
83    *          service handle of type AbstractName(MsaWS)
84    * @param altitle
85    *          DOCUMENT ME!
86    * @param msa
87    *          DOCUMENT ME!
88    * @param submitGaps
89    *          DOCUMENT ME!
90    * @param preserveOrder
91    *          DOCUMENT ME!
92    */
93
94   public MsaWSClient(Jws2Instance sh, WsParamSetI preset,
95           List<Argument> arguments, boolean editParams, String altitle,
96           jalview.datamodel.AlignmentView msa, boolean submitGaps,
97           boolean preserveOrder, Alignment seqdataset,
98           AlignFrame _alignFrame)
99   {
100     super(_alignFrame, preset, arguments);
101     if (!processParams(sh, editParams))
102     {
103       return;
104     }
105
106     if (!(sh.service instanceof MsaWS))
107     {
108       // redundant at mo - but may change
109       JOptionPane
110               .showMessageDialog(
111                       Desktop.desktop,
112                       MessageManager.formatMessage("label.service_called_is_not_msa_service", new String[]{sh.serviceType}),
113                       MessageManager.getString("label.internal_jalview_error"), JOptionPane.WARNING_MESSAGE);
114
115       return;
116     }
117     server = (MsaWS) sh.service;
118     if ((wsInfo = setWebService(sh, false)) == null)
119     {
120       JOptionPane.showMessageDialog(Desktop.desktop,
121                   MessageManager.formatMessage("label.msa_service_is_unknown", new String[]{sh.serviceType}),
122                   MessageManager.getString("label.internal_jalview_error"), JOptionPane.WARNING_MESSAGE);
123
124       return;
125     }
126     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
127
128   }
129
130   public MsaWSClient()
131   {
132     super();
133     // add a class reference to the list
134   }
135
136   private void startMsaWSClient(String altitle, AlignmentView msa,
137           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
138   {
139     // if (!locateWebService())
140     // {
141     // return;
142     // }
143
144     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
145             + " of " + altitle + "\nJob details\n");
146     String jobtitle = WebServiceName.toLowerCase();
147     if (jobtitle.endsWith("alignment"))
148     {
149       if (submitGaps
150               && (!jobtitle.endsWith("realignment") || jobtitle
151                       .indexOf("profile") == -1))
152       {
153         int pos = jobtitle.indexOf("alignment");
154         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
155                 + altitle;
156       }
157       else
158       {
159         jobtitle = WebServiceName + " of " + altitle;
160       }
161     }
162     else
163     {
164       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
165               + "alignment of " + altitle;
166     }
167
168     MsaWSThread msathread = new MsaWSThread(server, preset, paramset,
169             WsURL, wsInfo, alignFrame, WebServiceName, jobtitle, msa,
170             submitGaps, preserveOrder, seqdataset);
171     wsInfo.setthisService(msathread);
172     if (isValidAlignment(alignFrame.getCurrentView().getAlignment()))
173     {
174
175     msathread.start();
176     }
177   }
178
179   private boolean isValidAlignment(AlignmentI seqdataset)
180   {
181     String header = wsInfo.getInfoText()
182             + "\nValidating submited Alignment...";
183     wsInfo.setInfoText(header);
184     int validSeqCount = 0;
185     List<SequenceI> seqs = seqdataset.getSequences();
186     if (seqs.size() < 2)
187     {
188       wsInfo.setInfoText(header
189               + "\nA minimum of two sequences is required to perform this operation");
190       return false;
191     }
192
193     for (SequenceI seq : seqs)
194     {
195
196       if (seq.getSequenceAsString().matches(
197               "([(a-zA-Z?)(-?)(.?)]+)?[a-zA-Z]([(a-zA-Z?)(-?)(.?)]+)?"))
198       {
199         ++validSeqCount;
200       }
201       if (validSeqCount > 1)
202       {
203         wsInfo.setInfoText(header
204                 + "\nAlignment sequences was successfully validated");
205         return true;
206       }
207     }
208
209     wsInfo.setInfoText(header
210             + "\nA minimum of two sequences with at least one non-gap character in each sequence is required to perform this operation");
211     return false;
212   }
213
214   public static void main(String[] args)
215   {
216     System.out
217             .println("A"
218                     .matches("([(a-zA-Z?)(-?)(.?)]+)?[a-zA-Z]([(a-zA-Z?)(-?)(.?)]+)?"));
219   }
220
221
222
223   protected String getServiceActionKey()
224   {
225     return "MsaWS";
226   }
227
228   protected String getServiceActionDescription()
229   {
230     return "Multiple Sequence Alignment";
231   }
232
233   /**
234    * look at ourselves and work out if we are a service that can take a profile
235    * and align to it
236    * 
237    * @return true if we can send gapped sequences to the alignment service
238    */
239   private boolean canSubmitGaps()
240   {
241     // TODO: query service or extract service handle props to check if we can
242     // realign
243     return (WebServiceName.indexOf("lustal") > -1); // cheat!
244   }
245
246   public void attachWSMenuEntry(JMenu rmsawsmenu,
247           final Jws2Instance service, final AlignFrame alignFrame)
248   {
249     if (registerAAConWSInstance(rmsawsmenu, service, alignFrame))
250     {
251       // Alignment dependent analysis calculation WS gui
252       return;
253     }
254     setWebService(service, true); // headless
255     boolean finished = true, submitGaps = false;
256     JMenu msawsmenu = rmsawsmenu;
257     String svcname = WebServiceName;
258     if (svcname.endsWith("WS"))
259     {
260       svcname = svcname.substring(0, svcname.length() - 2);
261     }
262     String calcName = svcname + " ";
263     if (canSubmitGaps())
264     {
265       msawsmenu = new JMenu(svcname);
266       rmsawsmenu.add(msawsmenu);
267       calcName = "";
268     }
269     boolean hasparams = service.hasParameters();
270     do
271     {
272       String action = "Align ";
273       if (submitGaps == true)
274       {
275         action = "Realign ";
276         msawsmenu = new JMenu(MessageManager.formatMessage(
277                 "label.realign_with_params", new String[]
278                 { svcname }));
279         msawsmenu.setToolTipText(MessageManager
280                 .getString("label.align_sequences_to_existing_alignment"));
281         rmsawsmenu.add(msawsmenu);
282       }
283       final boolean withGaps = submitGaps;
284
285       JMenuItem method = new JMenuItem(MessageManager.formatMessage(
286               "label.calcname_with_default_settings", new String[]
287               { calcName }));
288       method.setToolTipText(MessageManager.formatMessage(
289               "label.action_with_default_settings", new String[]
290               { action }));
291
292       method.addActionListener(new ActionListener()
293       {
294         public void actionPerformed(ActionEvent e)
295         {
296           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
297           new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
298                   true, alignFrame.getViewport().getAlignment()
299                           .getDataset(), alignFrame);
300
301         }
302       });
303       msawsmenu.add(method);
304       if (hasparams)
305       {
306         // only add these menu options if the service has user-modifiable
307         // arguments
308         method = new JMenuItem(
309                 MessageManager.getString("label.edit_settings_and_run"));
310         method.setToolTipText(MessageManager
311                 .getString("label.view_and_change_parameters_before_alignment"));
312
313         method.addActionListener(new ActionListener()
314         {
315           public void actionPerformed(ActionEvent e)
316           {
317             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
318             new MsaWSClient(service, null, null, true, alignFrame
319                     .getTitle(), msa, withGaps, true, alignFrame
320                     .getViewport().getAlignment().getDataset(), alignFrame);
321
322           }
323         });
324         msawsmenu.add(method);
325         List<WsParamSetI> presets = service.getParamStore().getPresets();
326         if (presets != null && presets.size() > 0)
327         {
328           JMenu presetlist = new JMenu(MessageManager.formatMessage(
329                   "label.run_with_preset_params", new String[]
330                   { calcName }));
331
332           for (final WsParamSetI preset : presets)
333           {
334             final JMenuItem methodR = new JMenuItem(preset.getName());
335             methodR.setToolTipText(JvSwingUtils.wrapTooltip(true, "<p><strong>"
336                             + (preset.isModifiable() ? MessageManager.getString("label.user_preset")
337                                     : MessageManager.getString("label.service_preset")) + "</strong><br/>"
338                             + preset.getDescription() + "</p>"));
339             methodR.addActionListener(new ActionListener()
340             {
341               public void actionPerformed(ActionEvent e)
342               {
343                 AlignmentView msa = alignFrame
344                         .gatherSequencesForAlignment();
345                 new MsaWSClient(service, preset, alignFrame.getTitle(),
346                         msa, false, true, alignFrame.getViewport()
347                                 .getAlignment().getDataset(), alignFrame);
348
349               }
350
351             });
352             presetlist.add(methodR);
353           }
354           msawsmenu.add(presetlist);
355         }
356       }
357       if (!submitGaps && canSubmitGaps())
358       {
359         submitGaps = true;
360         finished = false;
361       }
362       else
363       {
364         finished = true;
365       }
366     } while (!finished);
367   }
368 }