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