2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.dbsources.das.datamodel;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.StringTokenizer;
29 import java.util.Vector;
31 import org.biodas.jdas.client.SequenceClient;
32 import org.biodas.jdas.client.adapters.sequence.DasSequenceAdapter;
33 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
34 import org.biodas.jdas.client.threads.SequenceClientMultipleSources;
35 import org.biodas.jdas.schema.sequence.SEQUENCE;
36 import org.biodas.jdas.schema.sources.COORDINATES;
37 import org.biodas.jdas.schema.sources.SOURCE;
38 import org.biodas.jdas.schema.sources.VERSION;
40 import com.stevesoft.pat.Regex;
42 import jalview.ws.dbsources.das.api.jalviewSourceI;
43 import jalview.ws.seqfetcher.*;
44 import jalview.bin.Cache;
45 import jalview.datamodel.Alignment;
46 import jalview.datamodel.AlignmentI;
47 import jalview.datamodel.DBRefEntry;
48 import jalview.datamodel.Sequence;
49 import jalview.datamodel.SequenceI;
52 * an instance of this class is created for each unique DAS Sequence source (ie
53 * one capable of handling the 'sequence' for a particular MapMaster)
58 public class DasSequenceSource extends DbSourceProxyImpl implements
61 private jalviewSourceI jsrc;
63 protected SOURCE source = null;
65 protected VERSION version = null;
67 protected COORDINATES coordsys = null;
69 protected String dbname = "DASCS";
71 protected String dbrefname = "das:source";
73 protected MultipleConnectionPropertyProviderI connprops = null;
76 * DAS sources are tier 1 - if we have a direct DB connection then we should prefer it
81 * create a new DbSource proxy for a DAS 1 source
84 * Human Readable Name to use when fetching from this source
86 * DbRefName for DbRefs attached to sequences retrieved from this
91 * specific coordinate system to use for this source
93 * if source is not capable of the 'sequence' command
95 public DasSequenceSource(String dbname, String dbrefname, SOURCE source,
96 VERSION version, COORDINATES coordsys,
97 MultipleConnectionPropertyProviderI connprops) throws Exception
99 if (!(jsrc = new JalviewSource(source, connprops, false))
102 throw new Exception("Source " + source.getTitle()
103 + " does not support the sequence command.");
105 this.tier = 1+((jsrc.isLocal() || jsrc.isReferenceSource()) ? 0 : 1);
106 this.source = source;
107 this.dbname = dbname;
108 this.dbrefname = dbrefname.toUpperCase();
109 if (coordsys != null)
111 this.coordsys = coordsys;
113 this.connprops = connprops;
116 public String getAccessionSeparator()
121 public Regex getAccessionValidator()
124 return Regex.perlCode("m/([^:]+)(:\\d+,\\d+)?/");
127 public String getDbName()
130 return dbname + " (DAS)";
133 public String getDbSource()
138 public String getDbVersion()
140 return coordsys != null ? coordsys.getVersion() : "";
143 public AlignmentI getSequenceRecords(String queries) throws Exception
145 StringTokenizer st = new StringTokenizer(queries, "\t");
146 List<String> toks = new ArrayList<String>(), src = new ArrayList<String>(), acIds = new ArrayList<String>();
147 while (st.hasMoreTokens())
150 toks.add(t = st.nextToken());
151 acIds.add(t.replaceAll(":[0-9,]+", ""));
153 src.add(jsrc.getSourceURL());
154 Map<String, Map<List<String>, DasSequenceAdapter>> resultset = new HashMap<String, Map<List<String>, DasSequenceAdapter>>();
155 Map<String, Map<List<String>, Exception>> errors = new HashMap<String, Map<List<String>, Exception>>();
157 // First try multiple sources
158 boolean multiple = true, retry = false;
164 // slow, fetch one at a time.
165 for (String sr : src)
168 .println("Retrieving IDs individually from das source: "
170 org.biodas.jdas.client.SequenceClient sq = new SequenceClient(
171 connprops.getConnectionPropertyProviderFor(sr));
172 for (String q : toks)
174 List<String> qset = Arrays.asList(new String[]
178 DasSequenceAdapter s = sq.fetchData(sr, qset);
179 Map<List<String>, DasSequenceAdapter> dss = resultset.get(sr);
184 dss = new HashMap<List<String>, DasSequenceAdapter>());
187 } catch (Exception x)
189 Map<List<String>, Exception> ers = errors.get(sr);
192 errors.put(sr, ers = new HashMap<List<String>, Exception>());
201 SequenceClientMultipleSources sclient;
202 sclient = new SequenceClientMultipleSources();
203 sclient.fetchData(src, toks, resultset, errors);
205 while (!sclient.isTerminated())
211 } catch (InterruptedException x)
215 if (resultset.isEmpty() && !errors.isEmpty())
223 if (resultset.isEmpty())
225 System.err.println("Sequence Query to " + jsrc.getTitle() + " with '"
226 + queries + "' returned no sequences.");
231 Vector<SequenceI> seqs = null;
232 for (Map.Entry<String, Map<List<String>, DasSequenceAdapter>> resset : resultset
235 for (Map.Entry<List<String>, DasSequenceAdapter> result : resset
236 .getValue().entrySet())
238 DasSequenceAdapter dasseqresp = result.getValue();
239 List<String> accessions = result.getKey();
240 for (SEQUENCE e : dasseqresp.getSequence())
242 String lbl = e.getId();
244 if (acIds.indexOf(lbl) == -1)
247 .println("Warning - received sequence event for strange accession code ("
254 if (e.getContent().length() == 0)
257 .println("Empty sequence returned for accession code ("
261 + " (source is " + getDbName());
265 seqs = new java.util.Vector<SequenceI>();
266 // JDAS returns a sequence complete with any newlines and spaces
268 Sequence sq = new Sequence(lbl, e.getContent().replaceAll(
270 sq.setStart(e.getStart().intValue());
271 sq.addDBRef(new DBRefEntry(getDbSource(), getDbVersion()
272 + ":" + e.getVersion(), lbl));
279 if (seqs == null || seqs.size() == 0)
281 SequenceI[] sqs = new SequenceI[seqs.size()];
282 for (int i = 0, iSize = seqs.size(); i < iSize; i++)
284 sqs[i] = (SequenceI) seqs.elementAt(i);
286 Alignment al = new Alignment(sqs);
287 if (jsrc.isFeatureSource())
289 java.util.Vector<jalviewSourceI> srcs = new java.util.Vector<jalviewSourceI>();
290 srcs.addElement(jsrc);
293 jalview.ws.DasSequenceFeatureFetcher dssf = new jalview.ws.DasSequenceFeatureFetcher(
294 sqs, null, srcs, false, false, multiple);
295 while (dssf.isRunning())
300 } catch (InterruptedException x)
306 } catch (Exception x)
309 .error("Couldn't retrieve features for sequence from its source.",
318 public String getTestQuery()
320 return coordsys == null ? "" : coordsys.getTestRange();
323 public boolean isValidReference(String accession)
325 // TODO try to validate an accession against source
326 // We don't really know how to do this without querying source
334 public SOURCE getSource()
340 * @return the coordsys
342 public COORDINATES getCoordsys()