From f3581eb4882e66b7d66a9573be5103358196fd2d Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Wed, 18 Oct 2006 13:10:10 +0000 Subject: [PATCH] Wait for response from server before next sequence --- src/jalview/io/DasSequenceFeatureFetcher.java | 618 ++++++++++++------------- 1 file changed, 299 insertions(+), 319 deletions(-) diff --git a/src/jalview/io/DasSequenceFeatureFetcher.java b/src/jalview/io/DasSequenceFeatureFetcher.java index ad2e37b..305160e 100755 --- a/src/jalview/io/DasSequenceFeatureFetcher.java +++ b/src/jalview/io/DasSequenceFeatureFetcher.java @@ -1,21 +1,21 @@ /* -* Jalview - A Sequence Alignment Editor and Viewer -* Copyright (C) 2006 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 -*/ + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2006 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.io; import jalview.datamodel.*; @@ -37,7 +37,6 @@ import org.biojava.dasobert.dasregistry.DasSource; import org.biojava.dasobert.das2.Das2Source; import org.biojava.dasobert.das2.DasSourceConverter; - import jalview.bin.Cache; import org.biojava.dasobert.dasregistry.DasCoordinateSystem; @@ -49,17 +48,14 @@ import javax.swing.*; * @author $author$ * @version $Revision$ */ -public class DasSequenceFeatureFetcher implements Runnable +public class DasSequenceFeatureFetcher { - SequenceI [] sequences; + SequenceI[] sequences; AlignFrame af; StringBuffer sbuffer = new StringBuffer(); Vector selectedSources; long startTime; - int threadsRunning = 0; - boolean allBatchesComplete = false; - /** * Creates a new SequenceFeatureFetcher object. @@ -68,7 +64,7 @@ public class DasSequenceFeatureFetcher implements Runnable * @param align DOCUMENT ME! * @param ap DOCUMENT ME! */ - public DasSequenceFeatureFetcher(SequenceI [] sequences, + public DasSequenceFeatureFetcher(SequenceI[] sequences, final AlignFrame af, Vector selectedSources) { @@ -83,7 +79,7 @@ public class DasSequenceFeatureFetcher implements Runnable DasCoordinateSystem[] coords = source.getCoordinateSystem(); for (int c = 0; c < coords.length; c++) { - if (coords[c].getName().indexOf("UniProt")>-1) + if (coords[c].getName().indexOf("UniProt") > -1) { uniprotCount++; break; @@ -92,15 +88,15 @@ public class DasSequenceFeatureFetcher implements Runnable } int refCount = 0; - for(int i=0; i0) + if (refCount < sequences.length && uniprotCount > 0) { - int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, + int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, "Do you want Jalview to find\n" - +"Uniprot Accession ids for given sequence names?", - "Find Uniprot Accession Ids", - JOptionPane.YES_NO_OPTION, - JOptionPane.QUESTION_MESSAGE); + + "Uniprot Accession ids for given sequence names?", + "Find Uniprot Accession Ids", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE); - if(reply == JOptionPane.YES_OPTION) - { - Thread thread = new Thread(new FetchDBRefs()); - thread.start(); - } - else - startFetching(); + if (reply == JOptionPane.YES_OPTION) + { + Thread thread = new Thread(new FetchDBRefs()); + thread.start(); + } + else + startFetching(); } else startFetching(); - /* System.out.println("User selection is " + - ( ( (float) uniprotCount / (float) selectedSources.size()) * 100) - + " % Uniprot, and "+refCount+" / " +sequences.length+" have uniprot accession"); -*/ - } - - void startFetching() - { - Thread thread = new Thread(this); - thread.start(); - } + } - class FetchDBRefs implements Runnable + class FetchDBRefs + implements Runnable { public void run() { new DBRefFetcher( - af.getViewport().getAlignment(), af).fetchDBRefs(true); + af.getViewport().getAlignment(), af).fetchDBRefs(true); startFetching(); } } - /** - * creates a jalview sequence feature from a das feature document - * @param dasfeature - * @return sequence feature object created using dasfeature information - */ - SequenceFeature newSequenceFeature(Map dasfeature, String nickname) - { - try { - /** - * Different qNames for a DAS Feature - are string keys to the HashMaps in features - * "METHOD") || - qName.equals("TYPE") || - qName.equals("START") || - qName.equals("END") || - qName.equals("NOTE") || - qName.equals("LINK") || - qName.equals("SCORE") - */ - String desc = new String(); - if (dasfeature.containsKey("NOTE")) - desc+=(String) dasfeature.get("NOTE"); - - - int start = 0, end = 0; - float score = 0f; - - try{ start = Integer.parseInt( dasfeature.get("START").toString()); } - catch( Exception ex){} - try{ end = Integer.parseInt( dasfeature.get("END").toString()); } - catch (Exception ex){} - try{ score = Integer.parseInt( dasfeature.get("SCORE").toString()); } - catch (Exception ex){} - - - SequenceFeature f = new SequenceFeature( - (String) dasfeature.get("TYPE"), - desc, - start, - end, - score, - nickname); - - if (dasfeature.containsKey("LINK")) - { - f.addLink(f.getType()+" "+f.begin+"_"+f.end - +"|"+ dasfeature.get("LINK")); - } - // (String) dasfeature.get("ID"), - //// (String) dasfeature.get("METHOD"), - // (String) dasfeature.get("SCORE"), - // null - // ); - - return f; - } - catch (Exception e) { - System.out.println("ERRR "+e); - e.printStackTrace(); - System.out.println("############"); - Cache.log.debug("Failed to parse "+dasfeature.toString(), e); - return null; - } - } - /** - * fetch and add das features to a sequence using the given source URL and Id to create a feature request - * @param seq - * @param SourceUrl - * @param id - */ - protected void createFeatureFetcher(final SequenceI seq, - final String sourceUrl, - String id, - String nickname) - { - ////////////// - /// fetch DAS features - final Das1Source source = new Das1Source(); - source.setUrl(sourceUrl); - source.setNickname(nickname); + /** + * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset + */ + void startFetching() + { + startTime = System.currentTimeMillis(); + af.setProgressBar("Fetching DAS Sequence Features", startTime); + DasSource[] sources = new jalview.gui.DasSourceBrowser().getDASSource(); - Cache.log.debug("new Das Feature Fetcher for " + id + " querying " + - sourceUrl); + if (selectedSources == null || selectedSources.size() == 0) + { + String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE", + "uniprot"); + StringTokenizer st = new StringTokenizer(active, "\t"); + Vector selectedSources = new Vector(); + String token; + while (st.hasMoreTokens()) + { + token = st.nextToken(); + for (int i = 0; i < sources.length; i++) + { + if (sources[i].getNickname().equals(token)) + { + selectedSources.addElement(sources[i]); + break; + } + } + } + } - if (id != null && id.length() > 0) - { - setThreadsRunning(+1); + if (selectedSources == null || selectedSources.size() == 0) + { + System.out.println("No DAS Sources active"); + af.setProgressBar("No DAS Sources Active", startTime); + return; + } - // int start=seq.getStart(), end = seq.getEnd(); - /* if(af.getViewport().getSelectionGroup()!=null) - { - SequenceI tmp = af.getViewport().getAlignment().findName(seq.getName()); - start = tmp.findPosition( - af.getViewport().getSelectionGroup().getStartRes() - ); + sourcesRemaining = selectedSources.size(); + //Now sending requests one at a time to each server + for (int sourceIndex = 0; sourceIndex < selectedSources.size(); + sourceIndex++) + { + DasSource dasSource = (DasSource) selectedSources.elementAt( + sourceIndex); + + nextSequence(dasSource, sequences[0]); + } + } + + int sourcesRemaining=0; + void responseComplete(DasSource dasSource, SequenceI seq) + { + if (seq != null) + { + for (int seqIndex = 0; seqIndex < sequences.length-1; seqIndex++) + { + if (sequences[seqIndex] == seq) + { + nextSequence(dasSource, sequences[++seqIndex]); + return; + } + } + } - end = tmp.findPosition( - af.getViewport().getSelectionGroup().getEndRes() - ); - }*/ + sourcesRemaining --; - FeatureThread fetcher = new FeatureThread(id - // + ":" + start + "," + end, - , source); + if(sourcesRemaining==0) + { + af.setProgressBar("DAS Feature Fetching Complete", startTime); + if(af.featureSettings!=null) + af.featureSettings.setTableData(); + } - fetcher.addFeatureListener(new FeatureListener() - { - public void comeBackLater(FeatureEvent e) - { - setThreadsRunning(-1); - Cache.log.debug("das source " + e.getDasSource().getNickname() + - " asked us to come back in " + e.getComeBackLater() + - " secs."); - } + } - public void newFeatures(FeatureEvent e) - { - setThreadsRunning(-1); - Das1Source ds = e.getDasSource(); - - Map[] features = e.getFeatures(); - // add features to sequence - Cache.log.debug("das source " + ds.getUrl() + " returned " + - features.length + " features"); - - if (features.length > 0) - { - for (int i = 0; i < features.length; i++) - { - SequenceFeature f = newSequenceFeature(features[i], - source.getNickname()); - - seq.addSequenceFeature(f); - } - } - else - { - System.out.println("No features found for sequence " +seq.getName() - +" from: "+ e.getDasSource().getNickname()); - } + void featuresAdded(SequenceI seq) + { + af.getFeatureRenderer().featuresAdded(); - } - } + int start = af.getViewport().getStartSeq(); + int end = af.getViewport().getEndSeq(); + int index; + for(index=start; index0) + // System.out.println("err "+ + for (int l = 0; l < cs.length; l++) + { + if (jalview.util.DBRefUtils.isDasCoordinateSystem(cs[l]. + getName(), uprefs[0])) + { - if(af.featureSettings!=null) - af.featureSettings.setTableData(); - } - } + Cache.log.debug("Launched fetcher for coordinate system " + + cs[l].getName()); + + createFeatureFetcher(seq, + dasSource, + uprefs[0].getAccessionId()); + } + } + } + } + else + { + String id = null; + // try and use the name as the sequence id + if (seq.getName().indexOf("|") > -1) + { + id = seq.getName().substring( + seq.getName().lastIndexOf("|") + 1); + } + else + { + id = seq.getName(); + } + if (id != null) + { + // Should try to call a general feature fetcher that + // queries many sources with name to discover applicable ID references + createFeatureFetcher(seq, + dasSource, + id); + } + } + + } /** - * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset + * fetch and add das features to a sequence using the given source URL and Id to create a feature request + * @param seq + * @param SourceUrl + * @param id */ - public void run() + protected void createFeatureFetcher(final SequenceI seq, + final DasSource dasSource, + String id) { - startTime = System.currentTimeMillis(); - af.setProgressBar("Fetching DAS Sequence Features", startTime); + ////////////// + /// fetch DAS features + final Das1Source source = new Das1Source(); + source.setUrl(dasSource.getUrl()); + source.setNickname(dasSource.getNickname()); - DasSource [] sources = new jalview.gui.DasSourceBrowser().getDASSource(); + Cache.log.debug("new Das Feature Fetcher for " + id + " querying " + + dasSource.getUrl()); - if(selectedSources==null || selectedSources.size()==0) + if (id != null && id.length() > 0) { - String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE", "uniprot"); - StringTokenizer st = new StringTokenizer(active, "\t"); - Vector selectedSources = new Vector(); - String token; - while (st.hasMoreTokens()) + FeatureThread fetcher = new FeatureThread(id + // + ":" + start + "," + end, + , source); + + fetcher.addFeatureListener(new FeatureListener() { - token = st.nextToken(); - for (int i = 0; i < sources.length; i++) + public void comeBackLater(FeatureEvent e) { - if (sources[i].getNickname().equals(token)) - { - selectedSources.addElement(sources[i]); - break; - } + responseComplete(dasSource, seq); + Cache.log.debug("das source " + e.getDasSource().getNickname() + + " asked us to come back in " + e.getComeBackLater() + + " secs."); } - } - } - if(selectedSources == null || selectedSources.size()==0) - { - System.out.println("No DAS Sources active"); - af.setProgressBar("No DAS Sources Active", startTime); - return; - } + public void newFeatures(FeatureEvent e) + { - try - { - //We must limit the feature fetching to 20 to prevent - //Servers being overloaded with too many requests - int batchCount = 0; - int batchMaxSize = 10; - int seqIndex = 0; - while (seqIndex < sequences.length) - { - batchCount ++; - if(batchCount>=batchMaxSize) - { - waitTillBatchComplete(); - batchCount = 0; - } + Das1Source ds = e.getDasSource(); - DBRefEntry [] uprefs = jalview.util.DBRefUtils.selectRefs(sequences[seqIndex].getDBRef(), - new String[] { - jalview.datamodel.DBRefSource.PDB, - jalview.datamodel.DBRefSource.UNIPROT}); + Map[] features = e.getFeatures(); + // add features to sequence + Cache.log.debug("das source " + ds.getUrl() + " returned " + + features.length + " features"); - for(int sourceIndex=0; sourceIndex 0) { - DasSource dasSource = (DasSource)selectedSources.elementAt(sourceIndex); - - if (uprefs != null) + for (int i = 0; i < features.length; i++) { - // we know the id for this entry, so don't note its ID in the unknownSequences list - for (int j = 0; j < uprefs.length; j++) - { + SequenceFeature f = newSequenceFeature(features[i], + source.getNickname()); - // Will have to pass any mapping information to the fetcher - the start/end for the DBRefEntry may not be the same as the sequence's start/end - DasCoordinateSystem cs[] = dasSource.getCoordinateSystem(); - for (int l=0; l -1) - { - id = sequences[seqIndex].getName().substring( - sequences[seqIndex].getName().lastIndexOf("|") + 1); - } - else - { - id = sequences[seqIndex].getName(); - } - if (id != null) - { - // Should try to call a general feature fetcher that queries many sources with name to discover applicable ID references - createFeatureFetcher(sequences[seqIndex], - dasSource.getUrl(), - id, - dasSource.getNickname()); - } + seq.addSequenceFeature(f); } + + featuresAdded(seq); + } + else + { + // System.out.println("No features found for " + seq.getName() + // + " from: " + e.getDasSource().getNickname()); } + responseComplete(dasSource, seq); - seqIndex++; - } - } - catch (Exception ex) - { - ex.printStackTrace(); + } + } + + ); + + fetcher.start(); } - allBatchesComplete = true; } - void waitTillBatchComplete() + /** + * creates a jalview sequence feature from a das feature document + * @param dasfeature + * @return sequence feature object created using dasfeature information + */ + SequenceFeature newSequenceFeature(Map dasfeature, String nickname) { - while( threadsRunning > 0 ) + try { - try{ - Thread.sleep(500); - - }catch(Exception ex) + /** + * Different qNames for a DAS Feature - are string keys to the HashMaps in features + * "METHOD") || + qName.equals("TYPE") || + qName.equals("START") || + qName.equals("END") || + qName.equals("NOTE") || + qName.equals("LINK") || + qName.equals("SCORE") + */ + String desc = new String(); + if (dasfeature.containsKey("NOTE")) + desc += (String) dasfeature.get("NOTE"); + + int start = 0, end = 0; + float score = 0f; + + try { - ex.printStackTrace(); + start = Integer.parseInt(dasfeature.get("START").toString()); + } + catch (Exception ex) + {} + try + { + end = Integer.parseInt(dasfeature.get("END").toString()); + } + catch (Exception ex) + {} + try + { + score = Integer.parseInt(dasfeature.get("SCORE").toString()); + } + catch (Exception ex) + {} + + SequenceFeature f = new SequenceFeature( + (String) dasfeature.get("TYPE"), + desc, + start, + end, + score, + nickname); + + if (dasfeature.containsKey("LINK")) + { + f.addLink(f.getType() + " " + f.begin + "_" + f.end + + "|" + dasfeature.get("LINK")); } - } + return f; + } + catch (Exception e) + { + System.out.println("ERRR " + e); + e.printStackTrace(); + System.out.println("############"); + Cache.log.debug("Failed to parse " + dasfeature.toString(), e); + return null; + } } - public static DasSource[] getDASSources() { DasSourceReaderImpl reader = new DasSourceReaderImpl(); - String registryURL =jalview.bin.Cache.getDefault("DAS_REGISTRY_URL", + String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL", "http://das.sanger.ac.uk/registry/das1/sources/" - ); + ); try { @@ -497,8 +478,7 @@ public class DasSequenceFeatureFetcher implements Runnable ex.printStackTrace(); return null; } - } -} +} -- 1.7.10.2