2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.ws.dbsources.das.datamodel;
20 import java.text.ParseException;
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.Hashtable;
24 import java.util.List;
27 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
28 import org.biodas.jdas.dassources.Capabilities;
29 import org.biodas.jdas.dassources.utils.DasTimeFormat;
30 import org.biodas.jdas.schema.sources.CAPABILITY;
31 import org.biodas.jdas.schema.sources.COORDINATES;
32 import org.biodas.jdas.schema.sources.MAINTAINER;
33 import org.biodas.jdas.schema.sources.PROP;
34 import org.biodas.jdas.schema.sources.SOURCE;
35 import org.biodas.jdas.schema.sources.VERSION;
37 import jalview.ws.dbsources.das.api.jalviewSourceI;
38 import jalview.ws.seqfetcher.DbSourceProxy;
40 public class JalviewSource implements jalviewSourceI
44 MultipleConnectionPropertyProviderI connprov;
46 public JalviewSource(SOURCE local2,
47 MultipleConnectionPropertyProviderI connprov, boolean local)
49 this.connprov = connprov;
55 public String getTitle()
57 return source.getTitle();
61 public VERSION getVersion()
64 return getVersionFor(source);
68 public String getDocHref()
70 return source.getDocHref();
74 public String getDescription()
76 return source.getDescription();
80 public String getUri()
82 return source.getUri();
86 public MAINTAINER getMAINTAINER()
88 return source.getMAINTAINER();
92 public String getEmail()
94 return (local) ? null : source.getMAINTAINER().getEmail();
97 boolean local = false;
100 public boolean isLocal()
106 public boolean isSequenceSource()
108 String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
109 for (String cp : getCapabilityList(getVersionFor(source)))
111 if (cp.equals(seqcap))
121 public boolean isFeatureSource()
123 String seqcap = "das1:" + Capabilities.FEATURES.getName();
124 for (String cp : getCapabilityList(getVersionFor(source)))
126 if (cp.equals(seqcap))
135 private VERSION getVersionFor(SOURCE ds)
137 VERSION latest = null;
138 for (VERSION v : ds.getVERSION())
141 || isLaterThan(latest.getCreated(), v.getCreated()))
143 // TODO: das 1.6 - should just get the first version - ignore other
144 // versions since not specified how to construct URL from version's URI
153 * compare date strings. null or unparseable dates are assumed to be oldest
157 * @return true iff ref comes before newer
159 private boolean isLaterThan(String ref, String newer)
161 Date refdate = null, newdate = null;
162 if (ref != null && ref.trim().length() > 0)
166 refdate = DasTimeFormat.fromDASString(ref.trim());
168 } catch (ParseException x)
172 if (newer != null && newer.trim().length() > 0)
176 newdate = DasTimeFormat.fromDASString(newer);
177 } catch (ParseException e)
185 return refdate.before(newdate);
193 // assume first instance of source is newest in list. - TODO: check if
194 // natural ordering of source versions is newest first or oldest first
198 public String[] getLabelsFor(VERSION v)
200 ArrayList<String> labels = new ArrayList<String>();
201 for (PROP p : v.getPROP())
203 if (p.getName().equalsIgnoreCase("LABEL"))
205 labels.add(p.getValue());
208 return labels.toArray(new String[0]);
211 private CAPABILITY getCapability(Capabilities capability)
213 for (CAPABILITY p : getVersion().getCAPABILITY())
215 if (p.getType().equalsIgnoreCase(capability.getName())
216 || p.getType().equalsIgnoreCase(
217 "das1:" + capability.getName()))
225 public String[] getCapabilityList(VERSION v)
228 ArrayList<String> labels = new ArrayList<String>();
229 for (CAPABILITY p : v.getCAPABILITY())
231 // TODO: work out what to do with namespace prefix
232 // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
233 // for moment, just show all capabilities...
234 if (p.getType().startsWith("das1:"))
236 labels.add(p.getType());
239 return labels.toArray(new String[0]);
243 public List<DbSourceProxy> getSequenceSourceProxies()
245 if (!isSequenceSource())
249 ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
252 VERSION v = getVersion();
253 Map<String, COORDINATES> latestc = new Hashtable<String, COORDINATES>();
254 for (COORDINATES cs : v.getCOORDINATES())
256 COORDINATES ltst = latestc.get(cs.getUri());
258 || ltst.getVersion() == null
259 || (ltst.getVersion() != null && cs.getVersion() != null && isLaterThan(
260 ltst.getVersion(), cs.getVersion())))
262 latestc.put(cs.getUri(), cs);
265 for (COORDINATES cs : latestc.values())
267 DasSequenceSource ds;
269 * if (css == null || css.length == 0) { // TODO: query das source
270 * directly to identify coordinate system... or // have to make up a
271 * coordinate system css = new DasCoordinateSystem[] { new
272 * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
273 * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
278 seqsources.add(ds = new DasSequenceSource(getTitle() + " ("
279 + cs.getAuthority() + " " + cs.getSource()
280 + (cs.getVersion() != null ? " " + cs.getVersion() : "")
281 + ")", cs.getAuthority(), source, v, cs, connprov));
282 if (seqsources.size() > 1)
284 System.err.println("Added another sequence DB source for "
285 + getTitle() + " (" + ds.getDbName() + ")");
287 } catch (Exception e)
289 System.err.println("Ignoring sequence coord system " + cs + " ("
290 + cs.getContent() + ") for source " + getTitle()
291 + "- threw exception when constructing fetcher.\n");
300 seqsources.add(new DasSequenceSource(getTitle(), getTitle(),
301 source, getVersion(), null, connprov));
302 } catch (Exception e)
304 // TODO Auto-generated catch block
309 if (seqsources.size() > 1)
312 DbSourceProxy[] tsort = seqsources.toArray(new DasSequenceSource[0]);
313 String[] nm = new String[tsort.length];
314 for (int i = 0; i < nm.length; i++)
316 nm[i] = tsort[i].getDbName().toLowerCase();
318 jalview.util.QuickSort.sort(nm, tsort);
320 for (DbSourceProxy ssrc : tsort)
322 seqsources.add(ssrc);
329 public String getSourceURL()
333 // kind of dumb, since
334 // org.biodas.jdas.dassources.utils.VersionAdapter.getSourceUriFromQueryUri()
336 // but this way, we can access non DAS 1.6 compliant sources (which have
337 // to have a URL like <sourcename>/das/ and cause a validation exception)
339 for (CAPABILITY cap : getVersion().getCAPABILITY())
341 String capname = cap.getType().substring(
342 cap.getType().indexOf(":") + 1);
343 int p = cap.getQueryUri().lastIndexOf(capname);
346 throw new Exception("Invalid das source: " + source.getUri());
348 if (cap.getQueryUri().charAt(p) == '/')
352 return cap.getQueryUri().substring(0, p);
354 } catch (Exception x)
356 System.err.println("Serious: Couldn't get the URL for source "
357 + source.getTitle());
364 public boolean isNewerThan(jalviewSourceI other)
366 return isLaterThan(getVersion().getCreated(), other.getVersion()