JAL-1423 - console error reporting and graceful handling for corrupt DAS local source...
[jalview.git] / src / jalview / ws / SequenceFetcher.java
index 077ce14..14b6997 100644 (file)
@@ -1,20 +1,20 @@
-/*\r
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
- * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
+ * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ */
 package jalview.ws;\r
 \r
 import jalview.datamodel.Alignment;\r
@@ -47,6 +47,10 @@ public class SequenceFetcher extends ASequenceFetcher
    */\r
   public SequenceFetcher()\r
   {\r
+    this(true);\r
+  }\r
+  public SequenceFetcher(boolean addDas)\r
+  {\r
     addDBRefSourceImpl(jalview.ws.dbsources.EmblSource.class);\r
     addDBRefSourceImpl(jalview.ws.dbsources.EmblCdsSouce.class);\r
     addDBRefSourceImpl(jalview.ws.dbsources.Uniprot.class);\r
@@ -59,16 +63,95 @@ public class SequenceFetcher extends ASequenceFetcher
     // PFAM\r
     addDBRefSourceImpl(jalview.ws.dbsources.RfamFull.class);\r
     addDBRefSourceImpl(jalview.ws.dbsources.RfamSeed.class);\r
-    registerDasSequenceSources();\r
+    if (addDas) {\r
+      registerDasSequenceSources();\r
+    }\r
   }\r
 \r
   /**\r
-   * return an ordered list of database sources suitable for using in a GUI\r
-   * element\r
+   * return an ordered list of database sources where non-das database classes\r
+   * appear before das database classes\r
    */\r
   public String[] getOrderedSupportedSources()\r
   {\r
     String[] srcs = this.getSupportedDb();\r
+    ArrayList<String> dassrc = new ArrayList<String>(), nondas = new ArrayList<String>();\r
+    for (int i = 0; i < srcs.length; i++)\r
+    {\r
+      boolean das = false, skip = false;\r
+      String nm;\r
+      for (DbSourceProxy dbs : getSourceProxy(srcs[i]))\r
+      {\r
+        // Skip the alignment databases for the moment - they're not useful for\r
+        // verifying a single sequence against its reference source\r
+        if (dbs.isA(DBRefSource.ALIGNMENTDB))\r
+        {\r
+          skip = true;\r
+        }\r
+        else\r
+        {\r
+          nm = dbs.getDbName();\r
+          if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)\r
+          {\r
+            if (nm.startsWith("das:"))\r
+            {\r
+              nm = nm.substring(4);\r
+              das = true;\r
+            }\r
+            break;\r
+          }\r
+        }\r
+      }\r
+      if (skip)\r
+      {\r
+        continue;\r
+      }\r
+      if (das)\r
+      {\r
+        dassrc.add(srcs[i]);\r
+      }\r
+      else\r
+      {\r
+        nondas.add(srcs[i]);\r
+      }\r
+    }\r
+    String[] tosort = nondas.toArray(new String[0]), sorted = nondas\r
+            .toArray(new String[0]);\r
+    for (int j = 0, jSize = sorted.length; j < jSize; j++)\r
+    {\r
+      tosort[j] = tosort[j].toLowerCase();\r
+    }\r
+    jalview.util.QuickSort.sort(tosort, sorted);\r
+    // construct array with all sources listed\r
+\r
+    srcs = new String[sorted.length + dassrc.size()];\r
+    int i = 0;\r
+    for (int j = sorted.length - 1; j >= 0; j--, i++)\r
+    {\r
+      srcs[i] = sorted[j];\r
+      sorted[j] = null;\r
+    }\r
+\r
+    sorted = dassrc.toArray(new String[0]);\r
+    tosort = dassrc.toArray(new String[0]);\r
+    for (int j = 0, jSize = sorted.length; j < jSize; j++)\r
+    {\r
+      tosort[j] = tosort[j].toLowerCase();\r
+    }\r
+    jalview.util.QuickSort.sort(tosort, sorted);\r
+    for (int j = sorted.length - 1; j >= 0; j--, i++)\r
+    {\r
+      srcs[i] = sorted[j];\r
+    }\r
+    return srcs;\r
+  }\r
+\r
+  /**\r
+   * return plaintext databse list suitable for using in a GUI 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
@@ -133,12 +216,22 @@ public class SequenceFetcher extends ASequenceFetcher
   {\r
     AlignmentI ds = null;\r
     Vector noProds = new Vector();\r
-    String usage = "SequenceFetcher.main [<DBNAME> <ACCNO>]\n"\r
+    String usage = "SequenceFetcher.main [-nodas] [<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 one argument, the argument will be resolved to one or more db sources and each will be queried with their test accession only.\n"\r
+            + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"\r
+            + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";\r
+    boolean withDas=true;\r
+    if (argv!=null && argv.length>0 && argv[0].toLowerCase().startsWith("-nodas"))\r
+    {\r
+      withDas=false;\r
+      String targs[] = new String[argv.length-1];\r
+      System.arraycopy(argv, 1, targs, 0, targs.length);\r
+      argv=targs;\r
+    }\r
     if (argv != null && argv.length > 0)\r
     {\r
-      List<DbSourceProxy> sps = new SequenceFetcher()\r
+      List<DbSourceProxy> sps = new SequenceFetcher(withDas)\r
               .getSourceProxy(argv[0]);\r
 \r
       if (sps != null)\r
@@ -148,11 +241,11 @@ public class SequenceFetcher extends ASequenceFetcher
           AlignmentI al = null;\r
           try\r
           {\r
-            al = sp.getSequenceRecords(argv[1]);\r
+            al = sp.getSequenceRecords(argv.length>1 ? argv[1] : sp.getTestQuery());\r
           } catch (Exception e)\r
           {\r
             e.printStackTrace();\r
-            System.err.println("Error when retrieving " + argv[1]\r
+            System.err.println("Error when retrieving " + (argv.length>1 ? argv[1] : sp.getTestQuery())\r
                     + " from " + argv[0] + "\nUsage: " + usage);\r
           }\r
           SequenceI[] prod = al.getSequencesArray();\r
@@ -177,7 +270,7 @@ public class SequenceFetcher extends ASequenceFetcher
       System.out.println(usage);\r
       return;\r
     }\r
-    ASequenceFetcher sfetcher = new SequenceFetcher();\r
+    ASequenceFetcher sfetcher = new SequenceFetcher(withDas);\r
     String[] dbSources = sfetcher.getSupportedDb();\r
     for (int dbsource = 0; dbsource < dbSources.length; dbsource++)\r
     {\r
@@ -347,4 +440,5 @@ public class SequenceFetcher extends ASequenceFetcher
       }\r
     }\r
   }\r
+\r
 }\r