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