update author list in license for (JAL-826)
[jalview.git] / src / jalview / ws / dbsources / das / DasSequenceSourceListener.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.das;\r
19 \r
20 import jalview.datamodel.AlignmentI;\r
21 import jalview.datamodel.Alignment;\r
22 import jalview.datamodel.DBRefEntry;\r
23 import jalview.datamodel.Sequence;\r
24 import jalview.datamodel.SequenceI;\r
25 import jalview.ws.dbsources.DasSequenceSource;\r
26 \r
27 import org.biojava.dasobert.eventmodel.SequenceEvent;\r
28 import org.biojava.dasobert.eventmodel.SequenceListener;\r
29 \r
30 /**\r
31  * Listen for sequence fetch events from a dasobert SequenceThread started with\r
32  * a query string and collect sequences returned from the DAS sequence source.\r
33  * \r
34  * @author JimP\r
35  * \r
36  */\r
37 public class DasSequenceSourceListener implements SequenceListener\r
38 {\r
39 \r
40   String ourAccession = null;\r
41 \r
42   DasSequenceSource oursource = null;\r
43 \r
44   /**\r
45    * \r
46    * @param source\r
47    *          the DAS Sequence DbProxy object containing database details for\r
48    *          this source\r
49    * @param query\r
50    *          the query string sent to the das source that we should be\r
51    *          listening for.\r
52    */\r
53   public DasSequenceSourceListener(DasSequenceSource source, String query)\r
54   {\r
55     oursource = source;\r
56     ourAccession = query;\r
57   }\r
58 \r
59   public void clearSelection()\r
60   {\r
61     // TODO Auto-generated method stub\r
62 \r
63   }\r
64 \r
65   java.util.Vector seqs = null;\r
66 \r
67   public void newSequence(SequenceEvent e)\r
68   {\r
69     if (!e.getAccessionCode().equals(ourAccession))\r
70     {\r
71       System.err\r
72               .println("Warning - received sequence event for strange accession code ("\r
73                       + e.getAccessionCode()\r
74                       + ") - we expected "\r
75                       + ourAccession);\r
76 \r
77       return;\r
78     }\r
79     if (seqs == null)\r
80     {\r
81       if (e.getSequence().length() == 0)\r
82       {\r
83         System.err.println("Empty sequence returned for accession code ("\r
84                 + e.getAccessionCode() + ") from " + oursource.getDbName());\r
85         called = true;\r
86         noSequences = true;\r
87         return;\r
88       }\r
89       seqs = new java.util.Vector();\r
90     }\r
91     Sequence sq = new Sequence(e.getAccessionCode(), e.getSequence());\r
92     sq.addDBRef(new DBRefEntry(oursource.getDbSource(), oursource\r
93             .getDbVersion() + ":" + e.getVersion(), e.getAccessionCode()));\r
94     seqs.addElement(sq);\r
95     called = true;\r
96   }\r
97 \r
98   public void selectedSeqPosition(int position)\r
99   {\r
100     // TODO Auto-generated method stub\r
101 \r
102   }\r
103 \r
104   public void selectedSeqRange(int start, int end)\r
105   {\r
106     // TODO Auto-generated method stub\r
107 \r
108   }\r
109 \r
110   public void selectionLocked(boolean flag)\r
111   {\r
112     // TODO Auto-generated method stub\r
113 \r
114   }\r
115 \r
116   public void newObjectRequested(String accessionCode)\r
117   {\r
118     // TODO Auto-generated method stub\r
119 \r
120   }\r
121 \r
122   boolean noSequences = false;\r
123 \r
124   public void noObjectFound(String accessionCode)\r
125   {\r
126     if (accessionCode.equals(ourAccession))\r
127     {\r
128       noSequences = true;\r
129       called = true;\r
130     }\r
131   }\r
132 \r
133   public boolean hasNoSequences()\r
134   {\r
135     return noSequences;\r
136   }\r
137 \r
138   boolean called = false;\r
139 \r
140   public boolean isNotCalled()\r
141   {\r
142     return !called;\r
143   }\r
144 \r
145   public AlignmentI getSequences()\r
146   {\r
147     if (noSequences || seqs != null && seqs.size() == 0)\r
148       return null;\r
149     SequenceI[] sqs = new SequenceI[seqs.size()];\r
150     for (int i = 0, iSize = seqs.size(); i < iSize; i++)\r
151     {\r
152       sqs[i] = (SequenceI) seqs.elementAt(i);\r
153     }\r
154     Alignment al = new Alignment(sqs);\r
155     if (oursource.getSource().hasCapability("features"))\r
156     {\r
157       java.util.Vector src = new java.util.Vector();\r
158       src.addElement(oursource.getSource());\r
159       new jalview.ws.DasSequenceFeatureFetcher(sqs, null, src, false, false);\r
160     }\r
161 \r
162     return al;\r
163   }\r
164 \r
165 }\r