new class
[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 \r
10 public class WSWUBlastClient\r
11 {\r
12   CutAndPasteTransfer cap = new CutAndPasteTransfer(false);\r
13 \r
14   public WSWUBlastClient(AlignmentI al, ArrayList ids)\r
15   {\r
16     JInternalFrame frame = new JInternalFrame();\r
17     cap.formatForOutput();\r
18     frame.setContentPane(cap);\r
19     cap.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(frame, "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      }\r
36 \r
37   }\r
38 \r
39   class BlastThread extends Thread\r
40   {\r
41     String sequence;\r
42     String id;\r
43     BlastThread(String id, String sequence)\r
44     {\r
45       this.sequence = sequence;\r
46       this.id = id;\r
47     }\r
48 \r
49     public void run()\r
50     {\r
51       FindUniprotIDFromUnknownSequence(id, sequence);\r
52     }\r
53   }\r
54 \r
55   public void FindUniprotIDFromUnknownSequence (String id, String s)\r
56   {\r
57         HashMap params = new HashMap();\r
58         params.put("database", "uniprot");\r
59         params.put("sensitivity","low");\r
60         params.put("sort","totalscore");\r
61         params.put("matrix","pam10");\r
62         params.put("program","blastp");\r
63         params.put("alignments","5");\r
64         params.put("outformat","xml");\r
65         byte[] sequence = s.getBytes();\r
66 \r
67         try {\r
68             Call call = (Call) new Service().createCall();\r
69             call.setTargetEndpointAddress (new java.net.URL("http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
70             call.setOperationName(new QName("WSWUBlast", "doWUBlast"));\r
71 \r
72             String result = (String) call.invoke(new Object[] {params,sequence});\r
73             parseResult(id, result);\r
74             }\r
75         catch (Exception exp) {\r
76              System.err.println ("ERROR:\n" + exp.toString());\r
77             exp.printStackTrace();\r
78         }\r
79     }\r
80 \r
81     void parseResult(String id1, String res)\r
82     {\r
83       StringTokenizer st = new StringTokenizer(res, "\n");\r
84       String data;\r
85       String id2;\r
86       int maxFound = 90;\r
87       StringBuffer buffer = new StringBuffer("\n\n"+id1+" :");\r
88 \r
89       while( st.hasMoreTokens() )\r
90       {\r
91         data = st.nextToken();\r
92 \r
93         if(data.indexOf("database=\"uniprot\" id=")>-1)\r
94         {\r
95           int index =  data.indexOf("database=\"uniprot\" id=")+ 23;\r
96           id2 = data.substring( index, data.indexOf("\"", index) );\r
97           while( data.indexOf("</alignment>")==-1)\r
98           {\r
99             data = st.nextToken();\r
100             if(data.indexOf("<identity>")>-1)\r
101             {\r
102               int value = Integer.parseInt( data.substring(data.indexOf("<identity>")+10, data.indexOf("</identity>")));\r
103               if(value>=maxFound)\r
104               {\r
105                 maxFound = value;\r
106                 buffer.append(" "+ id2 + " " + value+"%; ");\r
107               }\r
108             }\r
109           }\r
110 \r
111         }\r
112 \r
113 \r
114       }\r
115 \r
116       cap.setText(cap.getText()+buffer.toString());\r
117     }\r
118 \r
119 }\r