first stab at a JPredClient.
authorjprocter <Jim Procter>
Mon, 28 Feb 2005 10:43:43 +0000 (10:43 +0000)
committerjprocter <Jim Procter>
Mon, 28 Feb 2005 10:43:43 +0000 (10:43 +0000)
src/jalview/io/JPredClient.java [new file with mode: 0755]

diff --git a/src/jalview/io/JPredClient.java b/src/jalview/io/JPredClient.java
new file mode 100755 (executable)
index 0000000..c3415ee
--- /dev/null
@@ -0,0 +1,183 @@
+package jalview.io;\r
+\r
+import org.apache.axis.client.*;\r
+import javax.xml.namespace.QName;\r
+import java.util.*;\r
+import jalview.datamodel.*;\r
+import jalview.gui.*;\r
+import javax.swing.*;\r
+import java.util.*;\r
+import java.awt.*;\r
+import jalview.analysis.AlignSeq;\r
+import ext.vamsas.*;\r
+\r
+public class JPredClient\r
+{\r
+\r
+  JInternalFrame outputFrame = new JInternalFrame();\r
+  CutAndPasteTransfer output = new CutAndPasteTransfer(false);\r
+  int jobsRunning = 0;\r
+  ext.vamsas.JpredSoapBindingStub server;\r
+\r
+  public JPredClient(SequenceI seq)\r
+  {\r
+    output.formatForOutput();\r
+    outputFrame.setContentPane(output);\r
+\r
+    output.setText("Job details for prediction on sequence :\nName : "\r
+                   + seq.getName() + "\nSequence : " + seq.getSequence() + "\n");\r
+    Desktop.addInternalFrame(outputFrame,\r
+                             "JNet secondary structure prediction job", 800,\r
+                             300);\r
+\r
+    JPredWSServiceLocator loc = new JPredWSServiceLocator(); // Default\r
+    try {\r
+      this.server = (JpredSoapBindingStub) loc.getjpred(); // JBPNote will be set from properties\r
+    }\r
+    catch (Exception ex) {\r
+      output.setText("Serious! JPred Service location failed\nfor URL :"\r
+                     +loc.getjpredAddress()+"\n"+ex.getMessage());\r
+    }\r
+\r
+    JPredThread jthread = new JPredThread(seq);\r
+    jthread.start();\r
+    ImageTwirler thread = new ImageTwirler();\r
+    thread.start();\r
+  }\r
+\r
+  class ImageTwirler\r
+      extends Thread\r
+  {\r
+    ImageIcon[] imageIcon;\r
+    int imageIndex = 0;\r
+    public ImageTwirler()\r
+    {\r
+      imageIcon = new ImageIcon[9];\r
+      for (int i = 0; i < 9; i++)\r
+      {\r
+        java.net.URL url = getClass().getResource("/images/dna" + (i + 1) +\r
+                                                  ".gif");\r
+        if (url != null)\r
+          imageIcon[i] = new ImageIcon(url);\r
+      }\r
+    }\r
+\r
+    public void run()\r
+    {\r
+      while (jobsRunning > 0)\r
+      {\r
+        try\r
+        {\r
+          Thread.sleep(100);\r
+          imageIndex++;\r
+          imageIndex %= 9;\r
+          outputFrame.setFrameIcon(imageIcon[imageIndex]);\r
+          outputFrame.setTitle("JNet Secondary Structure Prediction : " +\r
+                               jobsRunning + " jobs running.");\r
+\r
+        }\r
+        catch (Exception ex)\r
+        {}\r
+\r
+      }\r
+    }\r
+  }\r
+\r
+  class JPredThread\r
+      extends Thread\r
+  {\r
+    String OutputHeader;\r
+    ext.vamsas.Secstructpred result;\r
+    ext.vamsas.Sequence sequence;\r
+    String jobId;\r
+    boolean jobComplete = false;\r
+    int allowedServerExceptions = 3; // thread dies if too many exceptions.\r
+    JPredThread(SequenceI seq)\r
+    {\r
+      OutputHeader = output.getText();\r
+      this.sequence = new ext.vamsas.Sequence();\r
+      this.sequence.setId(seq.getName());\r
+      this.sequence.setSeq(AlignSeq.extractGaps("-. ",seq.getSequence()));\r
+    }\r
+\r
+    public void run()\r
+    {\r
+\r
+      StartJob();\r
+\r
+      while (!jobComplete\r
+             && (allowedServerExceptions > 0))\r
+      {\r
+        try\r
+        {\r
+          result = server.getpredict(jobId);\r
+\r
+          if (result.getOutput().indexOf("Job") != 0)\r
+          {\r
+            parseResult();\r
+            jobComplete = true;\r
+            jobsRunning--;\r
+          }\r
+          else\r
+          {\r
+            output.setText(OutputHeader + "\n" + result.getOutput());\r
+          }\r
+          Thread.sleep(5000);\r
+          //      System.out.println("I'm alive "+seqid+" "+jobid);\r
+\r
+        }\r
+        catch (Exception ex)\r
+        {\r
+          allowedServerExceptions--;\r
+          String msg = output.getText();\r
+          output.setText(msg + "\nJPredWS Server exception!\n" + ex.getMessage());\r
+        }\r
+      }\r
+    }\r
+\r
+    void StartJob()\r
+    {\r
+      try\r
+      {\r
+        jobId = server.predict(sequence);\r
+        System.out.println(jobId);\r
+      }\r
+      catch (Exception e)\r
+      {\r
+        System.out.println("JPredWS Client: Failed to submit the prediction\n" +\r
+                           e.toString() + "\n");\r
+        e.printStackTrace();\r
+      }\r
+    }\r
+\r
+    void parseResult()\r
+    {\r
+      output.setText(OutputHeader + result.getOutput());\r
+      try {\r
+        JPredFile prediction=new JPredFile(result.getOutput(), "Paste");\r
+        SequenceI[] preds = prediction.getSeqsAsArray();\r
+        int i=0;\r
+        // Rename the query sequence\r
+        while (i<preds.length && !preds[i].getName().equals("QUERY")) {\r
+          i++;\r
+        }\r
+        if (i<preds.length) {\r
+          preds[i].setName(sequence.getId());\r
+        }\r
+        AlignFrame af = new AlignFrame(new Alignment(preds));\r
+        /*  JBPNote - need to set correct style of display for the secstrpred alignment object.\r
+            af.clustalColour.setSelected(true);\r
+            af.clustalColour_actionPerformed(null);\r
+         */\r
+        Desktop.addInternalFrame(af, "JNet Prediction for sequence "+sequence.getId(),\r
+                                 700, 500);\r
+      }\r
+      catch (Exception e) {\r
+        output.setText(output.getText()+"\nError! File did not parse correctly\n"+e.getMessage()+"\n");\r
+        e.printStackTrace();\r
+      }\r
+\r
+    }\r
+\r
+  }\r
+}\r