only need to remove progress bar if fetch wasn't cancelled (bug #0059977)
[jalview.git] / src / jalview / ws / DasSequenceFeatureFetcher.java
index 1038ed9..781cda9 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)\r
- * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)\r
+ * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
  * \r
  * This program is free software; you can redistribute it and/or\r
  * modify it under the terms of the GNU General Public License\r
@@ -31,6 +31,7 @@ import org.biojava.dasobert.eventmodel.*;
 import jalview.bin.Cache;\r
 import jalview.datamodel.*;\r
 import jalview.gui.*;\r
+import jalview.util.UrlLink;\r
 \r
 /**\r
  * DOCUMENT ME!\r
@@ -89,17 +90,37 @@ public class DasSequenceFeatureFetcher
     this(sequences, fsettings, selectedSources, true, true);\r
   }\r
 \r
-  public DasSequenceFeatureFetcher(SequenceI[] sequences,\r
-          FeatureSettings fsettings, Vector selectedSources,\r
+  public DasSequenceFeatureFetcher(SequenceI[] oursequences,\r
+          FeatureSettings fsettings, Vector ourselectedSources,\r
           boolean checkDbrefs, boolean promptFetchDbrefs)\r
   {\r
-    this.selectedSources = selectedSources;\r
-    this.sequences = sequences;\r
+    this.selectedSources = new Vector(); \r
+    Enumeration sources = ourselectedSources.elements();\r
+    // filter both sequences and sources to eliminate duplicates\r
+    while (sources.hasMoreElements())\r
+    {\r
+      Object src = sources.nextElement();\r
+      if (!selectedSources.contains(src)) { \r
+        selectedSources.addElement(src); \r
+      };\r
+    }\r
+    Vector sqs = new Vector();\r
+    for (int i=0; i<oursequences.length; i++)\r
+    {\r
+      if (!sqs.contains(oursequences[i]))\r
+      {\r
+        sqs.addElement(oursequences[i]);\r
+      }\r
+    }\r
+    sequences = new SequenceI[sqs.size()];\r
+    for (int i=0; i<sequences.length; i++) {\r
+      sequences[i] = (SequenceI) sqs.elementAt(i);\r
+    }\r
     if (fsettings != null)\r
     {\r
       this.fsettings = fsettings;\r
       this.af = fsettings.af;\r
-      af.getViewport().setShowSequenceFeatures(true);\r
+      af.setShowSeqFeatures(true);\r
     }\r
     int uniprotCount = 0;\r
     for (int i = 0; i < selectedSources.size(); i++)\r
@@ -191,14 +212,16 @@ public class DasSequenceFeatureFetcher
       af.setProgressBar("Fetching DAS Sequence Features", startTime);\r
     }\r
 \r
-    DasSource[] sources = new jalview.gui.DasSourceBrowser().getDASSource();\r
 \r
     if (selectedSources == null || selectedSources.size() == 0)\r
     {\r
+      try {\r
+        DasSource[] sources = new jalview.gui.DasSourceBrowser().getDASSource();\r
+      \r
       String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",\r
               "uniprot");\r
       StringTokenizer st = new StringTokenizer(active, "\t");\r
-      Vector selectedSources = new Vector();\r
+      selectedSources = new Vector();\r
       String token;\r
       while (st.hasMoreTokens())\r
       {\r
@@ -212,6 +235,10 @@ public class DasSequenceFeatureFetcher
           }\r
         }\r
       }\r
+      } catch (Exception ex)\r
+      {\r
+        debug("Exception whilst setting default feature sources from registry and local preferences.",ex);\r
+      }\r
     }\r
 \r
     if (selectedSources == null || selectedSources.size() == 0)\r
@@ -303,8 +330,9 @@ public class DasSequenceFeatureFetcher
   private void setGuiFetchComplete()\r
   {\r
 \r
-    if (af != null)\r
+    if (!cancelled && af != null)\r
     {\r
+      // only update the progress bar if we've completed the fetch normally\r
       af.setProgressBar("DAS Feature Fetching Complete", startTime);\r
     }\r
 \r
@@ -477,45 +505,48 @@ public class DasSequenceFeatureFetcher
         {\r
           for (int i = 0; i < features.length; i++)\r
           {\r
+            // standard DAS feature-> jalview sequence feature transformation\r
             SequenceFeature f = newSequenceFeature(features[i], source\r
                     .getNickname());\r
-            if (dbref.getMap() != null && f.getBegin() > 0\r
-                    && f.getEnd() > 0)\r
+            if (!parseSeqFeature(seq, f, features[i], source))\r
             {\r
-              debug("mapping from " + f.getBegin() + " - " + f.getEnd());\r
-              SequenceFeature vf[] = null;\r
-\r
-              try\r
-              {\r
-                vf = dbref.getMap().locateFeature(f);\r
-              } catch (Exception ex)\r
+              if (dbref.getMap() != null && f.getBegin() > 0\r
+                      && f.getEnd() > 0)\r
               {\r
-                Cache.log\r
-                        .info("Error in 'experimental' mapping of features. Please try to reproduce and then report info to help@jalview.org.");\r
-                Cache.log.info("Mapping feature from " + f.getBegin()\r
-                        + " to " + f.getEnd() + " in dbref "\r
-                        + dbref.getAccessionId() + " in "\r
-                        + dbref.getSource());\r
-                Cache.log.info("using das Source " + ds.getUrl());\r
-                Cache.log.info("Exception", ex);\r
-              }\r
+                debug("mapping from " + f.getBegin() + " - " + f.getEnd());\r
+                SequenceFeature vf[] = null;\r
 \r
-              if (vf != null)\r
-              {\r
-                for (int v = 0; v < vf.length; v++)\r
+                try\r
                 {\r
-                  debug("mapping to " + v + ": " + vf[v].getBegin() + " - "\r
-                          + vf[v].getEnd());\r
-                  seq.addSequenceFeature(vf[v]);\r
+                  vf = dbref.getMap().locateFeature(f);\r
+                } catch (Exception ex)\r
+                {\r
+                  Cache.log\r
+                          .info("Error in 'experimental' mapping of features. Please try to reproduce and then report info to jalview-discuss@jalview.org.");\r
+                  Cache.log.info("Mapping feature from " + f.getBegin()\r
+                          + " to " + f.getEnd() + " in dbref "\r
+                          + dbref.getAccessionId() + " in "\r
+                          + dbref.getSource());\r
+                  Cache.log.info("using das Source " + ds.getUrl());\r
+                  Cache.log.info("Exception", ex);\r
+                }\r
+\r
+                if (vf != null)\r
+                {\r
+                  for (int v = 0; v < vf.length; v++)\r
+                  {\r
+                    debug("mapping to " + v + ": " + vf[v].getBegin()\r
+                            + " - " + vf[v].getEnd());\r
+                    seq.addSequenceFeature(vf[v]);\r
+                  }\r
                 }\r
               }\r
-            }\r
-            else\r
-            {\r
-              seq.addSequenceFeature(f);\r
+              else\r
+              {\r
+                seq.addSequenceFeature(f);\r
+              }\r
             }\r
           }\r
-\r
           featuresAdded(seq);\r
         }\r
         else\r
@@ -578,10 +609,14 @@ public class DasSequenceFeatureFetcher
           {\r
             for (int i = 0; i < features.length; i++)\r
             {\r
+              // standard DAS feature-> jalview sequence feature transformation\r
               SequenceFeature f = newSequenceFeature(features[i], source\r
                       .getNickname());\r
-\r
-              seq.addSequenceFeature(f);\r
+              if (!parseSeqFeature(seq, f, features[i], source))\r
+              {\r
+                // just add as a simple sequence feature\r
+                seq.addSequenceFeature(f);\r
+              }\r
             }\r
 \r
             featuresAdded(seq);\r
@@ -609,6 +644,99 @@ public class DasSequenceFeatureFetcher
   }\r
 \r
   /**\r
+   * examine the given sequence feature to determine if it should actually be\r
+   * turned into sequence annotation or database cross references rather than a\r
+   * simple sequence feature.\r
+   * \r
+   * @param seq\r
+   *                the sequence to annotate\r
+   * @param f\r
+   *                the jalview sequence feature generated from the DAS feature\r
+   * @param map\r
+   *                the sequence feature attributes\r
+   * @param source\r
+   *                the source that emitted the feature\r
+   * @return true if feature was consumed as another kind of annotation.\r
+   */\r
+  protected boolean parseSeqFeature(SequenceI seq, SequenceFeature f,\r
+          Map map, Das1Source source)\r
+  {\r
+    SequenceI mseq = seq;\r
+    while (seq.getDatasetSequence()!=null)\r
+    {\r
+      seq = seq.getDatasetSequence();\r
+    }\r
+    if (f.getType() != null)\r
+    {\r
+      String type = f.getType();\r
+      if (type.equalsIgnoreCase("protein_name"))\r
+      {\r
+        // parse name onto the alignment sequence or the dataset sequence.\r
+        if (seq.getDescription()==null || seq.getDescription().trim().length() == 0)\r
+        {\r
+          // could look at the note series to pick out the first long name, for the moment just use the whole description string \r
+          seq.setDescription(f.getDescription());\r
+        }\r
+        if (mseq.getDescription()==null || mseq.getDescription().trim().length() == 0)\r
+        {\r
+          // could look at the note series to pick out the first long name, for the moment just use the whole description string \r
+          mseq.setDescription(f.getDescription());\r
+        }\r
+        return true;\r
+      }\r
+      // check if source has biosapiens or other sequence ontology label\r
+      if (type.equalsIgnoreCase("DBXREF") || type.equalsIgnoreCase("DBREF"))\r
+      {\r
+        // try to parse the accession out\r
+\r
+        DBRefEntry dbr = new DBRefEntry();\r
+        dbr.setVersion(source.getNickname());\r
+        StringTokenizer st = new StringTokenizer(f.getDescription(), ":");\r
+        if (st.hasMoreTokens())\r
+        {\r
+          dbr.setSource(st.nextToken());\r
+        }\r
+        if (st.hasMoreTokens())\r
+        {\r
+          dbr.setAccessionId(st.nextToken());\r
+        }\r
+        seq.addDBRef(dbr);\r
+        \r
+        if (f.links != null && f.links.size() > 0)\r
+        {\r
+          // feature is also appended to enable links to be seen.\r
+          // TODO: consider extending dbrefs to have their own links ?\r
+          // TODO: new feature: extract dbref links from DAS servers and add the URL pattern to the list of DB name associated links in the user's preferences ?\r
+          // for the moment - just fix up the existing feature so it displays correctly.\r
+          // f.setType(dbr.getSource());\r
+          //f.setDescription();\r
+          f.setValue("linkonly", Boolean.TRUE);\r
+          //f.setDescription("");\r
+          Vector newlinks = new Vector();\r
+          Enumeration it = f.links.elements();\r
+          while (it.hasMoreElements())\r
+          {\r
+            String elm;\r
+            UrlLink urllink = new UrlLink(elm = (String)it.nextElement());\r
+            if (urllink.isValid()) {\r
+              urllink.setLabel(f.getDescription());\r
+              newlinks.addElement(urllink.toString());\r
+            } else {\r
+              // couldn't parse the link properly. Keep it anyway - just in case.\r
+              debug("couldn't parse link string - "+elm);\r
+              newlinks.addElement(elm);\r
+            }\r
+          }\r
+          f.links = newlinks;\r
+          seq.addSequenceFeature(f);\r
+        }\r
+        return true;\r
+      }\r
+    }\r
+    return false;\r
+  }\r
+\r
+  /**\r
    * creates a jalview sequence feature from a das feature document\r
    * \r
    * @param dasfeature\r
@@ -651,7 +779,12 @@ public class DasSequenceFeatureFetcher
       }\r
       try\r
       {\r
-        score = (float) Double.parseDouble(dasfeature.get("SCORE").toString());\r
+        Object scr = dasfeature.get("SCORE");\r
+        if (scr != null)\r
+        {\r
+          score = (float) Double.parseDouble(scr.toString());\r
+\r
+        }\r
       } catch (Exception ex)\r
       {\r
       }\r
@@ -662,12 +795,14 @@ public class DasSequenceFeatureFetcher
       if (dasfeature.containsKey("LINK"))\r
       {\r
         // Do not put feature extent in link text for non-positional features\r
-        if (f.begin==0 && f.end==0)\r
-        {        f.addLink(f.getType()+"|"\r
-                + dasfeature.get("LINK"));\r
-        } else {\r
+        if (f.begin == 0 && f.end == 0)\r
+        {\r
+          f.addLink(f.getType() + "|" + dasfeature.get("LINK"));\r
+        }\r
+        else\r
+        {\r
           f.addLink(f.getType() + " " + f.begin + "_" + f.end + "|"\r
-                        + dasfeature.get("LINK"));\r
+                  + dasfeature.get("LINK"));\r
         }\r
       }\r
 \r