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