use new file filter
[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(result.getOutput(), "Paste");\r
158         SequenceI[] preds = prediction.getSeqsAsArray();\r
159         int i=0;\r
160         // Rename the query sequence\r
161         while (i<preds.length && !preds[i].getName().equals("QUERY")) {\r
162           i++;\r
163         }\r
164         if (i<preds.length) {\r
165           preds[i].setName(sequence.getId());\r
166         }\r
167         AlignFrame af = new AlignFrame(new Alignment(preds));\r
168         /*  JBPNote - need to set correct style of display for the secstrpred alignment object.\r
169             af.clustalColour.setSelected(true);\r
170             af.clustalColour_actionPerformed(null);\r
171          */\r
172         Desktop.addInternalFrame(af, "JNet Prediction for sequence "+sequence.getId(),\r
173                                  700, 500);\r
174       }\r
175       catch (Exception e) {\r
176         output.setText(output.getText()+"\nError! File did not parse correctly\n"+e.getMessage()+"\n");\r
177         e.printStackTrace();\r
178       }\r
179 \r
180     }\r
181 \r
182   }\r
183 }\r