JAL-1503 update version in GPL header
[jalview.git] / src / jalview / ws / jws2 / SequenceAnnotationWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws.jws2;
20
21 import jalview.api.AlignCalcWorkerI;
22 import jalview.bin.Cache;
23 import jalview.gui.AlignFrame;
24 import jalview.gui.Desktop;
25 import jalview.gui.JvSwingUtils;
26 import jalview.util.MessageManager;
27 import jalview.ws.jws2.dm.AAConSettings;
28 import jalview.ws.jws2.jabaws2.Jws2Instance;
29 import jalview.ws.params.WsParamSetI;
30 import jalview.ws.uimodel.AlignAnalysisUIText;
31
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.util.List;
35
36 import javax.swing.JCheckBoxMenuItem;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuItem;
39 import javax.swing.JOptionPane;
40 import javax.swing.event.MenuEvent;
41 import javax.swing.event.MenuListener;
42
43 /**
44  * @author jprocter
45  * 
46  */
47 public class SequenceAnnotationWSClient extends Jws2Client
48 {
49   /**
50    * initialise a client so its attachWSMenuEntry method can be called.
51    */
52   public SequenceAnnotationWSClient()
53   {
54     // TODO Auto-generated constructor stub
55   }
56
57   public SequenceAnnotationWSClient(final Jws2Instance sh,
58           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
59   {
60     super(alignFrame, preset, null);
61     initSequenceAnnotationWSClient(sh, alignFrame, preset, editParams);
62   }
63
64   // dan think. Do I need to change this method to run RNAalifold through the GUI
65   
66   public void initSequenceAnnotationWSClient(final Jws2Instance sh,
67           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
68   {
69         // dan changed! dan test. comment out if conditional
70 //    if (alignFrame.getViewport().getAlignment().isNucleotide())
71 //    {
72 //      JOptionPane.showMessageDialog(Desktop.desktop, sh.serviceType
73 //              + " can only be used\nfor amino acid alignments.",
74 //              "Wrong type of sequences!", JOptionPane.WARNING_MESSAGE);
75 //      return;
76 //
77 //    }
78     AlignAnalysisUIText aaui = sh.getAlignAnalysisUI();
79     if (aaui!=null)
80     {
81       Class clientClass = aaui.getClient();
82       
83       // Build an AACon style client - take alignment, return annotation for
84       // columns
85
86       List<AlignCalcWorkerI> clnts = alignFrame.getViewport()
87               .getCalcManager()
88               .getRegisteredWorkersOfClass(clientClass);
89       JabawsAlignCalcWorker worker;
90       if (clnts == null || clnts.size() == 0)
91       {
92         if (!processParams(sh, editParams))
93         {
94           return;
95         }
96         try {
97           worker = (JabawsAlignCalcWorker) (clientClass.getConstructor(
98                   new Class[] { Jws2Instance.class, 
99                           AlignFrame.class, WsParamSetI.class, 
100                           List.class }).newInstance(new Object[] { sh, alignFrame, this.preset, paramset}));
101         } catch (Exception x)
102         {
103           x.printStackTrace();
104           throw new Error("Implementation error",x);
105         }
106         alignFrame
107                 .getViewport()
108                 .getCalcManager()
109                 .registerWorker(
110                         worker);
111         alignFrame.getViewport().getCalcManager().startWorker(worker);
112
113       }
114       else
115       {
116         worker = (JabawsAlignCalcWorker) clnts.get(0);
117         if (editParams)
118         {
119           paramset = worker.getArguments();
120           preset = worker.getPreset();
121         }
122
123         if (!processParams(sh, editParams, true))
124         {
125           return;
126         }
127         // reinstate worker if it was blacklisted (might have happened due to
128         // invalid parameters)
129         alignFrame.getViewport().getCalcManager().workerMayRun(worker);
130         worker.updateParameters(this.preset, paramset);
131       }
132     }
133     if (sh.action.toLowerCase().contains("disorder"))
134     {
135       // build IUPred style client. take sequences, returns annotation per
136       // sequence.
137       if (!processParams(sh, editParams))
138       {
139         return;
140       }
141
142       alignFrame
143               .getViewport()
144               .getCalcManager()
145               .startWorker(
146                       new AADisorderClient(sh, alignFrame, preset, paramset));
147     }
148   }
149
150   public SequenceAnnotationWSClient(AAConSettings fave,
151           AlignFrame alignFrame, boolean b)
152   {
153     super(alignFrame, fave.getPreset(), fave.getJobArgset());
154     initSequenceAnnotationWSClient(fave.getService(), alignFrame,
155             fave.getPreset(), b);
156   }
157
158   /*
159    * (non-Javadoc)
160    * 
161    * @see jalview.ws.jws2.Jws2Client#attachWSMenuEntry(javax.swing.JMenu,
162    * jalview.ws.jws2.jabaws2.Jws2Instance, jalview.gui.AlignFrame)
163    */
164   public void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
165           final AlignFrame alignFrame)
166   {
167     if (registerAAConWSInstance(wsmenu, service, alignFrame)) {
168       // Alignment dependent analysis calculation WS gui
169       return;
170     }
171     boolean hasparams = service.hasParameters();
172     // Assume name ends in WS
173     String calcName = service.serviceType.substring(0,
174             service.serviceType.length() - 2);
175
176     JMenuItem annotservice = new JMenuItem(MessageManager.formatMessage("label.calcname_with_default_settings", new String[]{calcName}));
177     annotservice.addActionListener(new ActionListener()
178     {
179
180       @Override
181       public void actionPerformed(ActionEvent e)
182       {
183         new SequenceAnnotationWSClient(service, alignFrame, null, false);
184       }
185     });
186     wsmenu.add(annotservice);
187     if (hasparams)
188     {
189       // only add these menu options if the service has user-modifiable
190       // arguments
191       annotservice = new JMenuItem(MessageManager.getString("label.edit_settings_and_run"));
192       annotservice
193               .setToolTipText(MessageManager.getString("label.view_and_change_parameters_before_running_calculation"));
194
195       annotservice.addActionListener(new ActionListener()
196       {
197         public void actionPerformed(ActionEvent e)
198         {
199           new SequenceAnnotationWSClient(service, alignFrame, null, true);
200         }
201       });
202       wsmenu.add(annotservice);
203       List<WsParamSetI> presets = service.getParamStore().getPresets();
204       if (presets != null && presets.size() > 0)
205       {
206         JMenu presetlist = new JMenu("Run " + calcName + "with preset");
207
208         for (final WsParamSetI preset : presets)
209         {
210           final JMenuItem methodR = new JMenuItem(preset.getName());
211           methodR.setToolTipText("<html><p>"
212                   + JvSwingUtils.wrapTooltip("<strong>"
213                           + (preset.isModifiable() ? "User Preset"
214                                   : "Service Preset") + "</strong><br/>"
215                           + preset.getDescription() + "</p>") + "</html>");
216           methodR.addActionListener(new ActionListener()
217           {
218             public void actionPerformed(ActionEvent e)
219             {
220               new SequenceAnnotationWSClient(service, alignFrame, preset,
221                       false);
222             }
223
224           });
225           presetlist.add(methodR);
226         }
227         wsmenu.add(presetlist);
228       }
229
230     }
231     else
232     {
233       annotservice = new JMenuItem(MessageManager.getString("label.view_documentation"));
234       if (service.docUrl != null)
235       {
236         annotservice.addActionListener(new ActionListener()
237         {
238
239           @Override
240           public void actionPerformed(ActionEvent arg0)
241           {
242             Desktop.instance.showUrl(service.docUrl);
243           }
244         });
245         annotservice.setToolTipText("<html>"
246                 + JvSwingUtils.wrapTooltip("View <a href=\""
247                         + service.docUrl + "\">" + service.docUrl + "</a>")
248                 + "</html>");
249         wsmenu.add(annotservice);
250       }
251     }
252   }
253
254   
255   private boolean registerAAConWSInstance(final JMenu wsmenu,
256           final Jws2Instance service, final AlignFrame alignFrame)
257   {
258     final AlignAnalysisUIText aaui = service.getAlignAnalysisUI(); // null ; // AlignAnalysisUIText.aaConGUI.get(service.serviceType.toString());
259     if (aaui==null)
260     {
261       // not an instantaneous calculation GUI type service
262       return false;
263     }
264     // create the instaneous calculation GUI bits and update state if existing GUI elements already present
265     
266     JCheckBoxMenuItem _aaConEnabled = null;
267     for (int i = 0; i < wsmenu.getItemCount(); i++)
268     {
269       JMenuItem item = wsmenu.getItem(i);
270       if (item instanceof JCheckBoxMenuItem
271               && item.getText().equals(aaui.getAAconToggle()))
272       {
273         _aaConEnabled = (JCheckBoxMenuItem) item;
274       }
275     }
276     // is there an aaCon worker already present - if so, set it to use the
277     // given service handle
278     {
279       List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
280               .getCalcManager()
281               .getRegisteredWorkersOfClass(aaui.getClient());
282       if (aaconClient != null && aaconClient.size() > 0)
283       {
284         JabawsAlignCalcWorker worker = (JabawsAlignCalcWorker) aaconClient.get(0);
285         if (!worker.service.hosturl.equals(service.hosturl))
286         {
287           // javax.swing.SwingUtilities.invokeLater(new Runnable()
288           {
289             // @Override
290             // public void run()
291             {
292               removeCurrentAAConWorkerFor(aaui, alignFrame);
293               buildCurrentAAConWorkerFor(aaui, alignFrame, service);
294             }
295           }// );
296         }
297       }
298     }
299
300     // is there a service already registered ? there shouldn't be if we are
301     // being called correctly
302     if (_aaConEnabled == null)
303     {
304       final JCheckBoxMenuItem aaConEnabled = new JCheckBoxMenuItem(
305               aaui.getAAconToggle());
306
307       aaConEnabled.setToolTipText("<html><p>"
308               + JvSwingUtils.wrapTooltip(aaui.getAAconToggleTooltip() + "</p>")
309               + "</html>");
310       aaConEnabled.addActionListener(new ActionListener()
311       {
312         @Override
313         public void actionPerformed(ActionEvent arg0)
314         {
315           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
316                   .getCalcManager()
317                   .getRegisteredWorkersOfClass(aaui.getClient());
318           if (aaconClient != null && aaconClient.size() > 0)
319           {
320             removeCurrentAAConWorkerFor(aaui, alignFrame);
321           }
322           else
323           {
324             buildCurrentAAConWorkerFor(aaui, alignFrame);
325
326           }
327         }
328
329       });
330       wsmenu.add(aaConEnabled);
331       final JMenuItem modifyParams = new JMenuItem(aaui.getAAeditSettings());
332       modifyParams.setToolTipText("<html><p>"
333               + JvSwingUtils.wrapTooltip(aaui.getAAeditSettingsTooltip() + "</p>")
334               + "</html>");
335       modifyParams.addActionListener(new ActionListener()
336       {
337
338         @Override
339         public void actionPerformed(ActionEvent arg0)
340         {
341           showAAConAnnotationSettingsFor(aaui, alignFrame);
342         }
343       });
344       wsmenu.add(modifyParams);
345       wsmenu.addMenuListener(new MenuListener()
346       {
347
348         @Override
349         public void menuSelected(MenuEvent arg0)
350         {
351           // TODO: refactor to the implementing class.
352           if (alignFrame.getViewport().getAlignment()
353                   .isNucleotide() ? aaui.isNa() : aaui.isPr()) {
354             aaConEnabled.setEnabled(true);
355             modifyParams.setEnabled(true);
356           }
357           else {
358             aaConEnabled.setEnabled(false);
359             modifyParams.setEnabled(false);
360           }
361           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
362                   .getCalcManager()
363                   .getRegisteredWorkersOfClass(aaui.getClient());
364           if (aaconClient != null && aaconClient.size() > 0)
365           {
366             aaConEnabled.setSelected(true);
367           }
368           else
369           {
370             aaConEnabled.setSelected(false);
371           }
372         }
373
374         @Override
375         public void menuDeselected(MenuEvent arg0)
376         {
377           // TODO Auto-generated method stub
378
379         }
380
381         @Override
382         public void menuCanceled(MenuEvent arg0)
383         {
384           // TODO Auto-generated method stub
385
386         }
387       });
388
389     }
390     return true;
391   }
392
393   private static void showAAConAnnotationSettingsFor(final AlignAnalysisUIText aaui, AlignFrame alignFrame)
394   {
395     /*
396      * preferred settings Whether AACon is automatically recalculated Which
397      * AACon server to use What parameters to use
398      */
399     // could actually do a class search for this too
400     AAConSettings fave = (AAConSettings) alignFrame.getViewport()
401             .getCalcIdSettingsFor(aaui.getCalcId());
402     if (fave == null)
403     {
404       fave = createDefaultAAConSettings(aaui);
405     }
406     new SequenceAnnotationWSClient(fave, alignFrame, true);
407
408   }
409
410   private static void buildCurrentAAConWorkerFor(final AlignAnalysisUIText aaui, AlignFrame alignFrame)
411   {
412     buildCurrentAAConWorkerFor(aaui, alignFrame, null);
413   }
414
415   private static void buildCurrentAAConWorkerFor(final AlignAnalysisUIText aaui, AlignFrame alignFrame,
416           Jws2Instance service)
417   {
418     /*
419      * preferred settings Whether AACon is automatically recalculated Which
420      * AACon server to use What parameters to use
421      */
422     AAConSettings fave = (AAConSettings) alignFrame.getViewport()
423             .getCalcIdSettingsFor(aaui.getCalcId());
424     if (fave == null)
425     {
426       fave = createDefaultAAConSettings(aaui, service);
427     }
428     else
429     {
430       if (service != null
431               && !fave.getService().hosturl.equals(service.hosturl))
432       {
433         Cache.log.debug("Changing AACon service to " + service.hosturl
434                 + " from " + fave.getService().hosturl);
435         fave.setService(service);
436       }
437     }
438     new SequenceAnnotationWSClient(fave, alignFrame, false);
439   }
440
441   private static AAConSettings createDefaultAAConSettings(AlignAnalysisUIText aaui)
442   {
443     return createDefaultAAConSettings(aaui, null);
444   }
445
446   private static AAConSettings createDefaultAAConSettings(AlignAnalysisUIText aaui,
447           Jws2Instance service)
448   {
449     if (service != null)
450     {
451       if (!service.serviceType.toString().equals(
452               compbio.ws.client.Services.AAConWS.toString()))
453       {
454         Cache.log
455                 .warn("Ignoring invalid preferred service for AACon calculations (service type was "
456                         + service.serviceType + ")");
457         service = null;
458       }
459       else
460       {
461         // check service is actually in the list of currently avaialable
462         // services
463         if (!Jws2Discoverer.getDiscoverer().getServices().contains(service))
464         {
465           // it isn't ..
466           service = null;
467         }
468       }
469     }
470     if (service == null)
471     {
472       // get the default service for AACon
473       service = Jws2Discoverer.getDiscoverer().getPreferredServiceFor(null,
474               aaui.getServiceType());
475     }
476     if (service == null)
477     {
478       // TODO raise dialog box explaining error, and/or open the JABA
479       // preferences menu.
480       throw new Error("No AACon service found.");
481     }
482     return new AAConSettings(true, service, null, null);
483   }
484
485   private static void removeCurrentAAConWorkerFor(AlignAnalysisUIText aaui, AlignFrame alignFrame)
486   {
487     alignFrame.getViewport().getCalcManager()
488             .removeRegisteredWorkersOfClass(aaui.getClient());
489   }
490 }