X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2FDBRefFetcher.java;h=5400d4e83e8f69eb489c4b554bbddc01d858d1df;hb=faf6281b4ee692a989d0d625b1bb0d581cc0269c;hp=c2d7ad5957d50817d08d1e760f0ca0d51f64057f;hpb=08fbe7c3d40a4077ab519334952a079968c07cd1;p=jalview.git diff --git a/src/jalview/ws/DBRefFetcher.java b/src/jalview/ws/DBRefFetcher.java index c2d7ad5..5400d4e 100644 --- a/src/jalview/ws/DBRefFetcher.java +++ b/src/jalview/ws/DBRefFetcher.java @@ -1,37 +1,48 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * + * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1) + * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * * This program 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 2 * of the License, or (at your option) any later version. - * + * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.ws; -import java.io.*; -import java.util.*; - -import org.exolab.castor.mapping.*; -import org.exolab.castor.xml.*; -import jalview.analysis.*; -import jalview.datamodel.*; +import jalview.analysis.AlignSeq; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.DBRefEntry; +import jalview.datamodel.DBRefSource; import jalview.datamodel.Mapping; -import jalview.gui.*; -import jalview.ws.dbsources.Uniprot; -import jalview.ws.ebi.EBIFetchClient; +import jalview.datamodel.SequenceFeature; +import jalview.datamodel.SequenceI; +import jalview.gui.AlignFrame; +import jalview.gui.CutAndPasteTransfer; +import jalview.gui.Desktop; +import jalview.gui.IProgressIndicator; +import jalview.gui.OOMWarning; + +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.StringTokenizer; +import java.util.Vector; + +import org.biojava.dasobert.dasregistry.DasSource; + +import uk.ac.ebi.picr.model.UPEntry; /** - * DOCUMENT ME! + * Implements a runnable for validating a sequence against external databases + * and then propagating references and features onto the sequence(s) * * @author $author$ * @version $Revision$ @@ -40,13 +51,17 @@ public class DBRefFetcher implements Runnable { SequenceI[] dataset; - AlignFrame af; + IProgressIndicator af; CutAndPasteTransfer output = new CutAndPasteTransfer(); StringBuffer sbuffer = new StringBuffer(); boolean running = false; + /** + * picr client instance + */ + uk.ac.ebi.www.picr.AccessionMappingService.AccessionMapperInterface picrClient = null; // /This will be a collection of Vectors of sequenceI refs. // The key will be the seq name or accession id of the seq @@ -61,7 +76,8 @@ public class DBRefFetcher implements Runnable } /** - * Creates a new SequenceFeatureFetcher object. + * Creates a new SequenceFeatureFetcher object and fetches from the + * currently selected set of databases. * * @param seqs * fetch references for these sequences @@ -70,6 +86,20 @@ public class DBRefFetcher implements Runnable */ public DBRefFetcher(SequenceI[] seqs, AlignFrame af) { + this(seqs, af, null); + } + /** + * Creates a new SequenceFeatureFetcher object and fetches from the + * currently selected set of databases. + * + * @param seqs + * fetch references for these sequences + * @param af + * the parent alignframe for progress bar monitoring. + * @param sources array of database source strings to query references from + */ + public DBRefFetcher(SequenceI[] seqs, AlignFrame af, String[] sources) + { this.af = af; SequenceI[] ds = new SequenceI[seqs.length]; for (int i = 0; i < seqs.length; i++) @@ -80,18 +110,73 @@ public class DBRefFetcher implements Runnable ds[i] = seqs[i]; } this.dataset = ds; - sfetcher = new SequenceFetcher(); - // select appropriate databases based on alignFrame context. - if (af.getViewport().getAlignment().isNucleotide()) + // TODO Jalview 2.5 lots of this code should be in the gui package! + sfetcher = jalview.gui.SequenceFetcher.getSequenceFetcherSingleton(af); + if (sources==null) { - dbSources = DBRefSource.DNACODINGDBS; + // af.featureSettings_actionPerformed(null); + String[] defdb=null,otherdb = sfetcher.getDbInstances(jalview.ws.dbsources.DasSequenceSource.class); + Vector selsources = new Vector(), dasselsrc= (af.featureSettings!=null) ? af.featureSettings.getSelectedSources() + : new jalview.gui.DasSourceBrowser().getSelectedSources(); + Enumeration en = dasselsrc.elements(); + while (en.hasMoreElements()) + { + DasSource src = (DasSource) en.nextElement(); + selsources.addElement(src.getNickname()); + } + int osel = 0; + for (int o=0;otherdb!=null && o0) + { + String[] newsrc = new String[dbSources.length+otherdb.length]; + System.arraycopy(dbSources, 0, newsrc,0,dbSources.length); + System.arraycopy(otherdb, 0, newsrc,dbSources.length, otherdb.length); + dbSources = newsrc; } } - /** * start the fetcher thread * @@ -165,9 +250,16 @@ public class DBRefFetcher implements Runnable { throw new Error("Implementation error. Must initialise dbSources"); } + running = true; long startTime = System.currentTimeMillis(); af.setProgressBar("Fetching db refs", startTime); - running = true; + try { + picrClient = new uk.ac.ebi.www.picr.AccessionMappingService.AccessionMapperServiceLocator().getAccessionMapperPort(); + } catch (Exception e) + { + System.err.println("Couldn't locate PICR service instance.\n"); + e.printStackTrace(); + } int db = 0; Vector sdataset = new Vector(); for (int s = 0; s < dataset.length; s++) @@ -192,6 +284,10 @@ public class DBRefFetcher implements Runnable maxqlen = ((Integer) dbsource.getDbSourceProperties().get( DBRefSource.MULTIACC)).intValue(); } + else + { + maxqlen = 1; + } // iterate through db for each remaining un-verified sequence SequenceI[] currSeqs = new SequenceI[sdataset.size()]; sdataset.copyInto(currSeqs);// seqs that are to be validated against @@ -207,31 +303,38 @@ public class DBRefFetcher implements Runnable { // Still queries to make for current seqIndex StringBuffer queryString = new StringBuffer(""); - int nqSize = (maxqlen > queries.size()) ? queries.size() + int numq=0,nqSize = (maxqlen > queries.size()) ? queries.size() : maxqlen; - for (int nq = 0, numq = 0; nq < nqSize; nq++) + + while (queries.size()>0 && numq < nqSize) { - String query = (String) queries.elementAt(nq); + String query = (String) queries.elementAt(0); if (dbsource.isValidReference(query)) { - queryString.append((nq == 0) ? "" : dbsource + queryString.append((numq == 0) ? "" : dbsource .getAccessionSeparator()); queryString.append(query); numq++; } - } - for (int nq = 0; nq < nqSize; nq++) - { + // remove the extracted query string queries.removeElementAt(0); } // make the queries and process the response AlignmentI retrieved = null; try { + if (jalview.bin.Cache.log.isDebugEnabled()) + { + jalview.bin.Cache.log.debug("Querying "+dbsource.getDbName()+" with : '"+queryString.toString()+"'"); + } retrieved = dbsource.getSequenceRecords(queryString.toString()); } catch (Exception ex) { ex.printStackTrace(); + } catch (OutOfMemoryError err) + { + new OOMWarning("retrieving database references (" + + queryString.toString() + ")", err); } if (retrieved != null) { @@ -249,7 +352,7 @@ public class DBRefFetcher implements Runnable { dbSources[db] }); // jalview.datamodel.DBRefSource.UNIPROT // }); // check for existing dbrefs to use - if (uprefs != null) + if (uprefs != null && uprefs.length>0) { for (int j = 0; j < uprefs.length; j++) { @@ -267,8 +370,27 @@ public class DBRefFetcher implements Runnable while (st.hasMoreTokens()) { String token = st.nextToken(); - addSeqId(sequence, token); - queries.addElement(token.toUpperCase()); + UPEntry[] presp = null; + try { + presp=picrClient.getUPIForAccession(token, null, picrClient.getMappedDatabaseNames(), null, true); + } catch (Exception e) { + System.err.println("Exception with Picr for '"+token+"'\n"); + e.printStackTrace(); + } + if (presp!=null && presp.length>0) + { + for (int id=0;id0) + if (sqs != null && sqs.size() > 0) { Enumeration sqe = sqs.elements(); while (sqe.hasMoreElements()) @@ -383,9 +506,12 @@ public class DBRefFetcher implements Runnable for (int m = 0; m < sequenceMatches.size(); m++) { sequence = (SequenceI) sequenceMatches.elementAt(m); - // only update start and end positions and shift features if there are no existing references - // TODO: test for legacy where uniprot or EMBL refs exist but no mappings are made (but content matches retrieved set) - boolean updateRefFrame = sequence.getDBRef()==null || sequence.getDBRef().length==0; + // only update start and end positions and shift features if there are + // no existing references + // TODO: test for legacy where uniprot or EMBL refs exist but no + // mappings are made (but content matches retrieved set) + boolean updateRefFrame = sequence.getDBRef() == null + || sequence.getDBRef().length == 0; // verify sequence against the entry sequence String nonGapped = AlignSeq.extractGaps("-. ", @@ -405,20 +531,23 @@ public class DBRefFetcher implements Runnable + " SEQUENCE NOT %100 MATCH \n"); continue; } - + sbuffer.append(sequence.getName() + " HAS " + absStart - + " PREFIXED RESIDUES COMPARED TO " + dbSource+"\n"); + + " PREFIXED RESIDUES COMPARED TO " + dbSource + "\n"); // - // + " - ANY SEQUENCE FEATURES" - // + " HAVE BEEN ADJUSTED ACCORDINGLY \n"); + // + " - ANY SEQUENCE FEATURES" + // + " HAVE BEEN ADJUSTED ACCORDINGLY \n"); // absStart = 0; // create valid mapping between matching region of local sequence and // the mapped sequence mp = new Mapping(null, new int[] - { sequence.getStart()+absStart, sequence.getStart()+absStart+entrySeq.length()-1 }, new int[] - { entry.getStart(), - entry.getStart() + entrySeq.length() - 1 }, 1, 1); - updateRefFrame=false; // mapping is based on current start/end so don't modify start and end + { sequence.getStart() + absStart, + sequence.getStart() + absStart + entrySeq.length() - 1 }, + new int[] + { entry.getStart(), + entry.getStart() + entrySeq.length() - 1 }, 1, 1); + updateRefFrame = false; // mapping is based on current start/end so + // don't modify start and end } else { @@ -438,13 +567,15 @@ public class DBRefFetcher implements Runnable SequenceFeature[] sf = sequence.getSequenceFeatures(); int start = sequence.getStart(); int end = sequence.getEnd(); + int startShift = 1 - absStart - start; // how much the features are + // to be shifted by for (int sfi = 0; sfi < sf.length; sfi++) { if (sf[sfi].getBegin() >= start && sf[sfi].getEnd() <= end) { // shift feature along by absstart - sf[sfi].setBegin(sf[sfi].getBegin() + absStart); - sf[sfi].setEnd(sf[sfi].getEnd() + absStart); + sf[sfi].setBegin(sf[sfi].getBegin() + startShift); + sf[sfi].setEnd(sf[sfi].getEnd() + startShift); } } } @@ -465,6 +596,7 @@ public class DBRefFetcher implements Runnable // and remove it from the rest // TODO: decide if we should remove annotated sequence from set sdataset.remove(sequence); + // TODO: should we make a note of sequences that have received new DB ids, so we can query all enabled DAS servers for them ? } } }