Jalview 2.8 Source Header
[jalview.git] / src / jalview / ws / dbsources / das / datamodel / DasSequenceSource.java
index eeaccf4..f9b4c48 100644 (file)
-/*\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.dbsources.das.datamodel;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.StringTokenizer;\r
-import java.util.Vector;\r
-\r
-import org.biodas.jdas.client.ConnectionPropertyProviderI;\r
-import org.biodas.jdas.client.SequenceClient;\r
-import org.biodas.jdas.client.adapters.sequence.DasSequenceAdapter;\r
-import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;\r
-import org.biodas.jdas.client.threads.SequenceClientMultipleSources;\r
-import org.biodas.jdas.schema.sequence.SEQUENCE;\r
-import org.biodas.jdas.schema.sources.COORDINATES;\r
-import org.biodas.jdas.schema.sources.SOURCE;\r
-import org.biodas.jdas.schema.sources.VERSION;\r
-\r
-import com.stevesoft.pat.Regex;\r
-\r
-import jalview.ws.dbsources.das.api.jalviewSourceI;\r
-import jalview.ws.seqfetcher.*;\r
-import jalview.bin.Cache;\r
-import jalview.datamodel.Alignment;\r
-import jalview.datamodel.AlignmentI;\r
-import jalview.datamodel.DBRefEntry;\r
-import jalview.datamodel.Sequence;\r
-import jalview.datamodel.SequenceI;\r
-\r
-/**\r
- * an instance of this class is created for each unique DAS Sequence source (ie\r
- * one capable of handling the 'sequence' for a particular MapMaster)\r
- * \r
- * @author JimP\r
- * \r
- */\r
-public class DasSequenceSource extends DbSourceProxyImpl implements\r
-        DbSourceProxy\r
-{\r
-  private jalviewSourceI jsrc;\r
-\r
-  protected SOURCE source = null;\r
-\r
-  protected VERSION version = null;\r
-\r
-  protected COORDINATES coordsys = null;\r
-\r
-  protected String dbname = "DASCS";\r
-\r
-  protected String dbrefname = "das:source";\r
-\r
-  protected MultipleConnectionPropertyProviderI connprops = null;\r
-\r
-  /**\r
-   * create a new DbSource proxy for a DAS 1 source\r
-   * \r
-   * @param dbnbame\r
-   *          Human Readable Name to use when fetching from this source\r
-   * @param dbrefname\r
-   *          DbRefName for DbRefs attached to sequences retrieved from this\r
-   *          source\r
-   * @param source\r
-   *          Das1Source\r
-   * @param coordsys\r
-   *          specific coordinate system to use for this source\r
-   * @throws Exception\r
-   *           if source is not capable of the 'sequence' command\r
-   */\r
-  public DasSequenceSource(String dbname, String dbrefname, SOURCE source,\r
-          VERSION version, COORDINATES coordsys,\r
-          MultipleConnectionPropertyProviderI connprops) throws Exception\r
-  {\r
-    if (!(jsrc = new JalviewSource(source, connprops, false))\r
-            .isSequenceSource())\r
-    {\r
-      throw new Exception("Source " + source.getTitle()\r
-              + " does not support the sequence command.");\r
-    }\r
-    this.source = source;\r
-    this.dbname = dbname;\r
-    this.dbrefname = dbrefname.toUpperCase();\r
-    if (coordsys != null)\r
-    {\r
-      this.coordsys = coordsys;\r
-    }\r
-    this.connprops = connprops;\r
-  }\r
-\r
-  public String getAccessionSeparator()\r
-  {\r
-    return "\t";\r
-  }\r
-\r
-  public Regex getAccessionValidator()\r
-  {\r
-    /** ? * */\r
-    return Regex.perlCode("m/([^:]+)(:\\d+,\\d+)?/");\r
-  }\r
-\r
-  public String getDbName()\r
-  {\r
-    // TODO: map to\r
-    return dbname + " (DAS)";\r
-  }\r
-\r
-  public String getDbSource()\r
-  {\r
-    return dbrefname;\r
-  }\r
-\r
-  public String getDbVersion()\r
-  {\r
-    return coordsys != null ? coordsys.getVersion() : "";\r
-  }\r
-\r
-  public AlignmentI getSequenceRecords(String queries) throws Exception\r
-  {\r
-    StringTokenizer st = new StringTokenizer(queries, "\t");\r
-    List<String> toks = new ArrayList<String>(), src = new ArrayList<String>(), acIds = new ArrayList<String>();\r
-    while (st.hasMoreTokens())\r
-    {\r
-      String t;\r
-      toks.add(t = st.nextToken());\r
-      acIds.add(t.replaceAll(":[0-9,]+", ""));\r
-    }\r
-    src.add(jsrc.getSourceURL());\r
-    Map<String, Map<List<String>, DasSequenceAdapter>> resultset = new HashMap<String, Map<List<String>, DasSequenceAdapter>>();\r
-    Map<String, Map<List<String>, Exception>> errors = new HashMap<String, Map<List<String>, Exception>>();\r
-\r
-    // First try multiple sources\r
-    boolean multiple = true, retry = false;\r
-    do\r
-    {\r
-      if (!multiple)\r
-      {\r
-        retry = false;\r
-        // slow, fetch one at a time.\r
-        for (String sr : src)\r
-        {\r
-          System.err\r
-                  .println("Retrieving IDs individually from das source: "\r
-                          + sr);\r
-          org.biodas.jdas.client.SequenceClient sq = new SequenceClient(\r
-                  connprops.getConnectionPropertyProviderFor(sr));\r
-          for (String q : toks)\r
-          {\r
-            List<String> qset = Arrays.asList(new String[]\r
-            { q });\r
-            try\r
-            {\r
-              DasSequenceAdapter s = sq.fetchData(sr, qset);\r
-              Map<List<String>, DasSequenceAdapter> dss = resultset.get(sr);\r
-              if (dss == null)\r
-              {\r
-                resultset\r
-                        .put(sr,\r
-                                dss = new HashMap<List<String>, DasSequenceAdapter>());\r
-              }\r
-              dss.put(qset, s);\r
-            } catch (Exception x)\r
-            {\r
-              Map<List<String>, Exception> ers = errors.get(sr);\r
-              if (ers == null)\r
-              {\r
-                errors.put(sr, ers = new HashMap<List<String>, Exception>());\r
-              }\r
-              ers.put(qset, x);\r
-            }\r
-          }\r
-        }\r
-      }\r
-      else\r
-      {\r
-        SequenceClientMultipleSources sclient;\r
-        sclient = new SequenceClientMultipleSources();\r
-        sclient.fetchData(src, toks, resultset, errors);\r
-        sclient.shutDown();\r
-        while (!sclient.isTerminated())\r
-        {\r
-          try\r
-          {\r
-            Thread.sleep(200);\r
-\r
-          } catch (InterruptedException x)\r
-          {\r
-          }\r
-        }\r
-        if (resultset.isEmpty() && !errors.isEmpty())\r
-        {\r
-          retry = true;\r
-          multiple = false;\r
-        }\r
-      }\r
-    } while (retry);\r
-\r
-    if (resultset.isEmpty())\r
-    {\r
-      System.err.println("Sequence Query to " + jsrc.getTitle() + " with '"\r
-              + queries + "' returned no sequences.");\r
-      return null;\r
-    }\r
-    else\r
-    {\r
-      Vector<SequenceI> seqs = null;\r
-      for (Map.Entry<String, Map<List<String>, DasSequenceAdapter>> resset : resultset\r
-              .entrySet())\r
-      {\r
-        for (Map.Entry<List<String>, DasSequenceAdapter> result : resset\r
-                .getValue().entrySet())\r
-        {\r
-          DasSequenceAdapter dasseqresp = result.getValue();\r
-          List<String> accessions = result.getKey();\r
-          for (SEQUENCE e : dasseqresp.getSequence())\r
-          {\r
-            String lbl = e.getId();\r
-\r
-            if (acIds.indexOf(lbl) == -1)\r
-            {\r
-              System.err\r
-                      .println("Warning - received sequence event for strange accession code ("\r
-                              + lbl + ")");\r
-            }\r
-            else\r
-            {\r
-              if (seqs == null)\r
-              {\r
-                if (e.getContent().length() == 0)\r
-                {\r
-                  System.err\r
-                          .println("Empty sequence returned for accession code ("\r
-                                  + lbl\r
-                                  + ") from "\r
-                                  + resset.getKey()\r
-                                  + " (source is " + getDbName());\r
-                  continue;\r
-                }\r
-              }\r
-              seqs = new java.util.Vector<SequenceI>();\r
-              // JDAS returns a sequence complete with any newlines and spaces\r
-              // in the XML\r
-              Sequence sq = new Sequence(lbl, e.getContent().replaceAll(\r
-                      "\\s+", ""));\r
-              sq.setStart(e.getStart().intValue());\r
-              sq.addDBRef(new DBRefEntry(getDbSource(), getDbVersion()\r
-                      + ":" + e.getVersion(), lbl));\r
-              seqs.addElement(sq);\r
-            }\r
-          }\r
-        }\r
-      }\r
-\r
-      if (seqs == null || seqs.size() == 0)\r
-        return null;\r
-      SequenceI[] sqs = new SequenceI[seqs.size()];\r
-      for (int i = 0, iSize = seqs.size(); i < iSize; i++)\r
-      {\r
-        sqs[i] = (SequenceI) seqs.elementAt(i);\r
-      }\r
-      Alignment al = new Alignment(sqs);\r
-      if (jsrc.isFeatureSource())\r
-      {\r
-        java.util.Vector<jalviewSourceI> srcs = new java.util.Vector<jalviewSourceI>();\r
-        srcs.addElement(jsrc);\r
-        try\r
-        {\r
-          jalview.ws.DasSequenceFeatureFetcher dssf = new jalview.ws.DasSequenceFeatureFetcher(\r
-                  sqs, null, srcs, false, false, multiple);\r
-          while (dssf.isRunning())\r
-          {\r
-            try\r
-            {\r
-              Thread.sleep(200);\r
-            } catch (InterruptedException x)\r
-            {\r
-\r
-            }\r
-          }\r
-\r
-        } catch (Exception x)\r
-        {\r
-          Cache.log\r
-                  .error("Couldn't retrieve features for sequence from its source.",\r
-                          x);\r
-        }\r
-      }\r
-\r
-      return al;\r
-    }\r
-  }\r
-\r
-  public String getTestQuery()\r
-  {\r
-    return coordsys == null ? "" : coordsys.getTestRange();\r
-  }\r
-\r
-  public boolean isValidReference(String accession)\r
-  {\r
-    // TODO try to validate an accession against source\r
-    // We don't really know how to do this without querying source\r
-\r
-    return true;\r
-  }\r
-\r
-  /**\r
-   * @return the source\r
-   */\r
-  public SOURCE getSource()\r
-  {\r
-    return source;\r
-  }\r
-\r
-  /**\r
-   * @return the coordsys\r
-   */\r
-  public COORDINATES getCoordsys()\r
-  {\r
-    return coordsys;\r
-  }\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.dbsources.das.datamodel;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.Vector;
+
+import org.biodas.jdas.client.ConnectionPropertyProviderI;
+import org.biodas.jdas.client.SequenceClient;
+import org.biodas.jdas.client.adapters.sequence.DasSequenceAdapter;
+import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
+import org.biodas.jdas.client.threads.SequenceClientMultipleSources;
+import org.biodas.jdas.schema.sequence.SEQUENCE;
+import org.biodas.jdas.schema.sources.COORDINATES;
+import org.biodas.jdas.schema.sources.SOURCE;
+import org.biodas.jdas.schema.sources.VERSION;
+
+import com.stevesoft.pat.Regex;
+
+import jalview.ws.dbsources.das.api.jalviewSourceI;
+import jalview.ws.seqfetcher.*;
+import jalview.bin.Cache;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
+
+/**
+ * an instance of this class is created for each unique DAS Sequence source (ie
+ * one capable of handling the 'sequence' for a particular MapMaster)
+ * 
+ * @author JimP
+ * 
+ */
+public class DasSequenceSource extends DbSourceProxyImpl implements
+        DbSourceProxy
+{
+  private jalviewSourceI jsrc;
+
+  protected SOURCE source = null;
+
+  protected VERSION version = null;
+
+  protected COORDINATES coordsys = null;
+
+  protected String dbname = "DASCS";
+
+  protected String dbrefname = "das:source";
+
+  protected MultipleConnectionPropertyProviderI connprops = null;
+
+  /**
+   * create a new DbSource proxy for a DAS 1 source
+   * 
+   * @param dbnbame
+   *          Human Readable Name to use when fetching from this source
+   * @param dbrefname
+   *          DbRefName for DbRefs attached to sequences retrieved from this
+   *          source
+   * @param source
+   *          Das1Source
+   * @param coordsys
+   *          specific coordinate system to use for this source
+   * @throws Exception
+   *           if source is not capable of the 'sequence' command
+   */
+  public DasSequenceSource(String dbname, String dbrefname, SOURCE source,
+          VERSION version, COORDINATES coordsys,
+          MultipleConnectionPropertyProviderI connprops) throws Exception
+  {
+    if (!(jsrc = new JalviewSource(source, connprops, false))
+            .isSequenceSource())
+    {
+      throw new Exception("Source " + source.getTitle()
+              + " does not support the sequence command.");
+    }
+    this.source = source;
+    this.dbname = dbname;
+    this.dbrefname = dbrefname.toUpperCase();
+    if (coordsys != null)
+    {
+      this.coordsys = coordsys;
+    }
+    this.connprops = connprops;
+  }
+
+  public String getAccessionSeparator()
+  {
+    return "\t";
+  }
+
+  public Regex getAccessionValidator()
+  {
+    /** ? * */
+    return Regex.perlCode("m/([^:]+)(:\\d+,\\d+)?/");
+  }
+
+  public String getDbName()
+  {
+    // TODO: map to
+    return dbname + " (DAS)";
+  }
+
+  public String getDbSource()
+  {
+    return dbrefname;
+  }
+
+  public String getDbVersion()
+  {
+    return coordsys != null ? coordsys.getVersion() : "";
+  }
+
+  public AlignmentI getSequenceRecords(String queries) throws Exception
+  {
+    StringTokenizer st = new StringTokenizer(queries, "\t");
+    List<String> toks = new ArrayList<String>(), src = new ArrayList<String>(), acIds = new ArrayList<String>();
+    while (st.hasMoreTokens())
+    {
+      String t;
+      toks.add(t = st.nextToken());
+      acIds.add(t.replaceAll(":[0-9,]+", ""));
+    }
+    src.add(jsrc.getSourceURL());
+    Map<String, Map<List<String>, DasSequenceAdapter>> resultset = new HashMap<String, Map<List<String>, DasSequenceAdapter>>();
+    Map<String, Map<List<String>, Exception>> errors = new HashMap<String, Map<List<String>, Exception>>();
+
+    // First try multiple sources
+    boolean multiple = true, retry = false;
+    do
+    {
+      if (!multiple)
+      {
+        retry = false;
+        // slow, fetch one at a time.
+        for (String sr : src)
+        {
+          System.err
+                  .println("Retrieving IDs individually from das source: "
+                          + sr);
+          org.biodas.jdas.client.SequenceClient sq = new SequenceClient(
+                  connprops.getConnectionPropertyProviderFor(sr));
+          for (String q : toks)
+          {
+            List<String> qset = Arrays.asList(new String[]
+            { q });
+            try
+            {
+              DasSequenceAdapter s = sq.fetchData(sr, qset);
+              Map<List<String>, DasSequenceAdapter> dss = resultset.get(sr);
+              if (dss == null)
+              {
+                resultset
+                        .put(sr,
+                                dss = new HashMap<List<String>, DasSequenceAdapter>());
+              }
+              dss.put(qset, s);
+            } catch (Exception x)
+            {
+              Map<List<String>, Exception> ers = errors.get(sr);
+              if (ers == null)
+              {
+                errors.put(sr, ers = new HashMap<List<String>, Exception>());
+              }
+              ers.put(qset, x);
+            }
+          }
+        }
+      }
+      else
+      {
+        SequenceClientMultipleSources sclient;
+        sclient = new SequenceClientMultipleSources();
+        sclient.fetchData(src, toks, resultset, errors);
+        sclient.shutDown();
+        while (!sclient.isTerminated())
+        {
+          try
+          {
+            Thread.sleep(200);
+
+          } catch (InterruptedException x)
+          {
+          }
+        }
+        if (resultset.isEmpty() && !errors.isEmpty())
+        {
+          retry = true;
+          multiple = false;
+        }
+      }
+    } while (retry);
+
+    if (resultset.isEmpty())
+    {
+      System.err.println("Sequence Query to " + jsrc.getTitle() + " with '"
+              + queries + "' returned no sequences.");
+      return null;
+    }
+    else
+    {
+      Vector<SequenceI> seqs = null;
+      for (Map.Entry<String, Map<List<String>, DasSequenceAdapter>> resset : resultset
+              .entrySet())
+      {
+        for (Map.Entry<List<String>, DasSequenceAdapter> result : resset
+                .getValue().entrySet())
+        {
+          DasSequenceAdapter dasseqresp = result.getValue();
+          List<String> accessions = result.getKey();
+          for (SEQUENCE e : dasseqresp.getSequence())
+          {
+            String lbl = e.getId();
+
+            if (acIds.indexOf(lbl) == -1)
+            {
+              System.err
+                      .println("Warning - received sequence event for strange accession code ("
+                              + lbl + ")");
+            }
+            else
+            {
+              if (seqs == null)
+              {
+                if (e.getContent().length() == 0)
+                {
+                  System.err
+                          .println("Empty sequence returned for accession code ("
+                                  + lbl
+                                  + ") from "
+                                  + resset.getKey()
+                                  + " (source is " + getDbName());
+                  continue;
+                }
+              }
+              seqs = new java.util.Vector<SequenceI>();
+              // JDAS returns a sequence complete with any newlines and spaces
+              // in the XML
+              Sequence sq = new Sequence(lbl, e.getContent().replaceAll(
+                      "\\s+", ""));
+              sq.setStart(e.getStart().intValue());
+              sq.addDBRef(new DBRefEntry(getDbSource(), getDbVersion()
+                      + ":" + e.getVersion(), lbl));
+              seqs.addElement(sq);
+            }
+          }
+        }
+      }
+
+      if (seqs == null || seqs.size() == 0)
+        return null;
+      SequenceI[] sqs = new SequenceI[seqs.size()];
+      for (int i = 0, iSize = seqs.size(); i < iSize; i++)
+      {
+        sqs[i] = (SequenceI) seqs.elementAt(i);
+      }
+      Alignment al = new Alignment(sqs);
+      if (jsrc.isFeatureSource())
+      {
+        java.util.Vector<jalviewSourceI> srcs = new java.util.Vector<jalviewSourceI>();
+        srcs.addElement(jsrc);
+        try
+        {
+          jalview.ws.DasSequenceFeatureFetcher dssf = new jalview.ws.DasSequenceFeatureFetcher(
+                  sqs, null, srcs, false, false, multiple);
+          while (dssf.isRunning())
+          {
+            try
+            {
+              Thread.sleep(200);
+            } catch (InterruptedException x)
+            {
+
+            }
+          }
+
+        } catch (Exception x)
+        {
+          Cache.log
+                  .error("Couldn't retrieve features for sequence from its source.",
+                          x);
+        }
+      }
+
+      return al;
+    }
+  }
+
+  public String getTestQuery()
+  {
+    return coordsys == null ? "" : coordsys.getTestRange();
+  }
+
+  public boolean isValidReference(String accession)
+  {
+    // TODO try to validate an accession against source
+    // We don't really know how to do this without querying source
+
+    return true;
+  }
+
+  /**
+   * @return the source
+   */
+  public SOURCE getSource()
+  {
+    return source;
+  }
+
+  /**
+   * @return the coordsys
+   */
+  public COORDINATES getCoordsys()
+  {
+    return coordsys;
+  }
+}