2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.ws.dbsources.das.datamodel;
21 import java.text.ParseException;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.Hashtable;
25 import java.util.List;
28 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
29 import org.biodas.jdas.dassources.Capabilities;
30 import org.biodas.jdas.dassources.utils.DasTimeFormat;
31 import org.biodas.jdas.schema.sources.CAPABILITY;
32 import org.biodas.jdas.schema.sources.COORDINATES;
33 import org.biodas.jdas.schema.sources.MAINTAINER;
34 import org.biodas.jdas.schema.sources.PROP;
35 import org.biodas.jdas.schema.sources.SOURCE;
36 import org.biodas.jdas.schema.sources.VERSION;
38 import jalview.ws.dbsources.das.api.jalviewSourceI;
39 import jalview.ws.seqfetcher.DbSourceProxy;
41 public class JalviewSource implements jalviewSourceI
45 MultipleConnectionPropertyProviderI connprov;
47 public JalviewSource(SOURCE local2,
48 MultipleConnectionPropertyProviderI connprov, boolean local)
50 this.connprov = connprov;
56 public String getTitle()
58 return source.getTitle();
62 public VERSION getVersion()
65 return getVersionFor(source);
69 public String getDocHref()
71 return source.getDocHref();
75 public String getDescription()
77 return source.getDescription();
81 public String getUri()
83 return source.getUri();
87 public MAINTAINER getMAINTAINER()
89 return source.getMAINTAINER();
93 public String getEmail()
95 return (local) ? null : source.getMAINTAINER().getEmail();
98 boolean local = false;
101 public boolean isLocal()
107 public boolean isSequenceSource()
109 String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
110 for (String cp : getCapabilityList(getVersionFor(source)))
112 if (cp.equals(seqcap))
122 public boolean isFeatureSource()
124 String seqcap = "das1:" + Capabilities.FEATURES.getName();
125 for (String cp : getCapabilityList(getVersionFor(source)))
127 if (cp.equals(seqcap))
136 private VERSION getVersionFor(SOURCE ds)
138 VERSION latest = null;
139 for (VERSION v : ds.getVERSION())
142 || isLaterThan(latest.getCreated(), v.getCreated()))
144 // TODO: das 1.6 - should just get the first version - ignore other
145 // versions since not specified how to construct URL from version's URI
154 * compare date strings. null or unparseable dates are assumed to be oldest
158 * @return true iff ref comes before newer
160 private boolean isLaterThan(String ref, String newer)
162 Date refdate = null, newdate = null;
163 if (ref != null && ref.trim().length() > 0)
167 refdate = DasTimeFormat.fromDASString(ref.trim());
169 } catch (ParseException x)
173 if (newer != null && newer.trim().length() > 0)
177 newdate = DasTimeFormat.fromDASString(newer);
178 } catch (ParseException e)
186 return refdate.before(newdate);
194 // assume first instance of source is newest in list. - TODO: check if
195 // natural ordering of source versions is newest first or oldest first
199 public String[] getLabelsFor(VERSION v)
201 ArrayList<String> labels = new ArrayList<String>();
202 for (PROP p : v.getPROP())
204 if (p.getName().equalsIgnoreCase("LABEL"))
206 labels.add(p.getValue());
209 return labels.toArray(new String[0]);
212 private CAPABILITY getCapability(Capabilities capability)
214 for (CAPABILITY p : getVersion().getCAPABILITY())
216 if (p.getType().equalsIgnoreCase(capability.getName())
217 || p.getType().equalsIgnoreCase(
218 "das1:" + capability.getName()))
226 public String[] getCapabilityList(VERSION v)
229 ArrayList<String> labels = new ArrayList<String>();
230 for (CAPABILITY p : v.getCAPABILITY())
232 // TODO: work out what to do with namespace prefix
233 // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
234 // for moment, just show all capabilities...
235 if (p.getType().startsWith("das1:"))
237 labels.add(p.getType());
240 return labels.toArray(new String[0]);
244 public List<DbSourceProxy> getSequenceSourceProxies()
246 if (!isSequenceSource())
250 ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
253 VERSION v = getVersion();
254 Map<String, COORDINATES> latestc = new Hashtable<String, COORDINATES>();
255 for (COORDINATES cs : v.getCOORDINATES())
257 COORDINATES ltst = latestc.get(cs.getUri());
259 || ltst.getVersion() == null
260 || (ltst.getVersion() != null && cs.getVersion() != null && isLaterThan(
261 ltst.getVersion(), cs.getVersion())))
263 latestc.put(cs.getUri(), cs);
266 for (COORDINATES cs : latestc.values())
268 DasSequenceSource ds;
270 * if (css == null || css.length == 0) { // TODO: query das source
271 * directly to identify coordinate system... or // have to make up a
272 * coordinate system css = new DasCoordinateSystem[] { new
273 * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
274 * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
279 seqsources.add(ds = new DasSequenceSource(getTitle() + " ("
280 + cs.getAuthority() + " " + cs.getSource()
281 + (cs.getVersion() != null ? " " + cs.getVersion() : "")
282 + ")", cs.getAuthority(), source, v, cs, connprov));
283 if (seqsources.size() > 1)
285 System.err.println("Added another sequence DB source for "
286 + getTitle() + " (" + ds.getDbName() + ")");
288 } catch (Exception e)
290 System.err.println("Ignoring sequence coord system " + cs + " ("
291 + cs.getContent() + ") for source " + getTitle()
292 + "- threw exception when constructing fetcher.\n");
301 seqsources.add(new DasSequenceSource(getTitle(), getTitle(),
302 source, getVersion(), null, connprov));
303 } catch (Exception e)
305 // TODO Auto-generated catch block
310 if (seqsources.size() > 1)
313 DbSourceProxy[] tsort = seqsources.toArray(new DasSequenceSource[0]);
314 String[] nm = new String[tsort.length];
315 for (int i = 0; i < nm.length; i++)
317 nm[i] = tsort[i].getDbName().toLowerCase();
319 jalview.util.QuickSort.sort(nm, tsort);
321 for (DbSourceProxy ssrc : tsort)
323 seqsources.add(ssrc);
330 public String getSourceURL()
334 // kind of dumb, since
335 // org.biodas.jdas.dassources.utils.VersionAdapter.getSourceUriFromQueryUri()
337 // but this way, we can access non DAS 1.6 compliant sources (which have
338 // to have a URL like <sourcename>/das/ and cause a validation exception)
340 for (CAPABILITY cap : getVersion().getCAPABILITY())
342 String capname = cap.getType().substring(
343 cap.getType().indexOf(":") + 1);
344 int p = cap.getQueryUri().lastIndexOf(capname);
347 throw new Exception("Invalid das source: " + source.getUri());
349 if (cap.getQueryUri().charAt(p) == '/')
353 return cap.getQueryUri().substring(0, p);
355 } catch (Exception x)
357 System.err.println("Serious: Couldn't get the URL for source "
358 + source.getTitle());
365 public boolean isNewerThan(jalviewSourceI other)
367 return isLaterThan(getVersion().getCreated(), other.getVersion()
372 public boolean isReferenceSource()
374 // TODO check source object for indication that we are the primary for a DAS coordinate system