Formatting changes
[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.awt.*;\r
28 \r
29 import java.util.*;\r
30 \r
31 import javax.swing.*;\r
32 \r
33 import javax.xml.namespace.QName;\r
34 \r
35 \r
36 /**\r
37  * DOCUMENT ME!\r
38  *\r
39  * @author $author$\r
40  * @version $Revision$\r
41  */\r
42 public class WSWUBlastClient\r
43 {\r
44     CutAndPasteTransfer output = new CutAndPasteTransfer();\r
45     int jobsRunning = 0;\r
46 \r
47     /**\r
48      * Creates a new WSWUBlastClient object.\r
49      *\r
50      * @param al DOCUMENT ME!\r
51      * @param ids DOCUMENT ME!\r
52      */\r
53     public WSWUBlastClient(AlignmentI al, ArrayList ids)\r
54     {\r
55         output.setText(\r
56             "To display sequence features an exact Uniprot id with 100% sequence identity match must be entered." +\r
57             "\nIn order to display these features, try changing the names of your sequences to the ids suggested below.");\r
58         Desktop.addInternalFrame(output,\r
59             "BLASTing for unidentified sequences ", 800, 300);\r
60 \r
61         for (int i = 0; i < ids.size(); i++)\r
62         {\r
63             SequenceI sequence = al.findName(ids.get(i).toString());\r
64             StringBuffer nonGapped = new StringBuffer();\r
65 \r
66             for (int n = 0; n < sequence.getSequence().length(); n++)\r
67             {\r
68                 if (!jalview.util.Comparison.isGap(sequence.getCharAt(n)))\r
69                 {\r
70                     nonGapped.append(sequence.getCharAt(n));\r
71                 }\r
72             }\r
73 \r
74             BlastThread thread = new BlastThread(ids.get(i).toString(),\r
75                     nonGapped.toString());\r
76             thread.start();\r
77             jobsRunning++;\r
78         }\r
79 \r
80         ImageTwirler thread = new ImageTwirler();\r
81         thread.start();\r
82     }\r
83 \r
84     /**\r
85      * DOCUMENT ME!\r
86      *\r
87      * @param id1 DOCUMENT ME!\r
88      * @param res DOCUMENT ME!\r
89      */\r
90     void parseResult(String id1, String res)\r
91     {\r
92         StringTokenizer st = new StringTokenizer(res, "\n");\r
93         String data;\r
94         String id2;\r
95         int maxFound = 90;\r
96         StringBuffer buffer = new StringBuffer("\n\n" + id1 + " :");\r
97 \r
98         while (st.hasMoreTokens())\r
99         {\r
100             data = st.nextToken();\r
101 \r
102             if (data.indexOf("database=\"uniprot\" id=") > -1)\r
103             {\r
104                 int index = data.indexOf("database=\"uniprot\" id=") + 23;\r
105                 id2 = data.substring(index, data.indexOf("\"", index));\r
106 \r
107                 while (data.indexOf("</alignment>") == -1)\r
108                 {\r
109                     data = st.nextToken();\r
110 \r
111                     if (data.indexOf("<identity>") > -1)\r
112                     {\r
113                         int value = Integer.parseInt(data.substring(data.indexOf(\r
114                                         "<identity>") + 10,\r
115                                     data.indexOf("</identity>")));\r
116 \r
117                         if (value >= maxFound)\r
118                         {\r
119                             maxFound = value;\r
120                             buffer.append(" " + id2 + " " + value + "%; ");\r
121                         }\r
122                     }\r
123                 }\r
124             }\r
125         }\r
126 \r
127         output.setText(output.getText() + buffer.toString());\r
128     }\r
129 \r
130     class ImageTwirler extends Thread\r
131     {\r
132         ImageIcon[] imageIcon;\r
133         int imageIndex = 0;\r
134 \r
135         public ImageTwirler()\r
136         {\r
137             imageIcon = new ImageIcon[9];\r
138 \r
139             for (int i = 0; i < 9; i++)\r
140             {\r
141                 java.net.URL url = getClass().getResource("/images/dna" +\r
142                         (i + 1) + ".gif");\r
143 \r
144                 if (url != null)\r
145                 {\r
146                     imageIcon[i] = new ImageIcon(url);\r
147                 }\r
148             }\r
149         }\r
150 \r
151         public void run()\r
152         {\r
153             while (jobsRunning > 0)\r
154             {\r
155                 try\r
156                 {\r
157                     Thread.sleep(100);\r
158                     imageIndex++;\r
159                     imageIndex %= 9;\r
160                     output.setFrameIcon(imageIcon[imageIndex]);\r
161                     output.setTitle("BLASTing for unidentified sequences - " +\r
162                         jobsRunning + " jobs running.");\r
163                 }\r
164                 catch (Exception ex)\r
165                 {\r
166                 }\r
167             }\r
168         }\r
169     }\r
170 \r
171     class BlastThread extends Thread\r
172     {\r
173         String sequence;\r
174         String seqid;\r
175         String jobid;\r
176         boolean jobComplete = false;\r
177 \r
178         BlastThread(String id, String sequence)\r
179         {\r
180             this.sequence = sequence;\r
181             seqid = id;\r
182         }\r
183 \r
184         public void run()\r
185         {\r
186             StartJob();\r
187 \r
188             while (!jobComplete)\r
189             {\r
190                 try\r
191                 {\r
192                     Call call = (Call) new Service().createCall();\r
193                     call.setTargetEndpointAddress(new java.net.URL(\r
194                             "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
195                     call.setOperationName(new QName("WSWUBlast", "polljob"));\r
196 \r
197                     String result = (String) call.invoke(new Object[]\r
198                             {\r
199                                 jobid, "xml"\r
200                             });\r
201 \r
202                     if ((result.indexOf("JOB PENDING") == -1) &&\r
203                             (result.indexOf("JOB RUNNING") == -1))\r
204                     {\r
205                         parseResult(seqid, result);\r
206                         jobComplete = true;\r
207                         jobsRunning--;\r
208                     }\r
209 \r
210                     Thread.sleep(5000);\r
211 \r
212                     // System.out.println("WSWuBlastClient: I'm alive "+seqid+" "+jobid); // log.debug\r
213                 }\r
214                 catch (Exception ex)\r
215                 {\r
216                 }\r
217             }\r
218         }\r
219 \r
220         void StartJob()\r
221         {\r
222             HashMap params = new HashMap();\r
223             params.put("database", "uniprot");\r
224             params.put("sensitivity", "low");\r
225             params.put("sort", "totalscore");\r
226             params.put("matrix", "pam10");\r
227             params.put("program", "blastp");\r
228             params.put("alignments", "5");\r
229             params.put("outformat", "xml");\r
230             params.put("searchtype", "1");\r
231 \r
232             byte[] seqbytes = sequence.getBytes();\r
233 \r
234             try\r
235             {\r
236                 Call call = (Call) new Service().createCall();\r
237                 call.setTargetEndpointAddress(new java.net.URL(\r
238                         "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
239                 call.setOperationName(new QName("WSWUBlast", "doWUBlast"));\r
240 \r
241                 String result = (String) call.invoke(new Object[]\r
242                         {\r
243                             params, seqbytes\r
244                         });\r
245                 jobid = result;\r
246                 System.out.println(\r
247                     "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast JobId '" +\r
248                     jobid + "'");\r
249             }\r
250             catch (Exception exp)\r
251             {\r
252                 System.err.println("WSWUBlastClient error:\n" + exp.toString());\r
253                 exp.printStackTrace();\r
254             }\r
255         }\r
256     }\r
257 }\r