apply version 2.7 copyright
[jalview.git] / src / jalview / ws / SequenceFetcher.java
index 073d448..4f9e0ab 100644 (file)
@@ -1,5 +1,23 @@
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
+ * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle\r
+ * \r
+ * This file is part of Jalview.\r
+ * \r
+ * Jalview is free software: you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License \r
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
+ * \r
+ * Jalview is distributed in the hope that it will be useful, but \r
+ * WITHOUT ANY WARRANTY; without even the implied warranty \r
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
+ * PURPOSE.  See the GNU General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
 package jalview.ws;\r
 \r
+import java.util.ArrayList;\r
 import java.util.Enumeration;\r
 import java.util.Vector;\r
 \r
@@ -12,13 +30,15 @@ import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;\r
 import jalview.datamodel.DBRefSource;\r
 import jalview.datamodel.SequenceI;\r
+import jalview.ws.dbsources.DasSequenceSource;\r
 import jalview.ws.seqfetcher.ASequenceFetcher;\r
 import jalview.ws.seqfetcher.DbSourceProxy;\r
 \r
 /**\r
  * This is the the concrete implementation of the sequence retrieval interface\r
  * and abstract class in jalview.ws.seqfetcher. This implements the run-time\r
- * discovery of sequence database clients, and provides a hardwired main for testing all registered handlers. \r
+ * discovery of sequence database clients, and provides a hardwired main for\r
+ * testing all registered handlers.\r
  * \r
  */\r
 public class SequenceFetcher extends ASequenceFetcher\r
@@ -36,11 +56,74 @@ public class SequenceFetcher extends ASequenceFetcher
     addDBRefSourceImpl(jalview.ws.dbsources.Uniprot.class);\r
     addDBRefSourceImpl(jalview.ws.dbsources.UnprotName.class);\r
     addDBRefSourceImpl(jalview.ws.dbsources.Pdb.class);\r
-    addDBRefSourceImpl(jalview.ws.dbsources.Pfam.class);\r
+    addDBRefSourceImpl(jalview.ws.dbsources.PfamFull.class);\r
+    addDBRefSourceImpl(jalview.ws.dbsources.PfamSeed.class); // ensures Seed\r
+    // alignment is\r
+    // 'default' for\r
+    // PFAM\r
     registerDasSequenceSources();\r
   }\r
+\r
+  /**\r
+   * return an ordered list of database sources suitable for using in a GUI\r
+   * element\r
+   */\r
+  public String[] getOrderedSupportedSources()\r
+  {\r
+    String[] srcs = this.getSupportedDb();\r
+    ArrayList dassrc = new ArrayList(), nondas = new ArrayList();\r
+    for (int i = 0; i < srcs.length; i++)\r
+    {\r
+      String nm = getSourceProxy(srcs[i]).getDbName();\r
+      if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.DasSequenceSource)\r
+      {\r
+        if (nm.startsWith("das:"))\r
+        {\r
+          nm = nm.substring(4);\r
+        }\r
+        dassrc.add(new String[]\r
+        { srcs[i], nm.toUpperCase() });\r
+      }\r
+      else\r
+      {\r
+        nondas.add(new String[]\r
+        { srcs[i], nm.toUpperCase() });\r
+      }\r
+    }\r
+    Object[] sorted = nondas.toArray();\r
+    String[] tosort = new String[sorted.length];\r
+    nondas.clear();\r
+    for (int j = 0; j < sorted.length; j++)\r
+    {\r
+      tosort[j] = ((String[]) sorted[j])[1];\r
+    }\r
+    jalview.util.QuickSort.sort(tosort, sorted);\r
+    int i = 0;\r
+    for (int j = sorted.length - 1; j >= 0; j--, i++)\r
+    {\r
+      srcs[i] = ((String[]) sorted[j])[0];\r
+      sorted[j] = null;\r
+    }\r
+\r
+    sorted = dassrc.toArray();\r
+    tosort = new String[sorted.length];\r
+    dassrc.clear();\r
+    for (int j = 0; j < sorted.length; j++)\r
+    {\r
+      tosort[j] = ((String[]) sorted[j])[1];\r
+    }\r
+    jalview.util.QuickSort.sort(tosort, sorted);\r
+    for (int j = sorted.length - 1; j >= 0; j--, i++)\r
+    {\r
+      srcs[i] = ((String[]) sorted[j])[0];\r
+      sorted[j] = null;\r
+    }\r
+    return srcs;\r
+  }\r
+\r
   /**\r
    * simple run method to test dbsources.\r
+   * \r
    * @param argv\r
    */\r
   public static void main(String[] argv)\r
@@ -48,12 +131,12 @@ public class SequenceFetcher extends ASequenceFetcher
     AlignmentI ds = null;\r
     Vector noProds = new Vector();\r
     String usage = "SequenceFetcher.main [<DBNAME> <ACCNO>]\n"\r
