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