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