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 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.util.MessageManager;
43 import jalview.ws.dbsources.das.api.jalviewSourceI;
44 import jalview.ws.seqfetcher.*;
45 import jalview.bin.Cache;
46 import jalview.datamodel.Alignment;
47 import jalview.datamodel.AlignmentI;
48 import jalview.datamodel.DBRefEntry;
49 import jalview.datamodel.Sequence;
50 import jalview.datamodel.SequenceI;
53 * an instance of this class is created for each unique DAS Sequence source (ie
54 * one capable of handling the 'sequence' for a particular MapMaster)
59 public class DasSequenceSource extends DbSourceProxyImpl implements
62 private jalviewSourceI jsrc;
64 protected SOURCE source = null;
66 protected VERSION version = null;
68 protected COORDINATES coordsys = null;
70 protected String dbname = "DASCS";
72 protected String dbrefname = "das:source";
74 protected MultipleConnectionPropertyProviderI connprops = null;
77 * DAS sources are tier 1 - if we have a direct DB connection then we should
83 * create a new DbSource proxy for a DAS 1 source
86 * Human Readable Name to use when fetching from this source
88 * DbRefName for DbRefs attached to sequences retrieved from this
93 * specific coordinate system to use for this source
95 * if source is not capable of the 'sequence' command
97 public DasSequenceSource(String dbname, String dbrefname, SOURCE source,
98 VERSION version, COORDINATES coordsys,
99 MultipleConnectionPropertyProviderI connprops) throws Exception
101 if (!(jsrc = new JalviewSource(source, connprops, false))
104 throw new Exception(MessageManager.formatMessage("exception.das_source_doesnt_support_sequence_command", new String[]{source.getTitle()}));
106 this.tier = 1 + ((jsrc.isLocal() || jsrc.isReferenceSource()) ? 0 : 1);
107 this.source = source;
108 this.dbname = dbname;
109 this.dbrefname = dbrefname.toUpperCase();
110 if (coordsys != null)
112 this.coordsys = coordsys;
114 this.connprops = connprops;
117 public String getAccessionSeparator()
122 public Regex getAccessionValidator()
125 return Regex.perlCode("m/([^:]+)(:\\d+,\\d+)?/");
128 public String getDbName()
131 return dbname + " (DAS)";
134 public String getDbSource()
139 public String getDbVersion()
141 return coordsys != null ? coordsys.getVersion() : "";
144 public AlignmentI getSequenceRecords(String queries) throws Exception
146 StringTokenizer st = new StringTokenizer(queries, "\t");
147 List<String> toks = new ArrayList<String>(), src = new ArrayList<String>(), acIds = new ArrayList<String>();
148 while (st.hasMoreTokens())
151 toks.add(t = st.nextToken());
152 acIds.add(t.replaceAll(":[0-9,]+", ""));
154 src.add(jsrc.getSourceURL());
155 Map<String, Map<List<String>, DasSequenceAdapter>> resultset = new HashMap<String, Map<List<String>, DasSequenceAdapter>>();
156 Map<String, Map<List<String>, Exception>> errors = new HashMap<String, Map<List<String>, Exception>>();
158 // First try multiple sources
159 boolean multiple = true, retry = false;
165 // slow, fetch one at a time.
166 for (String sr : src)
169 .println("Retrieving IDs individually from das source: "
171 org.biodas.jdas.client.SequenceClient sq = new SequenceClient(
172 connprops.getConnectionPropertyProviderFor(sr));
173 for (String q : toks)
175 List<String> qset = Arrays.asList(new String[]
179 DasSequenceAdapter s = sq.fetchData(sr, qset);
180 Map<List<String>, DasSequenceAdapter> dss = resultset.get(sr);
185 dss = new HashMap<List<String>, DasSequenceAdapter>());
188 } catch (Exception x)
190 Map<List<String>, Exception> ers = errors.get(sr);
193 errors.put(sr, ers = new HashMap<List<String>, Exception>());
202 SequenceClientMultipleSources sclient;
203 sclient = new SequenceClientMultipleSources();
204 sclient.fetchData(src, toks, resultset, errors);
206 while (!sclient.isTerminated())
212 } catch (InterruptedException x)
216 if (resultset.isEmpty() && !errors.isEmpty())
224 if (resultset.isEmpty())
226 System.err.println("Sequence Query to " + jsrc.getTitle() + " with '"
227 + queries + "' returned no sequences.");
232 Vector<SequenceI> seqs = null;
233 for (Map.Entry<String, Map<List<String>, DasSequenceAdapter>> resset : resultset
236 for (Map.Entry<List<String>, DasSequenceAdapter> result : resset
237 .getValue().entrySet())
239 DasSequenceAdapter dasseqresp = result.getValue();
240 List<String> accessions = result.getKey();
241 for (SEQUENCE e : dasseqresp.getSequence())
243 String lbl = e.getId();
245 if (acIds.indexOf(lbl) == -1)
248 .println("Warning - received sequence event for strange accession code ("
255 if (e.getContent().length() == 0)
258 .println("Empty sequence returned for accession code ("
262 + " (source is " + getDbName());
266 seqs = new java.util.Vector<SequenceI>();
267 // JDAS returns a sequence complete with any newlines and spaces
269 Sequence sq = new Sequence(lbl, e.getContent().replaceAll(
271 sq.setStart(e.getStart().intValue());
272 sq.addDBRef(new DBRefEntry(getDbSource(), getDbVersion()
273 + ":" + e.getVersion(), lbl));
280 if (seqs == null || seqs.size() == 0)
282 SequenceI[] sqs = new SequenceI[seqs.size()];
283 for (int i = 0, iSize = seqs.size(); i < iSize; i++)
285 sqs[i] = (SequenceI) seqs.elementAt(i);
287 Alignment al = new Alignment(sqs);
288 if (jsrc.isFeatureSource())
290 java.util.Vector<jalviewSourceI> srcs = new java.util.Vector<jalviewSourceI>();
291 srcs.addElement(jsrc);
294 jalview.ws.DasSequenceFeatureFetcher dssf = new jalview.ws.DasSequenceFeatureFetcher(
295 sqs, null, srcs, false, false, multiple);
296 while (dssf.isRunning())
301 } catch (InterruptedException x)
307 } catch (Exception x)
310 .error("Couldn't retrieve features for sequence from its source.",
319 public String getTestQuery()
321 return coordsys == null ? "" : coordsys.getTestRange();
324 public boolean isValidReference(String accession)
326 // TODO try to validate an accession against source
327 // We don't really know how to do this without querying source
335 public SOURCE getSource()
341 * @return the coordsys
343 public COORDINATES getCoordsys()