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.text.ParseException;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.Hashtable;
27 import java.util.List;
30 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
31 import org.biodas.jdas.dassources.Capabilities;
32 import org.biodas.jdas.dassources.utils.DasTimeFormat;
33 import org.biodas.jdas.schema.sources.CAPABILITY;
34 import org.biodas.jdas.schema.sources.COORDINATES;
35 import org.biodas.jdas.schema.sources.MAINTAINER;
36 import org.biodas.jdas.schema.sources.PROP;
37 import org.biodas.jdas.schema.sources.SOURCE;
38 import org.biodas.jdas.schema.sources.VERSION;
40 import jalview.ws.dbsources.das.api.jalviewSourceI;
41 import jalview.ws.seqfetcher.DbSourceProxy;
43 public class JalviewSource implements jalviewSourceI
47 MultipleConnectionPropertyProviderI connprov;
49 public JalviewSource(SOURCE local2,
50 MultipleConnectionPropertyProviderI connprov, boolean local)
52 this.connprov = connprov;
58 public String getTitle()
60 return source.getTitle();
64 public VERSION getVersion()
67 return getVersionFor(source);
71 public String getDocHref()
73 return source.getDocHref();
77 public String getDescription()
79 return source.getDescription();
83 public String getUri()
85 return source.getUri();
89 public MAINTAINER getMAINTAINER()
91 return source.getMAINTAINER();
95 public String getEmail()
97 return (local) ? null : source.getMAINTAINER().getEmail();
100 boolean local = false;
103 public boolean isLocal()
109 public boolean isSequenceSource()
111 String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
112 for (String cp : getCapabilityList(getVersionFor(source)))
114 if (cp.equals(seqcap))
124 public boolean isFeatureSource()
126 String seqcap = "das1:" + Capabilities.FEATURES.getName();
127 for (String cp : getCapabilityList(getVersionFor(source)))
129 if (cp.equals(seqcap))
138 private VERSION getVersionFor(SOURCE ds)
140 VERSION latest = null;
141 for (VERSION v : ds.getVERSION())
144 || isLaterThan(latest.getCreated(), v.getCreated()))
146 // TODO: das 1.6 - should just get the first version - ignore other
147 // versions since not specified how to construct URL from version's URI
156 * compare date strings. null or unparseable dates are assumed to be oldest
160 * @return true iff ref comes before newer
162 private boolean isLaterThan(String ref, String newer)
164 Date refdate = null, newdate = null;
165 if (ref != null && ref.trim().length() > 0)
169 refdate = DasTimeFormat.fromDASString(ref.trim());
171 } catch (ParseException x)
175 if (newer != null && newer.trim().length() > 0)
179 newdate = DasTimeFormat.fromDASString(newer);
180 } catch (ParseException e)
188 return refdate.before(newdate);
196 // assume first instance of source is newest in list. - TODO: check if
197 // natural ordering of source versions is newest first or oldest first
201 public String[] getLabelsFor(VERSION v)
203 ArrayList<String> labels = new ArrayList<String>();
204 for (PROP p : v.getPROP())
206 if (p.getName().equalsIgnoreCase("LABEL"))
208 labels.add(p.getValue());
211 return labels.toArray(new String[0]);
214 private CAPABILITY getCapability(Capabilities capability)
216 for (CAPABILITY p : getVersion().getCAPABILITY())
218 if (p.getType().equalsIgnoreCase(capability.getName())
219 || p.getType().equalsIgnoreCase(
220 "das1:" + capability.getName()))
228 public String[] getCapabilityList(VERSION v)
231 ArrayList<String> labels = new ArrayList<String>();
232 for (CAPABILITY p : v.getCAPABILITY())
234 // TODO: work out what to do with namespace prefix
235 // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
236 // for moment, just show all capabilities...
237 if (p.getType().startsWith("das1:"))
239 labels.add(p.getType());
242 return labels.toArray(new String[0]);
246 public List<DbSourceProxy> getSequenceSourceProxies()
248 if (!isSequenceSource())
252 ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
255 VERSION v = getVersion();
256 Map<String, COORDINATES> latestc = new Hashtable<String, COORDINATES>();
257 for (COORDINATES cs : v.getCOORDINATES())
259 COORDINATES ltst = latestc.get(cs.getUri());
261 || ltst.getVersion() == null
262 || (ltst.getVersion() != null && cs.getVersion() != null && isLaterThan(
263 ltst.getVersion(), cs.getVersion())))
265 latestc.put(cs.getUri(), cs);
268 for (COORDINATES cs : latestc.values())
270 DasSequenceSource ds;
272 * if (css == null || css.length == 0) { // TODO: query das source
273 * directly to identify coordinate system... or // have to make up a
274 * coordinate system css = new DasCoordinateSystem[] { new
275 * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
276 * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
281 seqsources.add(ds = new DasSequenceSource(getTitle() + " ("
282 + cs.getAuthority() + " " + cs.getSource()
283 + (cs.getVersion() != null ? " " + cs.getVersion() : "")
284 + ")", cs.getAuthority(), source, v, cs, connprov));
285 if (seqsources.size() > 1)
287 System.err.println("Added another sequence DB source for "
288 + getTitle() + " (" + ds.getDbName() + ")");
290 } catch (Exception e)
292 System.err.println("Ignoring sequence coord system " + cs + " ("
293 + cs.getContent() + ") for source " + getTitle()
294 + "- threw exception when constructing fetcher.\n");
303 seqsources.add(new DasSequenceSource(getTitle(), getTitle(),
304 source, getVersion(), null, connprov));
305 } catch (Exception e)
307 // TODO Auto-generated catch block
312 if (seqsources.size() > 1)
315 DbSourceProxy[] tsort = seqsources.toArray(new DasSequenceSource[0]);
316 String[] nm = new String[tsort.length];
317 for (int i = 0; i < nm.length; i++)
319 nm[i] = tsort[i].getDbName().toLowerCase();
321 jalview.util.QuickSort.sort(nm, tsort);
323 for (DbSourceProxy ssrc : tsort)
325 seqsources.add(ssrc);
332 public String getSourceURL()
336 // kind of dumb, since
337 // org.biodas.jdas.dassources.utils.VersionAdapter.getSourceUriFromQueryUri()
339 // but this way, we can access non DAS 1.6 compliant sources (which have
340 // to have a URL like <sourcename>/das/ and cause a validation exception)
342 for (CAPABILITY cap : getVersion().getCAPABILITY())
344 String capname = cap.getType().substring(
345 cap.getType().indexOf(":") + 1);
346 int p = cap.getQueryUri().lastIndexOf(capname);
349 throw new Exception("Invalid das source: " + source.getUri());
351 if (cap.getQueryUri().charAt(p) == '/')
355 return cap.getQueryUri().substring(0, p);
357 } catch (Exception x)
359 System.err.println("Serious: Couldn't get the URL for source "
360 + source.getTitle());
367 public boolean isNewerThan(jalviewSourceI other)
369 return isLaterThan(getVersion().getCreated(), other.getVersion()
374 public boolean isReferenceSource()
376 // TODO check source object for indication that we are the primary for a DAS