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
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 selectedSources,
\r
95 boolean checkDbrefs, boolean promptFetchDbrefs)
\r
97 this.selectedSources = new Vector();
\r
98 Enumeration sources = selectedSources.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 (this.selectedSources.contains(src)) { selectedSources.addElement(src); };
\r
105 Vector sqs = new Vector();
\r
106 for (int i=0; i<oursequences.length; i++)
\r
108 if (!sqs.contains(oursequences[i]))
\r
110 sqs.addElement(oursequences[i]);
\r
113 sequences = new SequenceI[sqs.size()];
\r
114 for (int i=0; i<sequences.length; i++) {
\r
115 sequences[i] = (SequenceI) sqs.elementAt(i);
\r
117 if (fsettings != null)
\r
119 this.fsettings = fsettings;
\r
120 this.af = fsettings.af;
\r
121 af.setShowSeqFeatures(true);
\r
123 int uniprotCount = 0;
\r
124 for (int i = 0; i < selectedSources.size(); i++)
\r
126 DasSource source = (DasSource) selectedSources.elementAt(i);
\r
127 DasCoordinateSystem[] coords = source.getCoordinateSystem();
\r
128 for (int c = 0; c < coords.length; c++)
\r
130 // TODO: match UniProt coord system canonically (?) - does
\r
131 // UniProt==uniprot==UNIPROT ?
\r
132 if (coords[c].getName().indexOf("UniProt") > -1)
\r
141 for (int i = 0; i < sequences.length; i++)
\r
143 DBRefEntry[] dbref = sequences[i].getDBRef();
\r
146 for (int j = 0; j < dbref.length; j++)
\r
148 if (dbref[j].getSource().equals(
\r
149 jalview.datamodel.DBRefSource.UNIPROT))
\r
158 if (checkDbrefs && refCount < sequences.length && uniprotCount > 0)
\r
161 int reply = JOptionPane.YES_OPTION;
\r
162 if (promptFetchDbrefs)
\r
164 reply = JOptionPane
\r
165 .showInternalConfirmDialog(
\r
167 "Do you want Jalview to find\n"
\r
168 + "Uniprot Accession ids for given sequence names?",
\r
169 "Find Uniprot Accession Ids",
\r
170 JOptionPane.YES_NO_OPTION,
\r
171 JOptionPane.QUESTION_MESSAGE);
\r
174 if (reply == JOptionPane.YES_OPTION)
\r
176 Thread thread = new Thread(new FetchDBRefs());
\r
191 class FetchDBRefs implements Runnable
\r
195 new DBRefFetcher(sequences, af).fetchDBRefs(true);
\r
201 * Spawns a number of dasobert Fetcher threads to add features to sequences in
\r
204 void startFetching()
\r
207 startTime = System.currentTimeMillis();
\r
210 af.setProgressBar("Fetching DAS Sequence Features", startTime);
\r
214 if (selectedSources == null || selectedSources.size() == 0)
\r
217 DasSource[] sources = new jalview.gui.DasSourceBrowser().getDASSource();
\r
219 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
\r
221 StringTokenizer st = new StringTokenizer(active, "\t");
\r
222 selectedSources = new Vector();
\r
224 while (st.hasMoreTokens())
\r
226 token = st.nextToken();
\r
227 for (int i = 0; i < sources.length; i++)
\r
229 if (sources[i].getNickname().equals(token))
\r
231 selectedSources.addElement(sources[i]);
\r
236 } catch (Exception ex)
\r
238 debug("Exception whilst setting default feature sources from registry and local preferences.",ex);
\r
242 if (selectedSources == null || selectedSources.size() == 0)
\r
244 System.out.println("No DAS Sources active");
\r
246 setGuiNoDassourceActive();
\r
250 sourcesRemaining = selectedSources.size();
\r
251 // Now sending requests one at a time to each server
\r
252 for (int sourceIndex = 0; sourceIndex < selectedSources.size()
\r
253 && !cancelled; sourceIndex++)
\r
255 DasSource dasSource = (DasSource) selectedSources
\r
256 .elementAt(sourceIndex);
\r
258 nextSequence(dasSource, sequences[0]);
\r
262 private void setGuiNoDassourceActive()
\r
267 af.setProgressBar("No DAS Sources Active", startTime);
\r
269 if (getFeatSettings() != null)
\r
271 fsettings.noDasSourceActive();
\r
276 * Update our fsettings dialog reference if we didn't have one when we were
\r
277 * first initialised.
\r
279 * @return fsettings
\r
281 private FeatureSettings getFeatSettings()
\r
283 if (fsettings == null)
\r
287 fsettings = af.featureSettings;
\r
293 public void cancel()
\r
297 af.setProgressBar("DAS Feature Fetching Cancelled", startTime);
\r
302 int sourcesRemaining = 0;
\r
304 void responseComplete(DasSource dasSource, SequenceI seq)
\r
308 for (int seqIndex = 0; seqIndex < sequences.length - 1 && !cancelled; seqIndex++)
\r
310 if (sequences[seqIndex] == seq)
\r
312 nextSequence(dasSource, sequences[++seqIndex]);
\r
318 sourcesRemaining--;
\r
320 if (sourcesRemaining == 0)
\r
322 System.err.println("Fetching Complete.");
\r
323 setGuiFetchComplete();
\r
328 private void setGuiFetchComplete()
\r
333 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
336 if (af != null && af.featureSettings != null)
\r
338 af.featureSettings.setTableData();
\r
341 if (getFeatSettings() != null)
\r
343 fsettings.complete();
\r
347 void featuresAdded(SequenceI seq)
\r
351 // no gui to update with features.
\r
354 af.getFeatureRenderer().featuresAdded();
\r
356 int start = af.getViewport().getStartSeq();
\r
357 int end = af.getViewport().getEndSeq();
\r
359 for (index = start; index < end; index++)
\r
361 if (seq == af.getViewport().getAlignment().getSequenceAt(index)
\r
362 .getDatasetSequence())
\r
364 af.alignPanel.paintAlignment(true);
\r
370 void nextSequence(DasSource dasSource, SequenceI seq)
\r
374 DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(
\r
375 seq.getDBRef(), new String[]
\r
377 // jalview.datamodel.DBRefSource.PDB,
\r
378 jalview.datamodel.DBRefSource.UNIPROT,
\r
379 // jalview.datamodel.DBRefSource.EMBL - not tested on any EMBL coord
\r
382 // TODO: minimal list of DAS queries to make by querying with untyped ID if
\r
383 // distinct from any typed IDs
\r
385 boolean dasCoordSysFound = false;
\r
387 if (uprefs != null)
\r
389 // do any of these ids match the source's coordinate system ?
\r
390 for (int j = 0; !dasCoordSysFound && j < uprefs.length; j++)
\r
392 DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();
\r
394 for (int csIndex = 0; csIndex < cs.length && !dasCoordSysFound; csIndex++)
\r
397 && jalview.util.DBRefUtils.isDasCoordinateSystem(
\r
398 cs[csIndex].getName(), uprefs[j]))
\r
400 debug("Launched fetcher for coordinate system "
\r
401 + cs[0].getName());
\r
402 // Will have to pass any mapping information to the fetcher
\r
403 // - the start/end for the DBRefEntry may not be the same as the
\r
404 // sequence's start/end
\r
406 System.out.println(seq.getName() + " "
\r
407 + (seq.getDatasetSequence() == null) + " "
\r
408 + dasSource.getUrl());
\r
410 dasCoordSysFound = true; // break's out of the loop
\r
411 createFeatureFetcher(seq, dasSource, uprefs[j]);
\r
414 System.out.println("IGNORE " + cs[csIndex].getName());
\r
419 if (!dasCoordSysFound)
\r
422 // try and use the name as the sequence id
\r
423 if (seq.getName().indexOf("|") > -1)
\r
425 id = seq.getName().substring(seq.getName().lastIndexOf("|") + 1);
\r
426 if (id.trim().length() < 4)
\r
428 // hack - we regard a significant ID as being at least 4
\r
429 // non-whitespace characters
\r
430 id = seq.getName().substring(0, seq.getName().lastIndexOf("|"));
\r
431 if (id.indexOf("|") > -1)
\r
433 id = id.substring(id.lastIndexOf("|") + 1);
\r
439 id = seq.getName();
\r
443 // Should try to call a general feature fetcher that
\r
444 // queries many sources with name to discover applicable ID references
\r
445 createFeatureFetcher(seq, dasSource, id);
\r
452 * fetch and add das features to a sequence using the given source URL and
\r
453 * compatible DbRef id. new features are mapped using the DbRef mapping to the
\r
454 * local coordinate system.
\r
460 protected void createFeatureFetcher(final SequenceI seq,
\r
461 final DasSource dasSource, final DBRefEntry dbref)
\r
465 // / fetch DAS features
\r
466 final Das1Source source = new Das1Source();
\r
467 source.setUrl(dasSource.getUrl());
\r
468 source.setNickname(dasSource.getNickname());
\r
469 if (dbref == null || dbref.getAccessionId() == null
\r
470 || dbref.getAccessionId().length() < 1)
\r
472 responseComplete(dasSource, seq); // reduce thread count anyhow
\r
475 debug("new Das Feature Fetcher for " + dbref.getSource() + ":"
\r
476 + dbref.getAccessionId() + " querying " + dasSource.getUrl());
\r
477 FeatureThread fetcher = new FeatureThread(dbref.getAccessionId()
\r
478 // + ":" + start + "," + end,
\r
481 fetcher.addFeatureListener(new FeatureListener()
\r
483 public void comeBackLater(FeatureEvent e)
\r
485 responseComplete(dasSource, seq);
\r
486 debug("das source " + e.getSource().getNickname()
\r
487 + " asked us to come back in " + e.getComeBackLater()
\r
491 public void newFeatures(FeatureEvent e)
\r
494 Das1Source ds = e.getSource();
\r
496 Map[] features = e.getFeatures();
\r
497 // add features to sequence
\r
498 debug("das source " + ds.getUrl() + " returned " + features.length
\r
501 if (features.length > 0)
\r
503 for (int i = 0; i < features.length; i++)
\r
505 // standard DAS feature-> jalview sequence feature transformation
\r
506 SequenceFeature f = newSequenceFeature(features[i], source
\r
508 if (!parseSeqFeature(seq, f, features[i], source))
\r
510 if (dbref.getMap() != null && f.getBegin() > 0
\r
513 debug("mapping from " + f.getBegin() + " - " + f.getEnd());
\r
514 SequenceFeature vf[] = null;
\r
518 vf = dbref.getMap().locateFeature(f);
\r
519 } catch (Exception ex)
\r
522 .info("Error in 'experimental' mapping of features. Please try to reproduce and then report info to jalview-discuss@jalview.org.");
\r
523 Cache.log.info("Mapping feature from " + f.getBegin()
\r
524 + " to " + f.getEnd() + " in dbref "
\r
525 + dbref.getAccessionId() + " in "
\r
526 + dbref.getSource());
\r
527 Cache.log.info("using das Source " + ds.getUrl());
\r
528 Cache.log.info("Exception", ex);
\r
533 for (int v = 0; v < vf.length; v++)
\r
535 debug("mapping to " + v + ": " + vf[v].getBegin()
\r
536 + " - " + vf[v].getEnd());
\r
537 seq.addSequenceFeature(vf[v]);
\r
543 seq.addSequenceFeature(f);
\r
547 featuresAdded(seq);
\r
551 // System.out.println("No features found for " + seq.getName()
\r
552 // + " from: " + e.getDasSource().getNickname());
\r
554 responseComplete(dasSource, seq);
\r
564 protected void createFeatureFetcher(final SequenceI seq,
\r
565 final DasSource dasSource, String id)
\r
568 // / fetch DAS features
\r
569 final Das1Source source = new Das1Source();
\r
570 source.setUrl(dasSource.getUrl());
\r
571 source.setNickname(dasSource.getNickname());
\r
577 if (id != null && id.length() > 0)
\r
579 debug("new Das Feature Fetcher for " + id + " querying "
\r
580 + dasSource.getUrl());
\r
581 FeatureThread fetcher = new FeatureThread(id
\r
582 // + ":" + start + "," + end,
\r
585 fetcher.addFeatureListener(new FeatureListener()
\r
587 public void comeBackLater(FeatureEvent e)
\r
589 responseComplete(dasSource, seq);
\r
590 debug("das source " + e.getSource().getNickname()
\r
591 + " asked us to come back in " + e.getComeBackLater()
\r
595 public void newFeatures(FeatureEvent e)
\r
598 Das1Source ds = e.getSource();
\r
600 Map[] features = e.getFeatures();
\r
601 // add features to sequence
\r
602 debug("das source " + ds.getUrl() + " returned "
\r
603 + features.length + " features");
\r
605 if (features.length > 0)
\r
607 for (int i = 0; i < features.length; i++)
\r
609 // standard DAS feature-> jalview sequence feature transformation
\r
610 SequenceFeature f = newSequenceFeature(features[i], source
\r
612 if (!parseSeqFeature(seq, f, features[i], source))
\r
614 // just add as a simple sequence feature
\r
615 seq.addSequenceFeature(f);
\r
619 featuresAdded(seq);
\r
623 // System.out.println("No features found for " + seq.getName()
\r
624 // + " from: " + e.getDasSource().getNickname());
\r
626 responseComplete(dasSource, seq);
\r
637 // invalid fetch - indicate it is finished.
\r
638 debug("Skipping empty ID for querying " + dasSource.getUrl());
\r
639 responseComplete(dasSource, seq);
\r
644 * examine the given sequence feature to determine if it should actually be
\r
645 * turned into sequence annotation or database cross references rather than a
\r
646 * simple sequence feature.
\r
649 * the sequence to annotate
\r
651 * the jalview sequence feature generated from the DAS feature
\r
653 * the sequence feature attributes
\r
655 * the source that emitted the feature
\r
656 * @return true if feature was consumed as another kind of annotation.
\r
658 protected boolean parseSeqFeature(SequenceI seq, SequenceFeature f,
\r
659 Map map, Das1Source source)
\r
661 SequenceI mseq = seq;
\r
662 while (seq.getDatasetSequence()!=null)
\r
664 seq = seq.getDatasetSequence();
\r
666 if (f.getType() != null)
\r
668 String type = f.getType();
\r
669 if (type.equalsIgnoreCase("protein_name"))
\r
671 // parse name onto the alignment sequence or the dataset sequence.
\r
672 if (seq.getDescription()==null || seq.getDescription().trim().length() == 0)
\r
674 // could look at the note series to pick out the first long name, for the moment just use the whole description string
\r
675 seq.setDescription(f.getDescription());
\r
677 if (mseq.getDescription()==null || mseq.getDescription().trim().length() == 0)
\r
679 // could look at the note series to pick out the first long name, for the moment just use the whole description string
\r
680 mseq.setDescription(f.getDescription());
\r
684 // check if source has biosapiens or other sequence ontology label
\r
685 if (type.equalsIgnoreCase("DBXREF") || type.equalsIgnoreCase("DBREF"))
\r
687 // try to parse the accession out
\r
689 DBRefEntry dbr = new DBRefEntry();
\r
690 dbr.setVersion(source.getNickname());
\r
691 StringTokenizer st = new StringTokenizer(f.getDescription(), ":");
\r
692 if (st.hasMoreTokens())
\r
694 dbr.setSource(st.nextToken());
\r
696 if (st.hasMoreTokens())
\r
698 dbr.setAccessionId(st.nextToken());
\r
702 if (f.links != null && f.links.size() > 0)
\r
704 // feature is also appended to enable links to be seen.
\r
705 // TODO: consider extending dbrefs to have their own links ?
\r
706 // 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
707 // for the moment - just fix up the existing feature so it displays correctly.
\r
708 // f.setType(dbr.getSource());
\r
709 //f.setDescription();
\r
710 f.setValue("linkonly", Boolean.TRUE);
\r
711 //f.setDescription("");
\r
712 Vector newlinks = new Vector();
\r
713 Enumeration it = f.links.elements();
\r
714 while (it.hasMoreElements())
\r
717 UrlLink urllink = new UrlLink(elm = (String)it.nextElement());
\r
718 if (urllink.isValid()) {
\r
719 urllink.setLabel(f.getDescription());
\r
720 newlinks.addElement(urllink.toString());
\r
722 // couldn't parse the link properly. Keep it anyway - just in case.
\r
723 debug("couldn't parse link string - "+elm);
\r
724 newlinks.addElement(elm);
\r
727 f.links = newlinks;
\r
728 seq.addSequenceFeature(f);
\r
737 * creates a jalview sequence feature from a das feature document
\r
739 * @param dasfeature
\r
740 * @return sequence feature object created using dasfeature information
\r
742 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
744 if (dasfeature == null)
\r
751 * Different qNames for a DAS Feature - are string keys to the HashMaps in
\r
752 * features "METHOD") || qName.equals("TYPE") || qName.equals("START") ||
\r
753 * qName.equals("END") || qName.equals("NOTE") || qName.equals("LINK") ||
\r
754 * qName.equals("SCORE")
\r
756 String desc = new String();
\r
757 if (dasfeature.containsKey("NOTE"))
\r
759 desc += (String) dasfeature.get("NOTE");
\r
762 int start = 0, end = 0;
\r
767 start = Integer.parseInt(dasfeature.get("START").toString());
\r
768 } catch (Exception ex)
\r
773 end = Integer.parseInt(dasfeature.get("END").toString());
\r
774 } catch (Exception ex)
\r
779 Object scr = dasfeature.get("SCORE");
\r
782 score = (float) Double.parseDouble(scr.toString());
\r
785 } catch (Exception ex)
\r
789 SequenceFeature f = new SequenceFeature((String) dasfeature
\r
790 .get("TYPE"), desc, start, end, score, nickname);
\r
792 if (dasfeature.containsKey("LINK"))
\r
794 // Do not put feature extent in link text for non-positional features
\r
795 if (f.begin == 0 && f.end == 0)
\r
797 f.addLink(f.getType() + "|" + dasfeature.get("LINK"));
\r
801 f.addLink(f.getType() + " " + f.begin + "_" + f.end + "|"
\r
802 + dasfeature.get("LINK"));
\r
807 } catch (Exception e)
\r
809 System.out.println("ERRR " + e);
\r
810 e.printStackTrace();
\r
811 System.out.println("############");
\r
812 debug("Failed to parse " + dasfeature.toString(), e);
\r
818 * query the default DAS Source Registry for sources. Uses value of jalview
\r
819 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
\r
822 * @return list of sources
\r
824 public static DasSource[] getDASSources()
\r
827 String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
828 DasSourceBrowser.DEFAULT_REGISTRY);
\r
829 return getDASSources(registryURL);
\r
833 * query the given URL for DasSources.
\r
835 * @param registryURL
\r
836 * return sources from registryURL
\r
838 public static DasSource[] getDASSources(String registryURL)
\r
840 DasSourceReaderImpl reader = new DasSourceReaderImpl();
\r
844 URL url = new URL(registryURL);
\r
846 DasSource[] sources = reader.readDasSource(url);
\r
848 List das1sources = new ArrayList();
\r
849 for (int i = 0; i < sources.length; i++)
\r
851 DasSource ds = sources[i];
\r
852 if (ds instanceof Das2Source)
\r
854 Das2Source d2s = (Das2Source) ds;
\r
855 if (d2s.hasDas1Capabilities())
\r
857 Das1Source d1s = DasSourceConverter.toDas1Source(d2s);
\r
858 das1sources.add(d1s);
\r
862 else if (ds instanceof Das1Source)
\r
864 das1sources.add((Das1Source) ds);
\r
868 return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources
\r
870 } catch (Exception ex)
\r
872 ex.printStackTrace();
\r