Merge branch 'features/mchmmer' into merge/wsinterfaces_mchmmer_JAL-3070_JAL-1950
[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.jws2.dm.AAConSettings;
29 import jalview.ws.jws2.jabaws2.Jws2Instance;
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 Jws2Instance 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 Jws2Instance 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().getRegisteredWorkersOfClass(clientClass);
86       AbstractJabaCalcWorker worker;
87       if (clnts == null || clnts.size() == 0)
88       {
89         if (!processParams(sh, editParams))
90         {
91           return;
92         }
93         try
94         {
95           worker = (AbstractJabaCalcWorker) (clientClass
96                   .getConstructor(new Class[]
97                   { Jws2Instance.class, AlignFrame.class, WsParamSetI.class,
98                       List.class })
99                   .newInstance(new Object[]
100                   { sh, alignFrame, this.preset, paramset }));
101         } catch (Exception x)
102         {
103           x.printStackTrace();
104           throw new Error(
105                   MessageManager.getString("error.implementation_error"),
106                   x);
107         }
108         alignFrame.getViewport().getCalcManager().registerWorker(worker);
109         alignFrame.getViewport().getCalcManager().startWorker(worker);
110
111       }
112       else
113       {
114         worker = (AbstractJabaCalcWorker) clnts.get(0);
115         if (editParams)
116         {
117           paramset = worker.getArguments();
118           preset = worker.getPreset();
119         }
120
121         if (!processParams(sh, editParams, true))
122         {
123           return;
124         }
125         // reinstate worker if it was blacklisted (might have happened due to
126         // invalid parameters)
127         alignFrame.getViewport().getCalcManager().enableWorker(worker);
128         worker.updateParameters(this.preset, paramset);
129       }
130     }
131     if (sh.getAction().toLowerCase().contains("disorder"))
132     {
133       // build IUPred style client. take sequences, returns annotation per
134       // sequence.
135       if (!processParams(sh, editParams))
136       {
137         return;
138       }
139
140       alignFrame.getViewport().getCalcManager().startWorker(
141               new AADisorderClient(sh, alignFrame, preset, paramset));
142     }
143   }
144
145   public SequenceAnnotationWSClient(AAConSettings fave,
146           AlignFrame alignFrame, boolean b)
147   {
148     super(alignFrame, fave.getPreset(), fave.getJobArgset());
149     initSequenceAnnotationWSClient(fave.getService(), alignFrame,
150             fave.getPreset(), b);
151   }
152
153   /*
154    * (non-Javadoc)
155    * 
156    * @see jalview.ws.jws2.Jws2Client#attachWSMenuEntry(javax.swing.JMenu,
157    * jalview.ws.jws2.jabaws2.Jws2Instance, jalview.gui.AlignFrame)
158    */
159   @Override
160   public void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
161           final AlignFrame alignFrame)
162   {
163     if (registerAAConWSInstance(wsmenu, service, alignFrame))
164     {
165       // Alignment dependent analysis calculation WS gui
166       return;
167     }
168     boolean hasparams = service.hasParameters();
169     // Assume name ends in WS
170     String calcName = service.getName().substring(0,
171             service.getName().length() - 2);
172
173     JMenuItem annotservice = new JMenuItem(MessageManager.formatMessage(
174             "label.calcname_with_default_settings", new String[]
175             { calcName }));
176     annotservice.addActionListener(new ActionListener()
177     {
178
179       @Override
180       public void actionPerformed(ActionEvent e)
181       {
182         new SequenceAnnotationWSClient(service, alignFrame, null, false);
183       }
184     });
185     wsmenu.add(annotservice);
186     if (hasparams)
187     {
188       // only add these menu options if the service has user-modifiable
189       // arguments
190       annotservice = new JMenuItem(
191               MessageManager.getString("label.edit_settings_and_run"));
192       annotservice.setToolTipText(MessageManager.getString(
193               "label.view_and_change_parameters_before_running_calculation"));
194
195       annotservice.addActionListener(new ActionListener()
196       {
197         @Override
198         public void actionPerformed(ActionEvent e)
199         {
200           new SequenceAnnotationWSClient(service, alignFrame, null, true);
201         }
202       });
203       wsmenu.add(annotservice);
204       List<WsParamSetI> presets = service.getParamStore().getPresets();
205       if (presets != null && presets.size() > 0)
206       {
207         JMenu presetlist = new JMenu(MessageManager
208                 .formatMessage("label.run_with_preset", new String[]
209                 { calcName }));
210
211         for (final WsParamSetI preset : presets)
212         {
213           final JMenuItem methodR = new JMenuItem(preset.getName());
214           methodR.setToolTipText(JvSwingUtils.wrapTooltip(true, "<strong>"
215                   + (preset.isModifiable()
216                           ? MessageManager.getString("label.user_preset")
217                           : MessageManager
218                                   .getString("label.service_preset"))
219                   + "</strong><br/>" + preset.getDescription()));
220           methodR.addActionListener(new ActionListener()
221           {
222             @Override
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(
252                 JvSwingUtils.wrapTooltip(true, MessageManager.formatMessage(
253                         "label.view_service_doc_url", new String[]
254                         { service.docUrl, service.docUrl })));
255         wsmenu.add(annotservice);
256       }
257     }
258   }
259 }