First patch for * JAL-493
[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
23 import javax.swing.*;
24
25 import jalview.datamodel.*;
26 import jalview.gui.*;
27 import compbio.data.msa.MsaWS;
28 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
29
30 /**
31  * DOCUMENT ME!
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36 public class MsaWSClient extends Jws2Client
37 {
38   /**
39    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
40    */
41   MsaWS server;
42   AlignFrame alignFrame;
43
44   /**
45    * Creates a new MsaWSClient object that uses a service given by an externally
46    * retrieved ServiceHandle
47    * 
48    * @param sh
49    *          service handle of type AbstractName(MsaWS)
50    * @param altitle
51    *          DOCUMENT ME!
52    * @param msa
53    *          DOCUMENT ME!
54    * @param submitGaps
55    *          DOCUMENT ME!
56    * @param preserveOrder
57    *          DOCUMENT ME!
58    */
59
60   public MsaWSClient(Jws2Discoverer.Jws2Instance sh, String altitle,
61           jalview.datamodel.AlignmentView msa, boolean submitGaps,
62           boolean preserveOrder, Alignment seqdataset,
63           AlignFrame _alignFrame)
64   {
65     super();
66     alignFrame = _alignFrame;
67     if (!(sh.service instanceof MsaWS))
68     {
69       // redundant at mo - but may change
70       JOptionPane
71               .showMessageDialog(
72                       Desktop.desktop,
73                       "The Service called \n"
74                               + sh.serviceType
75                               + "\nis not a \nMultiple Sequence Alignment Service !",
76                       "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
77
78       return;
79     }
80     server = sh.service;
81     if ((wsInfo = setWebService(sh, false)) == null)
82     {
83       JOptionPane.showMessageDialog(Desktop.desktop,
84               "The Multiple Sequence Alignment Service named "
85                       + sh.serviceType + " is unknown",
86               "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
87
88       return;
89     }
90     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
91
92   }
93
94   public MsaWSClient()
95   {
96     super();
97     // add a class reference to the list
98   }
99
100   private void startMsaWSClient(String altitle, AlignmentView msa,
101           boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
102   {
103     //if (!locateWebService())
104    // {
105    //   return;
106    // }
107
108     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
109             + " of " + altitle + "\nJob details\n");
110     String jobtitle = WebServiceName.toLowerCase();
111     if (jobtitle.endsWith("alignment"))
112     {
113       if (submitGaps
114               && (!jobtitle.endsWith("realignment") || jobtitle
115                       .indexOf("profile") == -1))
116       {
117         int pos = jobtitle.indexOf("alignment");
118         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
119                 + altitle;
120       }
121       else
122       {
123         jobtitle = WebServiceName + " of " + altitle;
124       }
125     }
126     else
127     {
128       jobtitle = WebServiceName + (submitGaps ? " re" : " ")
129               + "alignment of " + altitle;
130     }
131
132     MsaWSThread msathread = new MsaWSThread(server, WsURL, wsInfo,
133             alignFrame, WebServiceName, jobtitle, msa, submitGaps,
134             preserveOrder, seqdataset);
135     wsInfo.setthisService(msathread);
136     msathread.start();
137   }
138
139   protected String getServiceActionKey()
140   {
141     return "MsaWS";
142   }
143
144   protected String getServiceActionDescription()
145   {
146     return "Multiple Sequence Alignment";
147   }
148
149   /**
150    * look at ourselves and work out if we are a service that can take a profile
151    * and align to it
152    * 
153    * @return true if we can send gapped sequences to the alignment service
154    */
155   private boolean canSubmitGaps()
156   {
157     // TODO: query service or extract service handle props to check if we can
158     // realign
159     return (WebServiceName.indexOf("lustal") > -1); // cheat!
160   }
161
162   public void attachWSMenuEntry(JMenu msawsmenu,
163           final Jws2Instance service, final AlignFrame alignFrame)
164   {
165     setWebService(service, true); // headless
166     JMenuItem method = new JMenuItem(WebServiceName);
167     method.setToolTipText(WsURL);
168     method.addActionListener(new ActionListener()
169     {
170       public void actionPerformed(ActionEvent e)
171       {
172         AlignmentView msa = alignFrame.gatherSequencesForAlignment();
173         new MsaWSClient(service, alignFrame.getTitle(),
174                 msa, false, true, alignFrame.getViewport().getAlignment()
175                         .getDataset(), alignFrame);
176
177       }
178
179     });
180     msawsmenu.add(method);
181     if (canSubmitGaps())
182     {
183       // We know that ClustalWS can accept partial alignments for refinement.
184       final JMenuItem methodR = new JMenuItem(WebServiceName 
185               + " (With Gaps)");
186       methodR.setToolTipText(WsURL);
187       methodR.addActionListener(new ActionListener()
188       {
189         public void actionPerformed(ActionEvent e)
190         {
191           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
192           new MsaWSClient(service, alignFrame.getTitle(),
193                   msa, true, true, alignFrame.getViewport().getAlignment()
194                           .getDataset(), alignFrame);
195
196         }
197
198       });
199       msawsmenu.add(methodR);
200
201     }
202
203   }
204
205 }