(JAL-976,JAL-975) disorder client menu items and check type of input sequence for...
[jalview.git] / src / jalview / ws / jws2 / SequenceAnnotationWSClient.java
1 /**
2  * 
3  */
4 package jalview.ws.jws2;
5
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.util.List;
9
10 import javax.swing.JMenu;
11 import javax.swing.JMenuItem;
12 import javax.swing.JOptionPane;
13
14 import compbio.metadata.Argument;
15
16 import jalview.api.AlignCalcWorkerI;
17 import jalview.datamodel.AlignmentView;
18 import jalview.gui.AlignFrame;
19 import jalview.gui.Desktop;
20 import jalview.gui.JalviewDialog;
21 import jalview.gui.JvSwingUtils;
22 import jalview.ws.jws2.jabaws2.Jws2Instance;
23 import jalview.ws.params.WsParamSetI;
24
25 /**
26  * @author jimp
27  * 
28  */
29 public class SequenceAnnotationWSClient extends Jws2Client
30 {
31
32   /**
33    * initialise a client so its attachWSMenuEntry method can be called.
34    */
35   public SequenceAnnotationWSClient()
36   {
37     // TODO Auto-generated constructor stub
38   }
39
40   public SequenceAnnotationWSClient(final Jws2Instance sh,
41           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
42   {
43     super(alignFrame, preset, null);
44     if (alignFrame.getViewport().getAlignment().isNucleotide())
45     {
46       JOptionPane
47               .showMessageDialog(
48                       Desktop.desktop,
49                       sh.serviceType+" can only be used\nfor amino acid alignments.",
50                       "Wrong type of sequences!",
51                       JOptionPane.WARNING_MESSAGE);
52       return;
53
54     }
55     if (sh.action.toLowerCase().contains("conservation"))
56     {
57       // Build an AACons style client - take alignment, return annotation for columns
58
59       List<AlignCalcWorkerI> clnts = alignFrame.getViewport()
60               .getCalcManager()
61               .getRegisteredWorkersOfClass(AAConsClient.class);
62       if (clnts == null || clnts.size() == 0)
63       {
64         if (!processParams(sh, editParams))
65         {
66           return;
67         }
68         alignFrame
69                 .getViewport()
70                 .getCalcManager()
71                 .registerWorker(
72                         new AAConsClient(sh, alignFrame, preset, paramset));
73       }
74       else
75       {
76         AAConsClient worker = (AAConsClient) clnts.get(0);
77         if (editParams)
78         {
79           paramset = worker.getArguments();
80           preset = worker.getPreset();
81         }
82
83         if (!processParams(sh, editParams, true))
84           return;
85         // reinstate worker if it was blacklisted (might have happened due to
86         // invalid parameters)
87         alignFrame.getViewport().getCalcManager().workerMayRun(worker);
88         worker.updateParameters(preset, paramset);
89
90       }
91     }
92     if (sh.action.toLowerCase().contains("disorder"))
93     {
94       // build IUPred style client. take sequences, returns annotation per sequence.
95       if (!processParams(sh, editParams))
96       {
97         return;
98       }
99
100       alignFrame
101               .getViewport()
102               .getCalcManager()
103               .startWorker(
104                       new AADisorderClient(sh, alignFrame, preset, paramset));
105     }
106
107   }
108
109   /*
110    * (non-Javadoc)
111    * 
112    * @see jalview.ws.jws2.Jws2Client#attachWSMenuEntry(javax.swing.JMenu,
113    * jalview.ws.jws2.jabaws2.Jws2Instance, jalview.gui.AlignFrame)
114    */
115   public void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
116           final AlignFrame alignFrame)
117   {
118     boolean hasparams = service.hasParameters();
119     // Assume name ends in WS
120     String calcName = service.serviceType.substring(0,service.serviceType.length()-2);
121
122     JMenuItem aacons = new JMenuItem(calcName + " Defaults");
123     aacons.addActionListener(new ActionListener()
124     {
125
126       @Override
127       public void actionPerformed(ActionEvent e)
128       {
129         new SequenceAnnotationWSClient(service, alignFrame, null, false);
130       }
131     });
132     wsmenu.add(aacons);
133     if (hasparams)
134     {
135       // only add these menu options if the service has user-modifiable
136       // arguments
137       aacons = new JMenuItem("Edit settings and run ...");
138       aacons.setToolTipText("View and change parameters before running calculation");
139
140       aacons.addActionListener(new ActionListener()
141       {
142         public void actionPerformed(ActionEvent e)
143         {
144           new SequenceAnnotationWSClient(service, alignFrame, null, true);
145         }
146       });
147       wsmenu.add(aacons);
148       List<WsParamSetI> presets = service.getParamStore().getPresets();
149       if (presets != null && presets.size() > 0)
150       {
151         JMenu presetlist = new JMenu("Run " + calcName + "with preset");
152
153         for (final WsParamSetI preset : presets)
154         {
155           final JMenuItem methodR = new JMenuItem(preset.getName());
156           methodR.setToolTipText("<html><p>"
157                   + JvSwingUtils.wrapTooltip("<strong>"
158                           + (preset.isModifiable() ? "User Preset"
159                                   : "Service Preset") + "</strong><br/>"
160                           + preset.getDescription() + "</p>") + "</html>");
161           methodR.addActionListener(new ActionListener()
162           {
163             public void actionPerformed(ActionEvent e)
164             {
165               new SequenceAnnotationWSClient(service, alignFrame, preset,
166                       false);
167             }
168
169           });
170           presetlist.add(methodR);
171         }
172         wsmenu.add(presetlist);
173       }
174
175     }
176   }
177 }