X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FDasSequenceFeatureFetcher.java;h=fbd7442cf929024aebfcabe32238dd533a232afb;hb=7bc226b58110fa26d9dbd3f0c78095d06909ffc3;hp=d0bc92db6d88573c92a04fadb0205d479969493d;hpb=3bdd4947c87108581abe34d05c878ee110b60f72;p=jalview.git diff --git a/src/jalview/io/DasSequenceFeatureFetcher.java b/src/jalview/io/DasSequenceFeatureFetcher.java index d0bc92d..fbd7442 100755 --- a/src/jalview/io/DasSequenceFeatureFetcher.java +++ b/src/jalview/io/DasSequenceFeatureFetcher.java @@ -1,46 +1,36 @@ /* -* Jalview - A Sequence Alignment Editor and Viewer -* Copyright (C) 2005 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) 2007 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.*; - -import jalview.gui.*; - +import java.net.*; import java.util.*; -import java.net.URL; - -import org.biojava.dasobert.das.FeatureThread; -import org.biojava.dasobert.dasregistry.Das1Source; -import org.biojava.dasobert.eventmodel.FeatureEvent; -import org.biojava.dasobert.eventmodel.FeatureListener; -import org.biojava.dasobert.dasregistry.DasSource; - -import org.biojava.dasobert.das2.io.DasSourceReaderImpl; -import org.biojava.dasobert.dasregistry.Das1Source; -import org.biojava.dasobert.dasregistry.DasSource; -import org.biojava.dasobert.das2.Das2Source; -import org.biojava.dasobert.das2.DasSourceConverter; - +import javax.swing.*; +import org.biojava.dasobert.das.*; +import org.biojava.dasobert.das2.*; +import org.biojava.dasobert.das2.io.*; +import org.biojava.dasobert.dasregistry.*; +import org.biojava.dasobert.eventmodel.*; import jalview.bin.Cache; - +import jalview.datamodel.*; +import jalview.gui.*; /** * DOCUMENT ME! @@ -48,276 +38,428 @@ import jalview.bin.Cache; * @author $author$ * @version $Revision$ */ -public class DasSequenceFeatureFetcher implements Runnable +public class DasSequenceFeatureFetcher { - final AlignmentI dataset; - final AlignFrame af; + SequenceI[] sequences; + AlignFrame af; + FeatureSettings fsettings; StringBuffer sbuffer = new StringBuffer(); + Vector selectedSources; + boolean cancelled = false; - + long startTime; /** * Creates a new SequenceFeatureFetcher object. + * Uses default * * @param align DOCUMENT ME! * @param ap DOCUMENT ME! */ - public DasSequenceFeatureFetcher(AlignmentI align, - AlignFrame af) + public DasSequenceFeatureFetcher(SequenceI[] sequences, + FeatureSettings fsettings, + Vector selectedSources) { - this.dataset = align.getDataset(); - this.af = af; + this.selectedSources = selectedSources; + this.sequences = sequences; + this.af = fsettings.af; + this.fsettings = fsettings; - Thread thread = new Thread(this); - thread.start(); - } - /** - * 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 - // ); - - // System.out.println(nickname+" "+f.getType()+" "+f.begin+" "+f.end); - return f; - } - catch (Exception e) { - e.printStackTrace(); - 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 Sequence seq, - final String sourceUrl, - String id, - String nickname) { - ////////////// - /// fetch DAS features - final Das1Source source = new Das1Source(); - source.setUrl(sourceUrl); - source.setNickname(nickname); - - - Cache.log.debug("new Das Feature Fetcher for " + id + " querying " + - sourceUrl); - if (id != null && id.length() > 0) + int uniprotCount = 0; + for (int i = 0; i < selectedSources.size(); i++) + { + DasSource source = (DasSource) selectedSources.elementAt(i); + DasCoordinateSystem[] coords = source.getCoordinateSystem(); + for (int c = 0; c < coords.length; c++) + { + if (coords[c].getName().indexOf("UniProt") > -1) + { + uniprotCount++; + break; + } + } + } + + int refCount = 0; + for (int i = 0; i < sequences.length; i++) + { + DBRefEntry[] dbref = sequences[i].getDBRef(); + if (dbref != null) + { + for (int j = 0; j < dbref.length; j++) + { + if (dbref[j].getSource() + .equals(jalview.datamodel.DBRefSource.UNIPROT)) { - FeatureThread fetcher = new FeatureThread(id - // + ":" + seq.getStart() + "," +seq.getEnd() - , source); + refCount++; + break; + } + } + } + } - fetcher.addFeatureListener(new FeatureListener() - { - public void comeBackLater(FeatureEvent e) - { - Cache.log.debug("das source " + e.getDasSource().getNickname() + - " asked us to come back in " + e.getComeBackLater() + - " secs."); - } - - public void newFeatures(FeatureEvent e) - { - 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); - } - } - } - } + if (refCount < sequences.length && uniprotCount > 0) + { - ); + 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); - //NOTE alignPanel listener will be called after the previous - //anonymous listener!!! - fetcher.addFeatureListener(af); + if (reply == JOptionPane.YES_OPTION) + { + Thread thread = new Thread(new FetchDBRefs()); + thread.start(); + } + else + { + startFetching(); + } + } + else + { + startFetching(); + } - fetcher.start(); - } } + + class FetchDBRefs + implements Runnable + { + public void run() + { + new DBRefFetcher( + af.getViewport().getAlignment(), af).fetchDBRefs(true); + startFetching(); + } + } + /** * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset */ - public void run() + void startFetching() { - long startTime = System.currentTimeMillis(); + cancelled = false; + startTime = System.currentTimeMillis(); af.setProgressBar("Fetching DAS Sequence Features", startTime); - DasSource [] sources = new jalview.gui.DasSourceBrowser().getDASSource(); + DasSource[] sources = new jalview.gui.DasSourceBrowser().getDASSource(); - 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()) + if (selectedSources == null || selectedSources.size() == 0) { - token = st.nextToken(); - for(int i=0; i 0) + { + // for (int l = 0; l < cs.length; l++) + { + // + if (jalview.util.DBRefUtils + .isDasCoordinateSystem(cs[0].getName(), uprefs[0])) { - // we know the id for this entry, so don't note its ID in the unknownSequences list - for (int j = 0; j < uprefs.size(); j++) - { - - // 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 - org.biojava.dasobert.dasregistry.DasCoordinateSystem cs[] = dasSource.getCoordinateSystem(); - for (int l=0; l -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); + } + } + + } + + /** + * 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 DasSource dasSource, + String id) + { + ////////////// + /// fetch DAS features + final Das1Source source = new Das1Source(); + source.setUrl(dasSource.getUrl()); + source.setNickname(dasSource.getNickname()); + + Cache.log.debug("new Das Feature Fetcher for " + id + " querying " + + dasSource.getUrl()); + + if (id != null && id.length() > 0) + { + FeatureThread fetcher = new FeatureThread(id + // + ":" + start + "," + end, + , source); + + fetcher.addFeatureListener(new FeatureListener() + { + public void comeBackLater(FeatureEvent e) + { + responseComplete(dasSource, seq); + Cache.log.debug("das source " + e.getDasSource().getNickname() + + " asked us to come back in " + e.getComeBackLater() + + " secs."); + } + + public void newFeatures(FeatureEvent e) + { + + 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++) { - String id = null; - // try and use the name as the sequence id - if (sequence.getName().indexOf("|") > -1) - { - id = sequence.getName().substring( - sequence.getName().lastIndexOf("|") + 1); - } - else - { - id = sequence.getName(); - } - if (id != null) - { - // Should try to call a general feature fetcher that queries many sources with name to discover applicable ID references - createFeatureFetcher(sequence, - dasSource.getUrl(), - id, - dasSource.getNickname()); - } + SequenceFeature f = newSequenceFeature(features[i], + source.getNickname()); + + seq.addSequenceFeature(f); } + + featuresAdded(seq); + } + else + { + // System.out.println("No features found for " + seq.getName() + // + " from: " + e.getDasSource().getNickname()); } + responseComplete(dasSource, seq); - seqIndex++; + } + } + + ); + + fetcher.start(); } + } + + /** + * 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")); + } + + return f; } - catch (Exception ex) + catch (Exception e) { - ex.printStackTrace(); + System.out.println("ERRR " + e); + e.printStackTrace(); + System.out.println("############"); + Cache.log.debug("Failed to parse " + dasfeature.toString(), e); + return null; } - - af.setProgressBar("DAS Feature Fetching Complete", startTime); } - public static DasSource[] getDASSources() { DasSourceReaderImpl reader = new DasSourceReaderImpl(); - String registryURL =jalview.bin.Cache.getDefault("DAS_REGISTRY_URL", - "http://www.spice-3d.org/dasregistry/das1/sources" - ); + String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL", + DasSourceBrowser.DEFAULT_REGISTRY + ); try { @@ -352,8 +494,6 @@ public class DasSequenceFeatureFetcher implements Runnable ex.printStackTrace(); return null; } - } -} - +}