JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / io / WSWUBlastClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.io;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.Sequence;
27 import jalview.gui.AlignmentPanel;
28 import jalview.gui.CutAndPasteTransfer;
29 import jalview.gui.Desktop;
30 import jalview.util.MessageManager;
31
32 import java.util.ArrayList;
33 import java.util.Enumeration;
34 import java.util.StringTokenizer;
35 import java.util.Vector;
36
37 import javax.swing.ImageIcon;
38 import javax.swing.JOptionPane;
39
40 import uk.ac.ebi.www.Data;
41 import uk.ac.ebi.www.InputParams;
42 import uk.ac.ebi.www.WSFile;
43 import uk.ac.ebi.www.WSWUBlast;
44 import uk.ac.ebi.www.WSWUBlastService;
45 import uk.ac.ebi.www.WSWUBlastServiceLocator;
46
47 /**
48  * DOCUMENT ME!
49  * 
50  * @author $author$
51  * @version $Revision$
52  */
53 public class WSWUBlastClient
54 {
55   AlignmentPanel ap;
56
57   AlignmentI al;
58
59   CutAndPasteTransfer output = new CutAndPasteTransfer();
60
61   int jobsRunning = 0;
62
63   Vector suggestedIds = new Vector();
64
65   /**
66    * Creates a new WSWUBlastClient object.
67    * 
68    * @param al
69    *          DOCUMENT ME!
70    * @param ids
71    *          DOCUMENT ME!
72    */
73   public WSWUBlastClient(AlignmentPanel ap, AlignmentI al, ArrayList ids)
74   {
75     this.ap = ap;
76     this.al = al;
77     output.setText(MessageManager
78             .getString("label.wswublast_client_credits"));
79
80     Desktop.addInternalFrame(output, MessageManager
81             .getString("label.blasting_for_unidentified_sequence"), 800,
82             300);
83
84     for (int i = 0; i < ids.size(); i++)
85     {
86       Sequence sequence = (Sequence) ids.get(i);
87       System.out.println(sequence.getName());
88
89       BlastThread thread = new BlastThread(sequence);
90       thread.start();
91       jobsRunning++;
92     }
93
94     ImageTwirler thread = new ImageTwirler();
95     thread.start();
96   }
97
98   /**
99    * DOCUMENT ME!
100    * 
101    * @param id1
102    *          DOCUMENT ME!
103    * @param res
104    *          DOCUMENT ME!
105    */
106   void parseResult(Sequence seq, String res)
107   {
108     StringTokenizer st = new StringTokenizer(res, "\n");
109     String data;
110     String id2;
111     int maxFound = 90;
112     StringBuffer buffer = new StringBuffer("\n\n" + seq.getName() + " :");
113
114     while (st.hasMoreTokens())
115     {
116       data = st.nextToken();
117
118       if (data.indexOf(">UNIPROT") > -1)
119       {
120         int index = data.indexOf(">UNIPROT") + 9;
121         id2 = data.substring(index, data.indexOf(" ", index));
122
123         boolean identitiesFound = false;
124         while (!identitiesFound)
125         {
126           data = st.nextToken();
127
128           if (data.indexOf("Identities") > -1)
129           {
130             identitiesFound = true;
131
132             int value = Integer.parseInt(data.substring(
133                     data.indexOf("(") + 1, data.indexOf("%")));
134
135             if (value >= maxFound)
136             {
137               maxFound = value;
138               buffer.append(" " + id2 + " " + value + "%; ");
139               suggestedIds.addElement(new Object[] { seq, id2 });
140             }
141           }
142         }
143       }
144     }
145
146     output.appendText(buffer.toString());
147   }
148
149   void updateIds()
150   {
151     // This must be outside the run() body as java 1.5
152     // will not return any value from the OptionPane to the expired thread.
153     int reply = JOptionPane.showConfirmDialog(Desktop.desktop,
154             "Automatically update suggested ids?",
155             "Auto replace sequence ids", JOptionPane.YES_NO_OPTION);
156
157     if (reply == JOptionPane.YES_OPTION)
158     {
159       Enumeration keys = suggestedIds.elements();
160       while (keys.hasMoreElements())
161       {
162         Object[] object = (Object[]) keys.nextElement();
163
164         Sequence oldseq = (Sequence) object[0];
165
166         oldseq.setName(object[1].toString());
167
168         // Oldseq is actually in the dataset, we must find the
169         // Visible seq and change its name also.
170         for (int i = 0; i < al.getHeight(); i++)
171         {
172           if (al.getSequenceAt(i).getDatasetSequence() == oldseq)
173           {
174             al.getSequenceAt(i).setName(oldseq.getName());
175             break;
176           }
177         }
178
179         DBRefEntry[] entries = oldseq.getDBRef();
180         if (entries != null)
181         {
182           oldseq.addDBRef(new jalview.datamodel.DBRefEntry(
183                   jalview.datamodel.DBRefSource.UNIPROT, "0", entries[0]
184                           .getAccessionId()));
185         }
186       }
187     }
188     ap.paintAlignment(true);
189
190   }
191
192   class ImageTwirler extends Thread
193   {
194     ImageIcon[] imageIcon;
195
196     int imageIndex = 0;
197
198     public ImageTwirler()
199     {
200       imageIcon = new ImageIcon[9];
201
202       for (int i = 0; i < 9; i++)
203       {
204         java.net.URL url = getClass().getResource(
205                 "/images/dna" + (i + 1) + ".gif");
206
207         if (url != null)
208         {
209           imageIcon[i] = new ImageIcon(url);
210         }
211       }
212     }
213
214     public void run()
215     {
216       while (jobsRunning > 0)
217       {
218         try
219         {
220           Thread.sleep(100);
221           imageIndex++;
222           imageIndex %= 9;
223           output.setFrameIcon(imageIcon[imageIndex]);
224           output.setTitle(MessageManager.formatMessage(
225                   "label.blasting_for_unidentified_sequence_jobs_running",
226                   new String[] { Integer.valueOf(jobsRunning).toString() }));
227         } catch (Exception ex)
228         {
229         }
230       }
231
232       if (jobsRunning == 0)
233       {
234         updateIds();
235       }
236     }
237   }
238
239   class BlastThread extends Thread
240   {
241     Sequence sequence;
242
243     String jobid;
244
245     boolean jobComplete = false;
246
247     BlastThread(Sequence sequence)
248     {
249       System.out.println("blasting for: " + sequence.getName());
250       this.sequence = sequence;
251     }
252
253     public void run()
254     {
255       StartJob();
256
257       while (!jobComplete)
258       {
259         try
260         {
261           WSWUBlastService service = new WSWUBlastServiceLocator();
262           WSWUBlast wublast = service.getWSWUBlast();
263           WSFile[] results = wublast.getResults(jobid);
264
265           if (results != null)
266           {
267             String result = new String(wublast.poll(jobid, "tooloutput"));
268             parseResult(sequence, result);
269             jobComplete = true;
270             jobsRunning--;
271           }
272           else
273           {
274             Thread.sleep(10000);
275             System.out.println("WSWuBlastClient: I'm alive "
276                     + sequence.getName() + " " + jobid); // log.debug
277           }
278         } catch (Exception ex)
279         {
280         }
281       }
282     }
283
284     void StartJob()
285     {
286       InputParams params = new InputParams();
287
288       params.setProgram("blastp");
289       params.setDatabase("uniprot");
290       params.setMatrix("pam10");
291
292       params.setNumal(5);
293       params.setSensitivity("low");
294       params.setSort("totalscore");
295       params.setOutformat("txt");
296       params.setAsync(true);
297
298       try
299       {
300         Data inputs[] = new Data[1];
301         Data input = new Data();
302         input.setType("sequence");
303         input.setContent(AlignSeq.extractGaps("-. ",
304                 sequence.getSequenceAsString()));
305         inputs[0] = input;
306
307         WSWUBlastService service = new WSWUBlastServiceLocator();
308         WSWUBlast wublast = service.getWSWUBlast();
309         jobid = wublast.runWUBlast(params, inputs);
310       } catch (Exception exp)
311       {
312         jobComplete = true;
313         jobsRunning--;
314         System.err.println("WSWUBlastClient error:\n" + exp.toString());
315         exp.printStackTrace();
316       }
317     }
318   }
319 }