REmove system.out
[jalview.git] / src / jalview / io / WSWUBlastClient.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.io;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import jalview.gui.*;\r
24 \r
25 import org.apache.axis.client.*;\r
26 \r
27 import java.util.*;\r
28 \r
29 import javax.swing.*;\r
30 \r
31 import javax.xml.namespace.QName;\r
32 \r
33 \r
34 /**\r
35  * DOCUMENT ME!\r
36  *\r
37  * @author $author$\r
38  * @version $Revision$\r
39  */\r
40 public class WSWUBlastClient\r
41 {\r
42     AlignmentPanel ap;\r
43     AlignmentI al;\r
44     CutAndPasteTransfer output = new CutAndPasteTransfer();\r
45     int jobsRunning = 0;\r
46 \r
47     Hashtable suggestedIds = new Hashtable();\r
48     /**\r
49      * Creates a new WSWUBlastClient object.\r
50      *\r
51      * @param al DOCUMENT ME!\r
52      * @param ids DOCUMENT ME!\r
53      */\r
54     public WSWUBlastClient(AlignmentPanel ap, AlignmentI al, ArrayList ids)\r
55     {\r
56         this.ap = ap;\r
57         this.al = al;\r
58         output.setText(\r
59             "To display sequence features an exact Uniprot id with 100% sequence identity match must be entered." +\r
60             "\nIn order to display these features, try changing the names of your sequences to the ids suggested below.");\r
61         Desktop.addInternalFrame(output,\r
62             "BLASTing for unidentified sequences ", 800, 300);\r
63 \r
64         for (int i = 0; i < ids.size(); i++)\r
65         {\r
66             SequenceI sequence = al.findName(ids.get(i).toString());\r
67             System.out.println(ids.get(i).toString());\r
68             StringBuffer nonGapped = new StringBuffer();\r
69 \r
70 \r
71             for (int n = 0; n < sequence.getSequence().length(); n++)\r
72             {\r
73                 if (!jalview.util.Comparison.isGap(sequence.getCharAt(n)))\r
74                 {\r
75                     nonGapped.append(sequence.getCharAt(n));\r
76                 }\r
77             }\r
78 \r
79             BlastThread thread = new BlastThread(ids.get(i).toString(),\r
80                     nonGapped.toString());\r
81             thread.start();\r
82             jobsRunning++;\r
83         }\r
84 \r
85         ImageTwirler thread = new ImageTwirler();\r
86         thread.start();\r
87     }\r
88 \r
89 \r
90     /**\r
91      * DOCUMENT ME!\r
92      *\r
93      * @param id1 DOCUMENT ME!\r
94      * @param res DOCUMENT ME!\r
95      */\r
96     void parseResult(String id1, String res)\r
97     {\r
98         StringTokenizer st = new StringTokenizer(res, "\n");\r
99         String data;\r
100         String id2;\r
101         int maxFound = 90;\r
102         StringBuffer buffer = new StringBuffer("\n\n" + id1 + " :");\r
103 \r
104         while (st.hasMoreTokens())\r
105         {\r
106             data = st.nextToken();\r
107 \r
108             if (data.indexOf(">UNIPROT") > -1)\r
109             {\r
110                 int index = data.indexOf(">UNIPROT") + 9;\r
111                 id2 = data.substring(index, data.indexOf(" ", index));\r
112 \r
113                 boolean identitiesFound = false;\r
114                 while (!identitiesFound)\r
115                 {\r
116                     data = st.nextToken();\r
117 \r
118                     if (data.indexOf("Identities") > -1)\r
119                     {\r
120                        identitiesFound = true;\r
121 \r
122                        int value = Integer.parseInt(data.substring(data.indexOf(\r
123                            "(") + 1,\r
124                                                                    data.indexOf("%")));\r
125 \r
126                         if (value >= maxFound)\r
127                         {\r
128                             maxFound = value;\r
129                             buffer.append(" " + id2 + " " + value + "%; ");\r
130                             if(!suggestedIds.containsKey(id1))\r
131                              {\r
132                                suggestedIds.put(id1, id2);\r
133                              }\r
134                         }\r
135                     }\r
136                 }\r
137             }\r
138         }\r
139 \r
140         output.appendText(buffer.toString());\r
141     }\r
142 \r
143     void updateIds()\r
144     {\r
145         // This must be outside the run() body as java 1.5\r
146      // will not return any value from the OptionPane to the expired thread.\r
147       int reply = JOptionPane.showConfirmDialog(\r
148           Desktop.desktop, "Automatically update suggested ids?",\r
149           "Auto replace sequence ids", JOptionPane.YES_NO_OPTION);\r
150 \r
151       if (reply == JOptionPane.YES_OPTION)\r
152       {\r
153         Enumeration keys = suggestedIds.keys();\r
154         while(keys.hasMoreElements())\r
155         {\r
156           String oldid = keys.nextElement().toString();\r
157           SequenceI sequence = al.findName(oldid);\r
158           sequence.setName( suggestedIds.get(oldid).toString() );\r
159 \r
160           sequence = sequence.getDatasetSequence();\r
161           if(sequence!=null)\r
162           {\r
163 \r
164             sequence.setName(suggestedIds.get(oldid).toString());\r
165 \r
166             Vector entries = sequence.getDBRef();\r
167             if(entries!=null)\r
168             {\r
169               DBRefEntry entry = (DBRefEntry) entries.elementAt(0);\r
170               sequence.addDBRef(new jalview.datamodel.DBRefEntry("UNIPROT",\r
171                   "0",\r
172                   entry.getAccessionId()));\r
173             }\r
174           }\r
175           System.out.println("replace "+oldid+" with "+suggestedIds.get(oldid));\r
176         }\r
177       }\r
178       ap.repaint();\r
179 \r
180     }\r
181 \r
182     class ImageTwirler extends Thread\r
183     {\r
184         ImageIcon[] imageIcon;\r
185         int imageIndex = 0;\r
186 \r
187         public ImageTwirler()\r
188         {\r
189             imageIcon = new ImageIcon[9];\r
190 \r
191             for (int i = 0; i < 9; i++)\r
192             {\r
193                 java.net.URL url = getClass().getResource("/images/dna" +\r
194                         (i + 1) + ".gif");\r
195 \r
196                 if (url != null)\r
197                 {\r
198                     imageIcon[i] = new ImageIcon(url);\r
199                 }\r
200             }\r
201         }\r
202 \r
203         public void run()\r
204         {\r
205             while (jobsRunning > 0)\r
206             {\r
207                 try\r
208                 {\r
209                     Thread.sleep(100);\r
210                     imageIndex++;\r
211                     imageIndex %= 9;\r
212                     output.setFrameIcon(imageIcon[imageIndex]);\r
213                     output.setTitle("BLASTing for unidentified sequences - " +\r
214                         jobsRunning + " jobs running.");\r
215                 }\r
216                 catch (Exception ex)\r
217                 {\r
218                 }\r
219             }\r
220 \r
221             if (jobsRunning == 0)\r
222             {\r
223               updateIds();\r
224             }\r
225         }\r
226     }\r
227 \r
228     class BlastThread extends Thread\r
229     {\r
230         String sequence;\r
231         String seqid;\r
232         String jobid;\r
233         boolean jobComplete = false;\r
234 \r
235         BlastThread(String id, String sequence)\r
236         {\r
237           System.out.println("blasting for: "+id);\r
238             this.sequence = sequence;\r
239             seqid = id;\r
240         }\r
241 \r
242         public void run()\r
243         {\r
244             StartJob();\r
245 \r
246             while (!jobComplete)\r
247             {\r
248                 try\r
249                 {\r
250                     Call call = (Call) new Service().createCall();\r
251                     call.setTargetEndpointAddress(new java.net.URL(\r
252                             "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
253                     call.setOperationName(new QName("WSWUBlast", "polljob"));\r
254 \r
255                     Object object = (String) call.invoke(new Object[]\r
256                             {\r
257                                 jobid, "xml"\r
258                             });\r
259 \r
260                     if(object instanceof String)\r
261                     {\r
262                       parseResult(seqid, (String)object);\r
263                       jobComplete = true;\r
264                       jobsRunning--;\r
265                     }\r
266 \r
267                     Thread.sleep(5000);\r
268 \r
269                     System.out.println("WSWuBlastClient: I'm alive "+seqid+" "+jobid); // log.debug\r
270                 }\r
271                 catch (Exception ex)\r
272                 {\r
273                 }\r
274             }\r
275         }\r
276 \r
277         void StartJob()\r
278         {\r
279             HashMap params = new HashMap();\r
280             params.put("database", "uniprot");\r
281             params.put("sensitivity", "low");\r
282             params.put("sort", "totalscore");\r
283             params.put("matrix", "pam10");\r
284             params.put("program", "blastp");\r
285             params.put("alignments", "5");\r
286             params.put("type", "xml");\r
287             params.put("async", "true");\r
288 \r
289             byte[] seqbytes = sequence.getBytes();\r
290 \r
291             try\r
292             {\r
293                 Call call = (Call) new Service().createCall();\r
294                 call.setTargetEndpointAddress(new java.net.URL(\r
295                         "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
296                 call.setOperationName(new QName("WSWUBlast", "doWUBlast"));\r
297 \r
298                 Object object = call.invoke(new Object[]\r
299                         {\r
300                             params, seqbytes\r
301                         });\r
302 \r
303                 if(object instanceof byte[])\r
304                   jobid = new String( (byte[])object);\r
305 \r
306                 else\r
307                 {\r
308                   jobComplete = true;\r
309                   jobsRunning--;\r
310                   parseResult(seqid, (String)object);\r
311                 }\r
312 \r
313             }\r
314             catch (Exception exp)\r
315             {\r
316                 System.err.println("WSWUBlastClient error:\n" + exp.toString());\r
317                 exp.printStackTrace();\r
318             }\r
319         }\r
320     }\r
321 }\r