2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
\r
3 * Copyright (C) 2011 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.biodas.jdas.client.SourcesClient;
\r
26 import org.biodas.jdas.schema.sources.SOURCE;
\r
27 import org.biodas.jdas.schema.sources.SOURCES;
\r
28 import org.biojava.dasobert.das.*;
\r
29 import org.biojava.dasobert.das2.*;
\r
30 import org.biojava.dasobert.das2.io.*;
\r
31 import org.biojava.dasobert.dasregistry.*;
\r
32 import org.biojava.dasobert.eventmodel.*;
\r
33 import jalview.bin.Cache;
\r
34 import jalview.datamodel.*;
\r
35 import jalview.gui.*;
\r
36 import jalview.util.UrlLink;
\r
42 * @version $Revision$
\r
44 public class DasSequenceFeatureFetcher
\r
46 SequenceI[] sequences;
\r
50 FeatureSettings fsettings;
\r
52 StringBuffer sbuffer = new StringBuffer();
\r
54 Vector selectedSources;
\r
56 boolean cancelled = false;
\r
58 private void debug(String mesg)
\r
63 private void debug(String mesg, Exception e)
\r
65 if (Cache.log != null)
\r
67 Cache.log.debug(mesg, e);
\r
71 System.err.println(mesg);
\r
74 e.printStackTrace();
\r
82 * Creates a new SequenceFeatureFetcher object. Uses default
\r
89 public DasSequenceFeatureFetcher(SequenceI[] sequences,
\r
90 FeatureSettings fsettings, Vector selectedSources)
\r
92 this(sequences, fsettings, selectedSources, true, true);
\r
95 public DasSequenceFeatureFetcher(SequenceI[] oursequences,
\r
96 FeatureSettings fsettings, Vector ourselectedSources,
\r
97 boolean checkDbrefs, boolean promptFetchDbrefs)
\r
99 this.selectedSources = new Vector();
\r
100 Enumeration sources = ourselectedSources.elements();
\r
101 // filter both sequences and sources to eliminate duplicates
\r
102 while (sources.hasMoreElements())
\r
104 Object src = sources.nextElement();
\r
105 if (!selectedSources.contains(src))
\r
107 selectedSources.addElement(src);
\r
111 Vector sqs = new Vector();
\r
112 for (int i = 0; i < oursequences.length; i++)
\r
114 if (!sqs.contains(oursequences[i]))
\r
116 sqs.addElement(oursequences[i]);
\r
119 sequences = new SequenceI[sqs.size()];
\r
120 for (int i = 0; i < sequences.length; i++)
\r
122 sequences[i] = (SequenceI) sqs.elementAt(i);
\r
124 if (fsettings != null)
\r
126 this.fsettings = fsettings;
\r
127 this.af = fsettings.af;
\r
128 af.setShowSeqFeatures(true);
\r
130 int uniprotCount = 0;
\r
131 for (int i = 0; i < selectedSources.size(); i++)
\r
133 DasSource source = (DasSource) selectedSources.elementAt(i);
\r
134 DasCoordinateSystem[] coords = source.getCoordinateSystem();
\r
135 for (int c = 0; c < coords.length; c++)
\r
137 // TODO: match UniProt coord system canonically (?) - does
\r
138 // UniProt==uniprot==UNIPROT ?
\r
139 if (coords[c].getName().indexOf("UniProt") > -1)
\r
148 for (int i = 0; i < sequences.length; i++)
\r
150 DBRefEntry[] dbref = sequences[i].getDBRef();
\r
153 for (int j = 0; j < dbref.length; j++)
\r
155 if (dbref[j].getSource().equals(
\r
156 jalview.datamodel.DBRefSource.UNIPROT))
\r
165 if (checkDbrefs && refCount < sequences.length && uniprotCount > 0)
\r
168 int reply = JOptionPane.YES_OPTION;
\r
169 if (promptFetchDbrefs)
\r
171 reply = JOptionPane
\r
172 .showInternalConfirmDialog(
\r
174 "Do you want Jalview to find\n"
\r
175 + "Uniprot Accession ids for given sequence names?",
\r
176 "Find Uniprot Accession Ids",
\r
177 JOptionPane.YES_NO_OPTION,
\r
178 JOptionPane.QUESTION_MESSAGE);
\r
181 if (reply == JOptionPane.YES_OPTION)
\r
183 Thread thread = new Thread(new FetchDBRefs());
\r
198 class FetchDBRefs implements Runnable
\r
202 new DBRefFetcher(sequences, af).fetchDBRefs(true);
\r
208 * Spawns a number of dasobert Fetcher threads to add features to sequences in
\r
211 void startFetching()
\r
214 startTime = System.currentTimeMillis();
\r
217 af.setProgressBar("Fetching DAS Sequence Features", startTime);
\r
220 if (selectedSources == null || selectedSources.size() == 0)
\r
224 DasSource[] sources = new jalview.gui.DasSourceBrowser()
\r
227 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
\r
229 StringTokenizer st = new StringTokenizer(active, "\t");
\r
230 selectedSources = new Vector();
\r
232 while (st.hasMoreTokens())
\r
234 token = st.nextToken();
\r
235 for (int i = 0; i < sources.length; i++)
\r
237 if (sources[i].getNickname().equals(token))
\r
239 selectedSources.addElement(sources[i]);
\r
244 } catch (Exception ex)
\r
246 debug("Exception whilst setting default feature sources from registry and local preferences.",
\r
251 if (selectedSources == null || selectedSources.size() == 0)
\r
253 System.out.println("No DAS Sources active");
\r
255 setGuiNoDassourceActive();
\r
259 sourcesRemaining = selectedSources.size();
\r
260 // Now sending requests one at a time to each server
\r
261 for (int sourceIndex = 0; sourceIndex < selectedSources.size()
\r
262 && !cancelled; sourceIndex++)
\r
264 DasSource dasSource = (DasSource) selectedSources
\r
265 .elementAt(sourceIndex);
\r
267 nextSequence(dasSource, sequences[0]);
\r
271 private void setGuiNoDassourceActive()
\r
276 af.setProgressBar("No DAS Sources Active", startTime);
\r
278 if (getFeatSettings() != null)
\r
280 fsettings.noDasSourceActive();
\r
285 * Update our fsettings dialog reference if we didn't have one when we were
\r
286 * first initialised.
\r
288 * @return fsettings
\r
290 private FeatureSettings getFeatSettings()
\r
292 if (fsettings == null)
\r
296 fsettings = af.featureSettings;
\r
302 public void cancel()
\r
306 af.setProgressBar("DAS Feature Fetching Cancelled", startTime);
\r
311 int sourcesRemaining = 0;
\r
313 void responseComplete(DasSource dasSource, SequenceI seq)
\r
317 for (int seqIndex = 0; seqIndex < sequences.length - 1 && !cancelled; seqIndex++)
\r
319 if (sequences[seqIndex] == seq)
\r
321 nextSequence(dasSource, sequences[++seqIndex]);
\r
327 sourcesRemaining--;
\r
329 if (sourcesRemaining == 0)
\r
331 System.err.println("Fetching Complete.");
\r
332 setGuiFetchComplete();
\r
337 private void setGuiFetchComplete()
\r
340 if (!cancelled && af != null)
\r
342 // only update the progress bar if we've completed the fetch normally
\r
343 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
346 if (af != null && af.featureSettings != null)
\r
348 af.featureSettings.setTableData();
\r
351 if (getFeatSettings() != null)
\r
353 fsettings.complete();
\r
357 void featuresAdded(SequenceI seq)
\r
361 // no gui to update with features.
\r
364 af.getFeatureRenderer().featuresAdded();
\r
366 int start = af.getViewport().getStartSeq();
\r
367 int end = af.getViewport().getEndSeq();
\r
369 for (index = start; index < end; index++)
\r
371 if (seq == af.getViewport().getAlignment().getSequenceAt(index)
\r
372 .getDatasetSequence())
\r
374 af.alignPanel.paintAlignment(true);
\r
380 void nextSequence(DasSource dasSource, SequenceI seq)
\r
384 DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(
\r
385 seq.getDBRef(), new String[]
\r
387 // jalview.datamodel.DBRefSource.PDB,
\r
388 jalview.datamodel.DBRefSource.UNIPROT,
\r
389 // jalview.datamodel.DBRefSource.EMBL - not tested on any EMBL coord
\r
392 // TODO: minimal list of DAS queries to make by querying with untyped ID if
\r
393 // distinct from any typed IDs
\r
395 boolean dasCoordSysFound = false;
\r
397 if (uprefs != null)
\r
399 // do any of these ids match the source's coordinate system ?
\r
400 for (int j = 0; !dasCoordSysFound && j < uprefs.length; j++)
\r
402 DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();
\r
404 for (int csIndex = 0; csIndex < cs.length && !dasCoordSysFound; csIndex++)
\r
407 && jalview.util.DBRefUtils.isDasCoordinateSystem(
\r
408 cs[csIndex].getName(), uprefs[j]))
\r
410 debug("Launched fetcher for coordinate system "
\r
411 + cs[0].getName());
\r
412 // Will have to pass any mapping information to the fetcher
\r
413 // - the start/end for the DBRefEntry may not be the same as the
\r
414 // sequence's start/end
\r
416 System.out.println(seq.getName() + " "
\r
417 + (seq.getDatasetSequence() == null) + " "
\r
418 + dasSource.getUrl());
\r
420 dasCoordSysFound = true; // break's out of the loop
\r
421 createFeatureFetcher(seq, dasSource, uprefs[j]);
\r
424 System.out.println("IGNORE " + cs[csIndex].getName());
\r
429 if (!dasCoordSysFound)
\r
432 // try and use the name as the sequence id
\r
433 if (seq.getName().indexOf("|") > -1)
\r
435 id = seq.getName().substring(seq.getName().lastIndexOf("|") + 1);
\r
436 if (id.trim().length() < 4)
\r
438 // hack - we regard a significant ID as being at least 4
\r
439 // non-whitespace characters
\r
440 id = seq.getName().substring(0, seq.getName().lastIndexOf("|"));
\r
441 if (id.indexOf("|") > -1)
\r
443 id = id.substring(id.lastIndexOf("|") + 1);
\r
449 id = seq.getName();
\r
453 // Should try to call a general feature fetcher that
\r
454 // queries many sources with name to discover applicable ID references
\r
455 createFeatureFetcher(seq, dasSource, id);
\r
462 * fetch and add das features to a sequence using the given source URL and
\r
463 * compatible DbRef id. new features are mapped using the DbRef mapping to the
\r
464 * local coordinate system.
\r
470 protected void createFeatureFetcher(final SequenceI seq,
\r
471 final DasSource dasSource, final DBRefEntry dbref)
\r
475 // / fetch DAS features
\r
476 final Das1Source source = new Das1Source();
\r
477 source.setUrl(dasSource.getUrl());
\r
478 source.setNickname(dasSource.getNickname());
\r
479 if (dbref == null || dbref.getAccessionId() == null
\r
480 || dbref.getAccessionId().length() < 1)
\r
482 responseComplete(dasSource, seq); // reduce thread count anyhow
\r
485 debug("new Das Feature Fetcher for " + dbref.getSource() + ":"
\r
486 + dbref.getAccessionId() + " querying " + dasSource.getUrl());
\r
487 FeatureThread fetcher = new FeatureThread(dbref.getAccessionId()
\r
488 // + ":" + start + "," + end,
\r
491 fetcher.addFeatureListener(new FeatureListener()
\r
493 public void comeBackLater(FeatureEvent e)
\r
495 responseComplete(dasSource, seq);
\r
496 debug("das source " + e.getSource().getNickname()
\r
497 + " asked us to come back in " + e.getComeBackLater()
\r
501 public void newFeatures(FeatureEvent e)
\r
504 Das1Source ds = e.getSource();
\r
506 Map[] features = e.getFeatures();
\r
507 // add features to sequence
\r
508 debug("das source " + ds.getUrl() + " returned " + features.length
\r
511 if (features.length > 0)
\r
513 for (int i = 0; i < features.length; i++)
\r
515 // standard DAS feature-> jalview sequence feature transformation
\r
516 SequenceFeature f = newSequenceFeature(features[i],
\r
517 source.getNickname());
\r
518 if (!parseSeqFeature(seq, f, features[i], source))
\r
520 if (dbref.getMap() != null && f.getBegin() > 0
\r
523 debug("mapping from " + f.getBegin() + " - " + f.getEnd());
\r
524 SequenceFeature vf[] = null;
\r
528 vf = dbref.getMap().locateFeature(f);
\r
529 } catch (Exception ex)
\r
532 .info("Error in 'experimental' mapping of features. Please try to reproduce and then report info to jalview-discuss@jalview.org.");
\r
533 Cache.log.info("Mapping feature from " + f.getBegin()
\r
534 + " to " + f.getEnd() + " in dbref "
\r
535 + dbref.getAccessionId() + " in "
\r
536 + dbref.getSource());
\r
537 Cache.log.info("using das Source " + ds.getUrl());
\r
538 Cache.log.info("Exception", ex);
\r
543 for (int v = 0; v < vf.length; v++)
\r
545 debug("mapping to " + v + ": " + vf[v].getBegin()
\r
546 + " - " + vf[v].getEnd());
\r
547 seq.addSequenceFeature(vf[v]);
\r
553 seq.addSequenceFeature(f);
\r
557 featuresAdded(seq);
\r
561 // System.out.println("No features found for " + seq.getName()
\r
562 // + " from: " + e.getDasSource().getNickname());
\r
564 responseComplete(dasSource, seq);
\r
574 protected void createFeatureFetcher(final SequenceI seq,
\r
575 final DasSource dasSource, String id)
\r
578 // / fetch DAS features
\r
579 final Das1Source source = new Das1Source();
\r
580 source.setUrl(dasSource.getUrl());
\r
581 source.setNickname(dasSource.getNickname());
\r
587 if (id != null && id.length() > 0)
\r
589 debug("new Das Feature Fetcher for " + id + " querying "
\r
590 + dasSource.getUrl());
\r
591 FeatureThread fetcher = new FeatureThread(id
\r
592 // + ":" + start + "," + end,
\r
595 fetcher.addFeatureListener(new FeatureListener()
\r
597 public void comeBackLater(FeatureEvent e)
\r
599 responseComplete(dasSource, seq);
\r
600 debug("das source " + e.getSource().getNickname()
\r
601 + " asked us to come back in " + e.getComeBackLater()
\r
605 public void newFeatures(FeatureEvent e)
\r
608 Das1Source ds = e.getSource();
\r
610 Map[] features = e.getFeatures();
\r
611 // add features to sequence
\r
612 debug("das source " + ds.getUrl() + " returned "
\r
613 + features.length + " features");
\r
615 if (features.length > 0)
\r
617 for (int i = 0; i < features.length; i++)
\r
619 // standard DAS feature-> jalview sequence feature transformation
\r
620 SequenceFeature f = newSequenceFeature(features[i],
\r
621 source.getNickname());
\r
622 if (!parseSeqFeature(seq, f, features[i], source))
\r
624 // just add as a simple sequence feature
\r
625 seq.addSequenceFeature(f);
\r
629 featuresAdded(seq);
\r
633 // System.out.println("No features found for " + seq.getName()
\r
634 // + " from: " + e.getDasSource().getNickname());
\r
636 responseComplete(dasSource, seq);
\r
647 // invalid fetch - indicate it is finished.
\r
648 debug("Skipping empty ID for querying " + dasSource.getUrl());
\r
649 responseComplete(dasSource, seq);
\r
654 * examine the given sequence feature to determine if it should actually be
\r
655 * turned into sequence annotation or database cross references rather than a
\r
656 * simple sequence feature.
\r
659 * the sequence to annotate
\r
661 * the jalview sequence feature generated from the DAS feature
\r
663 * the sequence feature attributes
\r
665 * the source that emitted the feature
\r
666 * @return true if feature was consumed as another kind of annotation.
\r
668 protected boolean parseSeqFeature(SequenceI seq, SequenceFeature f,
\r
669 Map map, Das1Source source)
\r
671 SequenceI mseq = seq;
\r
672 while (seq.getDatasetSequence() != null)
\r
674 seq = seq.getDatasetSequence();
\r
676 if (f.getType() != null)
\r
678 String type = f.getType();
\r
679 if (type.equalsIgnoreCase("protein_name"))
\r
681 // parse name onto the alignment sequence or the dataset sequence.
\r
682 if (seq.getDescription() == null
\r
683 || seq.getDescription().trim().length() == 0)
\r
685 // could look at the note series to pick out the first long name, for
\r
686 // the moment just use the whole description string
\r
687 seq.setDescription(f.getDescription());
\r
689 if (mseq.getDescription() == null
\r
690 || mseq.getDescription().trim().length() == 0)
\r
692 // could look at the note series to pick out the first long name, for
\r
693 // the moment just use the whole description string
\r
694 mseq.setDescription(f.getDescription());
\r
698 // check if source has biosapiens or other sequence ontology label
\r
699 if (type.equalsIgnoreCase("DBXREF") || type.equalsIgnoreCase("DBREF"))
\r
701 // try to parse the accession out
\r
703 DBRefEntry dbr = new DBRefEntry();
\r
704 dbr.setVersion(source.getNickname());
\r
705 StringTokenizer st = new StringTokenizer(f.getDescription(), ":");
\r
706 if (st.hasMoreTokens())
\r
708 dbr.setSource(st.nextToken());
\r
710 if (st.hasMoreTokens())
\r
712 dbr.setAccessionId(st.nextToken());
\r
716 if (f.links != null && f.links.size() > 0)
\r
718 // feature is also appended to enable links to be seen.
\r
719 // TODO: consider extending dbrefs to have their own links ?
\r
720 // TODO: new feature: extract dbref links from DAS servers and add the
\r
721 // URL pattern to the list of DB name associated links in the user's
\r
723 // for the moment - just fix up the existing feature so it displays
\r
725 // f.setType(dbr.getSource());
\r
726 // f.setDescription();
\r
727 f.setValue("linkonly", Boolean.TRUE);
\r
728 // f.setDescription("");
\r
729 Vector newlinks = new Vector();
\r
730 Enumeration it = f.links.elements();
\r
731 while (it.hasMoreElements())
\r
734 UrlLink urllink = new UrlLink(elm = (String) it.nextElement());
\r
735 if (urllink.isValid())
\r
737 urllink.setLabel(f.getDescription());
\r
738 newlinks.addElement(urllink.toString());
\r
742 // couldn't parse the link properly. Keep it anyway - just in
\r
744 debug("couldn't parse link string - " + elm);
\r
745 newlinks.addElement(elm);
\r
748 f.links = newlinks;
\r
749 seq.addSequenceFeature(f);
\r
758 * creates a jalview sequence feature from a das feature document
\r
760 * @param dasfeature
\r
761 * @return sequence feature object created using dasfeature information
\r
763 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
765 if (dasfeature == null)
\r
772 * Different qNames for a DAS Feature - are string keys to the HashMaps in
\r
773 * features "METHOD") || qName.equals("TYPE") || qName.equals("START") ||
\r
774 * qName.equals("END") || qName.equals("NOTE") || qName.equals("LINK") ||
\r
775 * qName.equals("SCORE")
\r
777 String desc = new String();
\r
778 if (dasfeature.containsKey("NOTE"))
\r
780 desc += (String) dasfeature.get("NOTE");
\r
783 int start = 0, end = 0;
\r
788 start = Integer.parseInt(dasfeature.get("START").toString());
\r
789 } catch (Exception ex)
\r
794 end = Integer.parseInt(dasfeature.get("END").toString());
\r
795 } catch (Exception ex)
\r
800 Object scr = dasfeature.get("SCORE");
\r
803 score = (float) Double.parseDouble(scr.toString());
\r
806 } catch (Exception ex)
\r
810 SequenceFeature f = new SequenceFeature(
\r
811 (String) dasfeature.get("TYPE"), desc, start, end, score,
\r
814 if (dasfeature.containsKey("LINK"))
\r
816 // Do not put feature extent in link text for non-positional features
\r
817 if (f.begin == 0 && f.end == 0)
\r
819 f.addLink(f.getType() + "|" + dasfeature.get("LINK"));
\r
823 f.addLink(f.getType() + " " + f.begin + "_" + f.end + "|"
\r
824 + dasfeature.get("LINK"));
\r
829 } catch (Exception e)
\r
831 System.out.println("ERRR " + e);
\r
832 e.printStackTrace();
\r
833 System.out.println("############");
\r
834 debug("Failed to parse " + dasfeature.toString(), e);
\r
840 * query the default DAS Source Registry for sources. Uses value of jalview
\r
841 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
\r
844 * @return list of sources
\r
846 public static SOURCE[] getDASSources()
\r
849 String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
850 DasSourceBrowser.DEFAULT_REGISTRY);
\r
851 return getDASSources(registryURL);
\r
855 * query the given URL for DasSources.
\r
857 * @param registryURL
\r
858 * return sources from registryURL
\r
860 public static SOURCE[] getDASSources(String registryURL)
\r
865 URL url = new URL(registryURL);
\r
866 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
\r
868 SOURCES sources = client.fetchData(registryURL);
\r
870 List<SOURCE> dassources = sources.getSOURCE();
\r
872 return dassources.toArray(new SOURCE[dassources.size()]);
\r
873 } catch (Exception ex)
\r
875 System.err.println("Failed to contact DAS1 registry at "
\r
877 ex.printStackTrace();
\r