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