2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
\r
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
23 import javax.swing.*;
\r
25 import org.biojava.dasobert.das.*;
\r
26 import org.biojava.dasobert.das2.*;
\r
27 import org.biojava.dasobert.das2.io.*;
\r
28 import org.biojava.dasobert.dasregistry.*;
\r
29 import org.biojava.dasobert.eventmodel.*;
\r
30 import jalview.bin.Cache;
\r
31 import jalview.datamodel.*;
\r
32 import jalview.gui.*;
\r
33 import jalview.util.UrlLink;
\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
55 private void debug(String mesg)
\r
60 private void debug(String mesg, Exception e)
\r
62 if (Cache.log != null)
\r
64 Cache.log.debug(mesg, e);
\r
68 System.err.println(mesg);
\r
71 e.printStackTrace();
\r
79 * Creates a new SequenceFeatureFetcher object. Uses default
\r
86 public DasSequenceFeatureFetcher(SequenceI[] sequences,
\r
87 FeatureSettings fsettings, Vector selectedSources)
\r
89 this(sequences, fsettings, selectedSources, true, true);
\r
92 public DasSequenceFeatureFetcher(SequenceI[] oursequences,
\r
93 FeatureSettings fsettings, Vector ourselectedSources,
\r
94 boolean checkDbrefs, boolean promptFetchDbrefs)
\r
96 this.selectedSources = new Vector();
\r
97 Enumeration sources = ourselectedSources.elements();
\r
98 // filter both sequences and sources to eliminate duplicates
\r
99 while (sources.hasMoreElements())
\r
101 Object src = sources.nextElement();
\r
102 if (!selectedSources.contains(src))
\r
104 selectedSources.addElement(src);
\r
108 Vector sqs = new Vector();
\r
109 for (int i = 0; i < oursequences.length; i++)
\r
111 if (!sqs.contains(oursequences[i]))
\r
113 sqs.addElement(oursequences[i]);
\r
116 sequences = new SequenceI[sqs.size()];
\r
117 for (int i = 0; i < sequences.length; i++)
\r
119 sequences[i] = (SequenceI) sqs.elementAt(i);
\r
121 if (fsettings != null)
\r
123 this.fsettings = fsettings;
\r
124 this.af = fsettings.af;
\r
125 af.setShowSeqFeatures(true);
\r
127 int uniprotCount = 0;
\r
128 for (int i = 0; i < selectedSources.size(); i++)
\r
130 DasSource source = (DasSource) selectedSources.elementAt(i);
\r
131 DasCoordinateSystem[] coords = source.getCoordinateSystem();
\r
132 for (int c = 0; c < coords.length; c++)
\r
134 // TODO: match UniProt coord system canonically (?) - does
\r
135 // UniProt==uniprot==UNIPROT ?
\r
136 if (coords[c].getName().indexOf("UniProt") > -1)
\r
145 for (int i = 0; i < sequences.length; i++)
\r
147 DBRefEntry[] dbref = sequences[i].getDBRef();
\r
150 for (int j = 0; j < dbref.length; j++)
\r
152 if (dbref[j].getSource().equals(
\r
153 jalview.datamodel.DBRefSource.UNIPROT))
\r
162 if (checkDbrefs && refCount < sequences.length && uniprotCount > 0)
\r
165 int reply = JOptionPane.YES_OPTION;
\r
166 if (promptFetchDbrefs)
\r
168 reply = JOptionPane
\r
169 .showInternalConfirmDialog(
\r
171 "Do you want Jalview to find\n"
\r
172 + "Uniprot Accession ids for given sequence names?",
\r
173 "Find Uniprot Accession Ids",
\r
174 JOptionPane.YES_NO_OPTION,
\r
175 JOptionPane.QUESTION_MESSAGE);
\r
178 if (reply == JOptionPane.YES_OPTION)
\r
180 Thread thread = new Thread(new FetchDBRefs());
\r
195 class FetchDBRefs implements Runnable
\r
199 new DBRefFetcher(sequences, af).fetchDBRefs(true);
\r
205 * Spawns a number of dasobert Fetcher threads to add features to sequences in
\r
208 void startFetching()
\r
211 startTime = System.currentTimeMillis();
\r
214 af.setProgressBar("Fetching DAS Sequence Features", startTime);
\r
217 if (selectedSources == null || selectedSources.size() == 0)
\r
221 DasSource[] sources = new jalview.gui.DasSourceBrowser()
\r
224 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
\r
226 StringTokenizer st = new StringTokenizer(active, "\t");
\r
227 selectedSources = new Vector();
\r
229 while (st.hasMoreTokens())
\r
231 token = st.nextToken();
\r
232 for (int i = 0; i < sources.length; i++)
\r
234 if (sources[i].getNickname().equals(token))
\r
236 selectedSources.addElement(sources[i]);
\r
241 } catch (Exception ex)
\r
244 "Exception whilst setting default feature sources from registry and local preferences.",
\r
249 if (selectedSources == null || selectedSources.size() == 0)
\r
251 System.out.println("No DAS Sources active");
\r
253 setGuiNoDassourceActive();
\r
257 sourcesRemaining = selectedSources.size();
\r
258 // Now sending requests one at a time to each server
\r
259 for (int sourceIndex = 0; sourceIndex < selectedSources.size()
\r
260 && !cancelled; sourceIndex++)
\r
262 DasSource dasSource = (DasSource) selectedSources
\r
263 .elementAt(sourceIndex);
\r
265 nextSequence(dasSource, sequences[0]);
\r
269 private void setGuiNoDassourceActive()
\r
274 af.setProgressBar("No DAS Sources Active", startTime);
\r
276 if (getFeatSettings() != null)
\r
278 fsettings.noDasSourceActive();
\r
283 * Update our fsettings dialog reference if we didn't have one when we were
\r
284 * first initialised.
\r
286 * @return fsettings
\r
288 private FeatureSettings getFeatSettings()
\r
290 if (fsettings == null)
\r
294 fsettings = af.featureSettings;
\r
300 public void cancel()
\r
304 af.setProgressBar("DAS Feature Fetching Cancelled", startTime);
\r
309 int sourcesRemaining = 0;
\r
311 void responseComplete(DasSource dasSource, SequenceI seq)
\r
315 for (int seqIndex = 0; seqIndex < sequences.length - 1 && !cancelled; seqIndex++)
\r
317 if (sequences[seqIndex] == seq)
\r
319 nextSequence(dasSource, sequences[++seqIndex]);
\r
325 sourcesRemaining--;
\r
327 if (sourcesRemaining == 0)
\r
329 System.err.println("Fetching Complete.");
\r
330 setGuiFetchComplete();
\r
335 private void setGuiFetchComplete()
\r
338 if (!cancelled && af != null)
\r
340 // only update the progress bar if we've completed the fetch normally
\r
341 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
344 if (af != null && af.featureSettings != null)
\r
346 af.featureSettings.setTableData();
\r
349 if (getFeatSettings() != null)
\r
351 fsettings.complete();
\r
355 void featuresAdded(SequenceI seq)
\r
359 // no gui to update with features.
\r
362 af.getFeatureRenderer().featuresAdded();
\r
364 int start = af.getViewport().getStartSeq();
\r
365 int end = af.getViewport().getEndSeq();
\r
367 for (index = start; index < end; index++)
\r
369 if (seq == af.getViewport().getAlignment().getSequenceAt(index)
\r
370 .getDatasetSequence())
\r
372 af.alignPanel.paintAlignment(true);
\r
378 void nextSequence(DasSource dasSource, SequenceI seq)
\r
382 DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(
\r
383 seq.getDBRef(), new String[]
\r
385 // jalview.datamodel.DBRefSource.PDB,
\r
386 jalview.datamodel.DBRefSource.UNIPROT,
\r
387 // jalview.datamodel.DBRefSource.EMBL - not tested on any EMBL coord
\r
390 // TODO: minimal list of DAS queries to make by querying with untyped ID if
\r
391 // distinct from any typed IDs
\r
393 boolean dasCoordSysFound = false;
\r
395 if (uprefs != null)
\r
397 // do any of these ids match the source's coordinate system ?
\r
398 for (int j = 0; !dasCoordSysFound && j < uprefs.length; j++)
\r
400 DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();
\r
402 for (int csIndex = 0; csIndex < cs.length && !dasCoordSysFound; csIndex++)
\r
405 && jalview.util.DBRefUtils.isDasCoordinateSystem(
\r
406 cs[csIndex].getName(), uprefs[j]))
\r
408 debug("Launched fetcher for coordinate system "
\r
409 + cs[0].getName());
\r
410 // Will have to pass any mapping information to the fetcher
\r
411 // - the start/end for the DBRefEntry may not be the same as the
\r
412 // sequence's start/end
\r
414 System.out.println(seq.getName() + " "
\r
415 + (seq.getDatasetSequence() == null) + " "
\r
416 + dasSource.getUrl());
\r
418 dasCoordSysFound = true; // break's out of the loop
\r
419 createFeatureFetcher(seq, dasSource, uprefs[j]);
\r
422 System.out.println("IGNORE " + cs[csIndex].getName());
\r
427 if (!dasCoordSysFound)
\r
430 // try and use the name as the sequence id
\r
431 if (seq.getName().indexOf("|") > -1)
\r
433 id = seq.getName().substring(seq.getName().lastIndexOf("|") + 1);
\r
434 if (id.trim().length() < 4)
\r
436 // hack - we regard a significant ID as being at least 4
\r
437 // non-whitespace characters
\r
438 id = seq.getName().substring(0, seq.getName().lastIndexOf("|"));
\r
439 if (id.indexOf("|") > -1)
\r
441 id = id.substring(id.lastIndexOf("|") + 1);
\r
447 id = seq.getName();
\r
451 // Should try to call a general feature fetcher that
\r
452 // queries many sources with name to discover applicable ID references
\r
453 createFeatureFetcher(seq, dasSource, id);
\r
460 * fetch and add das features to a sequence using the given source URL and
\r
461 * compatible DbRef id. new features are mapped using the DbRef mapping to the
\r
462 * local coordinate system.
\r
468 protected void createFeatureFetcher(final SequenceI seq,
\r
469 final DasSource dasSource, final DBRefEntry dbref)
\r
473 // / fetch DAS features
\r
474 final Das1Source source = new Das1Source();
\r
475 source.setUrl(dasSource.getUrl());
\r
476 source.setNickname(dasSource.getNickname());
\r
477 if (dbref == null || dbref.getAccessionId() == null
\r
478 || dbref.getAccessionId().length() < 1)
\r
480 responseComplete(dasSource, seq); // reduce thread count anyhow
\r
483 debug("new Das Feature Fetcher for " + dbref.getSource() + ":"
\r
484 + dbref.getAccessionId() + " querying " + dasSource.getUrl());
\r
485 FeatureThread fetcher = new FeatureThread(dbref.getAccessionId()
\r
486 // + ":" + start + "," + end,
\r
489 fetcher.addFeatureListener(new FeatureListener()
\r
491 public void comeBackLater(FeatureEvent e)
\r
493 responseComplete(dasSource, seq);
\r
494 debug("das source " + e.getSource().getNickname()
\r
495 + " asked us to come back in " + e.getComeBackLater()
\r
499 public void newFeatures(FeatureEvent e)
\r
502 Das1Source ds = e.getSource();
\r
504 Map[] features = e.getFeatures();
\r
505 // add features to sequence
\r
506 debug("das source " + ds.getUrl() + " returned " + features.length
\r
509 if (features.length > 0)
\r
511 for (int i = 0; i < features.length; i++)
\r
513 // standard DAS feature-> jalview sequence feature transformation
\r
514 SequenceFeature f = newSequenceFeature(features[i], source
\r
516 if (!parseSeqFeature(seq, f, features[i], source))
\r
518 if (dbref.getMap() != null && f.getBegin() > 0
\r
521 debug("mapping from " + f.getBegin() + " - " + f.getEnd());
\r
522 SequenceFeature vf[] = null;
\r
526 vf = dbref.getMap().locateFeature(f);
\r
527 } catch (Exception ex)
\r
530 .info("Error in 'experimental' mapping of features. Please try to reproduce and then report info to jalview-discuss@jalview.org.");
\r
531 Cache.log.info("Mapping feature from " + f.getBegin()
\r
532 + " to " + f.getEnd() + " in dbref "
\r
533 + dbref.getAccessionId() + " in "
\r
534 + dbref.getSource());
\r
535 Cache.log.info("using das Source " + ds.getUrl());
\r
536 Cache.log.info("Exception", ex);
\r
541 for (int v = 0; v < vf.length; v++)
\r
543 debug("mapping to " + v + ": " + vf[v].getBegin()
\r
544 + " - " + vf[v].getEnd());
\r
545 seq.addSequenceFeature(vf[v]);
\r
551 seq.addSequenceFeature(f);
\r
555 featuresAdded(seq);
\r
559 // System.out.println("No features found for " + seq.getName()
\r
560 // + " from: " + e.getDasSource().getNickname());
\r
562 responseComplete(dasSource, seq);
\r
572 protected void createFeatureFetcher(final SequenceI seq,
\r
573 final DasSource dasSource, String id)
\r
576 // / fetch DAS features
\r
577 final Das1Source source = new Das1Source();
\r
578 source.setUrl(dasSource.getUrl());
\r
579 source.setNickname(dasSource.getNickname());
\r
585 if (id != null && id.length() > 0)
\r
587 debug("new Das Feature Fetcher for " + id + " querying "
\r
588 + dasSource.getUrl());
\r
589 FeatureThread fetcher = new FeatureThread(id
\r
590 // + ":" + start + "," + end,
\r
593 fetcher.addFeatureListener(new FeatureListener()
\r
595 public void comeBackLater(FeatureEvent e)
\r
597 responseComplete(dasSource, seq);
\r
598 debug("das source " + e.getSource().getNickname()
\r
599 + " asked us to come back in " + e.getComeBackLater()
\r
603 public void newFeatures(FeatureEvent e)
\r
606 Das1Source ds = e.getSource();
\r
608 Map[] features = e.getFeatures();
\r
609 // add features to sequence
\r
610 debug("das source " + ds.getUrl() + " returned "
\r
611 + features.length + " features");
\r
613 if (features.length > 0)
\r
615 for (int i = 0; i < features.length; i++)
\r
617 // standard DAS feature-> jalview sequence feature transformation
\r
618 SequenceFeature f = newSequenceFeature(features[i], source
\r
620 if (!parseSeqFeature(seq, f, features[i], source))
\r
622 // just add as a simple sequence feature
\r
623 seq.addSequenceFeature(f);
\r
627 featuresAdded(seq);
\r
631 // System.out.println("No features found for " + seq.getName()
\r
632 // + " from: " + e.getDasSource().getNickname());
\r
634 responseComplete(dasSource, seq);
\r
645 // invalid fetch - indicate it is finished.
\r
646 debug("Skipping empty ID for querying " + dasSource.getUrl());
\r
647 responseComplete(dasSource, seq);
\r
652 * examine the given sequence feature to determine if it should actually be
\r
653 * turned into sequence annotation or database cross references rather than a
\r
654 * simple sequence feature.
\r
657 * the sequence to annotate
\r
659 * the jalview sequence feature generated from the DAS feature
\r
661 * the sequence feature attributes
\r
663 * the source that emitted the feature
\r
664 * @return true if feature was consumed as another kind of annotation.
\r
666 protected boolean parseSeqFeature(SequenceI seq, SequenceFeature f,
\r
667 Map map, Das1Source source)
\r
669 SequenceI mseq = seq;
\r
670 while (seq.getDatasetSequence() != null)
\r
672 seq = seq.getDatasetSequence();
\r
674 if (f.getType() != null)
\r
676 String type = f.getType();
\r
677 if (type.equalsIgnoreCase("protein_name"))
\r
679 // parse name onto the alignment sequence or the dataset sequence.
\r
680 if (seq.getDescription() == null
\r
681 || seq.getDescription().trim().length() == 0)
\r
683 // could look at the note series to pick out the first long name, for
\r
684 // the moment just use the whole description string
\r
685 seq.setDescription(f.getDescription());
\r
687 if (mseq.getDescription() == null
\r
688 || mseq.getDescription().trim().length() == 0)
\r
690 // could look at the note series to pick out the first long name, for
\r
691 // the moment just use the whole description string
\r
692 mseq.setDescription(f.getDescription());
\r
696 // check if source has biosapiens or other sequence ontology label
\r
697 if (type.equalsIgnoreCase("DBXREF") || type.equalsIgnoreCase("DBREF"))
\r
699 // try to parse the accession out
\r
701 DBRefEntry dbr = new DBRefEntry();
\r
702 dbr.setVersion(source.getNickname());
\r
703 StringTokenizer st = new StringTokenizer(f.getDescription(), ":");
\r
704 if (st.hasMoreTokens())
\r
706 dbr.setSource(st.nextToken());
\r
708 if (st.hasMoreTokens())
\r
710 dbr.setAccessionId(st.nextToken());
\r
714 if (f.links != null && f.links.size() > 0)
\r
716 // feature is also appended to enable links to be seen.
\r
717 // TODO: consider extending dbrefs to have their own links ?
\r
718 // TODO: new feature: extract dbref links from DAS servers and add the
\r
719 // URL pattern to the list of DB name associated links in the user's
\r
721 // for the moment - just fix up the existing feature so it displays
\r
723 // f.setType(dbr.getSource());
\r
724 // f.setDescription();
\r
725 f.setValue("linkonly", Boolean.TRUE);
\r
726 // f.setDescription("");
\r
727 Vector newlinks = new Vector();
\r
728 Enumeration it = f.links.elements();
\r
729 while (it.hasMoreElements())
\r
732 UrlLink urllink = new UrlLink(elm = (String) it.nextElement());
\r
733 if (urllink.isValid())
\r
735 urllink.setLabel(f.getDescription());
\r
736 newlinks.addElement(urllink.toString());
\r
740 // couldn't parse the link properly. Keep it anyway - just in
\r
742 debug("couldn't parse link string - " + elm);
\r
743 newlinks.addElement(elm);
\r
746 f.links = newlinks;
\r
747 seq.addSequenceFeature(f);
\r
756 * creates a jalview sequence feature from a das feature document
\r
758 * @param dasfeature
\r
759 * @return sequence feature object created using dasfeature information
\r
761 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
763 if (dasfeature == null)
\r
770 * Different qNames for a DAS Feature - are string keys to the HashMaps in
\r
771 * features "METHOD") || qName.equals("TYPE") || qName.equals("START") ||
\r
772 * qName.equals("END") || qName.equals("NOTE") || qName.equals("LINK") ||
\r
773 * qName.equals("SCORE")
\r
775 String desc = new String();
\r
776 if (dasfeature.containsKey("NOTE"))
\r
778 desc += (String) dasfeature.get("NOTE");
\r
781 int start = 0, end = 0;
\r
786 start = Integer.parseInt(dasfeature.get("START").toString());
\r
787 } catch (Exception ex)
\r
792 end = Integer.parseInt(dasfeature.get("END").toString());
\r
793 } catch (Exception ex)
\r
798 Object scr = dasfeature.get("SCORE");
\r
801 score = (float) Double.parseDouble(scr.toString());
\r
804 } catch (Exception ex)
\r
808 SequenceFeature f = new SequenceFeature((String) dasfeature
\r
809 .get("TYPE"), desc, start, end, score, nickname);
\r
811 if (dasfeature.containsKey("LINK"))
\r
813 // Do not put feature extent in link text for non-positional features
\r
814 if (f.begin == 0 && f.end == 0)
\r
816 f.addLink(f.getType() + "|" + dasfeature.get("LINK"));
\r
820 f.addLink(f.getType() + " " + f.begin + "_" + f.end + "|"
\r
821 + dasfeature.get("LINK"));
\r
826 } catch (Exception e)
\r
828 System.out.println("ERRR " + e);
\r
829 e.printStackTrace();
\r
830 System.out.println("############");
\r
831 debug("Failed to parse " + dasfeature.toString(), e);
\r
837 * query the default DAS Source Registry for sources. Uses value of jalview
\r
838 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
\r
841 * @return list of sources
\r
843 public static DasSource[] getDASSources()
\r
846 String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
847 DasSourceBrowser.DEFAULT_REGISTRY);
\r
848 return getDASSources(registryURL);
\r
852 * query the given URL for DasSources.
\r
854 * @param registryURL
\r
855 * return sources from registryURL
\r
857 public static DasSource[] getDASSources(String registryURL)
\r
859 DasSourceReaderImpl reader = new DasSourceReaderImpl();
\r
863 URL url = new URL(registryURL);
\r
865 DasSource[] sources = reader.readDasSource(url);
\r
867 List das1sources = new ArrayList();
\r
868 for (int i = 0; i < sources.length; i++)
\r
870 DasSource ds = sources[i];
\r
871 if (ds instanceof Das2Source)
\r
873 Das2Source d2s = (Das2Source) ds;
\r
874 if (d2s.hasDas1Capabilities())
\r
876 Das1Source d1s = DasSourceConverter.toDas1Source(d2s);
\r
877 das1sources.add(d1s);
\r
881 else if (ds instanceof Das1Source)
\r
883 das1sources.add((Das1Source) ds);
\r
887 return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources
\r
889 } catch (Exception ex)
\r
891 System.err.println("Failed to contact DAS1 registry at "
\r
893 ex.printStackTrace();
\r