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.dassources.Capabilities;
11 import org.biodas.jdas.dassources.utils.DasTimeFormat;
12 import org.biodas.jdas.dassources.utils.RegistrySourceAdapter;
13 import org.biodas.jdas.schema.sources.CAPABILITY;
14 import org.biodas.jdas.schema.sources.COORDINATES;
15 import org.biodas.jdas.schema.sources.MAINTAINER;
16 import org.biodas.jdas.schema.sources.PROP;
17 import org.biodas.jdas.schema.sources.SOURCE;
18 import org.biodas.jdas.schema.sources.VERSION;
20 import jalview.ws.dbsources.das.api.jalviewSourceI;
21 import jalview.ws.seqfetcher.DbSourceProxy;
23 public class JalviewSource implements jalviewSourceI
27 public JalviewSource(SOURCE local2, boolean local)
34 public String getTitle()
36 return source.getTitle();
40 public VERSION getVersion()
43 return getVersionFor(source);
47 public String getDocHref()
49 return source.getDocHref();
53 public String getDescription()
55 return source.getDescription();
59 public String getUri()
61 return source.getUri();
65 public MAINTAINER getMAINTAINER()
67 return source.getMAINTAINER();
71 public String getEmail()
73 return (local) ? null : source.getMAINTAINER().getEmail();
76 boolean local = false;
79 public boolean isLocal()
85 public boolean isSequenceSource()
87 String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
88 for (String cp : getCapabilityList(getVersionFor(source)))
90 if (cp.equals(seqcap))
100 public boolean isFeatureSource()
102 String seqcap = "das1:" + Capabilities.FEATURES.getName();
103 for (String cp : getCapabilityList(getVersionFor(source)))
105 if (cp.equals(seqcap))
114 private VERSION getVersionFor(SOURCE ds)
116 VERSION latest = null;
117 for (VERSION v : ds.getVERSION())
120 || isLaterThan(latest.getCreated(), v.getCreated()))
122 // TODO: das 1.6 - should just get the first version - ignore other
123 // versions since not specified how to construct URL from version's URI
132 * compare date strings. null or unparseable dates are assumed to be oldest
136 * @return true iff ref comes before newer
138 private boolean isLaterThan(String ref, String newer)
140 Date refdate = null, newdate = null;
141 if (ref != null && ref.trim().length() > 0)
145 refdate = DasTimeFormat.fromDASString(ref.trim());
147 } catch (ParseException x)
151 if (newer != null && newer.trim().length() > 0)
155 newdate = DasTimeFormat.fromDASString(newer);
156 } catch (ParseException e)
164 return refdate.before(newdate);
172 // assume first instance of source is newest in list. - TODO: check if
173 // natural ordering of source versions is newest first or oldest first
177 public String[] getLabelsFor(VERSION v)
179 ArrayList<String> labels = new ArrayList<String>();
180 for (PROP p : v.getPROP())
182 if (p.getName().equalsIgnoreCase("LABEL"))
184 labels.add(p.getValue());
187 return labels.toArray(new String[0]);
190 private CAPABILITY getCapability(Capabilities capability)
192 for (CAPABILITY p : getVersion().getCAPABILITY())
194 if (p.getType().equalsIgnoreCase(capability.getName())
195 || p.getType().equalsIgnoreCase(
196 "das1:" + capability.getName()))
204 public String[] getCapabilityList(VERSION v)
207 ArrayList<String> labels = new ArrayList<String>();
208 for (CAPABILITY p : v.getCAPABILITY())
210 // TODO: work out what to do with namespace prefix
211 // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
212 // for moment, just show all capabilities...
213 if (p.getType().startsWith("das1:"))
215 labels.add(p.getType());
218 return labels.toArray(new String[0]);
222 public List<DbSourceProxy> getSequenceSourceProxies()
224 if (!isSequenceSource())
228 ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
231 VERSION v = getVersion();
232 Map<String, COORDINATES> latestc = new Hashtable<String, COORDINATES>();
233 for (COORDINATES cs : v.getCOORDINATES())
235 COORDINATES ltst = latestc.get(cs.getUri());
237 || ltst.getVersion() == null
238 || (ltst.getVersion() != null && cs.getVersion() != null && isLaterThan(
239 ltst.getVersion(), cs.getVersion())))
241 latestc.put(cs.getUri(), cs);
244 for (COORDINATES cs : latestc.values())
246 DasSequenceSource ds;
248 * if (css == null || css.length == 0) { // TODO: query das source
249 * directly to identify coordinate system... or // have to make up a
250 * coordinate system css = new DasCoordinateSystem[] { new
251 * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
252 * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
257 seqsources.add(ds = new DasSequenceSource(getTitle() + " ("
258 + cs.getAuthority() + " " + cs.getSource()
259 + (cs.getVersion() != null ? " " + cs.getVersion() : "")
260 + ")", cs.getAuthority(), source, v, cs));
261 if (seqsources.size() > 1)
263 System.err.println("Added another sequence DB source for "
264 + getTitle() + " (" + ds.getDbName() + ")");
266 } catch (Exception e)
268 System.err.println("Ignoring sequence coord system " + cs + " ("
269 + cs.getContent() + ") for source " + getTitle()
270 + "- threw exception when constructing fetcher.\n");
279 seqsources.add(new DasSequenceSource(getTitle(), getTitle(),
280 source, getVersion(), null));
281 } catch (Exception e)
283 // TODO Auto-generated catch block
288 if (seqsources.size() > 1)
291 DbSourceProxy[] tsort = seqsources.toArray(new DasSequenceSource[0]);
292 String[] nm = new String[tsort.length];
293 for (int i = 0; i < nm.length; i++)
295 nm[i] = tsort[i].getDbName().toLowerCase();
297 jalview.util.QuickSort.sort(nm, tsort);
299 for (DbSourceProxy ssrc : tsort)
301 seqsources.add(ssrc);
308 public String getSourceURL()
312 String url = new RegistrySourceAdapter(source)
313 .getOriginalDataSourceUri();
315 } catch (Exception x)
317 System.err.println("Serious: Couldn't get the URL for source "
318 + source.getTitle());
325 public boolean isNewerThan(jalviewSourceI other)
327 return isLaterThan(getVersion().getCreated(), other.getVersion()