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
27 MultipleConnectionPropertyProviderI connprov;
28 public JalviewSource(SOURCE local2, MultipleConnectionPropertyProviderI connprov, boolean local)
30 this.connprov=connprov;
36 public String getTitle()
38 return source.getTitle();
42 public VERSION getVersion()
45 return getVersionFor(source);
49 public String getDocHref()
51 return source.getDocHref();
55 public String getDescription()
57 return source.getDescription();
61 public String getUri()
63 return source.getUri();
67 public MAINTAINER getMAINTAINER()
69 return source.getMAINTAINER();
73 public String getEmail()
75 return (local) ? null : source.getMAINTAINER().getEmail();
78 boolean local = false;
81 public boolean isLocal()
87 public boolean isSequenceSource()
89 String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
90 for (String cp : getCapabilityList(getVersionFor(source)))
92 if (cp.equals(seqcap))
102 public boolean isFeatureSource()
104 String seqcap = "das1:" + Capabilities.FEATURES.getName();
105 for (String cp : getCapabilityList(getVersionFor(source)))
107 if (cp.equals(seqcap))
116 private VERSION getVersionFor(SOURCE ds)
118 VERSION latest = null;
119 for (VERSION v : ds.getVERSION())
122 || isLaterThan(latest.getCreated(), v.getCreated()))
124 // TODO: das 1.6 - should just get the first version - ignore other
125 // versions since not specified how to construct URL from version's URI
134 * compare date strings. null or unparseable dates are assumed to be oldest
138 * @return true iff ref comes before newer
140 private boolean isLaterThan(String ref, String newer)
142 Date refdate = null, newdate = null;
143 if (ref != null && ref.trim().length() > 0)
147 refdate = DasTimeFormat.fromDASString(ref.trim());
149 } catch (ParseException x)
153 if (newer != null && newer.trim().length() > 0)
157 newdate = DasTimeFormat.fromDASString(newer);
158 } catch (ParseException e)
166 return refdate.before(newdate);
174 // assume first instance of source is newest in list. - TODO: check if
175 // natural ordering of source versions is newest first or oldest first
179 public String[] getLabelsFor(VERSION v)
181 ArrayList<String> labels = new ArrayList<String>();
182 for (PROP p : v.getPROP())
184 if (p.getName().equalsIgnoreCase("LABEL"))
186 labels.add(p.getValue());
189 return labels.toArray(new String[0]);
192 private CAPABILITY getCapability(Capabilities capability)
194 for (CAPABILITY p : getVersion().getCAPABILITY())
196 if (p.getType().equalsIgnoreCase(capability.getName())
197 || p.getType().equalsIgnoreCase(
198 "das1:" + capability.getName()))
206 public String[] getCapabilityList(VERSION v)
209 ArrayList<String> labels = new ArrayList<String>();
210 for (CAPABILITY p : v.getCAPABILITY())
212 // TODO: work out what to do with namespace prefix
213 // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
214 // for moment, just show all capabilities...
215 if (p.getType().startsWith("das1:"))
217 labels.add(p.getType());
220 return labels.toArray(new String[0]);
224 public List<DbSourceProxy> getSequenceSourceProxies()
226 if (!isSequenceSource())
230 ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
233 VERSION v = getVersion();
234 Map<String, COORDINATES> latestc = new Hashtable<String, COORDINATES>();
235 for (COORDINATES cs : v.getCOORDINATES())
237 COORDINATES ltst = latestc.get(cs.getUri());
239 || ltst.getVersion() == null
240 || (ltst.getVersion() != null && cs.getVersion() != null && isLaterThan(
241 ltst.getVersion(), cs.getVersion())))
243 latestc.put(cs.getUri(), cs);
246 for (COORDINATES cs : latestc.values())
248 DasSequenceSource ds;
250 * if (css == null || css.length == 0) { // TODO: query das source
251 * directly to identify coordinate system... or // have to make up a
252 * coordinate system css = new DasCoordinateSystem[] { new
253 * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
254 * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
259 seqsources.add(ds = new DasSequenceSource(getTitle() + " ("
260 + cs.getAuthority() + " " + cs.getSource()
261 + (cs.getVersion() != null ? " " + cs.getVersion() : "")
262 + ")", cs.getAuthority(), source, v, cs, connprov));
263 if (seqsources.size() > 1)
265 System.err.println("Added another sequence DB source for "
266 + getTitle() + " (" + ds.getDbName() + ")");
268 } catch (Exception e)
270 System.err.println("Ignoring sequence coord system " + cs + " ("
271 + cs.getContent() + ") for source " + getTitle()
272 + "- threw exception when constructing fetcher.\n");
281 seqsources.add(new DasSequenceSource(getTitle(), getTitle(),
282 source, getVersion(), null, connprov));
283 } catch (Exception e)
285 // TODO Auto-generated catch block
290 if (seqsources.size() > 1)
293 DbSourceProxy[] tsort = seqsources.toArray(new DasSequenceSource[0]);
294 String[] nm = new String[tsort.length];
295 for (int i = 0; i < nm.length; i++)
297 nm[i] = tsort[i].getDbName().toLowerCase();
299 jalview.util.QuickSort.sort(nm, tsort);
301 for (DbSourceProxy ssrc : tsort)
303 seqsources.add(ssrc);
310 public String getSourceURL()
314 // kind of dumb, since org.biodas.jdas.dassources.utils.VersionAdapter.getSourceUriFromQueryUri() does this,
315 // but this way, we can access non DAS 1.6 compliant sources (which have to have a URL like <sourcename>/das/ and cause a validation exception)
317 for (CAPABILITY cap : getVersion().getCAPABILITY())
319 String capname = cap.getType().substring(
320 cap.getType().indexOf(":") + 1);
321 int p=cap.getQueryUri().lastIndexOf(capname);
323 throw new Exception("Invalid das source: "+source.getUri());
325 if (cap.getQueryUri().charAt(p)=='/')
329 return cap.getQueryUri().substring(0,p);
331 } catch (Exception x)
333 System.err.println("Serious: Couldn't get the URL for source "
334 + source.getTitle());
341 public boolean isNewerThan(jalviewSourceI other)
343 return isLaterThan(getVersion().getCreated(), other.getVersion()