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.dbsources.das.api.jalviewSourceI;
31 import jalview.ws.seqfetcher.DbSourceProxy;
32 import jalview.ws.seqfetcher.DbSourceProxyImpl;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.StringTokenizer;
40 import java.util.Vector;
42 import org.biodas.jdas.client.SequenceClient;
43 import org.biodas.jdas.client.adapters.sequence.DasSequenceAdapter;
44 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
45 import org.biodas.jdas.client.threads.SequenceClientMultipleSources;
46 import org.biodas.jdas.schema.sequence.SEQUENCE;
47 import org.biodas.jdas.schema.sources.COORDINATES;
48 import org.biodas.jdas.schema.sources.SOURCE;
49 import org.biodas.jdas.schema.sources.VERSION;
51 import com.stevesoft.pat.Regex;
54 * an instance of this class is created for each unique DAS Sequence source (ie
55 * one capable of handling the 'sequence' for a particular MapMaster)
60 public class DasSequenceSource extends DbSourceProxyImpl implements
63 private jalviewSourceI jsrc;
65 protected SOURCE source = null;
67 protected VERSION version = null;
69 protected COORDINATES coordsys = null;
71 protected String dbname = "DASCS";
73 protected String dbrefname = "das:source";
75 protected MultipleConnectionPropertyProviderI connprops = null;
78 * DAS sources are tier 1 - if we have a direct DB connection then we should
84 * create a new DbSource proxy for a DAS 1 source
87 * Human Readable Name to use when fetching from this source
89 * DbRefName for DbRefs attached to sequences retrieved from this
94 * specific coordinate system to use for this source
96 * if source is not capable of the 'sequence' command
98 public DasSequenceSource(String dbname, String dbrefname, SOURCE source,
99 VERSION version, COORDINATES coordsys,
100 MultipleConnectionPropertyProviderI connprops) throws Exception
102 if (!(jsrc = new JalviewSource(source, connprops, false))
105 throw new Exception(MessageManager.formatMessage(
106 "exception.das_source_doesnt_support_sequence_command",
107 new String[] { source.getTitle() }));
109 this.tier = 1 + ((jsrc.isLocal() || jsrc.isReferenceSource()) ? 0 : 1);
110 this.source = source;
111 this.dbname = dbname;
112 this.dbrefname = dbrefname.toUpperCase();
113 if (coordsys != null)
115 this.coordsys = coordsys;
117 this.connprops = connprops;
120 public String getAccessionSeparator()
125 public Regex getAccessionValidator()
128 return Regex.perlCode("m/([^:]+)(:\\d+,\\d+)?/");
131 public String getDbName()
134 return dbname + " (DAS)";
137 public String getDbSource()
142 public String getDbVersion()
144 return coordsys != null ? coordsys.getVersion() : "";
147 public AlignmentI getSequenceRecords(String queries) throws Exception
149 StringTokenizer st = new StringTokenizer(queries, "\t");
150 List<String> toks = new ArrayList<String>(), src = new ArrayList<String>(), acIds = new ArrayList<String>();
151 while (st.hasMoreTokens())
154 toks.add(t = st.nextToken());
155 acIds.add(t.replaceAll(":[0-9,]+", ""));
157 src.add(jsrc.getSourceURL());
158 Map<String, Map<List<String>, DasSequenceAdapter>> resultset = new HashMap<String, Map<List<String>, DasSequenceAdapter>>();
159 Map<String, Map<List<String>, Exception>> errors = new HashMap<String, Map<List<String>, Exception>>();
161 // First try multiple sources
162 boolean multiple = true, retry = false;
168 // slow, fetch one at a time.
169 for (String sr : src)
172 .println("Retrieving IDs individually from das source: "
174 org.biodas.jdas.client.SequenceClient sq = new SequenceClient(
175 connprops.getConnectionPropertyProviderFor(sr));
176 for (String q : toks)
178 List<String> qset = Arrays.asList(new String[] { q });
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)
284 SequenceI[] sqs = new SequenceI[seqs.size()];
285 for (int i = 0, iSize = seqs.size(); i < iSize; i++)
287 sqs[i] = (SequenceI) seqs.elementAt(i);
289 Alignment al = new Alignment(sqs);
290 if (jsrc.isFeatureSource())
292 java.util.Vector<jalviewSourceI> srcs = new java.util.Vector<jalviewSourceI>();
293 srcs.addElement(jsrc);
296 jalview.ws.DasSequenceFeatureFetcher dssf = new jalview.ws.DasSequenceFeatureFetcher(
297 sqs, null, srcs, false, false, multiple);
298 while (dssf.isRunning())
303 } catch (InterruptedException x)
309 } catch (Exception x)
312 .error("Couldn't retrieve features for sequence from its source.",
321 public String getTestQuery()
323 return coordsys == null ? "" : coordsys.getTestRange();
326 public boolean isValidReference(String accession)
328 // TODO try to validate an accession against source
329 // We don't really know how to do this without querying source
337 public SOURCE getSource()
343 * @return the coordsys
345 public COORDINATES getCoordsys()