applied copyright 2008
[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 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   protected String dbname="DASCS";\r
48   protected String dbrefname="das:source";\r
49   protected DasCoordinateSystem coordsys = null;\r
50   /**\r
51    * create a new DbSource proxy for a DAS 1 source\r
52    * @param dbnbame Human Readable Name to use when fetching from this source\r
53    * @param dbrefname DbRefName for DbRefs attached to sequences retrieved from this source\r
54    * @param source Das1Source\r
55    * @param coordsys specific coordinate system to use for this source\r
56    * @throws Exception if source is not capable of the 'sequence' command\r
57    */\r
58   public DasSequenceSource(String dbname, String dbrefname, Das1Source source,\r
59             DasCoordinateSystem coordsys) throws Exception\r
60   {\r
61     if (!source.hasCapability("sequence"))\r
62     {\r
63       throw new Exception("Source "+source.getNickname()+" does not support the sequence command.");\r
64     }\r
65     this.source = source;\r
66     this.dbname = dbname;\r
67     this.dbrefname=dbrefname;\r
68     this.coordsys = coordsys;\r
69   }\r
70   public String getAccessionSeparator()\r
71   {\r
72     // cope with single sequences only\r
73     return null;\r
74   }\r
75 \r
76   public Regex getAccessionValidator()\r
77   {\r
78     /** ? **/\r
79     return Regex.perlCode("\\S+");\r
80   }\r
81 \r
82   public String getDbName()\r
83   {\r
84     // TODO: map to\r
85      return dbname+" (DAS)";\r
86   }\r
87 \r
88   public String getDbSource()\r
89   {\r
90     return dbrefname;\r
91   }\r
92 \r
93   public String getDbVersion()\r
94   {\r
95     return coordsys.getVersion();\r
96   }\r
97 \r
98   public AlignmentI getSequenceRecords(String queries) throws Exception\r
99   {\r
100     SequenceThread seqfetcher = new org.biojava.dasobert.das.SequenceThread(queries, source);\r
101     DasSequenceSourceListener ourlistener = new DasSequenceSourceListener(this, queries);\r
102     seqfetcher.addSequenceListener(ourlistener);\r
103     seqfetcher.start();\r
104     try { Thread.sleep(5); } catch (Exception e) {};\r
105     while (ourlistener.isNotCalled() && seqfetcher.isAlive())\r
106     {\r
107       try { Thread.sleep(5); } catch (Exception e) { };\r
108     }\r
109     if (ourlistener.isNotCalled() || ourlistener.hasNoSequences())\r
110     {\r
111       System.err.println("Sequence Query to "+source.getNickname()+" with '"+queries+"' returned no sequences.");\r
112       return null;\r
113     } else {\r
114       return ourlistener.getSequences();\r
115     }\r
116   }\r
117 \r
118   public String getTestQuery()\r
119   {\r
120     return coordsys.getTestCode();\r
121   }\r
122   public boolean isValidReference(String accession)\r
123   {\r
124     // TODO try to validate an accession against source\r
125     // We don't really know how to do this without querying source\r
126     \r
127     return true;\r
128   }\r
129   /**\r
130    * @return the source\r
131    */\r
132   public Das1Source getSource()\r
133   {\r
134     return source;\r
135   }\r
136   /**\r
137    * @return the coordsys\r
138    */\r
139   public DasCoordinateSystem getCoordsys()\r
140   {\r
141     return coordsys;\r
142   }\r
143 }\r