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