2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
\r
3 * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\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
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
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
18 package jalview.ws.dbsources.das.datamodel;
\r
20 import java.util.ArrayList;
\r
21 import java.util.Arrays;
\r
22 import java.util.HashMap;
\r
23 import java.util.List;
\r
24 import java.util.Map;
\r
25 import java.util.StringTokenizer;
\r
26 import java.util.Vector;
\r
28 import org.biodas.jdas.client.ConnectionPropertyProviderI;
\r
29 import org.biodas.jdas.client.SequenceClient;
\r
30 import org.biodas.jdas.client.adapters.sequence.DasSequenceAdapter;
\r
31 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
\r
32 import org.biodas.jdas.client.threads.SequenceClientMultipleSources;
\r
33 import org.biodas.jdas.schema.sequence.SEQUENCE;
\r
34 import org.biodas.jdas.schema.sources.COORDINATES;
\r
35 import org.biodas.jdas.schema.sources.SOURCE;
\r
36 import org.biodas.jdas.schema.sources.VERSION;
\r
38 import com.stevesoft.pat.Regex;
\r
40 import jalview.ws.dbsources.das.api.jalviewSourceI;
\r
41 import jalview.ws.seqfetcher.*;
\r
42 import jalview.bin.Cache;
\r
43 import jalview.datamodel.Alignment;
\r
44 import jalview.datamodel.AlignmentI;
\r
45 import jalview.datamodel.DBRefEntry;
\r
46 import jalview.datamodel.Sequence;
\r
47 import jalview.datamodel.SequenceI;
\r
50 * an instance of this class is created for each unique DAS Sequence source (ie
\r
51 * one capable of handling the 'sequence' for a particular MapMaster)
\r
56 public class DasSequenceSource extends DbSourceProxyImpl implements
\r
59 private jalviewSourceI jsrc;
\r
61 protected SOURCE source = null;
\r
63 protected VERSION version = null;
\r
65 protected COORDINATES coordsys = null;
\r
67 protected String dbname = "DASCS";
\r
69 protected String dbrefname = "das:source";
\r
70 protected MultipleConnectionPropertyProviderI connprops=null;
\r
72 * create a new DbSource proxy for a DAS 1 source
\r
75 * Human Readable Name to use when fetching from this source
\r
77 * DbRefName for DbRefs attached to sequences retrieved from this
\r
82 * specific coordinate system to use for this source
\r
84 * if source is not capable of the 'sequence' command
\r
86 public DasSequenceSource(String dbname, String dbrefname, SOURCE source,
\r
87 VERSION version, COORDINATES coordsys, MultipleConnectionPropertyProviderI connprops) throws Exception
\r
89 if (!(jsrc = new JalviewSource(source, connprops, false)).isSequenceSource())
\r
91 throw new Exception("Source " + source.getTitle()
\r
92 + " does not support the sequence command.");
\r
94 this.source = source;
\r
95 this.dbname = dbname;
\r
96 this.dbrefname = dbrefname;
\r
97 this.coordsys = coordsys;
\r
98 this.connprops=connprops;
\r
101 public String getAccessionSeparator()
\r
106 public Regex getAccessionValidator()
\r
109 return Regex.perlCode("\\S+");
\r
112 public String getDbName()
\r
115 return dbname + " (DAS)";
\r
118 public String getDbSource()
\r
123 public String getDbVersion()
\r
125 return coordsys.getVersion();
\r
128 public AlignmentI getSequenceRecords(String queries) throws Exception
\r
130 StringTokenizer st = new StringTokenizer(queries, "\t");
\r
131 List<String> toks = new ArrayList<String>(), src = new ArrayList<String>(), acIds = new ArrayList<String>();
\r
132 while (st.hasMoreTokens())
\r
135 toks.add(t = st.nextToken());
\r
136 acIds.add(t.replaceAll(":[0-9,]+", ""));
\r
138 src.add(jsrc.getSourceURL());
\r
139 Map<String, Map<List<String>, DasSequenceAdapter>> resultset = new HashMap<String, Map<List<String>, DasSequenceAdapter>>();
\r
140 Map<String, Map<List<String>, Exception>> errors = new HashMap<String, Map<List<String>, Exception>>();
\r
142 // First try multiple sources
\r
143 boolean multiple = true, retry = false;
\r
149 // slow, fetch one at a time.
\r
150 for (String sr : src)
\r
153 .println("Retrieving IDs individually from das source: "
\r
155 org.biodas.jdas.client.SequenceClient sq = new SequenceClient(connprops.getConnectionPropertyProviderFor(sr));
\r
156 for (String q : toks)
\r
158 List<String> qset = Arrays.asList(new String[]
\r
162 DasSequenceAdapter s = sq.fetchData(sr, qset);
\r
163 Map<List<String>, DasSequenceAdapter> dss = resultset.get(sr);
\r
168 dss = new HashMap<List<String>, DasSequenceAdapter>());
\r
171 } catch (Exception x)
\r
173 Map<List<String>, Exception> ers = errors.get(sr);
\r
176 errors.put(sr, ers = new HashMap<List<String>, Exception>());
\r
185 SequenceClientMultipleSources sclient;
\r
186 sclient = new SequenceClientMultipleSources();
\r
187 sclient.fetchData(src, toks, resultset, errors);
\r
188 sclient.shutDown();
\r
189 while (!sclient.isTerminated())
\r
195 } catch (InterruptedException x)
\r
199 if (resultset.isEmpty() && !errors.isEmpty())
\r
207 if (resultset.isEmpty())
\r
209 System.err.println("Sequence Query to " + jsrc.getTitle() + " with '"
\r
210 + queries + "' returned no sequences.");
\r
215 Vector<SequenceI> seqs = null;
\r
216 for (Map.Entry<String, Map<List<String>, DasSequenceAdapter>> resset : resultset
\r
219 for (Map.Entry<List<String>, DasSequenceAdapter> result : resset
\r
220 .getValue().entrySet())
\r
222 DasSequenceAdapter dasseqresp = result.getValue();
\r
223 List<String> accessions = result.getKey();
\r
224 for (SEQUENCE e : dasseqresp.getSequence())
\r
226 String lbl = e.getId();
\r
228 if (acIds.indexOf(lbl) == -1)
\r
231 .println("Warning - received sequence event for strange accession code ("
\r
238 if (e.getContent().length() == 0)
\r
241 .println("Empty sequence returned for accession code ("
\r
245 + " (source is " + getDbName());
\r
249 seqs = new java.util.Vector<SequenceI>();
\r
250 // JDAS returns a sequence complete with any newlines and spaces
\r
252 Sequence sq = new Sequence(lbl, e.getContent().replaceAll(
\r
254 sq.addDBRef(new DBRefEntry(getDbSource(), getDbVersion()
\r
255 + ":" + e.getVersion(), lbl));
\r
256 seqs.addElement(sq);
\r
262 if (seqs == null || seqs.size() == 0)
\r
264 SequenceI[] sqs = new SequenceI[seqs.size()];
\r
265 for (int i = 0, iSize = seqs.size(); i < iSize; i++)
\r
267 sqs[i] = (SequenceI) seqs.elementAt(i);
\r
269 Alignment al = new Alignment(sqs);
\r
270 if (jsrc.isFeatureSource())
\r
272 java.util.Vector<jalviewSourceI> srcs = new java.util.Vector<jalviewSourceI>();
\r
273 srcs.addElement(jsrc);
\r
276 jalview.ws.DasSequenceFeatureFetcher dssf=new jalview.ws.DasSequenceFeatureFetcher(sqs, null, srcs, false,
\r
278 } catch (Exception x)
\r
281 .error("Couldn't retrieve features for sequence from its source.",
\r
290 public String getTestQuery()
\r
292 return coordsys.getTestRange();
\r
295 public boolean isValidReference(String accession)
\r
297 // TODO try to validate an accession against source
\r
298 // We don't really know how to do this without querying source
\r
304 * @return the source
\r
306 public SOURCE getSource()
\r
312 * @return the coordsys
\r
314 public COORDINATES getCoordsys()
\r