-      +"With no arguments, all DbSources will be queried with their test Accession number.\n"\r
-      +"If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.";\r
+            + "With no arguments, all DbSources will be queried with their test Accession number.\n"\r
+            + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.";\r
     if (argv != null && argv.length > 0)\r
     {\r
       DbSourceProxy sp = new SequenceFetcher().getSourceProxy(argv[0]);\r
-      if (sp!=null)\r
+      if (sp != null)\r
       {\r
         AlignmentI al = null;\r
         try\r
@@ -62,40 +145,46 @@ public class SequenceFetcher extends ASequenceFetcher
         } catch (Exception e)\r
         {\r
           e.printStackTrace();\r
-          System.err.println("Error when retrieving "+argv[1]+" from "+argv[0]+"\nUsage: "+usage);\r
+          System.err.println("Error when retrieving " + argv[1] + " from "\r
+                  + argv[0] + "\nUsage: " + usage);\r
         }\r
         SequenceI[] prod = al.getSequencesArray();\r
-        if (al!=null)\r
+        if (al != null)\r
         {\r
           for (int p = 0; p < prod.length; p++)\r
           {\r
             System.out.println("Prod " + p + ": "\r
-                    + prod[p].getDisplayId(true) + " : "+prod[p].getDescription());\r
+                    + prod[p].getDisplayId(true) + " : "\r
+                    + prod[p].getDescription());\r
           }\r
         }\r
         return;\r
-      } else {\r
-        System.err.println("Can't resolve "+argv[0]+" as a database name. Allowed values are :\n"+new SequenceFetcher().getSupportedDb());\r
       }\r
-      System.out\r
-              .println(usage);\r
+      else\r
+      {\r
+        System.err.println("Can't resolve " + argv[0]\r
+                + " as a database name. Allowed values are :\n"\r
+                + new SequenceFetcher().getSupportedDb());\r
+      }\r
+      System.out.println(usage);\r
     }\r
     ASequenceFetcher sfetcher = new SequenceFetcher();\r
     String[] dbSources = sfetcher.getSupportedDb();\r
