2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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 jalview.bin.Cache;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceI;
29 import jalview.util.MessageManager;
30 import jalview.ws.DasSequenceFeatureFetcher;
31 import jalview.ws.dbsources.das.api.jalviewSourceI;
32 import jalview.ws.seqfetcher.DbSourceProxy;
33 import jalview.ws.seqfetcher.DbSourceProxyImpl;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.StringTokenizer;
41 import java.util.Vector;
43 import org.biodas.jdas.client.SequenceClient;
44 import org.biodas.jdas.client.adapters.sequence.DasSequenceAdapter;
45 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
46 import org.biodas.jdas.client.threads.SequenceClientMultipleSources;
47 import org.biodas.jdas.schema.sequence.SEQUENCE;
48 import org.biodas.jdas.schema.sources.COORDINATES;
49 import org.biodas.jdas.schema.sources.SOURCE;
50 import org.biodas.jdas.schema.sources.VERSION;
52 import com.stevesoft.pat.Regex;
55 * an instance of this class is created for each unique DAS Sequence source (ie
56 * one capable of handling the 'sequence' for a particular MapMaster)
61 public class DasSequenceSource extends DbSourceProxyImpl implements
64 private jalviewSourceI jsrc;
66 protected SOURCE source = null;
68 protected VERSION version = null;
70 protected COORDINATES coordsys = null;
72 protected String dbname = "DASCS";
74 protected String dbrefname = "das:source";
76 protected MultipleConnectionPropertyProviderI connprops = null;
79 * DAS sources are tier 1 - if we have a direct DB connection then we should
85 * create a new DbSource proxy for a DAS 1 source
88 * Human Readable Name to use when fetching from this source
90 * DbRefName for DbRefs attached to sequences retrieved from this
95 * specific coordinate system to use for this source
97 * if source is not capable of the 'sequence' command
99 public DasSequenceSource(String dbname, String dbrefname, SOURCE source,
100 VERSION version, COORDINATES coordsys,
101 MultipleConnectionPropertyProviderI connprops) throws Exception
103 if (!(jsrc = new JalviewSource(source, connprops, false))
106 throw new Exception(MessageManager.formatMessage("exception.das_source_doesnt_support_sequence_command", new String[]{source.getTitle()}));
108 this.tier = 1 + ((jsrc.isLocal() || jsrc.isReferenceSource()) ? 0 : 1);
109 this.source = source;
110 this.dbname = dbname;
111 this.dbrefname = dbrefname.toUpperCase();
112 if (coordsys != null)
114 this.coordsys = coordsys;
116 this.connprops = connprops;
119 public String getAccessionSeparator()
124 public Regex getAccessionValidator()
127 return Regex.perlCode("m/([^:]+)(:\\d+,\\d+)?/");
130 public String getDbName()
133 return dbname + " (DAS)";
136 public String getDbSource()
141 public String getDbVersion()
143 return coordsys != null ? coordsys.getVersion() : "";
146 public AlignmentI getSequenceRecords(String queries) throws Exception
148 StringTokenizer st = new StringTokenizer(queries, "\t");
149 List<String> toks = new ArrayList<String>(), src = new ArrayList<String>(), acIds = new ArrayList<String>();
150 while (st.hasMoreTokens())
153 toks.add(t = st.nextToken());
154 acIds.add(t.replaceAll(":[0-9,]+", ""));
156 src.add(jsrc.getSourceURL());
157 Map<String, Map<List<String>, DasSequenceAdapter>> resultset = new HashMap<String, Map<List<String>, DasSequenceAdapter>>();
158 Map<String, Map<List<String>, Exception>> errors = new HashMap<String, Map<List<String>, Exception>>();
160 // First try multiple sources
161 boolean multiple = true, retry = false;
167 // slow, fetch one at a time.
168 for (String sr : src)
171 .println("Retrieving IDs individually from das source: "
173 org.biodas.jdas.client.SequenceClient sq = new SequenceClient(
174 connprops.getConnectionPropertyProviderFor(sr));
175 for (String q : toks)
177 List<String> qset = Arrays.asList(new String[]
181 DasSequenceAdapter s = sq.fetchData(sr, qset);
182 Map<List<String>, DasSequenceAdapter> dss = resultset.get(sr);
187 dss = new HashMap<List<String>, DasSequenceAdapter>());
190 } catch (Exception x)
192 Map<List<String>, Exception> ers = errors.get(sr);
195 errors.put(sr, ers = new HashMap<List<String>, Exception>());
204 SequenceClientMultipleSources sclient;
205 sclient = new SequenceClientMultipleSources();
206 sclient.fetchData(src, toks, resultset, errors);
208 while (!sclient.isTerminated())
214 } catch (InterruptedException x)
218 if (resultset.isEmpty() && !errors.isEmpty())
226 if (resultset.isEmpty())
228 System.err.println("Sequence Query to " + jsrc.getTitle() + " with '"
229 + queries + "' returned no sequences.");
234 Vector<SequenceI> seqs = null;
235 for (Map.Entry<String, Map<List<String>, DasSequenceAdapter>> resset : resultset
238 for (Map.Entry<List<String>, DasSequenceAdapter> result : resset
239 .getValue().entrySet())
241 DasSequenceAdapter dasseqresp = result.getValue();
242 List<String> accessions = result.getKey();
243 for (SEQUENCE e : dasseqresp.getSequence())
245 String lbl = e.getId();
247 if (acIds.indexOf(lbl) == -1)
250 .println("Warning - received sequence event for strange accession code ("
257 if (e.getContent().length() == 0)
260 .println("Empty sequence returned for accession code ("
264 + " (source is " + getDbName());
268 seqs = new java.util.Vector<SequenceI>();
269 // JDAS returns a sequence complete with any newlines and spaces
271 Sequence sq = new Sequence(lbl, e.getContent().replaceAll(
273 sq.setStart(e.getStart().intValue());
274 sq.addDBRef(new DBRefEntry(getDbSource(), getDbVersion()
275 + ":" + e.getVersion(), lbl));
282 if (seqs == null || seqs.size() == 0)
286 SequenceI[] sqs = new SequenceI[seqs.size()];
287 for (int i = 0, iSize = seqs.size(); i < iSize; i++)
289 sqs[i] = seqs.elementAt(i);
291 Alignment al = new Alignment(sqs);
292 if (jsrc.isFeatureSource())
294 java.util.Vector<jalviewSourceI> srcs = new java.util.Vector<jalviewSourceI>();
295 srcs.addElement(jsrc);
298 DasSequenceFeatureFetcher dssf = new DasSequenceFeatureFetcher(
299 sqs, null, srcs, false, false, multiple);
300 while (dssf.isRunning())
305 } catch (InterruptedException x)
311 } catch (Exception x)
314 .error("Couldn't retrieve features for sequence from its source.",
323 public String getTestQuery()
325 return coordsys == null ? "" : coordsys.getTestRange();
328 public boolean isValidReference(String accession)
330 // TODO try to validate an accession against source
331 // We don't really know how to do this without querying source
339 public SOURCE getSource()
345 * @return the coordsys
347 public COORDINATES getCoordsys()