00ff54512445938bbc30118f44b23cfdb910a690
[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 \r
15 public class JPredClient\r
16 {\r
17 \r
18   JInternalFrame outputFrame = new JInternalFrame();\r
19   CutAndPasteTransfer output = new CutAndPasteTransfer(false);\r
20   int jobsRunning = 0;\r
21   ext.vamsas.JpredSoapBindingStub server;\r
22 \r
23   public JPredClient(SequenceI[] msf)\r
24   {\r
25     SequenceI seq = msf[0];\r
26     output.formatForOutput();\r
27     outputFrame.setContentPane(output);\r
28 \r
29     output.setText("Job details for MSA based prediction on sequence :\nName : "\r
30                    + seq.getName() + "\nSequence : "\r
31                    + AlignSeq.extractGaps("-. ",seq.getSequence()) + "\n");\r
32     Desktop.addInternalFrame(outputFrame,\r
33                              "JNet secondary structure prediction job", 800,\r
34                              300);\r
35 \r
36     JPredWSServiceLocator loc = new JPredWSServiceLocator(); // Default\r
37     try {\r
38       this.server = (JpredSoapBindingStub) loc.getjpred(); // JBPNote will be set from properties\r
39     }\r
40     catch (Exception ex) {\r
41       output.setText("Serious! JPred Service location failed\nfor URL :"\r
42                      +loc.getjpredAddress()+"\n"+ex.getMessage());\r
43     }\r
44 \r
45     JPredThread jthread = new JPredThread(msf);\r
46     jthread.start();\r
47     ImageTwirler thread = new ImageTwirler();\r
48     thread.start();\r
49   }\r
50 \r
51   public JPredClient(SequenceI seq)\r
52   {\r
53     output.formatForOutput();\r
54     outputFrame.setContentPane(output);\r
55 \r
56     output.setText("Job details for prediction on sequence :\nName : "\r
57                    + seq.getName() + "\nSequence : " + AlignSeq.extractGaps("-. ",seq.getSequence()) + "\n");\r
58     Desktop.addInternalFrame(outputFrame,\r
59                              "JNet secondary structure prediction job", 800,\r
60                              300);\r
61 \r
62     JPredWSServiceLocator loc = new JPredWSServiceLocator(); // Default\r
63     try {\r
64       this.server = (JpredSoapBindingStub) loc.getjpred(); // JBPNote will be set from properties\r
65     }\r
66     catch (Exception ex) {\r
67       output.setText("Serious! JPred Service location failed\nfor URL :"\r
68                      +loc.getjpredAddress()+"\n"+ex.getMessage());\r
69     }\r
70 \r
71     JPredThread jthread = new JPredThread(seq);\r
72     jthread.start();\r
73     ImageTwirler thread = new ImageTwirler();\r
74     thread.start();\r
75   }\r
76 \r
77   class ImageTwirler\r
78       extends Thread\r
79   {\r
80     ImageIcon[] imageIcon;\r
81     int imageIndex = 0;\r
82     public ImageTwirler()\r
83     {\r
84       imageIcon = new ImageIcon[9];\r
85       for (int i = 0; i < 9; i++)\r
86       {\r
87         java.net.URL url = getClass().getResource("/images/dna" + (i + 1) +\r
88                                                   ".gif");\r
89         if (url != null)\r
90           imageIcon[i] = new ImageIcon(url);\r
91       }\r
92     }\r
93 \r
94     public void run()\r
95     {\r
96       while (jobsRunning > 0)\r
97       {\r
98         try\r
99         {\r
100           Thread.sleep(100);\r
101           imageIndex++;\r
102           imageIndex %= 9;\r
103           outputFrame.setFrameIcon(imageIcon[imageIndex]);\r
104           outputFrame.setTitle("JNet Secondary Structure Prediction : " +\r
105                                jobsRunning + " jobs running.");\r
106 \r
107         }\r
108         catch (Exception ex)\r
109         {}\r
110 \r
111       }\r
112     }\r
113   }\r
114 \r
115   class JPredThread\r
116       extends Thread\r
117   {\r
118     String OutputHeader;\r
119     ext.vamsas.JpredResult result;\r
120     ext.vamsas.Sequence sequence;\r
121     ext.vamsas.Msfalignment msa;\r
122     String jobId;\r
123     boolean jobComplete = false;\r
124     int allowedServerExceptions = 3; // thread dies if too many exceptions.\r
125     JPredThread(SequenceI seq)\r
126     {\r
127       OutputHeader = output.getText();\r
128       this.sequence = new ext.vamsas.Sequence();\r
129       this.sequence.setId(seq.getName());\r
130       this.sequence.setSeq(AlignSeq.extractGaps("-. ",seq.getSequence()));\r
131     }\r
132 \r
133     JPredThread(SequenceI[] msf)\r
134     {\r
135       OutputHeader = output.getText();\r
136       this.sequence = new ext.vamsas.Sequence();\r
137       this.sequence.setId(msf[0].getName());\r
138       this.sequence.setSeq(AlignSeq.extractGaps("-. ",msf[0].getSequence()));\r
139       jalview.io.MSFfile mwrite = new jalview.io.MSFfile();\r
140       this.msa = new ext.vamsas.Msfalignment();\r
141       msa.setMsf(mwrite.print(msf));\r
142     }\r
143 \r
144 \r
145     public void run()\r
146     {\r
147 \r
148       StartJob();\r
149 \r
150       while (!jobComplete\r
151              && (allowedServerExceptions > 0))\r
152       {\r
153         try\r
154         {\r
155           result = server.getresult(jobId);\r
156 \r
157           if (result.isFinished())\r
158           {\r
159             parseResult();\r
160             jobComplete = true;\r
161             jobsRunning--;\r
162           }\r
163           else\r
164           {\r
165             output.setText(OutputHeader + "\n" + result.getStatus());\r
166             if (! (result.isJobFailed() || result.isServerError()))\r
167             {\r
168               Thread.sleep(5000);\r
169               //      System.out.println("I'm alive "+seqid+" "+jobid);\r
170             }\r
171           }\r
172         }\r
173         catch (Exception ex)\r
174         {\r
175           allowedServerExceptions--;\r
176           String msg = output.getText();\r
177           output.setText(msg + "\nJPredWS Server exception!\n" + ex.getMessage());\r
178         }\r
179       }\r
180     }\r
181 \r
182     void StartJob()\r
183     {\r
184       try\r
185       {\r
186         if (msa!=null)  {\r
187           jobId = server.predictOnMsa(msa);\r
188         } else {\r
189           jobId = server.predict(sequence);\r
190         }\r
191         System.out.println(jobId);\r
192       }\r
193       catch (Exception e)\r
194       {\r
195         System.out.println("JPredWS Client: Failed to submit the prediction\n" +\r
196                            e.toString() + "\n");\r
197         e.printStackTrace();\r
198       }\r
199     }\r
200 \r
201     private void addFloatAnnotations(Alignment al, int[] gapmap, Vector values, String Symname, String Visname, float min, float max, int winLength) {\r
202 \r
203       Annotation[] annotations = new Annotation[al.getWidth()];\r
204       for (int j = 0; j < values.size(); j++)\r
205       {\r
206         float value = Float.parseFloat(values.get(j).toString());\r
207         annotations[gapmap[j]] = new Annotation("", value+"",' ',value);\r
208       }\r
209       al.addAnnotation(new AlignmentAnnotation(Symname, Visname, annotations, min, max, winLength));\r
210     }\r
211 \r
212     void parseResult()\r
213     {\r
214       // OutputHeader = output.getText();\r
215       if (result.getStatus()!=null) {\r
216         OutputHeader += "\n"+result.getStatus();\r
217       }\r
218       if (result.getPredfile()!=null) {\r
219         OutputHeader += "\n"+result.getPredfile();\r
220       // JBPNote The returned files from a webservice could be hidden behind icons in the monitor window that, when clicked, pop up their corresponding data\r
221       }\r
222       if (result.getAligfile()!=null) {\r
223         OutputHeader += "\n"+result.getAligfile();\r
224       }\r
225       output.setText(OutputHeader);\r
226       try {\r
227         // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt", "File");\r
228         jalview.io.JPredFile prediction = new jalview.io.JPredFile(result.getPredfile(), "Paste");\r
229         SequenceI[] preds = prediction.getSeqsAsArray();\r
230         Alignment al;\r
231         int FirstSeq; // the position of the query sequence in Alignment al\r
232         boolean noMsa = true; // set if no MSA has been returned by JPred\r
233 \r
234         if (this.msa!=null && result.getAligfile()!=null) {\r
235           // we ignore the returned alignment if we only predicted on a single sequence\r
236           String format = jalview.io.IdentifyFile.Identify(result.getAligfile(), "Paste");\r
237           if (jalview.io.FormatProperties.contains(format))\r
238           {\r
239             al = new Alignment(jalview.io.FormatAdapter.read(result.getAligfile(),"Paste",format));\r
240             noMsa = false;\r
241             FirstSeq = 0;\r
242           }\r
243           else\r
244           {\r
245             throw (new Exception("Unknown format 'format' for file : \n" +\r
246                              result.getAligfile()));\r
247           }\r
248 \r
249         } else {\r
250           al = new Alignment(preds);\r
251           FirstSeq = prediction.QuerySeqPosition;\r
252         }\r
253 \r
254         AlignmentAnnotation annot;\r
255         Annotation [] annotations = null;\r
256         int i = 0;\r
257         int width = preds[0].getSequence().length();\r
258 \r
259 \r
260         int[] gapmap = al.getSequenceAt(FirstSeq).gapMap();\r
261         if (gapmap.length!=width) {\r
262           throw (new Exception("Jpred Client Error\nNumber of residues in supposed query sequence :\n"\r
263                                +al.getSequenceAt(FirstSeq).getName()+"\n"\r
264                                +al.getSequenceAt(FirstSeq).getSequence()\r
265                                +"\nDiffer from number of prediction sites in \n"+result.getPredfile()+"\n"));\r
266         }\r
267         // JBPNote Should also rename the query sequence sometime...\r
268         i=0;\r
269         while (i < preds.length)\r
270         {\r
271           String id = preds[i].getName().toUpperCase();\r
272           if(id.startsWith("LUPAS") || id.startsWith("JNET") || id.startsWith("JPRED"))\r
273           {\r
274             annotations = new Annotation[al.getWidth()];\r
275 \r
276             if(id.equals("JNETPRED")\r
277                || id.equals("JNETPSSM")\r
278                || id.equals("JNETFREQ")\r
279                || id.equals("JNETHMM")\r
280                || id.equals("JNETALIGN")\r
281                || id.equals("JPRED"))\r
282             {\r
283               for (int j = 0; j < width; j++)\r
284                 annotations[gapmap[j]] = new Annotation("", "", preds[i].getCharAt(j), 0);\r
285             }\r
286             else if(id.equals("JNETCONF"))\r
287             {\r
288               for (int j = 0; j < width; j++)\r
289               {\r
290                 float value = Float.parseFloat(preds[i].getCharAt(j)+"");\r
291                 annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(j)+"", "",preds[i].getCharAt(j),value);\r
292               }\r
293             }\r
294             else\r
295             {\r
296               for (int j = 0; j < width; j++) {\r
297                 annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(j)+"", "", ' ', 0);\r
298               }\r
299             }\r
300 \r
301             if(id.equals("JNETCONF"))\r
302               annot = new AlignmentAnnotation(preds[i].getName(),\r
303                                            "JNet Output",\r
304                                            annotations,0f,10f,1);\r
305 \r
306             else   annot = new AlignmentAnnotation(preds[i].getName(),\r
307                                             "JNet Output",\r
308                                             annotations);\r
309             al.addAnnotation(annot);\r
310             if (noMsa)\r
311               al.deleteSequence(preds[i]);\r
312           }\r
313           i++;\r
314         }\r
315 \r
316         Hashtable scores = prediction.Scores;\r
317         addFloatAnnotations(al, gapmap,  (Vector)scores.get("JNETPROPH"),\r
318                             "JnetpropH", "Jnet Helix Propensity", 0f,1f,1);\r
319 \r
320         addFloatAnnotations(al, gapmap,  (Vector)scores.get("JNETPROPB"),\r
321                             "JnetpropB", "Jnet Beta Sheed Propensity", 0f,1f,1);\r
322 \r
323         addFloatAnnotations(al, gapmap,  (Vector)scores.get("JNETPROPC"),\r
324                             "JnetpropC", "Jnet Coil Propensity", 0f,1f,1);\r
325 \r
326         AlignFrame af = new AlignFrame(al);\r
327 \r
328 \r
329         Desktop.addInternalFrame(af,\r
330                                  "JNet Prediction for sequence ",\r
331                                  700, 500);\r
332       }catch(Exception ex){ex.printStackTrace();}\r
333 \r
334     }\r
335 \r
336   }\r
337 }\r
338 \r