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