Blasts asynchronously
authoramwaterhouse <Andrew Waterhouse>
Wed, 9 Feb 2005 15:29:57 +0000 (15:29 +0000)
committeramwaterhouse <Andrew Waterhouse>
Wed, 9 Feb 2005 15:29:57 +0000 (15:29 +0000)
src/jalview/io/WSWUBlastClient.java

index ed29f89..f2c2047 100755 (executable)
@@ -6,19 +6,23 @@ import java.util.*;
 import jalview.datamodel.*;\r
 import jalview.gui.*;\r
 import javax.swing.*;\r
+import java.util.*;\r
+import java.awt.*;\r
 \r
 public class WSWUBlastClient\r
 {\r
-  CutAndPasteTransfer cap = new CutAndPasteTransfer(false);\r
+  JInternalFrame outputFrame = new JInternalFrame();\r
+  CutAndPasteTransfer output = new CutAndPasteTransfer(false);\r
+  int jobsRunning = 0;\r
 \r
   public WSWUBlastClient(AlignmentI al, ArrayList ids)\r
   {\r
-    JInternalFrame frame = new JInternalFrame();\r
-    cap.formatForOutput();\r
-    frame.setContentPane(cap);\r
-    cap.setText("To display sequence features an exact Uniprot id with 100% sequence identity match must be entered."\r
+\r
+    output.formatForOutput();\r
+    outputFrame.setContentPane(output);\r
+    output.setText("To display sequence features an exact Uniprot id with 100% sequence identity match must be entered."\r
                 +"\nIn order to display these features, try changing the names of your sequences to the ids suggested below.");\r
-    Desktop.addInternalFrame(frame, "BLASTing for unidentified sequences ", 800,300);\r
+    Desktop.addInternalFrame(outputFrame, "BLASTing for unidentified sequences ", 800,300);\r
 \r
     for(int i=0; i<ids.size(); i++)\r
     {\r
@@ -31,52 +35,121 @@ public class WSWUBlastClient
       }\r
 \r
      BlastThread thread = new BlastThread(ids.get(i).toString(),  nonGapped.toString());\r
-     thread.start();\r
+  //   thread.start();\r
+     jobsRunning ++;\r
      }\r
+     ImageTwirler thread = new ImageTwirler();\r
+     thread.start();\r
+  }\r
+\r
+  class ImageTwirler 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("/dna" + (i+1) + ".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
+          Thread.sleep(100);\r
+          imageIndex++;\r
+          imageIndex %=9;\r
+          outputFrame.setFrameIcon( imageIcon[imageIndex]);\r
+          outputFrame.setTitle("BLASTing for unidentified sequences - "+jobsRunning+" jobs running.");\r
 \r
+        }catch(Exception ex){}\r
+\r
+      }\r
+    }\r
   }\r
 \r
+\r
+\r
+\r
   class BlastThread extends Thread\r
   {\r
     String sequence;\r
-    String id;\r
+    String seqid;\r
+    String jobid;\r
+    boolean jobComplete = false;\r
+\r
     BlastThread(String id, String sequence)\r
     {\r
       this.sequence = sequence;\r
-      this.id = id;\r
+      seqid = id;\r
     }\r
 \r
     public void run()\r
     {\r
-      FindUniprotIDFromUnknownSequence(id, sequence);\r
-    }\r
-  }\r
+      StartJob();\r
 \r
-  public void FindUniprotIDFromUnknownSequence (String id, String s)\r
-  {\r
-        HashMap params = new HashMap();\r
-        params.put("database", "uniprot");\r
-        params.put("sensitivity","low");\r
-        params.put("sort","totalscore");\r
-        params.put("matrix","pam10");\r
-        params.put("program","blastp");\r
-        params.put("alignments","5");\r
-        params.put("outformat","xml");\r
-        byte[] sequence = s.getBytes();\r
-\r
-        try {\r
-            Call call = (Call) new Service().createCall();\r
-            call.setTargetEndpointAddress (new java.net.URL("http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
-            call.setOperationName(new QName("WSWUBlast", "doWUBlast"));\r
-\r
-            String result = (String) call.invoke(new Object[] {params,sequence});\r
-            parseResult(id, result);\r
-            }\r
-        catch (Exception exp) {\r
-             System.err.println ("ERROR:\n" + exp.toString());\r
-            exp.printStackTrace();\r
+      while (!jobComplete)\r
+      {\r
+        try\r
+        {\r
+          Call call = (Call)new Service().createCall();\r
+          call.setTargetEndpointAddress(new java.net.URL(\r
+              "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
+          call.setOperationName(new QName("WSWUBlast", "polljob"));\r
+          String result = (String) call.invoke(new Object[]\r
+                                               {jobid, "xml"});\r
+          if(result.indexOf("JOB PENDING")==-1 && result.indexOf("JOB RUNNING")==-1)\r
+          {\r
+                parseResult(seqid, result);\r
+                jobComplete = true;\r
+                jobsRunning --;\r
+          }\r
+          Thread.sleep(5000);\r
+          System.out.println("I'm alive "+seqid+" "+jobid);\r
         }\r
+        catch (Exception ex)\r
+        {}\r
+      }\r
+    }\r
+\r
+    void StartJob()\r
+    {\r
+      HashMap params = new HashMap();\r
+      params.put("database", "uniprot");\r
+      params.put("sensitivity", "low");\r
+      params.put("sort", "totalscore");\r
+      params.put("matrix", "pam10");\r
+      params.put("program", "blastp");\r
+      params.put("alignments", "5");\r
+      params.put("outformat", "xml");\r
+      params.put("searchtype", "1");\r
+      byte[] seqbytes = sequence.getBytes();\r
+\r
+      try\r
+      {\r
+        Call call = (Call)new Service().createCall();\r
+        call.setTargetEndpointAddress(new java.net.URL(\r
+            "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
+        call.setOperationName(new QName("WSWUBlast", "doWUBlast"));\r
+        String result = (String) call.invoke(new Object[]\r
+                                             {params, seqbytes});\r
+        jobid = result;\r
+        System.out.println(jobid);\r
+\r
+      }\r
+      catch (Exception exp)\r
+      {\r
+        System.err.println("ERROR:\n" + exp.toString());\r
+        exp.printStackTrace();\r
+      }\r
     }\r
+  }\r
 \r
     void parseResult(String id1, String res)\r
     {\r
@@ -113,7 +186,7 @@ public class WSWUBlastClient
 \r
       }\r
 \r
-      cap.setText(cap.getText()+buffer.toString());\r
+      output.setText(output.getText()+buffer.toString());\r
     }\r
 \r
 }\r