JAL-3066 Sequence annotation services.
[jalview.git] / src / jalview / ws / jws2 / SequenceAnnotationWSClient.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.jws2;
22
23 import jalview.api.AlignCalcWorkerI;
24 import jalview.gui.AlignFrame;
25 import jalview.gui.Desktop;
26 import jalview.gui.JvSwingUtils;
27 import jalview.util.MessageManager;
28 import jalview.ws.api.ServiceWithParameters;
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.JMenu;
39 import javax.swing.JMenuItem;
40
41 /**
42  * @author jprocter
43  * 
44  */
45 public class SequenceAnnotationWSClient extends Jws2Client
46 {
47   /**
48    * initialise a client so its attachWSMenuEntry method can be called.
49    */
50   public SequenceAnnotationWSClient()
51   {
52     // TODO Auto-generated constructor stub
53   }
54
55   public SequenceAnnotationWSClient(final ServiceWithParameters sh,
56           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
57   {
58     super(alignFrame, preset, null);
59     initSequenceAnnotationWSClient(sh, alignFrame, preset, editParams);
60   }
61
62   // dan think. Do I need to change this method to run RNAalifold through the
63   // GUI
64
65   public void initSequenceAnnotationWSClient(final ServiceWithParameters sh,
66           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
67   {
68     // dan changed! dan test. comment out if conditional
69     // if (alignFrame.getViewport().getAlignment().isNucleotide())
70     // {
71     // JvOptionPane.showMessageDialog(Desktop.desktop, sh.serviceType
72     // + " can only be used\nfor amino acid alignments.",
73     // "Wrong type of sequences!", JvOptionPane.WARNING_MESSAGE);
74     // return;
75     //
76     // }
77     AlignAnalysisUIText aaui = sh.getAlignAnalysisUI();
78     if (aaui != null)
79     {
80       Class clientClass = aaui.getClient();
81
82       // Build an AACon style client - take alignment, return annotation for
83       // columns
84
85       List<AlignCalcWorkerI> clnts = alignFrame.getViewport()
86               .getCalcManager()
87               .getRegisteredWorkersOfClass(SeqAnnotationServiceCalcWorker.class);
88
89       SeqAnnotationServiceCalcWorker worker = null;
90       if (clnts != null)
91       {
92         for (AlignCalcWorkerI _worker : clnts)
93         {
94           worker = (SeqAnnotationServiceCalcWorker) _worker;
95           if (worker.hasService()
96                   && worker.getService().getClass().equals(clientClass))
97           {
98             break;
99           }
100           worker = null;
101         }
102       }
103       if (worker == null)
104       {
105         if (!processParams(sh, editParams))
106         {
107           return;
108         }
109         try
110         {
111           worker = new SeqAnnotationServiceCalcWorker(sh, alignFrame, this.preset,
112                   paramset);
113         } catch (Exception x)
114         {
115           x.printStackTrace();
116           throw new Error(
117                   MessageManager.getString("error.implementation_error"),
118                   x);
119         }
120         alignFrame.getViewport().getCalcManager().registerWorker(worker);
121         alignFrame.getViewport().getCalcManager().startWorker(worker);
122
123       }
124       else
125       {
126         if (editParams)
127         {
128           paramset = worker.getArguments();
129           preset = worker.getPreset();
130         }
131
132         if (!processParams(sh, editParams, true))
133         {
134           return;
135         }
136         // reinstate worker if it was blacklisted (might have happened due to
137         // invalid parameters)
138         alignFrame.getViewport().getCalcManager().enableWorker(worker);
139         worker.updateParameters(this.preset, paramset);
140       }
141     }
142     if (!sh.isInteractiveUpdate())
143     {
144       // build IUPred style client. take sequences, returns annotation per
145       // sequence.
146       if (!processParams(sh, editParams))
147       {
148         return;
149       }
150
151       alignFrame.getViewport().getCalcManager().startWorker(
152               new SeqAnnotationServiceCalcWorker(sh, alignFrame, preset, paramset));
153     }
154   }
155
156   public SequenceAnnotationWSClient(AAConSettings fave,
157           AlignFrame alignFrame, boolean b)
158   {
159     super(alignFrame, fave.getPreset(), fave.getArgumentSet());
160     initSequenceAnnotationWSClient(fave.getService(), alignFrame,
161             fave.getPreset(), b);
162   }
163
164   /*
165    * (non-Javadoc)
166    * 
167    * @see jalview.ws.jws2.Jws2Client#attachWSMenuEntry(javax.swing.JMenu,
168    * jalview.ws.jws2.jabaws2.Jws2Instance, jalview.gui.AlignFrame)
169    */
170   @Override
171   public void attachWSMenuEntry(JMenu wsmenu,
172           final ServiceWithParameters service,
173           final AlignFrame alignFrame)
174   {
175     if (service instanceof Jws2Instance
176         && Jws2ClientFactory.registerAAConWSInstance(wsmenu, (Jws2Instance) service, alignFrame))
177     {
178       // Alignment dependent analysis calculation WS gui
179       return;
180     }
181     boolean hasparams = service.hasParameters();
182     String calcName = service.getName();
183     if (calcName.endsWith("WS"))
184     {
185       // Remove "WS" suffix
186       calcName = calcName.substring(0, calcName.length() - 2);
187     }
188
189     JMenuItem annotservice = new JMenuItem(MessageManager.formatMessage(
190             "label.calcname_with_default_settings", new String[]
191             { calcName }));
192     annotservice.addActionListener(new ActionListener()
193     {
194
195       @Override
196       public void actionPerformed(ActionEvent e)
197       {
198         new SequenceAnnotationWSClient(service, alignFrame,
199                 null, false);
200       }
201     });
202     wsmenu.add(annotservice);
203     if (hasparams)
204     {
205       // only add these menu options if the service has user-modifiable
206       // arguments
207       annotservice = new JMenuItem(
208               MessageManager.getString("label.edit_settings_and_run"));
209       annotservice.setToolTipText(MessageManager.getString(
210               "label.view_and_change_parameters_before_running_calculation"));
211
212       annotservice.addActionListener(new ActionListener()
213       {
214         @Override
215         public void actionPerformed(ActionEvent e)
216         {
217           new SequenceAnnotationWSClient(service, alignFrame,
218                   null, true);
219         }
220       });
221       wsmenu.add(annotservice);
222       List<WsParamSetI> presets = service.getParamStore().getPresets();
223       if (presets != null && presets.size() > 0)
224       {
225         JMenu presetlist = new JMenu(MessageManager
226                 .formatMessage("label.run_with_preset", new String[]
227                 { calcName }));
228
229         for (final WsParamSetI preset : presets)
230         {
231           final JMenuItem methodR = new JMenuItem(preset.getName());
232           methodR.setToolTipText(JvSwingUtils.wrapTooltip(true, "<strong>"
233                   + (preset.isModifiable()
234                           ? MessageManager.getString("label.user_preset")
235                           : MessageManager
236                                   .getString("label.service_preset"))
237                   + "</strong><br/>" + preset.getDescription()));
238           methodR.addActionListener(new ActionListener()
239           {
240             @Override
241             public void actionPerformed(ActionEvent e)
242             {
243               new SequenceAnnotationWSClient(service,
244                       alignFrame, preset,
245                       false);
246             }
247
248           });
249           presetlist.add(methodR);
250         }
251         wsmenu.add(presetlist);
252       }
253
254     }
255     else
256     {
257       annotservice = new JMenuItem(
258               MessageManager.getString("label.view_documentation"));
259       if (service != null && service.hasDocumentationUrl())
260       {
261         annotservice.addActionListener(new ActionListener()
262         {
263
264           @Override
265           public void actionPerformed(ActionEvent arg0)
266           {
267             Desktop.instance.showUrl(service.getDocumentationUrl());
268           }
269         });
270         annotservice.setToolTipText(
271                 JvSwingUtils.wrapTooltip(true, MessageManager.formatMessage(
272                         "label.view_service_doc_url", new String[]
273                         { service.getDocumentationUrl(),
274                             service.getDocumentationUrl() })));
275         wsmenu.add(annotservice);
276       }
277     }
278   }
279 }