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.ws.dbsources.das.api.jalviewSourceI;
25 import jalview.ws.seqfetcher.DbSourceProxy;
27 import java.text.ParseException;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.Hashtable;
31 import java.util.List;
34 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
35 import org.biodas.jdas.dassources.Capabilities;
36 import org.biodas.jdas.dassources.utils.DasTimeFormat;
37 import org.biodas.jdas.schema.sources.CAPABILITY;
38 import org.biodas.jdas.schema.sources.COORDINATES;
39 import org.biodas.jdas.schema.sources.MAINTAINER;
40 import org.biodas.jdas.schema.sources.PROP;
41 import org.biodas.jdas.schema.sources.SOURCE;
42 import org.biodas.jdas.schema.sources.VERSION;
44 public class JalviewSource implements jalviewSourceI
48 MultipleConnectionPropertyProviderI connprov;
50 public JalviewSource(SOURCE local2,
51 MultipleConnectionPropertyProviderI connprov, boolean local)
53 this.connprov = connprov;
59 public String getTitle()
61 return source.getTitle();
65 public VERSION getVersion()
68 return getVersionFor(source);
72 public String getDocHref()
74 return source.getDocHref();
78 public String getDescription()
80 return source.getDescription();
84 public String getUri()
86 return source.getUri();
90 public MAINTAINER getMAINTAINER()
92 return source.getMAINTAINER();
96 public String getEmail()
98 return (local) ? null : source.getMAINTAINER().getEmail();
101 boolean local = false;
104 public boolean isLocal()
110 public boolean isSequenceSource()
112 String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
113 for (String cp : getCapabilityList(getVersionFor(source)))
115 if (cp.equals(seqcap))
125 public boolean isFeatureSource()
127 String seqcap = "das1:" + Capabilities.FEATURES.getName();
128 for (String cp : getCapabilityList(getVersionFor(source)))
130 if (cp.equals(seqcap))
139 private VERSION getVersionFor(SOURCE ds)
141 VERSION latest = null;
142 for (VERSION v : ds.getVERSION())
145 || isLaterThan(latest.getCreated(), v.getCreated()))
147 // TODO: das 1.6 - should just get the first version - ignore other
148 // versions since not specified how to construct URL from version's URI
157 * compare date strings. null or unparseable dates are assumed to be oldest
161 * @return true iff ref comes before newer
163 private boolean isLaterThan(String ref, String newer)
165 Date refdate = null, newdate = null;
166 if (ref != null && ref.trim().length() > 0)
170 refdate = DasTimeFormat.fromDASString(ref.trim());
172 } catch (ParseException x)
176 if (newer != null && newer.trim().length() > 0)
180 newdate = DasTimeFormat.fromDASString(newer);
181 } catch (ParseException e)
189 return refdate.before(newdate);
197 // assume first instance of source is newest in list. - TODO: check if
198 // natural ordering of source versions is newest first or oldest first
202 public String[] getLabelsFor(VERSION v)
204 ArrayList<String> labels = new ArrayList<String>();
205 for (PROP p : v.getPROP())
207 if (p.getName().equalsIgnoreCase("LABEL"))
209 labels.add(p.getValue());
212 return labels.toArray(new String[0]);
215 private CAPABILITY getCapability(Capabilities capability)
217 for (CAPABILITY p : getVersion().getCAPABILITY())
219 if (p.getType().equalsIgnoreCase(capability.getName())
220 || p.getType().equalsIgnoreCase(
221 "das1:" + capability.getName()))
229 public String[] getCapabilityList(VERSION v)
232 ArrayList<String> labels = new ArrayList<String>();
233 for (CAPABILITY p : v.getCAPABILITY())
235 // TODO: work out what to do with namespace prefix
236 // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
237 // for moment, just show all capabilities...
238 if (p.getType().startsWith("das1:"))
240 labels.add(p.getType());
243 return labels.toArray(new String[0]);
247 public List<DbSourceProxy> getSequenceSourceProxies()
249 if (!isSequenceSource())
253 ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
256 VERSION v = getVersion();
257 Map<String, COORDINATES> latestc = new Hashtable<String, COORDINATES>();
258 for (COORDINATES cs : v.getCOORDINATES())
260 COORDINATES ltst = latestc.get(cs.getUri());
262 || ltst.getVersion() == null
263 || (ltst.getVersion() != null && cs.getVersion() != null && isLaterThan(
264 ltst.getVersion(), cs.getVersion())))
266 latestc.put(cs.getUri(), cs);
269 for (COORDINATES cs : latestc.values())
271 DasSequenceSource ds;
273 * if (css == null || css.length == 0) { // TODO: query das source
274 * directly to identify coordinate system... or // have to make up a
275 * coordinate system css = new DasCoordinateSystem[] { new
276 * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
277 * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
282 seqsources.add(ds = new DasSequenceSource(getTitle() + " ("
283 + cs.getAuthority() + " " + cs.getSource()
284 + (cs.getVersion() != null ? " " + cs.getVersion() : "")
285 + ")", cs.getAuthority(), source, v, cs, connprov));
286 if (seqsources.size() > 1)
288 System.err.println("Added another sequence DB source for "
289 + getTitle() + " (" + ds.getDbName() + ")");
291 } catch (Exception e)
293 System.err.println("Ignoring sequence coord system " + cs + " ("
294 + cs.getContent() + ") for source " + getTitle()
295 + "- threw exception when constructing fetcher.\n");
304 seqsources.add(new DasSequenceSource(getTitle(), getTitle(),
305 source, getVersion(), null, connprov));
306 } catch (Exception e)
308 // TODO Auto-generated catch block
313 if (seqsources.size() > 1)
316 DbSourceProxy[] tsort = seqsources.toArray(new DasSequenceSource[0]);
317 String[] nm = new String[tsort.length];
318 for (int i = 0; i < nm.length; i++)
320 nm[i] = tsort[i].getDbName().toLowerCase();
322 jalview.util.QuickSort.sort(nm, tsort);
324 for (DbSourceProxy ssrc : tsort)
326 seqsources.add(ssrc);
333 public String getSourceURL()
337 // kind of dumb, since
338 // org.biodas.jdas.dassources.utils.VersionAdapter.getSourceUriFromQueryUri()
340 // but this way, we can access non DAS 1.6 compliant sources (which have
341 // to have a URL like <sourcename>/das/ and cause a validation exception)
343 for (CAPABILITY cap : getVersion().getCAPABILITY())
345 String capname = cap.getType().substring(
346 cap.getType().indexOf(":") + 1);
347 int p = cap.getQueryUri().lastIndexOf(capname);
350 throw new Exception(MessageManager.formatMessage(
351 "exception.invalid_das_source",
352 new String[] { source.getUri() }));
354 if (cap.getQueryUri().charAt(p) == '/')
358 return cap.getQueryUri().substring(0, p);
360 } catch (Exception x)
362 System.err.println("Serious: Couldn't get the URL for source "
363 + source.getTitle());
370 public boolean isNewerThan(jalviewSourceI other)
372 return isLaterThan(getVersion().getCreated(), other.getVersion()
377 public boolean isReferenceSource()
379 // TODO check source object for indication that we are the primary for a DAS