2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
\r
3 * Copyright (C) 2009 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
34 import jalview.util.UrlLink;
\r
40 * @version $Revision$
\r
42 public class DasSequenceFeatureFetcher
\r
44 SequenceI[] sequences;
\r
48 FeatureSettings fsettings;
\r
50 StringBuffer sbuffer = new StringBuffer();
\r
52 Vector selectedSources;
\r
54 boolean cancelled = false;
\r
56 private void debug(String mesg)
\r
61 private void debug(String mesg, Exception e)
\r
63 if (Cache.log != null)
\r
65 Cache.log.debug(mesg, e);
\r
69 System.err.println(mesg);
\r
72 e.printStackTrace();
\r
80 * Creates a new SequenceFeatureFetcher object. Uses default
\r
87 public DasSequenceFeatureFetcher(SequenceI[] sequences,
\r
88 FeatureSettings fsettings, Vector selectedSources)
\r
90 this(sequences, fsettings, selectedSources, true, true);
\r
93 public DasSequenceFeatureFetcher(SequenceI[] oursequences,
\r
94 FeatureSettings fsettings, Vector ourselectedSources,
\r
95 boolean checkDbrefs, boolean promptFetchDbrefs)
\r
97 this.selectedSources = new Vector();
\r
98 Enumeration sources = ourselectedSources.elements();
\r
99 // filter both sequences and sources to eliminate duplicates
\r
100 while (sources.hasMoreElements())
\r
102 Object src = sources.nextElement();
\r
103 if (!selectedSources.contains(src)) {
\r
104 selectedSources.addElement(src);
\r
107 Vector sqs = new Vector();
\r
108 for (int i=0; i<oursequences.length; i++)
\r
110 if (!sqs.contains(oursequences[i]))
\r
112 sqs.addElement(oursequences[i]);
\r
115 sequences = new SequenceI[sqs.size()];
\r
116 for (int i=0; i<sequences.length; i++) {
\r
117 sequences[i] = (SequenceI) sqs.elementAt(i);
\r
119 if (fsettings != null)
\r
121 this.fsettings = fsettings;
\r
122 this.af = fsettings.af;
\r
123 af.setShowSeqFeatures(true);
\r
125 int uniprotCount = 0;
\r
126 for (int i = 0; i < selectedSources.size(); i++)
\r
128 DasSource source = (DasSource) selectedSources.elementAt(i);
\r
129 DasCoordinateSystem[] coords = source.getCoordinateSystem();
\r
130 for (int c = 0; c < coords.length; c++)
\r
132 // TODO: match UniProt coord system canonically (?) - does
\r
133 // UniProt==uniprot==UNIPROT ?
\r
134 if (coords[c].getName().indexOf("UniProt") > -1)
\r
143 for (int i = 0; i < sequences.length; i++)
\r
145 DBRefEntry[] dbref = sequences[i].getDBRef();
\r
148 for (int j = 0; j < dbref.length; j++)
\r
150 if (dbref[j].getSource().equals(
\r
151 jalview.datamodel.DBRefSource.UNIPROT))
\r
160 if (checkDbrefs && refCount < sequences.length && uniprotCount > 0)
\r
163 int reply = JOptionPane.YES_OPTION;
\r
164 if (promptFetchDbrefs)
\r
166 reply = JOptionPane
\r
167 .showInternalConfirmDialog(
\r
169 "Do you want Jalview to find\n"
\r
170 + "Uniprot Accession ids for given sequence names?",
\r
171 "Find Uniprot Accession Ids",
\r
172 JOptionPane.YES_NO_OPTION,
\r
173 JOptionPane.QUESTION_MESSAGE);
\r
176 if (reply == JOptionPane.YES_OPTION)
\r
178 Thread thread = new Thread(new FetchDBRefs());
\r
193 class FetchDBRefs implements Runnable
\r
197 new DBRefFetcher(sequences, af).fetchDBRefs(true);
\r
203 * Spawns a number of dasobert Fetcher threads to add features to sequences in
\r
206 void startFetching()
\r
209 startTime = System.currentTimeMillis();
\r
212 af.setProgressBar("Fetching DAS Sequence Features", startTime);
\r
216 if (selectedSources == null || selectedSources.size() == 0)
\r
219 DasSource[] sources = new jalview.gui.DasSourceBrowser().getDASSource();
\r
221 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
\r
223 StringTokenizer st = new StringTokenizer(active, "\t");
\r
224 selectedSources = new Vector();
\r
226 while (st.hasMoreTokens())
\r
228 token = st.nextToken();
\r
229 for (int i = 0; i < sources.length; i++)
\r
231 if (sources[i].getNickname().equals(token))
\r
233 selectedSources.addElement(sources[i]);
\r
238 } catch (Exception ex)
\r
240 debug("Exception whilst setting default feature sources from registry and local preferences.",ex);
\r
244 if (selectedSources == null || selectedSources.size() == 0)
\r
246 System.out.println("No DAS Sources active");
\r
248 setGuiNoDassourceActive();
\r
252 sourcesRemaining = selectedSources.size();
\r
253 // Now sending requests one at a time to each server
\r
254 for (int sourceIndex = 0; sourceIndex < selectedSources.size()
\r
255 && !cancelled; sourceIndex++)
\r
257 DasSource dasSource = (DasSource) selectedSources
\r
258 .elementAt(sourceIndex);
\r
260 nextSequence(dasSource, sequences[0]);
\r
264 private void setGuiNoDassourceActive()
\r
269 af.setProgressBar("No DAS Sources Active", startTime);
\r
271 if (getFeatSettings() != null)
\r
273 fsettings.noDasSourceActive();
\r
278 * Update our fsettings dialog reference if we didn't have one when we were
\r
279 * first initialised.
\r
281 * @return fsettings
\r
283 private FeatureSettings getFeatSettings()
\r
285 if (fsettings == null)
\r
289 fsettings = af.featureSettings;
\r
295 public void cancel()
\r
299 af.setProgressBar("DAS Feature Fetching Cancelled", startTime);
\r
304 int sourcesRemaining = 0;
\r
306 void responseComplete(DasSource dasSource, SequenceI seq)
\r
310 for (int seqIndex = 0; seqIndex < sequences.length - 1 && !cancelled; seqIndex++)
\r
312 if (sequences[seqIndex] == seq)
\r
314 nextSequence(dasSource, sequences[++seqIndex]);
\r
320 sourcesRemaining--;
\r
322 if (sourcesRemaining == 0)
\r
324 System.err.println("Fetching Complete.");
\r
325 setGuiFetchComplete();
\r
330 private void setGuiFetchComplete()
\r
333 if (!cancelled && af != null)
\r
335 // only update the progress bar if we've completed the fetch normally
\r
336 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
339 if (af != null && af.featureSettings != null)
\r
341 af.featureSettings.setTableData();
\r
344 if (getFeatSettings() != null)
\r
346 fsettings.complete();
\r
350 void featuresAdded(SequenceI seq)
\r
354 // no gui to update with features.
\r
357 af.getFeatureRenderer().featuresAdded();
\r
359 int start = af.getViewport().getStartSeq();
\r
360 int end = af.getViewport().getEndSeq();
\r
362 for (index = start; index < end; index++)
\r
364 if (seq == af.getViewport().getAlignment().getSequenceAt(index)
\r
365 .getDatasetSequence())
\r
367 af.alignPanel.paintAlignment(true);
\r
373 void nextSequence(DasSource dasSource, SequenceI seq)
\r
377 DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(
\r
378 seq.getDBRef(), new String[]
\r
380 // jalview.datamodel.DBRefSource.PDB,
\r
381 jalview.datamodel.DBRefSource.UNIPROT,
\r
382 // jalview.datamodel.DBRefSource.EMBL - not tested on any EMBL coord
\r
385 // TODO: minimal list of DAS queries to make by querying with untyped ID if
\r
386 // distinct from any typed IDs
\r
388 boolean dasCoordSysFound = false;
\r
390 if (uprefs != null)
\r
392 // do any of these ids match the source's coordinate system ?
\r
393 for (int j = 0; !dasCoordSysFound && j < uprefs.length; j++)
\r
395 DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();
\r
397 for (int csIndex = 0; csIndex < cs.length && !dasCoordSysFound; csIndex++)
\r
400 && jalview.util.DBRefUtils.isDasCoordinateSystem(
\r
401 cs[csIndex].getName(), uprefs[j]))
\r
403 debug("Launched fetcher for coordinate system "
\r
404 + cs[0].getName());
\r
405 // Will have to pass any mapping information to the fetcher
\r
406 // - the start/end for the DBRefEntry may not be the same as the
\r
407 // sequence's start/end
\r
409 System.out.println(seq.getName() + " "
\r
410 + (seq.getDatasetSequence() == null) + " "
\r
411 + dasSource.getUrl());
\r
413 dasCoordSysFound = true; // break's out of the loop
\r
414 createFeatureFetcher(seq, dasSource, uprefs[j]);
\r
417 System.out.println("IGNORE " + cs[csIndex].getName());
\r
422 if (!dasCoordSysFound)
\r
425 // try and use the name as the sequence id
\r
426 if (seq.getName().indexOf("|") > -1)
\r
428 id = seq.getName().substring(seq.getName().lastIndexOf("|") + 1);
\r
429 if (id.trim().length() < 4)
\r
431 // hack - we regard a significant ID as being at least 4
\r
432 // non-whitespace characters
\r
433 id = seq.getName().substring(0, seq.getName().lastIndexOf("|"));
\r
434 if (id.indexOf("|") > -1)
\r
436 id = id.substring(id.lastIndexOf("|") + 1);
\r
442 id = seq.getName();
\r
446 // Should try to call a general feature fetcher that
\r
447 // queries many sources with name to discover applicable ID references
\r
448 createFeatureFetcher(seq, dasSource, id);
\r
455 * fetch and add das features to a sequence using the given source URL and
\r
456 * compatible DbRef id. new features are mapped using the DbRef mapping to the
\r
457 * local coordinate system.
\r
463 protected void createFeatureFetcher(final SequenceI seq,
\r
464 final DasSource dasSource, final DBRefEntry dbref)
\r
468 // / fetch DAS features
\r
469 final Das1Source source = new Das1Source();
\r
470 source.setUrl(dasSource.getUrl());
\r
471 source.setNickname(dasSource.getNickname());
\r
472 if (dbref == null || dbref.getAccessionId() == null
\r
473 || dbref.getAccessionId().length() < 1)
\r
475 responseComplete(dasSource, seq); // reduce thread count anyhow
\r
478 debug("new Das Feature Fetcher for " + dbref.getSource() + ":"
\r
479 + dbref.getAccessionId() + " querying " + dasSource.getUrl());
\r
480 FeatureThread fetcher = new FeatureThread(dbref.getAccessionId()
\r
481 // + ":" + start + "," + end,
\r
484 fetcher.addFeatureListener(new FeatureListener()
\r
486 public void comeBackLater(FeatureEvent e)
\r
488 responseComplete(dasSource, seq);
\r
489 debug("das source " + e.getSource().getNickname()
\r
490 + " asked us to come back in " + e.getComeBackLater()
\r
494 public void newFeatures(FeatureEvent e)
\r
497 Das1Source ds = e.getSource();
\r
499 Map[] features = e.getFeatures();
\r
500 // add features to sequence
\r
501 debug("das source " + ds.getUrl() + " returned " + features.length
\r
504 if (features.length > 0)
\r
506 for (int i = 0; i < features.length; i++)
\r
508 // standard DAS feature-> jalview sequence feature transformation
\r
509 SequenceFeature f = newSequenceFeature(features[i], source
\r
511 if (!parseSeqFeature(seq, f, features[i], source))
\r
513 if (dbref.getMap() != null && f.getBegin() > 0
\r
516 debug("mapping from " + f.getBegin() + " - " + f.getEnd());
\r
517 SequenceFeature vf[] = null;
\r
521 vf = dbref.getMap().locateFeature(f);
\r
522 } catch (Exception ex)
\r
525 .info("Error in 'experimental' mapping of features. Please try to reproduce and then report info to jalview-discuss@jalview.org.");
\r
526 Cache.log.info("Mapping feature from " + f.getBegin()
\r
527 + " to " + f.getEnd() + " in dbref "
\r
528 + dbref.getAccessionId() + " in "
\r
529 + dbref.getSource());
\r
530 Cache.log.info("using das Source " + ds.getUrl());
\r
531 Cache.log.info("Exception", ex);
\r
536 for (int v = 0; v < vf.length; v++)
\r
538 debug("mapping to " + v + ": " + vf[v].getBegin()
\r
539 + " - " + vf[v].getEnd());
\r
540 seq.addSequenceFeature(vf[v]);
\r
546 seq.addSequenceFeature(f);
\r
550 featuresAdded(seq);
\r
554 // System.out.println("No features found for " + seq.getName()
\r
555 // + " from: " + e.getDasSource().getNickname());
\r
557 responseComplete(dasSource, seq);
\r
567 protected void createFeatureFetcher(final SequenceI seq,
\r
568 final DasSource dasSource, String id)
\r
571 // / fetch DAS features
\r
572 final Das1Source source = new Das1Source();
\r
573 source.setUrl(dasSource.getUrl());
\r
574 source.setNickname(dasSource.getNickname());
\r
580 if (id != null && id.length() > 0)
\r
582 debug("new Das Feature Fetcher for " + id + " querying "
\r
583 + dasSource.getUrl());
\r
584 FeatureThread fetcher = new FeatureThread(id
\r
585 // + ":" + start + "," + end,
\r
588 fetcher.addFeatureListener(new FeatureListener()
\r
590 public void comeBackLater(FeatureEvent e)
\r
592 responseComplete(dasSource, seq);
\r
593 debug("das source " + e.getSource().getNickname()
\r
594 + " asked us to come back in " + e.getComeBackLater()
\r
598 public void newFeatures(FeatureEvent e)
\r
601 Das1Source ds = e.getSource();
\r
603 Map[] features = e.getFeatures();
\r
604 // add features to sequence
\r
605 debug("das source " + ds.getUrl() + " returned "
\r
606 + features.length + " features");
\r
608 if (features.length > 0)
\r
610 for (int i = 0; i < features.length; i++)
\r
612 // standard DAS feature-> jalview sequence feature transformation
\r
613 SequenceFeature f = newSequenceFeature(features[i], source
\r
615 if (!parseSeqFeature(seq, f, features[i], source))
\r
617 // just add as a simple sequence feature
\r
618 seq.addSequenceFeature(f);
\r
622 featuresAdded(seq);
\r
626 // System.out.println("No features found for " + seq.getName()
\r
627 // + " from: " + e.getDasSource().getNickname());
\r
629 responseComplete(dasSource, seq);
\r
640 // invalid fetch - indicate it is finished.
\r
641 debug("Skipping empty ID for querying " + dasSource.getUrl());
\r
642 responseComplete(dasSource, seq);
\r
647 * examine the given sequence feature to determine if it should actually be
\r
648 * turned into sequence annotation or database cross references rather than a
\r
649 * simple sequence feature.
\r
652 * the sequence to annotate
\r
654 * the jalview sequence feature generated from the DAS feature
\r
656 * the sequence feature attributes
\r
658 * the source that emitted the feature
\r
659 * @return true if feature was consumed as another kind of annotation.
\r
661 protected boolean parseSeqFeature(SequenceI seq, SequenceFeature f,
\r
662 Map map, Das1Source source)
\r
664 SequenceI mseq = seq;
\r
665 while (seq.getDatasetSequence()!=null)
\r
667 seq = seq.getDatasetSequence();
\r
669 if (f.getType() != null)
\r
671 String type = f.getType();
\r
672 if (type.equalsIgnoreCase("protein_name"))
\r
674 // parse name onto the alignment sequence or the dataset sequence.
\r
675 if (seq.getDescription()==null || seq.getDescription().trim().length() == 0)
\r
677 // could look at the note series to pick out the first long name, for the moment just use the whole description string
\r
678 seq.setDescription(f.getDescription());
\r
680 if (mseq.getDescription()==null || mseq.getDescription().trim().length() == 0)
\r
682 // could look at the note series to pick out the first long name, for the moment just use the whole description string
\r
683 mseq.setDescription(f.getDescription());
\r
687 // check if source has biosapiens or other sequence ontology label
\r
688 if (type.equalsIgnoreCase("DBXREF") || type.equalsIgnoreCase("DBREF"))
\r
690 // try to parse the accession out
\r
692 DBRefEntry dbr = new DBRefEntry();
\r
693 dbr.setVersion(source.getNickname());
\r
694 StringTokenizer st = new StringTokenizer(f.getDescription(), ":");
\r
695 if (st.hasMoreTokens())
\r
697 dbr.setSource(st.nextToken());
\r
699 if (st.hasMoreTokens())
\r
701 dbr.setAccessionId(st.nextToken());
\r
705 if (f.links != null && f.links.size() > 0)
\r
707 // feature is also appended to enable links to be seen.
\r
708 // TODO: consider extending dbrefs to have their own links ?
\r
709 // TODO: new feature: extract dbref links from DAS servers and add the URL pattern to the list of DB name associated links in the user's preferences ?
\r
710 // for the moment - just fix up the existing feature so it displays correctly.
\r
711 // f.setType(dbr.getSource());
\r
712 //f.setDescription();
\r
713 f.setValue("linkonly", Boolean.TRUE);
\r
714 //f.setDescription("");
\r
715 Vector newlinks = new Vector();
\r
716 Enumeration it = f.links.elements();
\r
717 while (it.hasMoreElements())
\r
720 UrlLink urllink = new UrlLink(elm = (String)it.nextElement());
\r
721 if (urllink.isValid()) {
\r
722 urllink.setLabel(f.getDescription());
\r
723 newlinks.addElement(urllink.toString());
\r
725 // couldn't parse the link properly. Keep it anyway - just in case.
\r
726 debug("couldn't parse link string - "+elm);
\r
727 newlinks.addElement(elm);
\r
730 f.links = newlinks;
\r
731 seq.addSequenceFeature(f);
\r
740 * creates a jalview sequence feature from a das feature document
\r
742 * @param dasfeature
\r
743 * @return sequence feature object created using dasfeature information
\r
745 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
747 if (dasfeature == null)
\r
754 * Different qNames for a DAS Feature - are string keys to the HashMaps in
\r
755 * features "METHOD") || qName.equals("TYPE") || qName.equals("START") ||
\r
756 * qName.equals("END") || qName.equals("NOTE") || qName.equals("LINK") ||
\r
757 * qName.equals("SCORE")
\r
759 String desc = new String();
\r
760 if (dasfeature.containsKey("NOTE"))
\r
762 desc += (String) dasfeature.get("NOTE");
\r
765 int start = 0, end = 0;
\r
770 start = Integer.parseInt(dasfeature.get("START").toString());
\r
771 } catch (Exception ex)
\r
776 end = Integer.parseInt(dasfeature.get("END").toString());
\r
777 } catch (Exception ex)
\r
782 Object scr = dasfeature.get("SCORE");
\r
785 score = (float) Double.parseDouble(scr.toString());
\r
788 } catch (Exception ex)
\r
792 SequenceFeature f = new SequenceFeature((String) dasfeature
\r
793 .get("TYPE"), desc, start, end, score, nickname);
\r
795 if (dasfeature.containsKey("LINK"))
\r
797 // Do not put feature extent in link text for non-positional features
\r
798 if (f.begin == 0 && f.end == 0)
\r
800 f.addLink(f.getType() + "|" + dasfeature.get("LINK"));
\r
804 f.addLink(f.getType() + " " + f.begin + "_" + f.end + "|"
\r
805 + dasfeature.get("LINK"));
\r
810 } catch (Exception e)
\r
812 System.out.println("ERRR " + e);
\r
813 e.printStackTrace();
\r
814 System.out.println("############");
\r
815 debug("Failed to parse " + dasfeature.toString(), e);
\r
821 * query the default DAS Source Registry for sources. Uses value of jalview
\r
822 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
\r
825 * @return list of sources
\r
827 public static DasSource[] getDASSources()
\r
830 String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
831 DasSourceBrowser.DEFAULT_REGISTRY);
\r
832 return getDASSources(registryURL);
\r
836 * query the given URL for DasSources.
\r
838 * @param registryURL
\r
839 * return sources from registryURL
\r
841 public static DasSource[] getDASSources(String registryURL)
\r
843 DasSourceReaderImpl reader = new DasSourceReaderImpl();
\r
847 URL url = new URL(registryURL);
\r
849 DasSource[] sources = reader.readDasSource(url);
\r
851 List das1sources = new ArrayList();
\r
852 for (int i = 0; i < sources.length; i++)
\r
854 DasSource ds = sources[i];
\r
855 if (ds instanceof Das2Source)
\r
857 Das2Source d2s = (Das2Source) ds;
\r
858 if (d2s.hasDas1Capabilities())
\r
860 Das1Source d1s = DasSourceConverter.toDas1Source(d2s);
\r
861 das1sources.add(d1s);
\r
865 else if (ds instanceof Das1Source)
\r
867 das1sources.add((Das1Source) ds);
\r
871 return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources
\r
873 } catch (Exception ex)
\r
875 System.err.println("Failed to contact DAS1 registry at "+registryURL);
\r
876 ex.printStackTrace();
\r