-    for (int dbsource=0; dbsource<dbSources.length;dbsource++)\r
+    for (int dbsource = 0; dbsource < dbSources.length; dbsource++)\r
     {\r
       String db = dbSources[dbsource];\r
       // skip me\r
       if (db.equals(DBRefSource.PDB))\r
         continue;\r
       DbSourceProxy sp = sfetcher.getSourceProxy(db);\r
-      System.out\r
-              .println("Source: "+sp.getDbName()+" (" + db + "): retrieving test:" + sp.getTestQuery());\r
+      System.out.println("Source: " + sp.getDbName() + " (" + db\r
+              + "): retrieving test:" + sp.getTestQuery());\r
       AlignmentI al = null;\r
       try\r
       {\r
         al = sp.getSequenceRecords(sp.getTestQuery());\r
-        if (al != null && al.getHeight() > 0 && sp.getDbSourceProperties()!=null)\r
+        if (al != null && al.getHeight() > 0\r
+                && sp.getDbSourceProperties() != null)\r
         {\r
           boolean dna = sp.getDbSourceProperties().containsKey(\r
                   DBRefSource.DNACODINGSEQDB)\r
@@ -195,7 +284,8 @@ public class SequenceFetcher extends ASequenceFetcher
         Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(\r
                 seqs, dna, null, ds);\r
         System.out.println("Found "\r
-                + ((prodal == null) ? "no" : "" + prodal.getHeight()) + " products");\r
+                + ((prodal == null) ? "no" : "" + prodal.getHeight())\r
+                + " products");\r
         if (prodal != null)\r
         {\r
           SequenceI[] prod = prodal.getSequencesArray(); // note\r
@@ -209,7 +299,7 @@ public class SequenceFetcher extends ASequenceFetcher
           // mapping\r
           // (if\r
           // present)\r
-            for (int p = 0; p < prod.length; p++)\r
+          for (int p = 0; p < prod.length; p++)\r
           {\r
             System.out.println("Prod " + p + ": "\r
                     + prod[p].getDisplayId(true));\r
@@ -219,54 +309,99 @@ public class SequenceFetcher extends ASequenceFetcher
 \r
     }\r
   }\r
+\r
   /**\r
-   * query the currently defined DAS source registry for sequence sources and add a DasSequenceSource instance for each source to the SequenceFetcher source list.\r
+   * query the currently defined DAS source registry for sequence sources and\r
+   * add a DasSequenceSource instance for each source to the SequenceFetcher\r
+   * source list.\r
    */\r
-  public void registerDasSequenceSources() {\r
-    DasSource[] sources = jalview.ws.DasSequenceFeatureFetcher.getDASSources();\r
-    for (int s=0;s<sources.length; s++)\r
+  public void registerDasSequenceSources()\r
+  {\r
+    DasSource[] sources = jalview.ws.DasSequenceFeatureFetcher\r
+            .getDASSources();\r
+    if (sources != null)\r
     {\r
-      Das1Source d1s=null;\r
-      if (sources[s].hasCapability("sequence"))\r
+      for (int s = 0; sources != null && s < sources.length; s++)\r
       {\r
-        if (sources[s] instanceof Das2Source)\r
-        {\r
-          if (((Das2Source)sources[s]).hasDas1Capabilities()) {\r
-            try {\r
-              d1s = org.biojava.dasobert.das2.DasSourceConverter.toDas1Source((Das2Source) sources[s]);\r
-            } catch (Exception e)\r
-            {\r
-              System.err.println("Ignoring DAS2 sequence source "+sources[s].getNickname()+" - couldn't map to Das1Source.\n");\r
-              e.printStackTrace();\r
-            }\r
-          }\r
-        } else {\r
-          if (sources[s] instanceof Das1Source)\r
-          { \r
-            d1s = (Das1Source) sources[s];\r
-          }\r
-        }\r
+        addDasSequenceSource(sources[s]);\r
       }\r
-      if (d1s!=null)\r
+    }\r
+\r
+    Vector localsources = jalview.bin.Cache.getLocalDasSources();\r
+    if (localsources != null)\r
+    {\r
+      for (Enumeration ls = localsources.elements(); ls.hasMoreElements();)\r
       {\r
-        DasCoordinateSystem[] css = d1s.getCoordinateSystem();\r
-        for (int c=0;c<css.length;c++)\r
+        addDasSequenceSource((DasSource) ls.nextElement());\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * Try to create and add a DasSequenceSource to the list of sources.\r
+   * \r
+   * @param source\r
+   * @return null if no source was added, or the new DasSequenceSource created\r
+   */\r
+  DasSequenceSource addDasSequenceSource(DasSource source)\r
+  {\r
+    DasSequenceSource ds = null;\r
+    Das1Source d1s = null;\r
+    if (source.hasCapability("sequence"))\r
+    {\r
+      if (source instanceof Das2Source)\r
+      {\r
+        if (((Das2Source) source).hasDas1Capabilities())\r
         {\r
-          try {\r
-            addDbRefSourceImpl(\r
-                    new jalview.ws.dbsources.DasSequenceSource("das:"+d1s.getNickname()+" ("+css[c].getName()+")",\r
-                            css[c].getName(),d1s, css[c]));\r
+          try\r
+          {\r
+            d1s = org.biojava.dasobert.das2.DasSourceConverter\r
+                    .toDas1Source((Das2Source) source);\r
           } catch (Exception e)\r
           {\r
-            System.err.println("Ignoring sequence coord system "+c+" ("+\r
-                    css[c].getName()+") for source "+d1s.getNickname()\r
-                    + "- threw exception when constructing fetcher.\n");\r
+            System.err.println("Ignoring DAS2 sequence source "\r
+                    + source.getNickname()\r
+                    + " - couldn't map to Das1Source.\n");\r
             e.printStackTrace();\r
           }\r
         }\r
       }\r
-          \r
+      else\r
+      {\r
+        if (source instanceof Das1Source)\r
+        {\r
+          d1s = (Das1Source) source;\r
+        }\r
+      }\r
+    }\r
+    if (d1s != null)\r
+    {\r
+      DasCoordinateSystem[] css = d1s.getCoordinateSystem();\r
+      if (css == null || css.length == 0)\r
+      {\r
+        // TODO: query das source directly to identify coordinate system... or\r
+        // have to make up a coordinate system\r
+        css = new DasCoordinateSystem[]\r
+        { new DasCoordinateSystem() };\r
+        css[0].setName(d1s.getNickname());\r
+        css[0].setUniqueId(d1s.getNickname());\r
+      }\r
+      for (int c = 0; c < css.length; c++)\r
+      {\r
+        try\r
+        {\r
+          addDbRefSourceImpl(ds = new DasSequenceSource("das:"\r
+                  + d1s.getNickname() + " (" + css[c].getName() + ")",\r
+                  css[c].getName(), d1s, css[c]));\r
+        } catch (Exception e)\r
+        {\r
+          System.err.println("Ignoring sequence coord system " + c + " ("\r
+                  + css[c].getName() + ") for source " + d1s.getNickname()\r
+                  + "- threw exception when constructing fetcher.\n");\r
+          e.printStackTrace();\r
+        }\r
+      }\r
     }\r
+    return ds;\r
   }\r
-\r
 }\r