vamsasDemo new branch
[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             StringBuffer nonGapped = new StringBuffer();\r
68 \r
69 \r
70             for (int n = 0; n < sequence.getSequence().length(); n++)\r
71             {\r
72                 if (!jalview.util.Comparison.isGap(sequence.getCharAt(n)))\r
73                 {\r
74                     nonGapped.append(sequence.getCharAt(n));\r
75                 }\r
76             }\r
77 \r
78             BlastThread thread = new BlastThread(ids.get(i).toString(),\r
79                     nonGapped.toString());\r
80             thread.start();\r
81             jobsRunning++;\r
82         }\r
83 \r
84         ImageTwirler thread = new ImageTwirler();\r
85         thread.start();\r
86     }\r
87 \r
88 \r
89     /**\r
90      * DOCUMENT ME!\r
91      *\r
92      * @param id1 DOCUMENT ME!\r
93      * @param res DOCUMENT ME!\r
94      */\r
95     void parseResult(String id1, String res)\r
96     {\r
97         StringTokenizer st = new StringTokenizer(res, "\n");\r
98         String data;\r
99         String id2;\r
100         int maxFound = 90;\r
101         StringBuffer buffer = new StringBuffer("\n\n" + id1 + " :");\r
102 \r
103         while (st.hasMoreTokens())\r
104         {\r
105             data = st.nextToken();\r
106 \r
107             if (data.indexOf(">UNIPROT") > -1)\r
108             {\r
109                 int index = data.indexOf(">UNIPROT") + 9;\r
110                 id2 = data.substring(index, data.indexOf(" ", index));\r
111 \r
112                 boolean identitiesFound = false;\r
113                 while (!identitiesFound)\r
114                 {\r
115                     data = st.nextToken();\r
116 \r
117                     if (data.indexOf("Identities") > -1)\r
118                     {\r
119                        identitiesFound = true;\r
120 \r
121                        int value = Integer.parseInt(data.substring(data.indexOf(\r
122                            "(") + 1,\r
123                                                                    data.indexOf("%")));\r
124 \r
125                         if (value >= maxFound)\r
126                         {\r
127                             maxFound = value;\r
128                             buffer.append(" " + id2 + " " + value + "%; ");\r
129                             suggestedIds.put(id1, id2);\r
130                         }\r
131                     }\r
132                 }\r
133             }\r
134         }\r
135 \r
136         output.appendText(buffer.toString());\r
137     }\r
138 \r
139     void updateIds()\r
140     {\r
141 \r
142     }\r
143 \r
144     class ImageTwirler extends Thread\r
145     {\r
146         ImageIcon[] imageIcon;\r
147         int imageIndex = 0;\r
148 \r
149         public ImageTwirler()\r
150         {\r
151             imageIcon = new ImageIcon[9];\r
152 \r
153             for (int i = 0; i < 9; i++)\r
154             {\r
155                 java.net.URL url = getClass().getResource("/images/dna" +\r
156                         (i + 1) + ".gif");\r
157 \r
158                 if (url != null)\r
159                 {\r
160                     imageIcon[i] = new ImageIcon(url);\r
161                 }\r
162             }\r
163         }\r
164 \r
165         public void run()\r
166         {\r
167             while (jobsRunning > 0)\r
168             {\r
169                 try\r
170                 {\r
171                     Thread.sleep(100);\r
172                     imageIndex++;\r
173                     imageIndex %= 9;\r
174                     output.setFrameIcon(imageIcon[imageIndex]);\r
175                     output.setTitle("BLASTing for unidentified sequences - " +\r
176                         jobsRunning + " jobs running.");\r
177                 }\r
178                 catch (Exception ex)\r
179                 {\r
180                 }\r
181             }\r
182 \r
183             if (jobsRunning == 0)\r
184             {\r
185               int reply = JOptionPane.showConfirmDialog(\r
186                   Desktop.desktop, "Automatically update suggested ids?",\r
187                   "Auto replace sequence ids", JOptionPane.YES_NO_OPTION);\r
188 \r
189               if (reply == JOptionPane.YES_OPTION)\r
190               {\r
191                 Enumeration keys = suggestedIds.keys();\r
192                 while(keys.hasMoreElements())\r
193                 {\r
194                   String oldid = keys.nextElement().toString();\r
195                   SequenceI sequence = al.findName(oldid);\r
196                   sequence.setName( suggestedIds.get(oldid).toString() );\r
197 \r
198                   sequence = al.getDataset().findName(oldid);\r
199                   Vector entries = sequence.getDBRef();\r
200                   DBRefEntry entry = (DBRefEntry) entries.elementAt(0);\r
201                   sequence.addDBRef(new jalview.datamodel.DBRefEntry("UNIPROT",\r
202                       "0",\r
203                       entry.getAccessionId()));\r
204 \r
205                   sequence.setName(suggestedIds.get(oldid).toString());\r
206 \r
207                   System.out.println("replace "+oldid+" with "+suggestedIds.get(oldid));\r
208                 }\r
209               }\r
210               ap.repaint();\r
211             }\r
212         }\r
213     }\r
214 \r
215     class BlastThread extends Thread\r
216     {\r
217         String sequence;\r
218         String seqid;\r
219         String jobid;\r
220         boolean jobComplete = false;\r
221 \r
222         BlastThread(String id, String sequence)\r
223         {\r
224           System.out.println("blasting for: "+id);\r
225             this.sequence = sequence;\r
226             seqid = id;\r
227         }\r
228 \r
229         public void run()\r
230         {\r
231             StartJob();\r
232 \r
233             while (!jobComplete)\r
234             {\r
235                 try\r
236                 {\r
237                     Call call = (Call) new Service().createCall();\r
238                     call.setTargetEndpointAddress(new java.net.URL(\r
239                             "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
240                     call.setOperationName(new QName("WSWUBlast", "polljob"));\r
241 \r
242                     Object object = (String) call.invoke(new Object[]\r
243                             {\r
244                                 jobid, "xml"\r
245                             });\r
246 \r
247                     if(object instanceof String)\r
248                     {\r
249                       parseResult(seqid, (String)object);\r
250                       jobComplete = true;\r
251                       jobsRunning--;\r
252                     }\r
253 \r
254                     Thread.sleep(5000);\r
255 \r
256                     System.out.println("WSWuBlastClient: I'm alive "+seqid+" "+jobid); // log.debug\r
257                 }\r
258                 catch (Exception ex)\r
259                 {\r
260                 }\r
261             }\r
262         }\r
263 \r
264         void StartJob()\r
265         {\r
266             HashMap params = new HashMap();\r
267             params.put("database", "uniprot");\r
268             params.put("sensitivity", "low");\r
269             params.put("sort", "totalscore");\r
270             params.put("matrix", "pam10");\r
271             params.put("program", "blastp");\r
272             params.put("alignments", "5");\r
273             params.put("type", "xml");\r
274             params.put("async", "true");\r
275 \r
276             byte[] seqbytes = sequence.getBytes();\r
277 \r
278             try\r
279             {\r
280                 Call call = (Call) new Service().createCall();\r
281                 call.setTargetEndpointAddress(new java.net.URL(\r
282                         "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
283                 call.setOperationName(new QName("WSWUBlast", "doWUBlast"));\r
284 \r
285                 Object object = call.invoke(new Object[]\r
286                         {\r
287                             params, seqbytes\r
288                         });\r
289 \r
290                 if(object instanceof byte[])\r
291                   jobid = new String( (byte[])object);\r
292 \r
293                 else\r
294                 {\r
295                   jobComplete = true;\r
296                   jobsRunning--;\r
297                   parseResult(seqid, (String)object);\r
298                 }\r
299 \r
300             }\r
301             catch (Exception exp)\r
302             {\r
303                 System.err.println("WSWUBlastClient error:\n" + exp.toString());\r
304                 exp.printStackTrace();\r
305             }\r
306         }\r
307     }\r
308 }\r