From 35b3d0f762b0619a3d27d805d6495b5fe8eb0093 Mon Sep 17 00:00:00 2001 From: jprocter Date: Wed, 29 Feb 2012 16:13:22 +0000 Subject: [PATCH] updated das sequence source for jdas client and refactored to implementation package --- src/jalview/ws/dbsources/DasSequenceSource.java | 168 ------------- .../dbsources/das/datamodel/DasSequenceSource.java | 253 ++++++++++++++++++++ 2 files changed, 253 insertions(+), 168 deletions(-) delete mode 100644 src/jalview/ws/dbsources/DasSequenceSource.java create mode 100644 src/jalview/ws/dbsources/das/datamodel/DasSequenceSource.java diff --git a/src/jalview/ws/dbsources/DasSequenceSource.java b/src/jalview/ws/dbsources/DasSequenceSource.java deleted file mode 100644 index dc0f0a5..0000000 --- a/src/jalview/ws/dbsources/DasSequenceSource.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7) - * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle - * - * This file is part of Jalview. - * - * Jalview is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * Jalview is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with Jalview. If not, see . - */ -package jalview.ws.dbsources; - -import java.util.Hashtable; - -import com.stevesoft.pat.Regex; - -import jalview.ws.dbsources.das.DasSequenceSourceListener; -import jalview.ws.seqfetcher.*; -import jalview.datamodel.AlignmentI; - -/** - * an instance of this class is created for each unique DAS Sequence source (ie - * one capable of handling the 'sequence' for a particular MapMaster) - * - * @author JimP - * - */ -public class DasSequenceSource extends DbSourceProxyImpl implements - DbSourceProxy -{ - protected Das1Source source = null; - - protected String dbname = "DASCS"; - - protected String dbrefname = "das:source"; - - protected DasCoordinateSystem coordsys = null; - - /** - * create a new DbSource proxy for a DAS 1 source - * - * @param dbnbame - * Human Readable Name to use when fetching from this source - * @param dbrefname - * DbRefName for DbRefs attached to sequences retrieved from this - * source - * @param source - * Das1Source - * @param coordsys - * specific coordinate system to use for this source - * @throws Exception - * if source is not capable of the 'sequence' command - */ - public DasSequenceSource(String dbname, String dbrefname, - Das1Source source, DasCoordinateSystem coordsys) throws Exception - { - if (!source.hasCapability("sequence")) - { - throw new Exception("Source " + source.getNickname() - + " does not support the sequence command."); - } - this.source = source; - this.dbname = dbname; - this.dbrefname = dbrefname; - this.coordsys = coordsys; - } - - public String getAccessionSeparator() - { - // cope with single sequences only - return null; - } - - public Regex getAccessionValidator() - { - /** ? * */ - return Regex.perlCode("\\S+"); - } - - public String getDbName() - { - // TODO: map to - return dbname + " (DAS)"; - } - - public String getDbSource() - { - return dbrefname; - } - - public String getDbVersion() - { - return coordsys.getVersion(); - } - - public AlignmentI getSequenceRecords(String queries) throws Exception - { - SequenceThread seqfetcher = new org.biojava.dasobert.das.SequenceThread( - queries, source); - DasSequenceSourceListener ourlistener = new DasSequenceSourceListener( - this, queries); - seqfetcher.addSequenceListener(ourlistener); - seqfetcher.start(); - try - { - Thread.sleep(5); - } catch (Exception e) - { - } - ; - while (ourlistener.isNotCalled() && seqfetcher.isAlive()) - { - try - { - Thread.sleep(5); - } catch (Exception e) - { - } - ; - } - if (ourlistener.isNotCalled() || ourlistener.hasNoSequences()) - { - System.err.println("Sequence Query to " + source.getNickname() - + " with '" + queries + "' returned no sequences."); - return null; - } - else - { - return ourlistener.getSequences(); - } - } - - public String getTestQuery() - { - return coordsys.getTestCode(); - } - - public boolean isValidReference(String accession) - { - // TODO try to validate an accession against source - // We don't really know how to do this without querying source - - return true; - } - - /** - * @return the source - */ - public Das1Source getSource() - { - return source; - } - - /** - * @return the coordsys - */ - public DasCoordinateSystem getCoordsys() - { - return coordsys; - } -} diff --git a/src/jalview/ws/dbsources/das/datamodel/DasSequenceSource.java b/src/jalview/ws/dbsources/das/datamodel/DasSequenceSource.java new file mode 100644 index 0000000..c839b79 --- /dev/null +++ b/src/jalview/ws/dbsources/das/datamodel/DasSequenceSource.java @@ -0,0 +1,253 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7) + * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Jalview. If not, see . + */ +package jalview.ws.dbsources.das.datamodel; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.Vector; + +import org.biodas.jdas.client.adapters.sequence.DasSequenceAdapter; +import org.biodas.jdas.client.threads.SequenceClientMultipleSources; +import org.biodas.jdas.schema.sequence.SEQUENCE; +import org.biodas.jdas.schema.sources.COORDINATES; +import org.biodas.jdas.schema.sources.SOURCE; +import org.biodas.jdas.schema.sources.VERSION; + +import com.stevesoft.pat.Regex; + +import jalview.ws.dbsources.das.api.jalviewSourceI; +import jalview.ws.seqfetcher.*; +import jalview.bin.Cache; +import jalview.datamodel.Alignment; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.DBRefEntry; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; + +/** + * an instance of this class is created for each unique DAS Sequence source (ie + * one capable of handling the 'sequence' for a particular MapMaster) + * + * @author JimP + * + */ +public class DasSequenceSource extends DbSourceProxyImpl implements + DbSourceProxy +{ + private jalviewSourceI jsrc; + + protected SOURCE source = null; + + protected VERSION version = null; + + protected COORDINATES coordsys = null; + + protected String dbname = "DASCS"; + + protected String dbrefname = "das:source"; + + /** + * create a new DbSource proxy for a DAS 1 source + * + * @param dbnbame + * Human Readable Name to use when fetching from this source + * @param dbrefname + * DbRefName for DbRefs attached to sequences retrieved from this + * source + * @param source + * Das1Source + * @param coordsys + * specific coordinate system to use for this source + * @throws Exception + * if source is not capable of the 'sequence' command + */ + public DasSequenceSource(String dbname, String dbrefname, SOURCE source, + VERSION version, COORDINATES coordsys) throws Exception + { + if (!(jsrc = new JalviewSource(source, false)).isSequenceSource()) + { + throw new Exception("Source " + source.getTitle() + + " does not support the sequence command."); + } + this.source = source; + this.dbname = dbname; + this.dbrefname = dbrefname; + this.coordsys = coordsys; + } + + public String getAccessionSeparator() + { + return "\t"; + } + + public Regex getAccessionValidator() + { + /** ? * */ + return Regex.perlCode("\\S+"); + } + + public String getDbName() + { + // TODO: map to + return dbname + " (DAS)"; + } + + public String getDbSource() + { + return dbrefname; + } + + public String getDbVersion() + { + return coordsys.getVersion(); + } + + + public AlignmentI getSequenceRecords(String queries) throws Exception + { + StringTokenizer st = new StringTokenizer(queries, "\t"); + List toks = new ArrayList(), src = new ArrayList(); + while (st.hasMoreTokens()) + { + toks.add(st.nextToken()); + } + src.add(jsrc.getSourceURL()); + Map, DasSequenceAdapter>> resultset = new HashMap, DasSequenceAdapter>>(); + Map, Exception>> errors = new HashMap, Exception>>(); + SequenceClientMultipleSources sclient; + sclient = new SequenceClientMultipleSources(); + sclient.fetchData(src, toks, resultset, errors); + sclient.shutDown(); + while (!sclient.isTerminated()) + { + try {Thread.sleep(200); + + } catch (InterruptedException x) {} + } + + if (resultset.isEmpty()) + { + System.err.println("Sequence Query to " + jsrc.getTitle() + " with '" + + queries + "' returned no sequences."); + return null; + } + else + { + Vector seqs=null; + for (Map.Entry, DasSequenceAdapter>> resset : resultset + .entrySet()) + { + for (Map.Entry, DasSequenceAdapter> result : resset + .getValue().entrySet()) + { + DasSequenceAdapter dasseqresp = result.getValue(); + List accessions = result.getKey(); + for (SEQUENCE e : dasseqresp.getSequence()) + { + String lbl = e.getId(); + + if (toks.indexOf(lbl) == -1) + { + System.err + .println("Warning - received sequence event for strange accession code (" + + lbl + ")"); + } + else + { + if (seqs == null) + { + if (e.getContent().length() == 0) + { + System.err + .println("Empty sequence returned for accession code (" + + lbl + + ") from " + + resset.getKey() + + " (source is " + + getDbName()); + continue; + } + } + seqs = new java.util.Vector(); + // JDAS returns a sequence complete with any newlines and spaces in the XML + Sequence sq = new Sequence(lbl, e.getContent().replaceAll("\\s+", "")); + sq.addDBRef(new DBRefEntry(getDbSource(), getDbVersion() + + ":" + e.getVersion(), lbl)); + seqs.addElement(sq); + } + } + } + } + + if (seqs != null && seqs.size() == 0) + return null; + SequenceI[] sqs = new SequenceI[seqs.size()]; + for (int i = 0, iSize = seqs.size(); i < iSize; i++) + { + sqs[i] = (SequenceI) seqs.elementAt(i); + } + Alignment al = new Alignment(sqs); + if (jsrc.isFeatureSource()) + { + java.util.Vector srcs = new java.util.Vector(); + srcs.addElement(jsrc); + try { + new jalview.ws.DasSequenceFeatureFetcher(sqs, null, srcs, false, + false); + } catch (Exception x) + { + Cache.log.error("Couldn't retrieve features for sequence from its source.",x); + } + } + + return al; + } + } + + public String getTestQuery() + { + return coordsys.getTestRange(); + } + + public boolean isValidReference(String accession) + { + // TODO try to validate an accession against source + // We don't really know how to do this without querying source + + return true; + } + + /** + * @return the source + */ + public SOURCE getSource() + { + return source; + } + + /** + * @return the coordsys + */ + public COORDINATES getCoordsys() + { + return coordsys; + } +} -- 1.7.10.2