7c2675d2f9ced7b757bc214ab6c822df578c8dd3
[jalview.git] / src / jalview / io / JPredClient.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 import jalview.analysis.AlignSeq;\r
12 import ext.vamsas.*;\r
13 \r
14 public class JPredClient\r
15 {\r
16 \r
17   JInternalFrame outputFrame = new JInternalFrame();\r
18   CutAndPasteTransfer output = new CutAndPasteTransfer(false);\r
19   int jobsRunning = 0;\r
20   ext.vamsas.JpredSoapBindingStub server;\r
21 \r
22   public JPredClient(SequenceI seq)\r
23   {\r
24     output.formatForOutput();\r
25     outputFrame.setContentPane(output);\r
26 \r
27     output.setText("Job details for prediction on sequence :\nName : "\r
28                    + seq.getName() + "\nSequence : " + seq.getSequence() + "\n");\r
29     Desktop.addInternalFrame(outputFrame,\r
30                              "JNet secondary structure prediction job", 800,\r
31                              300);\r
32 \r
33     JPredWSServiceLocator loc = new JPredWSServiceLocator(); // Default\r
34     try {\r
35       this.server = (JpredSoapBindingStub) loc.getjpred(); // JBPNote will be set from properties\r
36     }\r
37     catch (Exception ex) {\r
38       output.setText("Serious! JPred Service location failed\nfor URL :"\r
39                      +loc.getjpredAddress()+"\n"+ex.getMessage());\r
40     }\r
41 \r
42     JPredThread jthread = new JPredThread(seq);\r
43     jthread.start();\r
44     ImageTwirler thread = new ImageTwirler();\r
45     thread.start();\r
46   }\r
47 \r
48   class ImageTwirler\r
49       extends Thread\r
50   {\r
51     ImageIcon[] imageIcon;\r
52     int imageIndex = 0;\r
53     public ImageTwirler()\r
54     {\r
55       imageIcon = new ImageIcon[9];\r
56       for (int i = 0; i < 9; i++)\r
57       {\r
58         java.net.URL url = getClass().getResource("/images/dna" + (i + 1) +\r
59                                                   ".gif");\r
60         if (url != null)\r
61           imageIcon[i] = new ImageIcon(url);\r
62       }\r
63     }\r
64 \r
65     public void run()\r
66     {\r
67       while (jobsRunning > 0)\r
68       {\r
69         try\r
70         {\r
71           Thread.sleep(100);\r
72           imageIndex++;\r
73           imageIndex %= 9;\r
74           outputFrame.setFrameIcon(imageIcon[imageIndex]);\r
75           outputFrame.setTitle("JNet Secondary Structure Prediction : " +\r
76                                jobsRunning + " jobs running.");\r
77 \r
78         }\r
79         catch (Exception ex)\r
80         {}\r
81 \r
82       }\r
83     }\r
84   }\r
85 \r
86   class JPredThread\r
87       extends Thread\r
88   {\r
89     String OutputHeader;\r
90     ext.vamsas.Secstructpred result;\r
91     ext.vamsas.Sequence sequence;\r
92     String jobId;\r
93     boolean jobComplete = false;\r
94     int allowedServerExceptions = 3; // thread dies if too many exceptions.\r
95     JPredThread(SequenceI seq)\r
96     {\r
97       OutputHeader = output.getText();\r
98       this.sequence = new ext.vamsas.Sequence();\r
99       this.sequence.setId(seq.getName());\r
100       this.sequence.setSeq(AlignSeq.extractGaps("-. ",seq.getSequence()));\r
101     }\r
102 \r
103     public void run()\r
104     {\r
105 \r
106       StartJob();\r
107 \r
108       while (!jobComplete\r
109              && (allowedServerExceptions > 0))\r
110       {\r
111         try\r
112         {\r
113           result = server.getpredict(jobId);\r
114 \r
115           if (result.getOutput().indexOf("Job") != 0)\r
116           {\r
117             parseResult();\r
118             jobComplete = true;\r
119             jobsRunning--;\r
120           }\r
121           else\r
122           {\r
123             output.setText(OutputHeader + "\n" + result.getOutput());\r
124           }\r
125           Thread.sleep(5000);\r
126           //      System.out.println("I'm alive "+seqid+" "+jobid);\r
127 \r
128         }\r
129         catch (Exception ex)\r
130         {\r
131           allowedServerExceptions--;\r
132           String msg = output.getText();\r
133           output.setText(msg + "\nJPredWS Server exception!\n" + ex.getMessage());\r
134         }\r
135       }\r
136     }\r
137 \r
138     void StartJob()\r
139     {\r
140       try\r
141       {\r
142         jobId = server.predict(sequence);\r
143         System.out.println(jobId);\r
144       }\r
145       catch (Exception e)\r
146       {\r
147         System.out.println("JPredWS Client: Failed to submit the prediction\n" +\r
148                            e.toString() + "\n");\r
149         e.printStackTrace();\r
150       }\r
151     }\r
152 \r
153     void parseResult()\r
154     {\r
155       output.setText(OutputHeader + result.getOutput());\r
156       try {\r
157         // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt", "File");\r
158         JPredFile prediction=new JPredFile(result.getOutput(), "Paste");\r
159         SequenceI[] preds = prediction.getSeqsAsArray();\r
160         Alignment al = new Alignment(preds);\r
161 \r
162         AlignmentAnnotation annot;\r
163         Annotation [] annotations = null;\r
164         int i = 0;\r
165         // Rename the query sequence\r
166         while (i < preds.length)\r
167         {\r
168           String id = preds[i].getName().toUpperCase();\r
169           if(id.startsWith("LUPAS") || id.startsWith("JNET"))\r
170           {\r
171             annotations = new Annotation[al.getWidth()];\r
172 \r
173             if(id.equals("JNETPRED")\r
174                || id.equals("JNETPSSM")\r
175                || id.equals("JNETFREQ")\r
176                || id.equals("JNETHMM")\r
177                || id.equals("JNETALIGN"))\r
178             {\r
179               for (int j = 0; j < al.getWidth(); j++)\r
180                 annotations[j] = new Annotation("", "", preds[i].getCharAt(j), 0);\r
181             }\r
182             else if(id.equals("JNETCONF"))\r
183             {\r
184               for (int j = 0; j < al.getWidth(); j++)\r
185               {\r
186                 float value = Float.parseFloat(preds[i].getCharAt(j)+"");\r
187                 annotations[j] = new Annotation(preds[i].getCharAt(j)+"", "",preds[i].getCharAt(j),value);\r
188               }\r
189             }\r
190             else\r
191             {\r
192               for (int j = 0; j < al.getWidth(); j++)\r
193                 annotations[j] = new Annotation(preds[i].getCharAt(j)+"", "", ' ', 0);\r
194             }\r
195 \r
196             if(id.equals("JNETCONF"))\r
197               annot = new AlignmentAnnotation(preds[i].getName(),\r
198                                            "Jpred Output",\r
199                                            annotations,0f,10f,1);\r
200 \r
201             else   annot = new AlignmentAnnotation(preds[i].getName(),\r
202                                             "Jpred Output",\r
203                                             annotations);\r
204             al.addAnnotation(annot);\r
205             al.deleteSequence(preds[i]);\r
206           }\r
207           i++;\r
208         }\r
209 \r
210         Hashtable scores = prediction.Scores;\r
211 \r
212         Vector values = (Vector)scores.get("JNETPROPH");\r
213         annotations = new Annotation[al.getWidth()];\r
214         for (int j = 0; j < al.getWidth(); j++)\r
215         {\r
216           float value = Float.parseFloat(values.get(j).toString());\r
217           annotations[j] = new Annotation("", value+"",' ',value);\r
218         }\r
219         annot = new AlignmentAnnotation("Jnetproph", "Jpred Output", annotations, 0f,1f,1);\r
220         al.addAnnotation(annot);\r
221 \r
222         values = (Vector)scores.get("JNETPROPB");\r
223              annotations = new Annotation[al.getWidth()];\r
224              for (int j = 0; j < al.getWidth(); j++)\r
225              {\r
226                float value = Float.parseFloat(values.get(j).toString());\r
227                annotations[j] = new Annotation("", value+"",' ',value);\r
228              }\r
229              annot = new AlignmentAnnotation("JnetpropB", "Jpred Output", annotations, 0f,1f,1);\r
230              al.addAnnotation(annot);\r
231 \r
232              values = (Vector)scores.get("JNETPROPC");\r
233                   annotations = new Annotation[al.getWidth()];\r
234                   for (int j = 0; j < al.getWidth(); j++)\r
235                   {\r
236                     float value = Float.parseFloat(values.get(j).toString());\r
237                     annotations[j] = new Annotation("", value+"",' ',value);\r
238                   }\r
239                   annot = new AlignmentAnnotation("JnetpropC", "Jpred Output", annotations, 0f,1f,1);\r
240                   al.addAnnotation(annot);\r
241 \r
242 \r
243 \r
244         AlignFrame af = new AlignFrame(al);\r
245 \r
246 \r
247         /*  JBPNote - need to set correct style of display for the secstrpred alignment object.\r
248             af.clustalColour.setSelected(true);\r
249             af.clustalColour_actionPerformed(null);\r
250          */\r
251         Desktop.addInternalFrame(af,\r
252                                  "JNet Prediction for sequence ",\r
253                                  700, 500);\r
254       }catch(Exception ex){ex.printStackTrace();}\r
255 \r
256     }\r
257 \r
258   }\r
259 }\r
260 \r