updated jalview version of dasobert 1.53e client and added Das Sequence Source discov...
[jalview.git] / src / jalview / ws / dbsources / DasSequenceSource.java
1 package jalview.ws.dbsources;\r
2 \r
3 import java.util.Hashtable;\r
4 \r
5 import org.biojava.dasobert.das.SequenceThread;\r
6 import org.biojava.dasobert.das2.Das2Source;\r
7 import org.biojava.dasobert.dasregistry.Das1Source;\r
8 import org.biojava.dasobert.dasregistry.DasCoordinateSystem;\r
9 import org.biojava.dasobert.dasregistry.DasSource;\r
10 import org.biojava.dasobert.eventmodel.SequenceEvent;\r
11 import org.biojava.dasobert.eventmodel.SequenceListener;\r
12 \r
13 import com.stevesoft.pat.Regex;\r
14 \r
15 import jalview.ws.dbsources.das.DasSequenceSourceListener;\r
16 import jalview.ws.seqfetcher.*;\r
17 import jalview.datamodel.AlignmentI;\r
18 \r
19 /**\r
20  * an instance of this class is created for each unique DAS Sequence source (ie one capable of handling the 'sequence' for a particular MapMaster)\r
21  *  \r
22  * @author JimP\r
23  *\r
24  */\r
25 public class DasSequenceSource extends DbSourceProxyImpl implements\r
26         DbSourceProxy\r
27 {\r
28   protected Das1Source source=null;\r
29   protected String dbname="DASCS";\r
30   protected String dbrefname="das:source";\r
31   protected DasCoordinateSystem coordsys = null;\r
32   /**\r
33    * create a new DbSource proxy for a DAS 1 source\r
34    * @param dbnbame Human Readable Name to use when fetching from this source\r
35    * @param dbrefname DbRefName for DbRefs attached to sequences retrieved from this source\r
36    * @param source Das1Source\r
37    * @param coordsys specific coordinate system to use for this source\r
38    * @throws Exception if source is not capable of the 'sequence' command\r
39    */\r
40   public DasSequenceSource(String dbname, String dbrefname, Das1Source source,\r
41             DasCoordinateSystem coordsys) throws Exception\r
42   {\r
43     if (!source.hasCapability("sequence"))\r
44     {\r
45       throw new Exception("Source "+source.getNickname()+" does not support the sequence command.");\r
46     }\r
47     this.source = source;\r
48     this.dbname = dbname;\r
49     this.dbrefname=dbrefname;\r
50     this.coordsys = coordsys;\r
51   }\r
52   public String getAccessionSeparator()\r
53   {\r
54     // cope with single sequences only\r
55     return null;\r
56   }\r
57 \r
58   public Regex getAccessionValidator()\r
59   {\r
60     /** ? **/\r
61     return Regex.perlCode("\\S+");\r
62   }\r
63 \r
64   public String getDbName()\r
65   {\r
66     // TODO: map to\r
67      return dbname+" (DAS)";\r
68   }\r
69 \r
70   public String getDbSource()\r
71   {\r
72     return dbrefname;\r
73   }\r
74 \r
75   public String getDbVersion()\r
76   {\r
77     return coordsys.getVersion();\r
78   }\r
79 \r
80   public AlignmentI getSequenceRecords(String queries) throws Exception\r
81   {\r
82     SequenceThread seqfetcher = new org.biojava.dasobert.das.SequenceThread(queries, source);\r
83     DasSequenceSourceListener ourlistener = new DasSequenceSourceListener(this, queries);\r
84     seqfetcher.addSequenceListener(ourlistener);\r
85     seqfetcher.start();\r
86     try { Thread.sleep(5); } catch (Exception e) {};\r
87     while (ourlistener.isNotCalled() && seqfetcher.isAlive())\r
88     {\r
89       try { Thread.sleep(5); } catch (Exception e) { };\r
90     }\r
91     if (ourlistener.isNotCalled() || ourlistener.hasNoSequences())\r
92     {\r
93       System.err.println("Sequence Query to "+source.getNickname()+" with '"+queries+"' returned no sequences.");\r
94       return null;\r
95     } else {\r
96       return ourlistener.getSequences();\r
97     }\r
98   }\r
99 \r
100   public String getTestQuery()\r
101   {\r
102     return coordsys.getTestCode();\r
103   }\r
104   public boolean isValidReference(String accession)\r
105   {\r
106     // TODO try to validate an accession against source\r
107     // We don't really know how to do this without querying source\r
108     \r
109     return true;\r
110   }\r
111   /**\r
112    * @return the source\r
113    */\r
114   public Das1Source getSource()\r
115   {\r
116     return source;\r
117   }\r
118   /**\r
119    * @return the coordsys\r
120    */\r
121   public DasCoordinateSystem getCoordsys()\r
122   {\r
123     return coordsys;\r
124   }\r
125 }\r