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