2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
\r
3 * Copyright (C) 2008 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
54 private void debug(String mesg)
\r
58 private void debug(String mesg, Exception e)
\r
60 if (Cache.log!=null)
\r
62 Cache.log.debug(mesg, e);
\r
64 System.err.println(mesg);
\r
67 e.printStackTrace();
\r
74 * Creates a new SequenceFeatureFetcher object.
\r
77 * @param align DOCUMENT ME!
\r
78 * @param ap DOCUMENT ME!
\r
80 public DasSequenceFeatureFetcher(SequenceI[] sequences,
\r
81 FeatureSettings fsettings, Vector selectedSources)
\r
83 this(sequences, fsettings, selectedSources, true, true);
\r
85 public DasSequenceFeatureFetcher(SequenceI[] sequences,
\r
86 FeatureSettings fsettings, Vector selectedSources, boolean checkDbrefs, boolean promptFetchDbrefs)
\r
88 this.selectedSources = selectedSources;
\r
89 this.sequences = sequences;
\r
90 if (fsettings!=null)
\r
92 this.fsettings = fsettings;
\r
93 this.af = fsettings.af;
\r
94 af.getViewport().setShowSequenceFeatures(true);
\r
96 int uniprotCount = 0;
\r
97 for (int i = 0; i < selectedSources.size(); i++)
\r
99 DasSource source = (DasSource) selectedSources.elementAt(i);
\r
100 DasCoordinateSystem[] coords = source.getCoordinateSystem();
\r
101 for (int c = 0; c < coords.length; c++)
\r
103 // TODO: match UniProt coord system canonically (?) - does UniProt==uniprot==UNIPROT ?
\r
104 if (coords[c].getName().indexOf("UniProt") > -1)
\r
113 for (int i = 0; i < sequences.length; i++)
\r
115 DBRefEntry[] dbref = sequences[i].getDBRef();
\r
118 for (int j = 0; j < dbref.length; j++)
\r
120 if (dbref[j].getSource().equals(
\r
121 jalview.datamodel.DBRefSource.UNIPROT))
\r
130 if (checkDbrefs && refCount < sequences.length && uniprotCount > 0)
\r
133 int reply = JOptionPane.YES_OPTION;
\r
134 if (promptFetchDbrefs)
\r
136 reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
\r
137 "Do you want Jalview to find\n"
\r
138 + "Uniprot Accession ids for given sequence names?",
\r
139 "Find Uniprot Accession Ids", JOptionPane.YES_NO_OPTION,
\r
140 JOptionPane.QUESTION_MESSAGE);
\r
144 if (reply == JOptionPane.YES_OPTION)
\r
146 Thread thread = new Thread(new FetchDBRefs());
\r
161 class FetchDBRefs implements Runnable
\r
165 new DBRefFetcher(sequences, af).fetchDBRefs(true);
\r
171 * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset
\r
173 void startFetching()
\r
176 startTime = System.currentTimeMillis();
\r
179 af.setProgressBar("Fetching DAS Sequence Features", startTime);
\r
182 DasSource[] sources = new jalview.gui.DasSourceBrowser().getDASSource();
\r
184 if (selectedSources == null || selectedSources.size() == 0)
\r
186 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
\r
188 StringTokenizer st = new StringTokenizer(active, "\t");
\r
189 Vector selectedSources = new Vector();
\r
191 while (st.hasMoreTokens())
\r
193 token = st.nextToken();
\r
194 for (int i = 0; i < sources.length; i++)
\r
196 if (sources[i].getNickname().equals(token))
\r
198 selectedSources.addElement(sources[i]);
\r
205 if (selectedSources == null || selectedSources.size() == 0)
\r
207 System.out.println("No DAS Sources active");
\r
209 setGuiNoDassourceActive();
\r
213 sourcesRemaining = selectedSources.size();
\r
214 //Now sending requests one at a time to each server
\r
215 for (int sourceIndex = 0; sourceIndex < selectedSources.size()
\r
216 && !cancelled; sourceIndex++)
\r
218 DasSource dasSource = (DasSource) selectedSources
\r
219 .elementAt(sourceIndex);
\r
221 nextSequence(dasSource, sequences[0]);
\r
225 private void setGuiNoDassourceActive()
\r
230 af.setProgressBar("No DAS Sources Active", startTime);
\r
232 if (getFeatSettings()!=null)
\r
234 fsettings.noDasSourceActive();
\r
239 * Update our fsettings dialog reference if we didn't have one when we were first initialised.
\r
240 * @return fsettings
\r
242 private FeatureSettings getFeatSettings()
\r
244 if (fsettings == null)
\r
248 fsettings = af.featureSettings;
\r
254 public void cancel()
\r
258 af.setProgressBar("DAS Feature Fetching Cancelled", startTime);
\r
263 int sourcesRemaining = 0;
\r
265 void responseComplete(DasSource dasSource, SequenceI seq)
\r
269 for (int seqIndex = 0; seqIndex < sequences.length - 1 && !cancelled; seqIndex++)
\r
271 if (sequences[seqIndex] == seq)
\r
273 nextSequence(dasSource, sequences[++seqIndex]);
\r
279 sourcesRemaining--;
\r
281 if (sourcesRemaining == 0)
\r
283 System.err.println("Fetching Complete.");
\r
284 setGuiFetchComplete();
\r
289 private void setGuiFetchComplete()
\r
294 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
297 if (af != null && af.featureSettings != null)
\r
299 af.featureSettings.setTableData();
\r
302 if (getFeatSettings() != null)
\r
304 fsettings.complete();
\r
308 void featuresAdded(SequenceI seq)
\r
312 // no gui to update with features.
\r
315 af.getFeatureRenderer().featuresAdded();
\r
317 int start = af.getViewport().getStartSeq();
\r
318 int end = af.getViewport().getEndSeq();
\r
320 for (index = start; index < end; index++)
\r
322 if (seq == af.getViewport().getAlignment().getSequenceAt(index)
\r
323 .getDatasetSequence())
\r
325 af.alignPanel.paintAlignment(true);
\r
331 void nextSequence(DasSource dasSource, SequenceI seq)
\r
335 DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(
\r
336 seq.getDBRef(), new String[]
\r
338 // jalview.datamodel.DBRefSource.PDB,
\r
339 jalview.datamodel.DBRefSource.UNIPROT,
\r
340 // jalview.datamodel.DBRefSource.EMBL - not tested on any EMBL coord sys sources
\r
342 // TODO: minimal list of DAS queries to make by querying with untyped ID if distinct from any typed IDs
\r
344 boolean dasCoordSysFound = false;
\r
346 if (uprefs != null)
\r
348 // do any of these ids match the source's coordinate system ?
\r
349 for (int j = 0; !dasCoordSysFound && j < uprefs.length; j++)
\r
351 DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();
\r
353 for (int csIndex = 0; csIndex < cs.length && !dasCoordSysFound; csIndex++)
\r
356 && jalview.util.DBRefUtils.isDasCoordinateSystem(
\r
357 cs[csIndex].getName(), uprefs[j]))
\r
359 debug("Launched fetcher for coordinate system "
\r
360 + cs[0].getName());
\r
361 // Will have to pass any mapping information to the fetcher
\r
362 //- the start/end for the DBRefEntry may not be the same as the sequence's start/end
\r
364 System.out.println(seq.getName() + " "
\r
365 + (seq.getDatasetSequence() == null) + " "
\r
366 + dasSource.getUrl());
\r
368 dasCoordSysFound = true; // break's out of the loop
\r
369 createFeatureFetcher(seq, dasSource, uprefs[j]);
\r
372 System.out.println("IGNORE " + cs[csIndex].getName());
\r
377 if (!dasCoordSysFound)
\r
380 // try and use the name as the sequence id
\r
381 if (seq.getName().indexOf("|") > -1)
\r
383 id = seq.getName().substring(seq.getName().lastIndexOf("|") + 1);
\r
384 if (id.trim().length()<4)
\r
386 // hack - we regard a significant ID as being at least 4 non-whitespace characters
\r
387 id = seq.getName().substring(0, seq.getName().lastIndexOf("|"));
\r
388 if (id.indexOf("|")>-1)
\r
390 id = id.substring(id.lastIndexOf("|")+1);
\r
396 id = seq.getName();
\r
400 // Should try to call a general feature fetcher that
\r
401 // queries many sources with name to discover applicable ID references
\r
402 createFeatureFetcher(seq, dasSource, id);
\r
409 * fetch and add das features to a sequence using the given source URL and compatible DbRef id.
\r
410 * new features are mapped using the DbRef mapping to the local coordinate system.
\r
415 protected void createFeatureFetcher(final SequenceI seq,
\r
416 final DasSource dasSource, final DBRefEntry dbref)
\r
420 /// fetch DAS features
\r
421 final Das1Source source = new Das1Source();
\r
422 source.setUrl(dasSource.getUrl());
\r
423 source.setNickname(dasSource.getNickname());
\r
424 if (dbref == null || dbref.getAccessionId() == null
\r
425 || dbref.getAccessionId().length() < 1)
\r
427 responseComplete(dasSource, seq); // reduce thread count anyhow
\r
430 debug("new Das Feature Fetcher for " + dbref.getSource()
\r
431 + ":" + dbref.getAccessionId() + " querying "
\r
432 + dasSource.getUrl());
\r
433 FeatureThread fetcher = new FeatureThread(dbref.getAccessionId()
\r
434 // + ":" + start + "," + end,
\r
437 fetcher.addFeatureListener(new FeatureListener()
\r
439 public void comeBackLater(FeatureEvent e)
\r
441 responseComplete(dasSource, seq);
\r
442 debug("das source " + e.getSource().getNickname()
\r
443 + " asked us to come back in " + e.getComeBackLater()
\r
447 public void newFeatures(FeatureEvent e)
\r
450 Das1Source ds = e.getSource();
\r
452 Map[] features = e.getFeatures();
\r
453 // add features to sequence
\r
454 debug("das source " + ds.getUrl() + " returned "
\r
455 + features.length + " features");
\r
457 if (features.length > 0)
\r
459 for (int i = 0; i < features.length; i++)
\r
461 SequenceFeature f = newSequenceFeature(features[i], source
\r
463 if (dbref.getMap() != null && f.getBegin() > 0
\r
466 debug("mapping from " + f.getBegin() + " - "
\r
468 SequenceFeature vf[] = null;
\r
472 vf = dbref.getMap().locateFeature(f);
\r
473 } catch (Exception ex)
\r
476 .info("Error in 'experimental' mapping of features. Please try to reproduce and then report info to help@jalview.org.");
\r
477 Cache.log.info("Mapping feature from " + f.getBegin()
\r
478 + " to " + f.getEnd() + " in dbref "
\r
479 + dbref.getAccessionId() + " in "
\r
480 + dbref.getSource());
\r
481 Cache.log.info("using das Source " + ds.getUrl());
\r
482 Cache.log.info("Exception", ex);
\r
487 for (int v = 0; v < vf.length; v++)
\r
489 debug("mapping to " + v + ": "
\r
490 + vf[v].getBegin() + " - " + vf[v].getEnd());
\r
491 seq.addSequenceFeature(vf[v]);
\r
497 seq.addSequenceFeature(f);
\r
501 featuresAdded(seq);
\r
505 // System.out.println("No features found for " + seq.getName()
\r
506 // + " from: " + e.getDasSource().getNickname());
\r
508 responseComplete(dasSource, seq);
\r
518 protected void createFeatureFetcher(final SequenceI seq,
\r
519 final DasSource dasSource, String id)
\r
522 /// fetch DAS features
\r
523 final Das1Source source = new Das1Source();
\r
524 source.setUrl(dasSource.getUrl());
\r
525 source.setNickname(dasSource.getNickname());
\r
531 if (id != null && id.length() > 0)
\r
533 debug("new Das Feature Fetcher for " + id + " querying "
\r
534 + dasSource.getUrl());
\r
535 FeatureThread fetcher = new FeatureThread(id
\r
536 // + ":" + start + "," + end,
\r
539 fetcher.addFeatureListener(new FeatureListener()
\r
541 public void comeBackLater(FeatureEvent e)
\r
543 responseComplete(dasSource, seq);
\r
544 debug("das source " + e.getSource().getNickname()
\r
545 + " asked us to come back in " + e.getComeBackLater()
\r
549 public void newFeatures(FeatureEvent e)
\r
552 Das1Source ds = e.getSource();
\r
554 Map[] features = e.getFeatures();
\r
555 // add features to sequence
\r
556 debug("das source " + ds.getUrl() + " returned "
\r
557 + features.length + " features");
\r
559 if (features.length > 0)
\r
561 for (int i = 0; i < features.length; i++)
\r
563 SequenceFeature f = newSequenceFeature(features[i], source
\r
566 seq.addSequenceFeature(f);
\r
569 featuresAdded(seq);
\r
573 // System.out.println("No features found for " + seq.getName()
\r
574 // + " from: " + e.getDasSource().getNickname());
\r
576 responseComplete(dasSource, seq);
\r
585 // invalid fetch - indicate it is finished.
\r
586 debug("Skipping empty ID for querying "
\r
587 + dasSource.getUrl());
\r
588 responseComplete(dasSource, seq);
\r
593 * creates a jalview sequence feature from a das feature document
\r
594 * @param dasfeature
\r
595 * @return sequence feature object created using dasfeature information
\r
597 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
599 if (dasfeature == null)
\r
606 * Different qNames for a DAS Feature - are string keys to the HashMaps in features
\r
608 qName.equals("TYPE") ||
\r
609 qName.equals("START") ||
\r
610 qName.equals("END") ||
\r
611 qName.equals("NOTE") ||
\r
612 qName.equals("LINK") ||
\r
613 qName.equals("SCORE")
\r
615 String desc = new String();
\r
616 if (dasfeature.containsKey("NOTE"))
\r
618 desc += (String) dasfeature.get("NOTE");
\r
621 int start = 0, end = 0;
\r
626 start = Integer.parseInt(dasfeature.get("START").toString());
\r
627 } catch (Exception ex)
\r
632 end = Integer.parseInt(dasfeature.get("END").toString());
\r
633 } catch (Exception ex)
\r
638 score = Integer.parseInt(dasfeature.get("SCORE").toString());
\r
639 } catch (Exception ex)
\r
643 SequenceFeature f = new SequenceFeature((String) dasfeature
\r
644 .get("TYPE"), desc, start, end, score, nickname);
\r
646 if (dasfeature.containsKey("LINK"))
\r
648 f.addLink(f.getType() + " " + f.begin + "_" + f.end + "|"
\r
649 + dasfeature.get("LINK"));
\r
653 } catch (Exception e)
\r
655 System.out.println("ERRR " + e);
\r
656 e.printStackTrace();
\r
657 System.out.println("############");
\r
658 debug("Failed to parse " + dasfeature.toString(), e);
\r
663 * query the default DAS Source Registry for sources.
\r
664 * Uses value of jalview property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that doesn't exist.
\r
665 * @return list of sources
\r
667 public static DasSource[] getDASSources()
\r
670 String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
671 DasSourceBrowser.DEFAULT_REGISTRY);
\r
672 return getDASSources(registryURL);
\r
675 * query the given URL for DasSources.
\r
676 * @param registryURL
\r
677 * return sources from registryURL
\r
679 public static DasSource[] getDASSources(String registryURL)
\r
681 DasSourceReaderImpl reader = new DasSourceReaderImpl();
\r
685 URL url = new URL(registryURL);
\r
687 DasSource[] sources = reader.readDasSource(url);
\r
689 List das1sources = new ArrayList();
\r
690 for (int i = 0; i < sources.length; i++)
\r
692 DasSource ds = sources[i];
\r
693 if (ds instanceof Das2Source)
\r
695 Das2Source d2s = (Das2Source) ds;
\r
696 if (d2s.hasDas1Capabilities())
\r
698 Das1Source d1s = DasSourceConverter.toDas1Source(d2s);
\r
699 das1sources.add(d1s);
\r
703 else if (ds instanceof Das1Source)
\r
705 das1sources.add((Das1Source) ds);
\r
709 return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources
\r
711 } catch (Exception ex)
\r
713 ex.printStackTrace();
\r