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