vamsasDemo new branch
[jalview.git] / src / jalview / io / WSWUBlastClient.java
index c221d07..2e3ccfa 100755 (executable)
@@ -39,17 +39,22 @@ import javax.xml.namespace.QName;
  */\r
 public class WSWUBlastClient\r
 {\r
+    AlignmentPanel ap;\r
+    AlignmentI al;\r
     CutAndPasteTransfer output = new CutAndPasteTransfer();\r
     int jobsRunning = 0;\r
 \r
+    Hashtable suggestedIds = new Hashtable();\r
     /**\r
      * Creates a new WSWUBlastClient object.\r
      *\r
      * @param al DOCUMENT ME!\r
      * @param ids DOCUMENT ME!\r
      */\r
-    public WSWUBlastClient(AlignmentI al, ArrayList ids)\r
+    public WSWUBlastClient(AlignmentPanel ap, AlignmentI al, ArrayList ids)\r
     {\r
+        this.ap = ap;\r
+        this.al = al;\r
         output.setText(\r
             "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
@@ -61,6 +66,7 @@ public class WSWUBlastClient
             SequenceI sequence = al.findName(ids.get(i).toString());\r
             StringBuffer nonGapped = new StringBuffer();\r
 \r
+\r
             for (int n = 0; n < sequence.getSequence().length(); n++)\r
             {\r
                 if (!jalview.util.Comparison.isGap(sequence.getCharAt(n)))\r
@@ -79,6 +85,7 @@ public class WSWUBlastClient
         thread.start();\r
     }\r
 \r
+\r
     /**\r
      * DOCUMENT ME!\r
      *\r
@@ -97,32 +104,41 @@ public class WSWUBlastClient
         {\r
             data = st.nextToken();\r
 \r
-            if (data.indexOf("database=\"uniprot\" id=") > -1)\r
+            if (data.indexOf(">UNIPROT") > -1)\r
             {\r
-                int index = data.indexOf("database=\"uniprot\" id=") + 23;\r
-                id2 = data.substring(index, data.indexOf("\"", index));\r
+                int index = data.indexOf(">UNIPROT") + 9;\r
+                id2 = data.substring(index, data.indexOf(" ", index));\r
 \r
-                while (data.indexOf("</alignment>") == -1)\r
+                boolean identitiesFound = false;\r
+                while (!identitiesFound)\r
                 {\r
                     data = st.nextToken();\r
 \r
-                    if (data.indexOf("<identity>") > -1)\r
+                    if (data.indexOf("Identities") > -1)\r
                     {\r
-                        int value = Integer.parseInt(data.substring(data.indexOf(\r
-                                        "<identity>") + 10,\r
-                                    data.indexOf("</identity>")));\r
+                       identitiesFound = true;\r
+\r
+                       int value = Integer.parseInt(data.substring(data.indexOf(\r
+                           "(") + 1,\r
+                                                                   data.indexOf("%")));\r
 \r
                         if (value >= maxFound)\r
                         {\r
                             maxFound = value;\r
                             buffer.append(" " + id2 + " " + value + "%; ");\r
+                            suggestedIds.put(id1, id2);\r
                         }\r
                     }\r
                 }\r
             }\r
         }\r
 \r
-        output.setText(output.getText() + buffer.toString());\r
+        output.appendText(buffer.toString());\r
+    }\r
+\r
+    void updateIds()\r
+    {\r
+\r
     }\r
 \r
     class ImageTwirler extends Thread\r
@@ -163,6 +179,36 @@ public class WSWUBlastClient
                 {\r
                 }\r
             }\r
+\r
+            if (jobsRunning == 0)\r
+            {\r
+              int reply = JOptionPane.showConfirmDialog(\r
+                  Desktop.desktop, "Automatically update suggested ids?",\r
+                  "Auto replace sequence ids", JOptionPane.YES_NO_OPTION);\r
+\r
+              if (reply == JOptionPane.YES_OPTION)\r
+              {\r
+                Enumeration keys = suggestedIds.keys();\r
+                while(keys.hasMoreElements())\r
+                {\r
+                  String oldid = keys.nextElement().toString();\r
+                  SequenceI sequence = al.findName(oldid);\r
+                  sequence.setName( suggestedIds.get(oldid).toString() );\r
+\r
+                  sequence = al.getDataset().findName(oldid);\r
+                  Vector entries = sequence.getDBRef();\r
+                  DBRefEntry entry = (DBRefEntry) entries.elementAt(0);\r
+                  sequence.addDBRef(new jalview.datamodel.DBRefEntry("UNIPROT",\r
+                      "0",\r
+                      entry.getAccessionId()));\r
+\r
+                  sequence.setName(suggestedIds.get(oldid).toString());\r
+\r
+                  System.out.println("replace "+oldid+" with "+suggestedIds.get(oldid));\r
+                }\r
+              }\r
+              ap.repaint();\r
+            }\r
         }\r
     }\r
 \r
@@ -175,6 +221,7 @@ public class WSWUBlastClient
 \r
         BlastThread(String id, String sequence)\r
         {\r
+          System.out.println("blasting for: "+id);\r
             this.sequence = sequence;\r
             seqid = id;\r
         }\r
@@ -192,22 +239,21 @@ public class WSWUBlastClient
                             "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast"));\r
                     call.setOperationName(new QName("WSWUBlast", "polljob"));\r
 \r
-                    String result = (String) call.invoke(new Object[]\r
+                    Object object = (String) call.invoke(new Object[]\r
                             {\r
                                 jobid, "xml"\r
                             });\r
 \r
-                    if ((result.indexOf("JOB PENDING") == -1) &&\r
-                            (result.indexOf("JOB RUNNING") == -1))\r
+                    if(object instanceof String)\r
                     {\r
-                        parseResult(seqid, result);\r
-                        jobComplete = true;\r
-                        jobsRunning--;\r
+                      parseResult(seqid, (String)object);\r
+                      jobComplete = true;\r
+                      jobsRunning--;\r
                     }\r
 \r
                     Thread.sleep(5000);\r
 \r
-                    // System.out.println("WSWuBlastClient: I'm alive "+seqid+" "+jobid); // log.debug\r
+                    System.out.println("WSWuBlastClient: I'm alive "+seqid+" "+jobid); // log.debug\r
                 }\r
                 catch (Exception ex)\r
                 {\r
@@ -224,8 +270,8 @@ public class WSWUBlastClient
             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
+            params.put("type", "xml");\r
+            params.put("async", "true");\r
 \r
             byte[] seqbytes = sequence.getBytes();\r
 \r
@@ -236,14 +282,21 @@ public class WSWUBlastClient
                         "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[]\r
+                Object object = call.invoke(new Object[]\r
                         {\r
                             params, seqbytes\r
                         });\r
-                jobid = result;\r
-                System.out.println(\r
-                    "http://www.ebi.ac.uk/cgi-bin/webservices/WSWUBlast JobId '" +\r
-                    jobid + "'");\r
+\r
+                if(object instanceof byte[])\r
+                  jobid = new String( (byte[])object);\r
+\r
+                else\r
+                {\r
+                  jobComplete = true;\r
+                  jobsRunning--;\r
+                  parseResult(seqid, (String)object);\r
+                }\r
+\r
             }\r
             catch (Exception exp)\r
             {\r