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