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