1 package jalview.ws.dbsources.das.datamodel;
3 import java.text.ParseException;
4 import java.util.ArrayList;
6 import java.util.Hashtable;
10 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
11 import org.biodas.jdas.dassources.Capabilities;
12 import org.biodas.jdas.dassources.utils.DasTimeFormat;
13 import org.biodas.jdas.dassources.utils.RegistrySourceAdapter;
14 import org.biodas.jdas.schema.sources.CAPABILITY;
15 import org.biodas.jdas.schema.sources.COORDINATES;
16 import org.biodas.jdas.schema.sources.MAINTAINER;
17 import org.biodas.jdas.schema.sources.PROP;
18 import org.biodas.jdas.schema.sources.SOURCE;
19 import org.biodas.jdas.schema.sources.VERSION;
21 import jalview.ws.dbsources.das.api.jalviewSourceI;
22 import jalview.ws.seqfetcher.DbSourceProxy;
24 public class JalviewSource implements jalviewSourceI
28 MultipleConnectionPropertyProviderI connprov;
30 public JalviewSource(SOURCE local2,
31 MultipleConnectionPropertyProviderI connprov, boolean local)
33 this.connprov = connprov;
39 public String getTitle()
41 return source.getTitle();
45 public VERSION getVersion()
48 return getVersionFor(source);
52 public String getDocHref()
54 return source.getDocHref();
58 public String getDescription()
60 return source.getDescription();
64 public String getUri()
66 return source.getUri();
70 public MAINTAINER getMAINTAINER()
72 return source.getMAINTAINER();
76 public String getEmail()
78 return (local) ? null : source.getMAINTAINER().getEmail();
81 boolean local = false;
84 public boolean isLocal()
90 public boolean isSequenceSource()
92 String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
93 for (String cp : getCapabilityList(getVersionFor(source)))
95 if (cp.equals(seqcap))
105 public boolean isFeatureSource()
107 String seqcap = "das1:" + Capabilities.FEATURES.getName();
108 for (String cp : getCapabilityList(getVersionFor(source)))
110 if (cp.equals(seqcap))
119 private VERSION getVersionFor(SOURCE ds)
121 VERSION latest = null;
122 for (VERSION v : ds.getVERSION())
125 || isLaterThan(latest.getCreated(), v.getCreated()))
127 // TODO: das 1.6 - should just get the first version - ignore other
128 // versions since not specified how to construct URL from version's URI
137 * compare date strings. null or unparseable dates are assumed to be oldest
141 * @return true iff ref comes before newer
143 private boolean isLaterThan(String ref, String newer)
145 Date refdate = null, newdate = null;
146 if (ref != null && ref.trim().length() > 0)
150 refdate = DasTimeFormat.fromDASString(ref.trim());
152 } catch (ParseException x)
156 if (newer != null && newer.trim().length() > 0)
160 newdate = DasTimeFormat.fromDASString(newer);
161 } catch (ParseException e)
169 return refdate.before(newdate);
177 // assume first instance of source is newest in list. - TODO: check if
178 // natural ordering of source versions is newest first or oldest first
182 public String[] getLabelsFor(VERSION v)
184 ArrayList<String> labels = new ArrayList<String>();
185 for (PROP p : v.getPROP())
187 if (p.getName().equalsIgnoreCase("LABEL"))
189 labels.add(p.getValue());
192 return labels.toArray(new String[0]);
195 private CAPABILITY getCapability(Capabilities capability)
197 for (CAPABILITY p : getVersion().getCAPABILITY())
199 if (p.getType().equalsIgnoreCase(capability.getName())
200 || p.getType().equalsIgnoreCase(
201 "das1:" + capability.getName()))
209 public String[] getCapabilityList(VERSION v)
212 ArrayList<String> labels = new ArrayList<String>();
213 for (CAPABILITY p : v.getCAPABILITY())
215 // TODO: work out what to do with namespace prefix
216 // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
217 // for moment, just show all capabilities...
218 if (p.getType().startsWith("das1:"))
220 labels.add(p.getType());
223 return labels.toArray(new String[0]);
227 public List<DbSourceProxy> getSequenceSourceProxies()
229 if (!isSequenceSource())
233 ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
236 VERSION v = getVersion();
237 Map<String, COORDINATES> latestc = new Hashtable<String, COORDINATES>();
238 for (COORDINATES cs : v.getCOORDINATES())
240 COORDINATES ltst = latestc.get(cs.getUri());
242 || ltst.getVersion() == null
243 || (ltst.getVersion() != null && cs.getVersion() != null && isLaterThan(
244 ltst.getVersion(), cs.getVersion())))
246 latestc.put(cs.getUri(), cs);
249 for (COORDINATES cs : latestc.values())
251 DasSequenceSource ds;
253 * if (css == null || css.length == 0) { // TODO: query das source
254 * directly to identify coordinate system... or // have to make up a
255 * coordinate system css = new DasCoordinateSystem[] { new
256 * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
257 * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
262 seqsources.add(ds = new DasSequenceSource(getTitle() + " ("
263 + cs.getAuthority() + " " + cs.getSource()
264 + (cs.getVersion() != null ? " " + cs.getVersion() : "")
265 + ")", cs.getAuthority(), source, v, cs, connprov));
266 if (seqsources.size() > 1)
268 System.err.println("Added another sequence DB source for "
269 + getTitle() + " (" + ds.getDbName() + ")");
271 } catch (Exception e)
273 System.err.println("Ignoring sequence coord system " + cs + " ("
274 + cs.getContent() + ") for source " + getTitle()
275 + "- threw exception when constructing fetcher.\n");
284 seqsources.add(new DasSequenceSource(getTitle(), getTitle(),
285 source, getVersion(), null, connprov));
286 } catch (Exception e)
288 // TODO Auto-generated catch block
293 if (seqsources.size() > 1)
296 DbSourceProxy[] tsort = seqsources.toArray(new DasSequenceSource[0]);
297 String[] nm = new String[tsort.length];
298 for (int i = 0; i < nm.length; i++)
300 nm[i] = tsort[i].getDbName().toLowerCase();
302 jalview.util.QuickSort.sort(nm, tsort);
304 for (DbSourceProxy ssrc : tsort)
306 seqsources.add(ssrc);
313 public String getSourceURL()
317 // kind of dumb, since
318 // org.biodas.jdas.dassources.utils.VersionAdapter.getSourceUriFromQueryUri()
320 // but this way, we can access non DAS 1.6 compliant sources (which have
321 // to have a URL like <sourcename>/das/ and cause a validation exception)
323 for (CAPABILITY cap : getVersion().getCAPABILITY())
325 String capname = cap.getType().substring(
326 cap.getType().indexOf(":") + 1);
327 int p = cap.getQueryUri().lastIndexOf(capname);
330 throw new Exception("Invalid das source: " + source.getUri());
332 if (cap.getQueryUri().charAt(p) == '/')
336 return cap.getQueryUri().substring(0, p);
338 } catch (Exception x)
340 System.err.println("Serious: Couldn't get the URL for source "
341 + source.getTitle());
348 public boolean isNewerThan(jalviewSourceI other)
350 return isLaterThan(getVersion().getCreated(), other.getVersion()