updated das sequence source for jdas client and refactored to implementation package
authorjprocter <jprocter@compbio.dundee.ac.uk>
Wed, 29 Feb 2012 16:13:22 +0000 (16:13 +0000)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Wed, 29 Feb 2012 16:13:22 +0000 (16:13 +0000)
src/jalview/ws/dbsources/DasSequenceSource.java [deleted file]
src/jalview/ws/dbsources/das/datamodel/DasSequenceSource.java [new file with mode: 0644]

diff --git a/src/jalview/ws/dbsources/DasSequenceSource.java b/src/jalview/ws/dbsources/DasSequenceSource.java
deleted file mode 100644 (file)
index dc0f0a5..0000000
+++ /dev/null
@@ -1,168 +0,0 @@
-/*\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;\r
-\r
-import java.util.Hashtable;\r
-\r
-import com.stevesoft.pat.Regex;\r
-\r
-import jalview.ws.dbsources.das.DasSequenceSourceListener;\r
-import jalview.ws.seqfetcher.*;\r
-import jalview.datamodel.AlignmentI;\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
-  protected Das1Source source = null;\r
-\r
-  protected String dbname = "DASCS";\r
-\r
-  protected String dbrefname = "das:source";\r
-\r
-  protected DasCoordinateSystem coordsys = 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,\r
-          Das1Source source, DasCoordinateSystem coordsys) throws Exception\r
-  {\r
-    if (!source.hasCapability("sequence"))\r
-    {\r
-      throw new Exception("Source " + source.getNickname()\r
-              + " does not support the sequence command.");\r
-    }\r
-    this.source = source;\r
-    this.dbname = dbname;\r
-    this.dbrefname = dbrefname;\r
-    this.coordsys = coordsys;\r
-  }\r
-\r
-  public String getAccessionSeparator()\r
-  {\r
-    // cope with single sequences only\r
-    return null;\r
-  }\r
-\r
-  public Regex getAccessionValidator()\r
-  {\r
-    /** ? * */\r
-    return Regex.perlCode("\\S+");\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.getVersion();\r
-  }\r
-\r
-  public AlignmentI getSequenceRecords(String queries) throws Exception\r
-  {\r
-    SequenceThread seqfetcher = new org.biojava.dasobert.das.SequenceThread(\r
-            queries, source);\r
-    DasSequenceSourceListener ourlistener = new DasSequenceSourceListener(\r
-            this, queries);\r
-    seqfetcher.addSequenceListener(ourlistener);\r
-    seqfetcher.start();\r
-    try\r
-    {\r
-      Thread.sleep(5);\r
-    } catch (Exception e)\r
-    {\r
-    }\r
-    ;\r
-    while (ourlistener.isNotCalled() && seqfetcher.isAlive())\r
-    {\r
-      try\r
-      {\r
-        Thread.sleep(5);\r
-      } catch (Exception e)\r
-      {\r
-      }\r
-      ;\r
-    }\r
-    if (ourlistener.isNotCalled() || ourlistener.hasNoSequences())\r
-    {\r
-      System.err.println("Sequence Query to " + source.getNickname()\r
-              + " with '" + queries + "' returned no sequences.");\r
-      return null;\r
-    }\r
-    else\r
-    {\r
-      return ourlistener.getSequences();\r
-    }\r
-  }\r
-\r
-  public String getTestQuery()\r
-  {\r
-    return coordsys.getTestCode();\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 Das1Source getSource()\r
-  {\r
-    return source;\r
-  }\r
-\r
-  /**\r
-   * @return the coordsys\r
-   */\r
-  public DasCoordinateSystem getCoordsys()\r
-  {\r
-    return coordsys;\r
-  }\r
-}\r
diff --git a/src/jalview/ws/dbsources/das/datamodel/DasSequenceSource.java b/src/jalview/ws/dbsources/das/datamodel/DasSequenceSource.java
new file mode 100644 (file)
index 0000000..c839b79
--- /dev/null
@@ -0,0 +1,253 @@
+/*\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.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.adapters.sequence.DasSequenceAdapter;\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
+  /**\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) throws Exception\r
+  {\r
+    if (!(jsrc = new JalviewSource(source, false)).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;\r
+    this.coordsys = coordsys;\r
+  }\r
+\r
+  public String getAccessionSeparator()\r
+  {\r
+    return "\t";\r
+  }\r
+\r
+  public Regex getAccessionValidator()\r
+  {\r
+    /** ? * */\r
+    return Regex.perlCode("\\S+");\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.getVersion();\r
+  }\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>();\r
+    while (st.hasMoreTokens())\r
+    {\r
+      toks.add(st.nextToken());\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
+    SequenceClientMultipleSources sclient;\r
+    sclient = new SequenceClientMultipleSources();\r
+    sclient.fetchData(src, toks, resultset, errors);\r
+    sclient.shutDown();\r
+    while (!sclient.isTerminated())\r
+    {\r
+      try {Thread.sleep(200);\r
+      \r
+      } catch (InterruptedException x) {}\r
+    }\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 (toks.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 "\r
+                                  + getDbName());\r
+                  continue;\r
+                }\r
+              }\r
+              seqs = new java.util.Vector<SequenceI>();\r
+              // JDAS returns a sequence complete with any newlines and spaces in the XML\r
+              Sequence sq = new Sequence(lbl, e.getContent().replaceAll("\\s+", ""));\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
+          new jalview.ws.DasSequenceFeatureFetcher(sqs, null, srcs, false,\r
+                false);\r
+        } catch (Exception x)\r
+        {\r
+          Cache.log.error("Couldn't retrieve features for sequence from its source.",x);\r
+        }\r
+      }\r
+\r
+      return al;\r
+    }\r
+  }\r
+\r
+  public String getTestQuery()\r
+  {\r
+    return 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