JAL-1807 explicit imports (jalview.ws.*)
[jalview.git] / src / jalview / ws / jws1 / JPredClient.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.jws1;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.analysis.SeqsetUtils;
25 import jalview.bin.Cache;
26 import jalview.datamodel.AlignmentView;
27 import jalview.datamodel.SeqCigar;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.Desktop;
31 import jalview.gui.WebserviceInfo;
32 import jalview.util.MessageManager;
33
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.util.Hashtable;
37
38 import javax.swing.JMenu;
39 import javax.swing.JMenuItem;
40 import javax.swing.JOptionPane;
41
42 import ext.vamsas.Jpred;
43 import ext.vamsas.JpredServiceLocator;
44 import ext.vamsas.JpredSoapBindingStub;
45 import ext.vamsas.ServiceHandle;
46
47 public class JPredClient extends WS1Client
48 {
49   /**
50    * crate a new GUI JPred Job
51    * 
52    * @param sh
53    *          ServiceHandle
54    * @param title
55    *          String
56    * @param msa
57    *          boolean - true - submit alignment as a sequence profile
58    * @param alview
59    *          AlignmentView
60    * @param viewonly
61    *          TODO
62    */
63   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
64           boolean msa, AlignmentView alview, AlignFrame parentFrame,
65           boolean viewonly)
66   {
67     super();
68     wsInfo = setWebService(sh);
69     startJPredClient(title, msa, alview, parentFrame, viewonly);
70
71   }
72
73   /**
74    * startJPredClient TODO: refine submission to cope with local prediction of
75    * visible regions or multiple single sequence jobs TODO: sequence
76    * representative support - could submit alignment of representatives as msa.
77    * TODO: msa hidden region prediction - submit each chunk for prediction.
78    * concatenate results of each. TODO: single seq prediction - submit each
79    * contig of each sequence for prediction (but must cope with flanking regions
80    * and short seqs)
81    * 
82    * @param title
83    *          String
84    * @param msa
85    *          boolean
86    * @param alview
87    *          AlignmentView
88    * @param viewonly
89    *          if true then the prediction will be made just on the concatenated
90    *          visible regions
91    */
92   private void startJPredClient(String title, boolean msa,
93           AlignmentView alview, AlignFrame parentFrame,
94           boolean viewonly)
95   {
96     AlignmentView input = alview;
97     if (wsInfo == null)
98     {
99       wsInfo = setWebService();
100     }
101     Jpred server = locateWebService();
102     if (server == null)
103     {
104       Cache.log.warn("Couldn't find a Jpred webservice to invoke!");
105       return;
106     }
107     SeqCigar[] msf = null;
108     SequenceI seq = null;
109     int[] delMap = null;
110     // original JNetClient behaviour - submit full length of sequence or profile
111     // and mask result.
112     msf = input.getSequences();
113     seq = msf[0].getSeq('-');
114
115     if (viewonly)
116     {
117       delMap = alview.getVisibleContigMapFor(seq.gapMap());
118     }
119     if (msa && msf.length > 1)
120     {
121
122       String altitle = getPredictionName(WebServiceName) + " on "
123               + (viewonly ? "visible " : "") + seq.getName()
124               + " using alignment from " + title;
125
126       SequenceI aln[] = new SequenceI[msf.length];
127       for (int i = 0, j = msf.length; i < j; i++)
128       {
129         aln[i] = msf[i].getSeq('-');
130       }
131
132       Hashtable SequenceInfo = SeqsetUtils.uniquify(aln, true);
133       if (viewonly)
134       {
135         // Remove hidden regions from sequence objects.
136         String seqs[] = alview.getSequenceStrings('-');
137         for (int i = 0, j = msf.length; i < j; i++)
138         {
139           aln[i].setSequence(seqs[i]);
140         }
141         seq.setSequence(seqs[0]);
142       }
143       wsInfo.setProgressText("Job details for "
144               + (viewonly ? "visible " : "") + "MSA based prediction ("
145               + title + ") on sequence :\n>" + seq.getName() + "\n"
146               + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
147               + "\n");
148       JPredThread jthread = new JPredThread(wsInfo, altitle, server,
149               SequenceInfo, aln, delMap, alview, parentFrame, WsURL);
150       wsInfo.setthisService(jthread);
151       jthread.start();
152     }
153     else
154     {
155       if (!msa && msf.length > 1)
156       {
157         throw new Error(MessageManager.getString("error.implementation_error_multiple_single_sequence_prediction_jobs_not_supported"));
158       }
159
160       String altitle = getPredictionName(WebServiceName) + " for "
161               + (viewonly ? "visible " : "") + "sequence " + seq.getName()
162               + " from " + title;
163       String seqname = seq.getName();
164       Hashtable SequenceInfo = SeqsetUtils.SeqCharacterHash(seq);
165       if (viewonly)
166       {
167         // Remove hidden regions from input sequence
168         String seqs[] = alview.getSequenceStrings('-');
169         seq.setSequence(seqs[0]);
170       }
171       wsInfo.setProgressText("Job details for prediction on "
172               + (viewonly ? "visible " : "") + "sequence :\n>" + seqname
173               + "\n"
174               + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
175               + "\n");
176       JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
177               SequenceInfo, seq, delMap, alview, parentFrame);
178       wsInfo.setthisService(jthread);
179       jthread.start();
180     }
181   }
182
183   private String getPredictionName(String webServiceName)
184   {
185     if (webServiceName.toLowerCase().indexOf(
186             "secondary structure prediction") > -1)
187     {
188       return webServiceName;
189     }
190     else
191     {
192       return webServiceName + "secondary structure prediction";
193     }
194   }
195
196   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
197           SequenceI seq, AlignFrame parentFrame)
198   {
199     super();
200     wsInfo = setWebService(sh);
201     startJPredClient(title, seq, parentFrame);
202   }
203
204   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
205           SequenceI[] msa, AlignFrame parentFrame)
206   {
207     wsInfo = setWebService(sh);
208     startJPredClient(title, msa, parentFrame);
209   }
210
211   public JPredClient(String title, SequenceI[] msf)
212   {
213     startJPredClient(title, msf, null);
214   }
215
216   public JPredClient(String title, SequenceI seq)
217   {
218     startJPredClient(title, seq, null);
219   }
220
221   public JPredClient()
222   {
223
224     super();
225     // add a class reference to the list
226   }
227
228   private void startJPredClient(String title, SequenceI[] msf,
229           AlignFrame parentFrame)
230   {
231     if (wsInfo == null)
232     {
233       wsInfo = setWebService();
234     }
235
236     SequenceI seq = msf[0];
237
238     String altitle = "JNet prediction on " + seq.getName()
239             + " using alignment from " + title;
240
241     wsInfo.setProgressText("Job details for MSA based prediction (" + title
242             + ") on sequence :\n>" + seq.getName() + "\n"
243             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
244     SequenceI aln[] = new SequenceI[msf.length];
245     for (int i = 0, j = msf.length; i < j; i++)
246     {
247       aln[i] = new jalview.datamodel.Sequence(msf[i]);
248     }
249
250     Hashtable SequenceInfo = SeqsetUtils.uniquify(aln,
251             true);
252
253     Jpred server = locateWebService();
254     if (server == null)
255     {
256       return;
257     }
258
259     JPredThread jthread = new JPredThread(wsInfo, altitle, server,
260             SequenceInfo, aln, null, null, parentFrame, WsURL);
261     wsInfo.setthisService(jthread);
262     jthread.start();
263   }
264
265   public void startJPredClient(String title, SequenceI seq,
266           AlignFrame parentFrame)
267   {
268     if (wsInfo == null)
269     {
270       wsInfo = setWebService();
271     }
272     wsInfo.setProgressText("Job details for prediction on sequence :\n>"
273             + seq.getName() + "\n"
274             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
275     String altitle = "JNet prediction for sequence " + seq.getName()
276             + " from " + title;
277
278     Hashtable SequenceInfo = SeqsetUtils.SeqCharacterHash(seq);
279
280     Jpred server = locateWebService();
281     if (server == null)
282     {
283       return;
284     }
285
286     JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
287             SequenceInfo, seq, null, null, parentFrame);
288     wsInfo.setthisService(jthread);
289     jthread.start();
290   }
291
292   private WebserviceInfo setWebService()
293   {
294     WebServiceName = "JNetWS";
295     WebServiceJobTitle = MessageManager.getString("label.jnet_secondary_structure_prediction");
296     WebServiceReference = "\"Cuff J. A and Barton G.J (2000) Application of "
297             + "multiple sequence alignment profiles to improve protein secondary structure prediction, "
298             + "Proteins 40:502-511\".";
299     WsURL = "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred";
300
301     WebserviceInfo wsInfo = new WebserviceInfo(WebServiceJobTitle,
302             WebServiceReference, true);
303
304     return wsInfo;
305   }
306
307   private ext.vamsas.Jpred locateWebService()
308   {
309     ext.vamsas.JpredServiceLocator loc = new JpredServiceLocator(); // Default
310     ext.vamsas.Jpred server = null;
311     try
312     {
313       server = loc.getjpred(new java.net.URL(WsURL)); // JBPNote will be set
314       // from properties
315       ((JpredSoapBindingStub) server).setTimeout(60000); // one minute stub
316       // ((JpredSoapBindingStub)this.server)._setProperty(org.apache.axis.encoding.C,
317       // Boolean.TRUE);
318
319     } catch (Exception ex)
320     {
321       JOptionPane.showMessageDialog(Desktop.desktop,
322                   MessageManager.formatMessage("label.secondary_structure_prediction_service_couldnt_be_located", new String[]{WebServiceName,WsURL}),
323                   MessageManager.getString("label.internal_jalview_error"),
324               JOptionPane.WARNING_MESSAGE);
325       wsInfo.setProgressText(MessageManager.formatMessage("label.secondary_structure_prediction_service_couldnt_be_located", new String[]{WebServiceName,WsURL})
326                   + "\n"
327               + ex.getMessage());
328       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
329
330     }
331
332     return server;
333   }
334
335   public void attachWSMenuEntry(JMenu wsmenu, final ServiceHandle sh,
336           final AlignFrame af)
337   {
338     final JMenuItem method = new JMenuItem(sh.getName());
339     method.setToolTipText(sh.getEndpointURL());
340     method.addActionListener(new ActionListener()
341     {
342       public void actionPerformed(ActionEvent e)
343       {
344         AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
345         if (msa.getSequences().length == 1)
346         {
347           // Single Sequence prediction
348           new jalview.ws.jws1.JPredClient(sh, af.getTitle(), false, msa,
349                   af, true);
350         }
351         else
352         {
353           if (msa.getSequences().length > 1)
354           {
355             // Sequence profile based prediction
356             new jalview.ws.jws1.JPredClient(sh, af.getTitle(), true, msa,
357                     af, true);
358           }
359         }
360       }
361     });
362     wsmenu.add(method);
363   }
364 }