merge from 2_4_Release branch
[jalview.git] / src / jalview / ws / dbsources / DasSequenceSource.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)\r
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  * \r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  * \r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  * \r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.ws.dbsources;\r
20 \r
21 import java.util.Hashtable;\r
22 \r
23 import org.biojava.dasobert.das.SequenceThread;\r
24 import org.biojava.dasobert.das2.Das2Source;\r
25 import org.biojava.dasobert.dasregistry.Das1Source;\r
26 import org.biojava.dasobert.dasregistry.DasCoordinateSystem;\r
27 import org.biojava.dasobert.dasregistry.DasSource;\r
28 import org.biojava.dasobert.eventmodel.SequenceEvent;\r
29 import org.biojava.dasobert.eventmodel.SequenceListener;\r
30 \r
31 import com.stevesoft.pat.Regex;\r
32 \r
33 import jalview.ws.dbsources.das.DasSequenceSourceListener;\r
34 import jalview.ws.seqfetcher.*;\r
35 import jalview.datamodel.AlignmentI;\r
36 \r
37 /**\r
38  * an instance of this class is created for each unique DAS Sequence source (ie\r
39  * one capable of handling the 'sequence' for a particular MapMaster)\r
40  * \r
41  * @author JimP\r
42  * \r
43  */\r
44 public class DasSequenceSource extends DbSourceProxyImpl implements\r
45         DbSourceProxy\r
46 {\r
47   protected Das1Source source = null;\r
48 \r
49   protected String dbname = "DASCS";\r
50 \r
51   protected String dbrefname = "das:source";\r
52 \r
53   protected DasCoordinateSystem coordsys = null;\r
54 \r
55   /**\r
56    * create a new DbSource proxy for a DAS 1 source\r
57    * \r
58    * @param dbnbame\r
59    *                Human Readable Name to use when fetching from this source\r
60    * @param dbrefname\r
61    *                DbRefName for DbRefs attached to sequences retrieved from\r
62    *                this source\r
63    * @param source\r
64    *                Das1Source\r
65    * @param coordsys\r
66    *                specific coordinate system to use for this source\r
67    * @throws Exception\r
68    *                 if source is not capable of the 'sequence' command\r
69    */\r
70   public DasSequenceSource(String dbname, String dbrefname,\r
71           Das1Source source, DasCoordinateSystem coordsys) throws Exception\r
72   {\r
73     if (!source.hasCapability("sequence"))\r
74     {\r
75       throw new Exception("Source " + source.getNickname()\r
76               + " does not support the sequence command.");\r
77     }\r
78     this.source = source;\r
79     this.dbname = dbname;\r
80     this.dbrefname = dbrefname;\r
81     this.coordsys = coordsys;\r
82   }\r
83 \r
84   public String getAccessionSeparator()\r
85   {\r
86     // cope with single sequences only\r
87     return null;\r
88   }\r
89 \r
90   public Regex getAccessionValidator()\r
91   {\r
92     /** ? * */\r
93     return Regex.perlCode("\\S+");\r
94   }\r
95 \r
96   public String getDbName()\r
97   {\r
98     // TODO: map to\r
99     return dbname + " (DAS)";\r
100   }\r
101 \r
102   public String getDbSource()\r
103   {\r
104     return dbrefname;\r
105   }\r
106 \r
107   public String getDbVersion()\r
108   {\r
109     return coordsys.getVersion();\r
110   }\r
111 \r
112   public AlignmentI getSequenceRecords(String queries) throws Exception\r
113   {\r
114     SequenceThread seqfetcher = new org.biojava.dasobert.das.SequenceThread(\r
115             queries, source);\r
116     DasSequenceSourceListener ourlistener = new DasSequenceSourceListener(\r
117             this, queries);\r
118     seqfetcher.addSequenceListener(ourlistener);\r
119     seqfetcher.start();\r
120     try\r
121     {\r
122       Thread.sleep(5);\r
123     } catch (Exception e)\r
124     {\r
125     }\r
126     ;\r
127     while (ourlistener.isNotCalled() && seqfetcher.isAlive())\r
128     {\r
129       try\r
130       {\r
131         Thread.sleep(5);\r
132       } catch (Exception e)\r
133       {\r
134       }\r
135       ;\r
136     }\r
137     if (ourlistener.isNotCalled() || ourlistener.hasNoSequences())\r
138     {\r
139       System.err.println("Sequence Query to " + source.getNickname()\r
140               + " with '" + queries + "' returned no sequences.");\r
141       return null;\r
142     }\r
143     else\r
144     {\r
145       return ourlistener.getSequences();\r
146     }\r
147   }\r
148 \r
149   public String getTestQuery()\r
150   {\r
151     return coordsys.getTestCode();\r
152   }\r
153 \r
154   public boolean isValidReference(String accession)\r
155   {\r
156     // TODO try to validate an accession against source\r
157     // We don't really know how to do this without querying source\r
158 \r
159     return true;\r
160   }\r
161 \r
162   /**\r
163    * @return the source\r
164    */\r
165   public Das1Source getSource()\r
166   {\r
167     return source;\r
168   }\r
169 \r
170   /**\r
171    * @return the coordsys\r
172    */\r
173   public DasCoordinateSystem getCoordsys()\r
174   {\r
175     return coordsys;\r
176   }\r
177 }\r