2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
24 import javax.swing.*;
\r
26 import org.biojava.dasobert.das.*;
\r
27 import org.biojava.dasobert.das2.*;
\r
28 import org.biojava.dasobert.das2.io.*;
\r
29 import org.biojava.dasobert.dasregistry.*;
\r
30 import org.biojava.dasobert.eventmodel.*;
\r
31 import jalview.bin.Cache;
\r
32 import jalview.datamodel.*;
\r
33 import jalview.gui.*;
\r
39 * @version $Revision$
\r
41 public class DasSequenceFeatureFetcher
\r
43 SequenceI[] sequences;
\r
47 FeatureSettings fsettings;
\r
49 StringBuffer sbuffer = new StringBuffer();
\r
51 Vector selectedSources;
\r
53 boolean cancelled = false;
\r
58 * Creates a new SequenceFeatureFetcher object.
\r
61 * @param align DOCUMENT ME!
\r
62 * @param ap DOCUMENT ME!
\r
64 public DasSequenceFeatureFetcher(SequenceI[] sequences,
\r
65 FeatureSettings fsettings, Vector selectedSources)
\r
67 this(sequences, fsettings, selectedSources, true, true);
\r
69 public DasSequenceFeatureFetcher(SequenceI[] sequences,
\r
70 FeatureSettings fsettings, Vector selectedSources, boolean checkDbrefs, boolean promptFetchDbrefs)
\r
72 this.selectedSources = selectedSources;
\r
73 this.sequences = sequences;
\r
74 if (fsettings!=null)
\r
76 this.fsettings = fsettings;
\r
77 this.af = fsettings.af;
\r
78 af.getViewport().setShowSequenceFeatures(true);
\r
80 int uniprotCount = 0;
\r
81 for (int i = 0; i < selectedSources.size(); i++)
\r
83 DasSource source = (DasSource) selectedSources.elementAt(i);
\r
84 DasCoordinateSystem[] coords = source.getCoordinateSystem();
\r
85 for (int c = 0; c < coords.length; c++)
\r
87 if (coords[c].getName().indexOf("UniProt") > -1)
\r
96 for (int i = 0; i < sequences.length; i++)
\r
98 DBRefEntry[] dbref = sequences[i].getDBRef();
\r
101 for (int j = 0; j < dbref.length; j++)
\r
103 if (dbref[j].getSource().equals(
\r
104 jalview.datamodel.DBRefSource.UNIPROT))
\r
113 if (checkDbrefs && refCount < sequences.length && uniprotCount > 0)
\r
116 int reply = JOptionPane.YES_OPTION;
\r
117 if (promptFetchDbrefs)
\r
119 reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
\r
120 "Do you want Jalview to find\n"
\r
121 + "Uniprot Accession ids for given sequence names?",
\r
122 "Find Uniprot Accession Ids", JOptionPane.YES_NO_OPTION,
\r
123 JOptionPane.QUESTION_MESSAGE);
\r
127 if (reply == JOptionPane.YES_OPTION)
\r
129 Thread thread = new Thread(new FetchDBRefs());
\r
144 class FetchDBRefs implements Runnable
\r
148 new DBRefFetcher(sequences, af).fetchDBRefs(true);
\r
154 * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset
\r
156 void startFetching()
\r
159 startTime = System.currentTimeMillis();
\r
160 af.setProgressBar("Fetching DAS Sequence Features", startTime);
\r
162 DasSource[] sources = new jalview.gui.DasSourceBrowser().getDASSource();
\r
164 if (selectedSources == null || selectedSources.size() == 0)
\r
166 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
\r
168 StringTokenizer st = new StringTokenizer(active, "\t");
\r
169 Vector selectedSources = new Vector();
\r
171 while (st.hasMoreTokens())
\r
173 token = st.nextToken();
\r
174 for (int i = 0; i < sources.length; i++)
\r
176 if (sources[i].getNickname().equals(token))
\r
178 selectedSources.addElement(sources[i]);
\r
185 if (selectedSources == null || selectedSources.size() == 0)
\r
187 System.out.println("No DAS Sources active");
\r
189 setGuiNoDassourceActive();
\r
193 sourcesRemaining = selectedSources.size();
\r
194 //Now sending requests one at a time to each server
\r
195 for (int sourceIndex = 0; sourceIndex < selectedSources.size()
\r
196 && !cancelled; sourceIndex++)
\r
198 DasSource dasSource = (DasSource) selectedSources
\r
199 .elementAt(sourceIndex);
\r
201 nextSequence(dasSource, sequences[0]);
\r
205 private void setGuiNoDassourceActive()
\r
210 af.setProgressBar("No DAS Sources Active", startTime);
\r
212 if (getFeatSettings()!=null)
\r
214 fsettings.noDasSourceActive();
\r
219 * Update our fsettings dialog reference if we didn't have one when we were first initialised.
\r
220 * @return fsettings
\r
222 private FeatureSettings getFeatSettings()
\r
224 if (fsettings == null)
\r
228 fsettings = af.featureSettings;
\r
234 public void cancel()
\r
238 af.setProgressBar("DAS Feature Fetching Cancelled", startTime);
\r
243 int sourcesRemaining = 0;
\r
245 void responseComplete(DasSource dasSource, SequenceI seq)
\r
249 for (int seqIndex = 0; seqIndex < sequences.length - 1 && !cancelled; seqIndex++)
\r
251 if (sequences[seqIndex] == seq)
\r
253 nextSequence(dasSource, sequences[++seqIndex]);
\r
259 sourcesRemaining--;
\r
261 if (sourcesRemaining == 0)
\r
263 System.err.println("Fetching Complete.");
\r
264 setGuiFetchComplete();
\r
269 private void setGuiFetchComplete()
\r
274 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
277 if (af != null && af.featureSettings != null)
\r
279 af.featureSettings.setTableData();
\r
282 if (getFeatSettings() != null)
\r
284 fsettings.complete();
\r
288 void featuresAdded(SequenceI seq)
\r
292 // no gui to update with features.
\r
295 af.getFeatureRenderer().featuresAdded();
\r
297 int start = af.getViewport().getStartSeq();
\r
298 int end = af.getViewport().getEndSeq();
\r
300 for (index = start; index < end; index++)
\r
302 if (seq == af.getViewport().getAlignment().getSequenceAt(index)
\r
303 .getDatasetSequence())
\r
305 af.alignPanel.paintAlignment(true);
\r
311 void nextSequence(DasSource dasSource, SequenceI seq)
\r
315 DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(
\r
316 seq.getDBRef(), new String[]
\r
318 // jalview.datamodel.DBRefSource.PDB,
\r
319 jalview.datamodel.DBRefSource.UNIPROT,
\r
320 // jalview.datamodel.DBRefSource.EMBL - not tested on any EMBL coord sys sources
\r
322 // TODO: minimal list of DAS queries to make by querying with untyped ID if distinct from any typed IDs
\r
324 boolean dasCoordSysFound = false;
\r
326 if (uprefs != null)
\r
328 // do any of these ids match the source's coordinate system ?
\r
329 for (int j = 0; !dasCoordSysFound && j < uprefs.length; j++)
\r
331 DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();
\r
333 for (int csIndex = 0; csIndex < cs.length && !dasCoordSysFound; csIndex++)
\r
336 && jalview.util.DBRefUtils.isDasCoordinateSystem(
\r
337 cs[csIndex].getName(), uprefs[j]))
\r
339 Cache.log.debug("Launched fetcher for coordinate system "
\r
340 + cs[0].getName());
\r
341 // Will have to pass any mapping information to the fetcher
\r
342 //- the start/end for the DBRefEntry may not be the same as the sequence's start/end
\r
344 System.out.println(seq.getName() + " "
\r
345 + (seq.getDatasetSequence() == null) + " "
\r
346 + dasSource.getUrl());
\r
348 dasCoordSysFound = true; // break's out of the loop
\r
349 createFeatureFetcher(seq, dasSource, uprefs[j]);
\r
352 System.out.println("IGNORE " + cs[csIndex].getName());
\r
357 if (!dasCoordSysFound)
\r
360 // try and use the name as the sequence id
\r
361 if (seq.getName().indexOf("|") > -1)
\r
363 id = seq.getName().substring(seq.getName().lastIndexOf("|") + 1);
\r
364 if (id.trim().length()<4)
\r
366 // hack - we regard a significant ID as being at least 4 non-whitespace characters
\r
367 id = seq.getName().substring(0, seq.getName().lastIndexOf("|"));
\r
368 if (id.indexOf("|")>-1)
\r
370 id = id.substring(id.lastIndexOf("|")+1);
\r
376 id = seq.getName();
\r
380 // Should try to call a general feature fetcher that
\r
381 // queries many sources with name to discover applicable ID references
\r
382 createFeatureFetcher(seq, dasSource, id);
\r
389 * fetch and add das features to a sequence using the given source URL and compatible DbRef id.
\r
390 * new features are mapped using the DbRef mapping to the local coordinate system.
\r
395 protected void createFeatureFetcher(final SequenceI seq,
\r
396 final DasSource dasSource, final DBRefEntry dbref)
\r
400 /// fetch DAS features
\r
401 final Das1Source source = new Das1Source();
\r
402 source.setUrl(dasSource.getUrl());
\r
403 source.setNickname(dasSource.getNickname());
\r
404 if (dbref == null || dbref.getAccessionId() == null
\r
405 || dbref.getAccessionId().length() < 1)
\r
407 responseComplete(dasSource, seq); // reduce thread count anyhow
\r
410 Cache.log.debug("new Das Feature Fetcher for " + dbref.getSource()
\r
411 + ":" + dbref.getAccessionId() + " querying "
\r
412 + dasSource.getUrl());
\r
413 FeatureThread fetcher = new FeatureThread(dbref.getAccessionId()
\r
414 // + ":" + start + "," + end,
\r
417 fetcher.addFeatureListener(new FeatureListener()
\r
419 public void comeBackLater(FeatureEvent e)
\r
421 responseComplete(dasSource, seq);
\r
422 Cache.log.debug("das source " + e.getDasSource().getNickname()
\r
423 + " asked us to come back in " + e.getComeBackLater()
\r
427 public void newFeatures(FeatureEvent e)
\r
430 Das1Source ds = e.getDasSource();
\r
432 Map[] features = e.getFeatures();
\r
433 // add features to sequence
\r
434 Cache.log.debug("das source " + ds.getUrl() + " returned "
\r
435 + features.length + " features");
\r
437 if (features.length > 0)
\r
439 for (int i = 0; i < features.length; i++)
\r
441 SequenceFeature f = newSequenceFeature(features[i], source
\r
443 if (dbref.getMap() != null && f.getBegin() > 0
\r
446 Cache.log.debug("mapping from " + f.getBegin() + " - "
\r
448 SequenceFeature vf[] = null;
\r
452 vf = dbref.getMap().locateFeature(f);
\r
453 } catch (Exception ex)
\r
456 .info("Error in 'experimental' mapping of features. Please try to reproduce and then report info to help@jalview.org.");
\r
457 Cache.log.info("Mapping feature from " + f.getBegin()
\r
458 + " to " + f.getEnd() + " in dbref "
\r
459 + dbref.getAccessionId() + " in "
\r
460 + dbref.getSource());
\r
461 Cache.log.info("using das Source " + ds.getUrl());
\r
462 Cache.log.info("Exception", ex);
\r
467 for (int v = 0; v < vf.length; v++)
\r
469 Cache.log.debug("mapping to " + v + ": "
\r
470 + vf[v].getBegin() + " - " + vf[v].getEnd());
\r
471 seq.addSequenceFeature(vf[v]);
\r
477 seq.addSequenceFeature(f);
\r
481 featuresAdded(seq);
\r
485 // System.out.println("No features found for " + seq.getName()
\r
486 // + " from: " + e.getDasSource().getNickname());
\r
488 responseComplete(dasSource, seq);
\r
498 protected void createFeatureFetcher(final SequenceI seq,
\r
499 final DasSource dasSource, String id)
\r
502 /// fetch DAS features
\r
503 final Das1Source source = new Das1Source();
\r
504 source.setUrl(dasSource.getUrl());
\r
505 source.setNickname(dasSource.getNickname());
\r
511 if (id != null && id.length() > 0)
\r
513 Cache.log.debug("new Das Feature Fetcher for " + id + " querying "
\r
514 + dasSource.getUrl());
\r
515 FeatureThread fetcher = new FeatureThread(id
\r
516 // + ":" + start + "," + end,
\r
519 fetcher.addFeatureListener(new FeatureListener()
\r
521 public void comeBackLater(FeatureEvent e)
\r
523 responseComplete(dasSource, seq);
\r
524 Cache.log.debug("das source " + e.getDasSource().getNickname()
\r
525 + " asked us to come back in " + e.getComeBackLater()
\r
529 public void newFeatures(FeatureEvent e)
\r
532 Das1Source ds = e.getDasSource();
\r
534 Map[] features = e.getFeatures();
\r
535 // add features to sequence
\r
536 Cache.log.debug("das source " + ds.getUrl() + " returned "
\r
537 + features.length + " features");
\r
539 if (features.length > 0)
\r
541 for (int i = 0; i < features.length; i++)
\r
543 SequenceFeature f = newSequenceFeature(features[i], source
\r
546 seq.addSequenceFeature(f);
\r
549 featuresAdded(seq);
\r
553 // System.out.println("No features found for " + seq.getName()
\r
554 // + " from: " + e.getDasSource().getNickname());
\r
556 responseComplete(dasSource, seq);
\r
565 // invalid fetch - indicate it is finished.
\r
566 Cache.log.debug("Skipping empty ID for querying "
\r
567 + dasSource.getUrl());
\r
568 responseComplete(dasSource, seq);
\r
573 * creates a jalview sequence feature from a das feature document
\r
574 * @param dasfeature
\r
575 * @return sequence feature object created using dasfeature information
\r
577 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
579 if (dasfeature == null)
\r
586 * Different qNames for a DAS Feature - are string keys to the HashMaps in features
\r
588 qName.equals("TYPE") ||
\r
589 qName.equals("START") ||
\r
590 qName.equals("END") ||
\r
591 qName.equals("NOTE") ||
\r
592 qName.equals("LINK") ||
\r
593 qName.equals("SCORE")
\r
595 String desc = new String();
\r
596 if (dasfeature.containsKey("NOTE"))
\r
598 desc += (String) dasfeature.get("NOTE");
\r
601 int start = 0, end = 0;
\r
606 start = Integer.parseInt(dasfeature.get("START").toString());
\r
607 } catch (Exception ex)
\r
612 end = Integer.parseInt(dasfeature.get("END").toString());
\r
613 } catch (Exception ex)
\r
618 score = Integer.parseInt(dasfeature.get("SCORE").toString());
\r
619 } catch (Exception ex)
\r
623 SequenceFeature f = new SequenceFeature((String) dasfeature
\r
624 .get("TYPE"), desc, start, end, score, nickname);
\r
626 if (dasfeature.containsKey("LINK"))
\r
628 f.addLink(f.getType() + " " + f.begin + "_" + f.end + "|"
\r
629 + dasfeature.get("LINK"));
\r
633 } catch (Exception e)
\r
635 System.out.println("ERRR " + e);
\r
636 e.printStackTrace();
\r
637 System.out.println("############");
\r
638 Cache.log.debug("Failed to parse " + dasfeature.toString(), e);
\r
643 * query the default DAS Source Registry for sources.
\r
644 * Uses value of jalview property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that doesn't exist.
\r
645 * @return list of sources
\r
647 public static DasSource[] getDASSources()
\r
650 String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
651 DasSourceBrowser.DEFAULT_REGISTRY);
\r
652 return getDASSources(registryURL);
\r
655 * query the given URL for DasSources.
\r
656 * @param registryURL
\r
657 * return sources from registryURL
\r
659 public static DasSource[] getDASSources(String registryURL)
\r
661 DasSourceReaderImpl reader = new DasSourceReaderImpl();
\r
665 URL url = new URL(registryURL);
\r
667 DasSource[] sources = reader.readDasSource(url);
\r
669 List das1sources = new ArrayList();
\r
670 for (int i = 0; i < sources.length; i++)
\r
672 DasSource ds = sources[i];
\r
673 if (ds instanceof Das2Source)
\r
675 Das2Source d2s = (Das2Source) ds;
\r
676 if (d2s.hasDas1Capabilities())
\r
678 Das1Source d1s = DasSourceConverter.toDas1Source(d2s);
\r
679 das1sources.add(d1s);
\r
683 else if (ds instanceof Das1Source)
\r
685 das1sources.add((Das1Source) ds);
\r
689 return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources
\r
691 } catch (Exception ex)
\r
693 ex.printStackTrace();
\r