JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / ws / SequenceFetcher.java
index 5243111..37946b1 100644 (file)
-package jalview.ws;\r
-\r
-import java.util.Enumeration;\r
-import java.util.Hashtable;\r
-import java.util.Vector;\r
-\r
-import jalview.datamodel.Alignment;\r
-import jalview.datamodel.AlignmentI;\r
-import jalview.datamodel.DBRefSource;\r
-import jalview.datamodel.SequenceI;\r
-import jalview.ws.seqfetcher.ASequenceFetcher;\r
-import jalview.ws.seqfetcher.DbSourceProxy;\r
-\r
-/**\r
- * prototype of abstract sequence retrieval interface\r
- * \r
- */\r
-public class SequenceFetcher extends ASequenceFetcher\r
-{\r
-  /**\r
-   * Thread safe construction of database proxies TODO: extend to a configurable\r
-   * database plugin mechanism where classes are instantiated by reflection and\r
-   * queried for their DbRefSource and version association.\r
-   * \r
-   */\r
-  public SequenceFetcher()\r
-  {\r
-    FETCHABLEDBS = new Hashtable();\r
-    FETCHABLEDBS.put(DBRefSource.EMBL,\r
-            new jalview.ws.dbsources.EmblSource());\r
-    FETCHABLEDBS.put(DBRefSource.EMBLCDS,\r
-            new jalview.ws.dbsources.EmblCdsSouce());\r
-    FETCHABLEDBS.put(DBRefSource.UNIPROT,\r
-            new jalview.ws.dbsources.Uniprot());\r
-    FETCHABLEDBS.put(DBRefSource.UP_NAME,\r
-            new jalview.ws.dbsources.Uniprot());\r
-    FETCHABLEDBS.put(DBRefSource.PDB, new jalview.ws.dbsources.Pdb());\r
-    FETCHABLEDBS.put(DBRefSource.PFAM, new jalview.ws.dbsources.Pfam());\r
-  };\r
-  \r
-  public static void main(String[] argv)\r
-  {\r
-    AlignmentI ds = null;\r
-    Vector noProds = new Vector();\r
-    if (argv != null && argv.length > 0)\r
-    {\r
-      System.out\r
-              .println("Ignoring arguments. Future Usage = dbname:query1;query2;...");\r
-    }\r
-    SequenceFetcher sfetcher = new SequenceFetcher();\r
-    Enumeration e = sfetcher.FETCHABLEDBS.keys();\r
-    while (e.hasMoreElements())\r
-    {\r
-      String db = (String) e.nextElement();\r
-      // skip me\r
-      if (db.equals(DBRefSource.PDB))\r
-        continue;\r
-      DbSourceProxy sp = sfetcher.getSourceProxy(db);\r
-      System.out\r
-              .println("" + db + ": retrieving test:" + sp.getTestQuery());\r
-      AlignmentI al = null;\r
-      try\r
-      {\r
-        al = sp.getSequenceRecords(sp.getTestQuery());\r
-        if (al != null && al.getHeight() > 0)\r
-        {\r
-          boolean dna = sp.getDbSourceProperties().containsKey(\r
-                  DBRefSource.DNACODINGSEQDB)\r
-                  || sp.getDbSourceProperties().containsKey(\r
-                          DBRefSource.DNASEQDB)\r
-                  || sp.getDbSourceProperties().containsKey(\r
-                          DBRefSource.CODINGSEQDB);\r
-          // try and find products\r
-          String types[] = jalview.analysis.CrossRef.findSequenceXrefTypes(\r
-                  dna, al.getSequencesArray());\r
-          if (types != null)\r
-          {\r
-            System.out.println("Xref Types for: "+(dna ? "dna" : "prot"));\r
-            for (int t = 0; t < types.length; t++)\r
-            {\r
-              System.out.println("Type: " + types[t]);\r
-              SequenceI[] prod = jalview.analysis.CrossRef.findXrefSequences(al\r
-                      .getSequencesArray(), dna, types[t]).getSequencesArray();\r
-              System.out.println("Found "\r
-                      + ((prod == null) ? "no" : "" + prod.length)\r
-                      + " products");\r
-              if (prod!=null)\r
-              {\r
-                for (int p=0; p<prod.length; p++)\r
-                {\r
-                  System.out.println("Prod "+p+": "+prod[p].getDisplayId(true));\r
-                }\r
-              }\r
-            }\r
-          } else {\r
-            noProds.addElement((dna ? new Object[] { al, al } : new Object[] { al }) ); \r
-          }\r
-\r
-        }\r
-      } catch (Exception ex)\r
-      {\r
-        System.out.println("ERROR:Failed to retrieve test query.");\r
-        ex.printStackTrace(System.out);\r
-      }\r
-      if (al == null)\r
-      {\r
-        System.out.println("ERROR:No alignment retrieved.");\r
-        StringBuffer raw = sp.getRawRecords();\r
-        if (raw != null)\r
-          System.out.println(raw.toString());\r
-        else\r
-          System.out.println("ERROR:No Raw results.");\r
-      }\r
-      else\r
-      {\r
-        System.out.println("Retrieved " + al.getHeight() + " sequences.");\r
-        for (int s = 0; s < al.getHeight(); s++)\r
-        {\r
-          SequenceI sq = al.getSequenceAt(s);\r
-          while (sq.getDatasetSequence() != null)\r
-          {\r
-            sq = sq.getDatasetSequence();\r
-\r
-          }\r
-          if (ds == null)\r
-          {\r
-            ds = new Alignment(new SequenceI[]\r
-            { sq });\r
-\r
-          }\r
-          else\r
-          {\r
-            ds.addSequence(sq);\r
-          }\r
-        }\r
-      }\r
-      System.out.flush();\r
-      System.err.flush();\r
-\r
-    }\r
-    if (noProds.size()>0)\r
-    {\r
-      Enumeration ts = noProds.elements();\r
-      while (ts.hasMoreElements())\r
-        \r
-      {\r
-        Object[] typeSq = (Object[]) ts.nextElement();\r
-        boolean dna = (typeSq.length>1);\r
-        AlignmentI al = (AlignmentI) typeSq[0];\r
-        System.out.println("Trying getProducts for "+al.getSequenceAt(0).getDisplayId(true));\r
-        System.out.println("Search DS Xref for: "+(dna ? "dna" : "prot"));\r
-        // have a bash at finding the products amongst all the retrieved sequences.\r
-        SequenceI[] prod = jalview.analysis.CrossRef.findXrefSequences(al\r
-              .getSequencesArray(), dna, null, ds).getSequencesArray(); // note should test rather than throw away codon mapping (if present)\r
-        System.out.println("Found "\r
-                + ((prod == null) ? "no" : "" + prod.length)\r
-                + " products");\r
-        if (prod!=null)\r
-        {\r
-          for (int p=0; p<prod.length; p++)\r
-          {\r
-            System.out.println("Prod "+p+": "+prod[p].getDisplayId(true));\r
-          }\r
-        }\r
-      }\r
-\r
-    }\r
-  }\r
-}\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * 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/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.ws;
+
+import jalview.ext.ensembl.EnsemblGene;
+import jalview.ext.ensembl.EnsemblGenomes;
+import jalview.ws.dbsources.EmblCdsSource;
+import jalview.ws.dbsources.EmblSource;
+import jalview.ws.dbsources.Pdb;
+import jalview.ws.dbsources.PfamFull;
+import jalview.ws.dbsources.PfamSeed;
+import jalview.ws.dbsources.RfamSeed;
+import jalview.ws.dbsources.Uniprot;
+import jalview.ws.dbsources.das.api.jalviewSourceI;
+import jalview.ws.seqfetcher.ASequenceFetcher;
+import jalview.ws.seqfetcher.DbSourceProxy;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This implements the run-time discovery of sequence database clients.
+ * 
+ */
+public class SequenceFetcher extends ASequenceFetcher
+{
+  /**
+   * Thread safe construction of database proxies TODO: extend to a configurable
+   * database plugin mechanism where classes are instantiated by reflection and
+   * queried for their DbRefSource and version association.
+   * 
+   */
+  public SequenceFetcher()
+  {
+    this(true);
+  }
+
+  public SequenceFetcher(boolean addDas)
+  {
+    addDBRefSourceImpl(EnsemblGene.class);
+    addDBRefSourceImpl(EnsemblGenomes.class);
+    addDBRefSourceImpl(EmblSource.class);
+    addDBRefSourceImpl(EmblCdsSource.class);
+    addDBRefSourceImpl(Uniprot.class);
+    addDBRefSourceImpl(Pdb.class);
+    addDBRefSourceImpl(PfamFull.class);
+    addDBRefSourceImpl(PfamSeed.class);
+    addDBRefSourceImpl(RfamSeed.class);
+
+    if (addDas)
+    {
+      registerDasSequenceSources();
+    }
+  }
+
+  /**
+   * return an ordered list of database sources where non-das database classes
+   * appear before das database classes
+   */
+  public String[] getOrderedSupportedSources()
+  {
+    String[] srcs = this.getSupportedDb();
+    ArrayList<String> dassrc = new ArrayList<String>(), nondas = new ArrayList<String>();
+    for (int i = 0; i < srcs.length; i++)
+    {
+      boolean das = false, skip = false;
+      String nm;
+      for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
+      {
+        // Skip the alignment databases for the moment - they're not useful for
+        // verifying a single sequence against its reference source
+        if (dbs.isAlignmentSource())
+        {
+          skip = true;
+        }
+        else
+        {
+          nm = dbs.getDbName();
+          if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
+          {
+            if (nm.startsWith("das:"))
+            {
+              nm = nm.substring(4);
+              das = true;
+            }
+            break;
+          }
+        }
+      }
+      if (skip)
+      {
+        continue;
+      }
+      if (das)
+      {
+        dassrc.add(srcs[i]);
+      }
+      else
+      {
+        nondas.add(srcs[i]);
+      }
+    }
+    String[] tosort = nondas.toArray(new String[0]), sorted = nondas
+            .toArray(new String[0]);
+    for (int j = 0, jSize = sorted.length; j < jSize; j++)
+    {
+      tosort[j] = tosort[j].toLowerCase();
+    }
+    jalview.util.QuickSort.sort(tosort, sorted);
+    // construct array with all sources listed
+
+    srcs = new String[sorted.length + dassrc.size()];
+    int i = 0;
+    for (int j = sorted.length - 1; j >= 0; j--, i++)
+    {
+      srcs[i] = sorted[j];
+      sorted[j] = null;
+    }
+
+    sorted = dassrc.toArray(new String[0]);
+    tosort = dassrc.toArray(new String[0]);
+    for (int j = 0, jSize = sorted.length; j < jSize; j++)
+    {
+      tosort[j] = tosort[j].toLowerCase();
+    }
+    jalview.util.QuickSort.sort(tosort, sorted);
+    for (int j = sorted.length - 1; j >= 0; j--, i++)
+    {
+      srcs[i] = sorted[j];
+    }
+    return srcs;
+  }
+
+  /**
+   * query the currently defined DAS source registry for sequence sources and
+   * add a DasSequenceSource instance for each source to the SequenceFetcher
+   * source list.
+   */
+  public void registerDasSequenceSources()
+  {
+    // TODO: define a context as a registry provider (either desktop,
+    // jalview.bin.cache, or something else).
+    for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry()
+            .getSources())
+    {
+      if (source.isSequenceSource())
+      {
+        List<DbSourceProxy> dassources = source.getSequenceSourceProxies();
+        for (DbSourceProxy seqsrc : dassources)
+        {
+          addDbRefSourceImpl(seqsrc);
+        }
+      }
+    }
+  }
+
+}