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.util.MessageManager;
24 import jalview.util.QuickSort;
25 import jalview.ws.dbsources.das.api.jalviewSourceI;
26 import jalview.ws.seqfetcher.DbSourceProxy;
28 import java.text.ParseException;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.Hashtable;
32 import java.util.List;
35 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
36 import org.biodas.jdas.dassources.Capabilities;
37 import org.biodas.jdas.dassources.utils.DasTimeFormat;
38 import org.biodas.jdas.schema.sources.CAPABILITY;
39 import org.biodas.jdas.schema.sources.COORDINATES;
40 import org.biodas.jdas.schema.sources.MAINTAINER;
41 import org.biodas.jdas.schema.sources.PROP;
42 import org.biodas.jdas.schema.sources.SOURCE;
43 import org.biodas.jdas.schema.sources.VERSION;
45 public class JalviewSource implements jalviewSourceI
49 MultipleConnectionPropertyProviderI connprov;
51 public JalviewSource(SOURCE local2,
52 MultipleConnectionPropertyProviderI connprov, boolean local)
54 this.connprov = connprov;
60 public String getTitle()
62 return source.getTitle();
66 public VERSION getVersion()
69 return getVersionFor(source);
73 public String getDocHref()
75 return source.getDocHref();
79 public String getDescription()
81 return source.getDescription();
85 public String getUri()
87 return source.getUri();
91 public MAINTAINER getMAINTAINER()
93 return source.getMAINTAINER();
97 public String getEmail()
99 return (local) ? null : source.getMAINTAINER().getEmail();
102 boolean local = false;
105 public boolean isLocal()
111 public boolean isSequenceSource()
113 String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
114 for (String cp : getCapabilityList(getVersionFor(source)))
116 if (cp.equals(seqcap))
126 public boolean isFeatureSource()
128 String seqcap = "das1:" + Capabilities.FEATURES.getName();
129 for (String cp : getCapabilityList(getVersionFor(source)))
131 if (cp.equals(seqcap))
140 private VERSION getVersionFor(SOURCE ds)
142 VERSION latest = null;
143 for (VERSION v : ds.getVERSION())
146 || isLaterThan(latest.getCreated(), v.getCreated()))
148 // TODO: das 1.6 - should just get the first version - ignore other
149 // versions since not specified how to construct URL from version's URI
158 * compare date strings. null or unparseable dates are assumed to be oldest
162 * @return true iff ref comes before newer
164 private boolean isLaterThan(String ref, String newer)
166 Date refdate = null, newdate = null;
167 if (ref != null && ref.trim().length() > 0)
171 refdate = DasTimeFormat.fromDASString(ref.trim());
173 } catch (ParseException x)
177 if (newer != null && newer.trim().length() > 0)
181 newdate = DasTimeFormat.fromDASString(newer);
182 } catch (ParseException e)
190 return refdate.before(newdate);
198 // assume first instance of source is newest in list. - TODO: check if
199 // natural ordering of source versions is newest first or oldest first
203 public String[] getLabelsFor(VERSION v)
205 ArrayList<String> labels = new ArrayList<String>();
206 for (PROP p : v.getPROP())
208 if (p.getName().equalsIgnoreCase("LABEL"))
210 labels.add(p.getValue());
213 return labels.toArray(new String[0]);
216 private CAPABILITY getCapability(Capabilities capability)
218 for (CAPABILITY p : getVersion().getCAPABILITY())
220 if (p.getType().equalsIgnoreCase(capability.getName())
221 || p.getType().equalsIgnoreCase(
222 "das1:" + capability.getName()))
230 public String[] getCapabilityList(VERSION v)
233 ArrayList<String> labels = new ArrayList<String>();
234 for (CAPABILITY p : v.getCAPABILITY())
236 // TODO: work out what to do with namespace prefix
237 // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
238 // for moment, just show all capabilities...
239 if (p.getType().startsWith("das1:"))
241 labels.add(p.getType());
244 return labels.toArray(new String[0]);
248 public List<DbSourceProxy> getSequenceSourceProxies()
250 if (!isSequenceSource())
254 ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
257 VERSION v = getVersion();
258 Map<String, COORDINATES> latestc = new Hashtable<String, COORDINATES>();
259 for (COORDINATES cs : v.getCOORDINATES())
261 COORDINATES ltst = latestc.get(cs.getUri());
263 || ltst.getVersion() == null
264 || (ltst.getVersion() != null && cs.getVersion() != null && isLaterThan(
265 ltst.getVersion(), cs.getVersion())))
267 latestc.put(cs.getUri(), cs);
270 for (COORDINATES cs : latestc.values())
272 DasSequenceSource ds;
274 * if (css == null || css.length == 0) { // TODO: query das source
275 * directly to identify coordinate system... or // have to make up a
276 * coordinate system css = new DasCoordinateSystem[] { new
277 * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
278 * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
283 seqsources.add(ds = new DasSequenceSource(getTitle() + " ("
284 + cs.getAuthority() + " " + cs.getSource()
285 + (cs.getVersion() != null ? " " + cs.getVersion() : "")
286 + ")", cs.getAuthority(), source, v, cs, connprov));
287 if (seqsources.size() > 1)
289 System.err.println("Added another sequence DB source for "
290 + getTitle() + " (" + ds.getDbName() + ")");
292 } catch (Exception e)
294 System.err.println("Ignoring sequence coord system " + cs + " ("
295 + cs.getContent() + ") for source " + getTitle()
296 + "- threw exception when constructing fetcher.\n");
305 seqsources.add(new DasSequenceSource(getTitle(), getTitle(),
306 source, getVersion(), null, connprov));
307 } catch (Exception e)
309 // TODO Auto-generated catch block
314 if (seqsources.size() > 1)
317 DbSourceProxy[] tsort = seqsources.toArray(new DasSequenceSource[0]);
318 String[] nm = new String[tsort.length];
319 for (int i = 0; i < nm.length; i++)
321 nm[i] = tsort[i].getDbName().toLowerCase();
323 QuickSort.sort(nm, tsort);
325 for (DbSourceProxy ssrc : tsort)
327 seqsources.add(ssrc);
334 public String getSourceURL()
338 // kind of dumb, since
339 // org.biodas.jdas.dassources.utils.VersionAdapter.getSourceUriFromQueryUri()
341 // but this way, we can access non DAS 1.6 compliant sources (which have
342 // to have a URL like <sourcename>/das/ and cause a validation exception)
344 for (CAPABILITY cap : getVersion().getCAPABILITY())
346 String capname = cap.getType().substring(
347 cap.getType().indexOf(":") + 1);
348 int p = cap.getQueryUri().lastIndexOf(capname);
351 throw new Exception(MessageManager.formatMessage("exception.invalid_das_source", new String[]{source.getUri()}));
353 if (cap.getQueryUri().charAt(p) == '/')
357 return cap.getQueryUri().substring(0, p);
359 } catch (Exception x)
361 System.err.println("Serious: Couldn't get the URL for source "
362 + source.getTitle());
369 public boolean isNewerThan(jalviewSourceI other)
371 return isLaterThan(getVersion().getCreated(), other.getVersion()
376 public boolean isReferenceSource()
378 // TODO check source object for indication that we are the primary for a DAS