package jalview.ws.dbsources.das.datamodel; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.biodas.jdas.dassources.Capabilities; import org.biodas.jdas.dassources.utils.DasTimeFormat; import org.biodas.jdas.dassources.utils.RegistrySourceAdapter; import org.biodas.jdas.schema.sources.CAPABILITY; import org.biodas.jdas.schema.sources.COORDINATES; import org.biodas.jdas.schema.sources.MAINTAINER; import org.biodas.jdas.schema.sources.PROP; import org.biodas.jdas.schema.sources.SOURCE; import org.biodas.jdas.schema.sources.VERSION; import jalview.ws.dbsources.das.api.jalviewSourceI; import jalview.ws.seqfetcher.DbSourceProxy; public class JalviewSource implements jalviewSourceI { SOURCE source; public JalviewSource(SOURCE local2, boolean local) { this.local = local; source = local2; } @Override public String getTitle() { return source.getTitle(); } @Override public VERSION getVersion() { return getVersionFor(source); } @Override public String getDocHref() { return source.getDocHref(); } @Override public String getDescription() { return source.getDescription(); } @Override public String getUri() { return source.getUri(); } @Override public MAINTAINER getMAINTAINER() { return source.getMAINTAINER(); } @Override public String getEmail() { return (local) ? null: source.getMAINTAINER().getEmail(); } boolean local = false; @Override public boolean isLocal() { return local; } @Override public boolean isSequenceSource() { String seqcap = "das1:" + Capabilities.SEQUENCE.getName(); for (String cp : getCapabilityList(getVersionFor(source))) { if (cp.equals(seqcap)) { return true; } } return false; } @Override public boolean isFeatureSource() { String seqcap = "das1:" + Capabilities.FEATURES.getName(); for (String cp : getCapabilityList(getVersionFor(source))) { if (cp.equals(seqcap)) { return true; } } return false; } private VERSION getVersionFor(SOURCE ds) { VERSION latest = null; for (VERSION v : ds.getVERSION()) { if (latest == null || isLaterThan(latest.getCreated(), v.getCreated())) { // TODO: das 1.6 - should just get the first version - ignore other // versions since not specified how to construct URL from version's URI // + source URI latest = v; } } return latest; } private boolean isLaterThan(String ref, String newer) { Date refdate = null, newdate = null; try { refdate = DasTimeFormat.fromDASString(ref); } catch (ParseException x) { return false; } try { newdate = DasTimeFormat.fromDASString(newer); } catch (ParseException e) { // TODO: handle exception } if (refdate != null) { if (newdate != null) { return refdate.before(newdate); } return false; } if (newdate != null) { return true; } // assume first instance of source is newest in list. - TODO: check if // natural ordering of source versions is newest first or oldest first return false; } public String[] getLabelsFor(VERSION v) { ArrayList labels = new ArrayList(); for (PROP p : v.getPROP()) { if (p.getName().equalsIgnoreCase("LABEL")) { labels.add(p.getValue()); } } return labels.toArray(new String[0]); } private CAPABILITY getCapability(Capabilities capability) { for (CAPABILITY p: getVersion().getCAPABILITY()) { if (p.getType().equalsIgnoreCase(capability.getName()) || p.getType().equalsIgnoreCase("das1:"+capability.getName())) { return p; } } return null; } public String[] getCapabilityList(VERSION v) { ArrayList labels = new ArrayList(); for (CAPABILITY p : v.getCAPABILITY()) { // TODO: work out what to do with namespace prefix // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ? // for moment, just show all capabilities... if (p.getType().startsWith("das1:")) { labels.add(p.getType()); } } return labels.toArray(new String[0]); } @Override public List getSequenceSourceProxies() { if (!isSequenceSource()) { return null; } ArrayList seqsources = new ArrayList(); if (!local) { VERSION v = getVersion(); for (COORDINATES cs : v.getCOORDINATES()) { /* * if (css == null || css.length == 0) { // TODO: query das source * directly to identify coordinate system... or // have to make up a * coordinate system css = new DasCoordinateSystem[] { new * DasCoordinateSystem() }; css[0].setName(d1s.getNickname()); * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c < * css.length; c++) { */ try { seqsources.add(new DasSequenceSource("das:" + getTitle() + " (" + cs.getContent() + ")", cs.getContent(), source, v, cs)); } catch (Exception e) { System.err.println("Ignoring sequence coord system " + cs + " (" + cs.getContent() + ") for source " + getTitle() + "- threw exception when constructing fetcher.\n"); e.printStackTrace(); } } } else { try { seqsources.add(new DasSequenceSource("das:"+getTitle(), getTitle(), source, getVersion(), null)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return seqsources; } @Override public String getSourceURL() { try { String url = new RegistrySourceAdapter(source).getOriginalDataSourceUri(); return url; } catch (Exception x) { System.err.println("Serious: Couldn't get the URL for source "+source.getTitle()); x.printStackTrace(); } return null; } }