b65242fd5d2e84ea34e9f0b8430299daf3b7f9ca
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws.jws2;
19
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.util.List;
23
24 import javax.swing.*;
25
26 import jalview.datamodel.*;
27 import jalview.gui.*;
28 import compbio.data.msa.MsaWS;
29 import compbio.metadata.Argument;
30 import compbio.metadata.Preset;
31 import compbio.metadata.PresetManager;
32 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
33
34 /**
35  * DOCUMENT ME!
36  * 
37  * @author $author$
38  * @version $Revision$
39  */
40 public class MsaWSClient extends Jws2Client
41 {
42   /**
43    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
44    */
45   MsaWS server;
46   AlignFrame alignFrame;
47   private Preset preset;
48   private List<Argument> paramset;
49
50
51   public MsaWSClient(Jws2Discoverer.Jws2Instance sh, String altitle,
52           jalview.datamodel.AlignmentView msa, boolean submitGaps,
53           boolean preserveOrder, Alignment seqdataset,
54           AlignFrame _alignFrame)
55   {
56     this(sh, null, null, altitle, msa, submitGaps, preserveOrder, seqdataset, _alignFrame);
57     // TODO Auto-generated constructor stub
58   }
59
60   public MsaWSClient(Jws2Discoverer.Jws2Instance sh, Preset preset, String altitle,
61           jalview.datamodel.AlignmentView msa, boolean submitGaps,
62           boolean preserveOrder, Alignment seqdataset,
63           AlignFrame _alignFrame)
64   {
65     this(sh, preset, null, altitle, msa, submitGaps, preserveOrder, seqdataset, _alignFrame);
66     // TODO Auto-generated constructor stub
67   }
68   /**
69    * Creates a new MsaWSClient object that uses a service given by an externally
70    * retrieved ServiceHandle
71    * 
72    * @param sh
73    *          service handle of type AbstractName(MsaWS)
74    * @param altitle
75    *          DOCUMENT ME!
76    * @param msa
77    *          DOCUMENT ME!
78    * @param submitGaps
79    *          DOCUMENT ME!
80    * @param preserveOrder
81    *          DOCUMENT ME!
82    */
83
84   public MsaWSClient(Jws2Discoverer.Jws2Instance sh, Preset preset, List<Argument> arguments, String altitle,
85           jalview.datamodel.AlignmentView msa, boolean submitGaps,
86           boolean preserveOrder, Alignment seqdataset,
87           AlignFrame _alignFrame)  {
88     super();
89     alignFrame = _alignFrame;
90     if (!(sh.service instanceof MsaWS))
91     {
92       // redundant at mo - but may change
93       JOptionPane
94               .showMessageDialog(
95                       Desktop.desktop,
96                       "The Service called \n"
97                               + sh.serviceType
98                               + "\nis not a \nMultiple Sequence Alignment Service !",
99                       "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
100
101       return;
102     }
103     server = sh.service;
104     this.preset = preset;
105     this.paramset = arguments;
106     if ((wsInfo = setWebService(sh, false)) == null)
107     {
108       JOptionPane.showMessageDialog(Desktop.desktop,
109               "The Multiple Sequence Alignment Service named "
110                       + sh.serviceType + " is unknown",
111               "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
112
113       return;
114     }
115     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
116
117   }
118
119   public MsaWSClient()
120   {
121     super();
122     // add a class reference to the list
123   }
124
125   private void startMsaWSClient(String altitle, AlignmentView msa,
126           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
127   {
128     //if (!locateWebService())
129    // {
130    //   return;
131    // }
132
133     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
134             + " of " + altitle + "\nJob details\n");
135     String jobtitle = WebServiceName.toLowerCase();
136     if (jobtitle.endsWith("alignment"))
137     {
138       if (submitGaps
139               && (!jobtitle.endsWith("realignment") || jobtitle
140                       .indexOf("profile") == -1))
141       {
142         int pos = jobtitle.indexOf("alignment");
143         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
144                 + altitle;
145       }
146       else
147       {
148         jobtitle = WebServiceName + " of " + altitle;
149       }
150     }
151     else
152     {
153       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
154               + "alignment of " + altitle;
155     }
156
157     MsaWSThread msathread = new MsaWSThread(server, preset, paramset, WsURL, wsInfo,
158             alignFrame, WebServiceName, jobtitle, msa, submitGaps,
159             preserveOrder, seqdataset);
160     wsInfo.setthisService(msathread);
161     msathread.start();
162   }
163
164   protected String getServiceActionKey()
165   {
166     return "MsaWS";
167   }
168
169   protected String getServiceActionDescription()
170   {
171     return "Multiple Sequence Alignment";
172   }
173
174   /**
175    * look at ourselves and work out if we are a service that can take a profile
176    * and align to it
177    * 
178    * @return true if we can send gapped sequences to the alignment service
179    */
180   private boolean canSubmitGaps()
181   {
182     // TODO: query service or extract service handle props to check if we can
183     // realign
184     return (WebServiceName.indexOf("lustal") > -1); // cheat!
185   }
186
187   public void attachWSMenuEntry(JMenu msawsmenu,
188           final Jws2Instance service, final AlignFrame alignFrame)
189   {
190     setWebService(service, true); // headless
191     JMenuItem method = new JMenuItem(WebServiceName);
192     method.setToolTipText(WsURL);
193     method.addActionListener(new ActionListener()
194     {
195       public void actionPerformed(ActionEvent e)
196       {
197         AlignmentView msa = alignFrame.gatherSequencesForAlignment();
198         new MsaWSClient(service, alignFrame.getTitle(),
199                 msa, false, true, alignFrame.getViewport().getAlignment()
200                         .getDataset(), alignFrame);
201
202       }
203
204     });
205     msawsmenu.add(method);
206     if (canSubmitGaps())
207     {
208       // We know that ClustalWS can accept partial alignments for refinement.
209       final JMenuItem methodR = new JMenuItem(WebServiceName 
210               + " (With Gaps)");
211       methodR.setToolTipText(WsURL);
212       methodR.addActionListener(new ActionListener()
213       {
214         public void actionPerformed(ActionEvent e)
215         {
216           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
217           new MsaWSClient(service, alignFrame.getTitle(),
218                   msa, true, true, alignFrame.getViewport().getAlignment()
219                           .getDataset(), alignFrame);
220
221         }
222
223       });
224       msawsmenu.add(methodR);
225
226     }
227     PresetManager presets = service.getPresets();
228     if (presets!=null && presets.getPresets().size()>0)
229     {
230       JMenu presetlist = new JMenu("Presets for "+WebServiceName+"(Without gaps)");
231       
232       for (final Preset preset : (List<Preset>) presets.getPresets())
233       {
234         final JMenuItem methodR = new JMenuItem(preset.getName());
235         methodR.setToolTipText(preset.getDescription());
236         methodR.addActionListener(new ActionListener()
237         {
238           public void actionPerformed(ActionEvent e)
239           {
240             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
241             new MsaWSClient(service, preset, alignFrame.getTitle(),
242                     msa, false, true, alignFrame.getViewport().getAlignment()
243                             .getDataset(), alignFrame);
244
245           }
246
247         });
248         presetlist.add(methodR);
249       }
250       msawsmenu.add(presetlist);
251     }
252
253   }
254
255 }