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