JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / jws2 / SequenceAnnotationWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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
67   // GUI
68
69   public void initSequenceAnnotationWSClient(final Jws2Instance sh,
70           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
71   {
72     // dan changed! dan test. comment out if conditional
73     // if (alignFrame.getViewport().getAlignment().isNucleotide())
74     // {
75     // JOptionPane.showMessageDialog(Desktop.desktop, sh.serviceType
76     // + " can only be used\nfor amino acid alignments.",
77     // "Wrong type of sequences!", JOptionPane.WARNING_MESSAGE);
78     // return;
79     //
80     // }
81     AlignAnalysisUIText aaui = sh.getAlignAnalysisUI();
82     if (aaui != null)
83     {
84       Class clientClass = aaui.getClient();
85
86       // Build an AACon style client - take alignment, return annotation for
87       // columns
88
89       List<AlignCalcWorkerI> clnts = alignFrame.getViewport()
90               .getCalcManager().getRegisteredWorkersOfClass(clientClass);
91       AbstractJabaCalcWorker worker;
92       if (clnts == null || clnts.size() == 0)
93       {
94         if (!processParams(sh, editParams))
95         {
96           return;
97         }
98         try
99         {
100           worker = (AbstractJabaCalcWorker) (clientClass
101                   .getConstructor(new Class[]
102                   { Jws2Instance.class, AlignFrame.class,
103                       WsParamSetI.class, List.class })
104                   .newInstance(new Object[]
105                   { sh, alignFrame, this.preset, paramset }));
106         } catch (Exception x)
107         {
108           x.printStackTrace();
109           throw new Error(MessageManager.getString("error.implementation_error"), x);
110         }
111         alignFrame.getViewport().getCalcManager().registerWorker(worker);
112         alignFrame.getViewport().getCalcManager().startWorker(worker);
113
114       }
115       else
116       {
117         worker = (AbstractJabaCalcWorker) clnts.get(0);
118         if (editParams)
119         {
120           paramset = worker.getArguments();
121           preset = worker.getPreset();
122         }
123
124         if (!processParams(sh, editParams, true))
125         {
126           return;
127         }
128         // reinstate worker if it was blacklisted (might have happened due to
129         // invalid parameters)
130         alignFrame.getViewport().getCalcManager().workerMayRun(worker);
131         worker.updateParameters(this.preset, paramset);
132       }
133     }
134     if (sh.action.toLowerCase().contains("disorder"))
135     {
136       // build IUPred style client. take sequences, returns annotation per
137       // sequence.
138       if (!processParams(sh, editParams))
139       {
140         return;
141       }
142
143       alignFrame
144               .getViewport()
145               .getCalcManager()
146               .startWorker(
147                       new AADisorderClient(sh, alignFrame, preset, paramset));
148     }
149   }
150
151   public SequenceAnnotationWSClient(AAConSettings fave,
152           AlignFrame alignFrame, boolean b)
153   {
154     super(alignFrame, fave.getPreset(), fave.getJobArgset());
155     initSequenceAnnotationWSClient(fave.getService(), alignFrame,
156             fave.getPreset(), b);
157   }
158
159   /*
160    * (non-Javadoc)
161    * 
162    * @see jalview.ws.jws2.Jws2Client#attachWSMenuEntry(javax.swing.JMenu,
163    * jalview.ws.jws2.jabaws2.Jws2Instance, jalview.gui.AlignFrame)
164    */
165   public void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
166           final AlignFrame alignFrame)
167   {
168     if (registerAAConWSInstance(wsmenu, service, alignFrame))
169     {
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(
179             "label.calcname_with_default_settings", new String[]
180             { calcName }));
181     annotservice.addActionListener(new ActionListener()
182     {
183
184       @Override
185       public void actionPerformed(ActionEvent e)
186       {
187         new SequenceAnnotationWSClient(service, alignFrame, null, false);
188       }
189     });
190     wsmenu.add(annotservice);
191     if (hasparams)
192     {
193       // only add these menu options if the service has user-modifiable
194       // arguments
195       annotservice = new JMenuItem(
196               MessageManager.getString("label.edit_settings_and_run"));
197       annotservice
198               .setToolTipText(MessageManager
199                       .getString("label.view_and_change_parameters_before_running_calculation"));
200
201       annotservice.addActionListener(new ActionListener()
202       {
203         public void actionPerformed(ActionEvent e)
204         {
205           new SequenceAnnotationWSClient(service, alignFrame, null, true);
206         }
207       });
208       wsmenu.add(annotservice);
209       List<WsParamSetI> presets = service.getParamStore().getPresets();
210       if (presets != null && presets.size() > 0)
211       {
212         JMenu presetlist = new JMenu(MessageManager.formatMessage("label.run_with_preset", new String[]{calcName}));
213
214         for (final WsParamSetI preset : presets)
215         {
216           final JMenuItem methodR = new JMenuItem(preset.getName());
217           methodR.setToolTipText(JvSwingUtils.wrapTooltip(true, "<strong>"
218                           + (preset.isModifiable() ? MessageManager.getString("label.user_preset")
219                                   : MessageManager.getString("label.service_preset")) + "</strong><br/>"
220                           + preset.getDescription()));
221           methodR.addActionListener(new ActionListener()
222           {
223             public void actionPerformed(ActionEvent e)
224             {
225               new SequenceAnnotationWSClient(service, alignFrame, preset,
226                       false);
227             }
228
229           });
230           presetlist.add(methodR);
231         }
232         wsmenu.add(presetlist);
233       }
234
235     }
236     else
237     {
238       annotservice = new JMenuItem(
239               MessageManager.getString("label.view_documentation"));
240       if (service.docUrl != null)
241       {
242         annotservice.addActionListener(new ActionListener()
243         {
244
245           @Override
246           public void actionPerformed(ActionEvent arg0)
247           {
248             Desktop.instance.showUrl(service.docUrl);
249           }
250         });
251         annotservice.setToolTipText(JvSwingUtils.wrapTooltip(true, MessageManager.formatMessage("label.view_service_doc_url", new String[]{service.docUrl,service.docUrl})));
252         wsmenu.add(annotservice);
253       }
254     }
255   }
256 }