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
335 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
338 if (af != null && af.featureSettings != null)
\r
340 af.featureSettings.setTableData();
\r
343 if (getFeatSettings() != null)
\r
345 fsettings.complete();
\r
349 void featuresAdded(SequenceI seq)
\r
353 // no gui to update with features.
\r
356 af.getFeatureRenderer().featuresAdded();
\r
358 int start = af.getViewport().getStartSeq();
\r
359 int end = af.getViewport().getEndSeq();
\r
361 for (index = start; index < end; index++)
\r
363 if (seq == af.getViewport().getAlignment().getSequenceAt(index)
\r
364 .getDatasetSequence())
\r
366 af.alignPanel.paintAlignment(true);
\r
372 void nextSequence(DasSource dasSource, SequenceI seq)
\r
376 DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(
\r
377 seq.getDBRef(), new String[]
\r
379 // jalview.datamodel.DBRefSource.PDB,
\r
380 jalview.datamodel.DBRefSource.UNIPROT,
\r
381 // jalview.datamodel.DBRefSource.EMBL - not tested on any EMBL coord
\r
384 // TODO: minimal list of DAS queries to make by querying with untyped ID if
\r
385 // distinct from any typed IDs
\r
387 boolean dasCoordSysFound = false;
\r
389 if (uprefs != null)
\r
391 // do any of these ids match the source's coordinate system ?
\r
392 for (int j = 0; !dasCoordSysFound && j < uprefs.length; j++)
\r
394 DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();
\r
396 for (int csIndex = 0; csIndex < cs.length && !dasCoordSysFound; csIndex++)
\r
399 && jalview.util.DBRefUtils.isDasCoordinateSystem(
\r
400 cs[csIndex].getName(), uprefs[j]))
\r
402 debug("Launched fetcher for coordinate system "
\r
403 + cs[0].getName());
\r
404 // Will have to pass any mapping information to the fetcher
\r
405 // - the start/end for the DBRefEntry may not be the same as the
\r
406 // sequence's start/end
\r
408 System.out.println(seq.getName() + " "
\r
409 + (seq.getDatasetSequence() == null) + " "
\r
410 + dasSource.getUrl());
\r
412 dasCoordSysFound = true; // break's out of the loop
\r
413 createFeatureFetcher(seq, dasSource, uprefs[j]);
\r
416 System.out.println("IGNORE " + cs[csIndex].getName());
\r
421 if (!dasCoordSysFound)
\r
424 // try and use the name as the sequence id
\r
425 if (seq.getName().indexOf("|") > -1)
\r
427 id = seq.getName().substring(seq.getName().lastIndexOf("|") + 1);
\r
428 if (id.trim().length() < 4)
\r
430 // hack - we regard a significant ID as being at least 4
\r
431 // non-whitespace characters
\r
432 id = seq.getName().substring(0, seq.getName().lastIndexOf("|"));
\r
433 if (id.indexOf("|") > -1)
\r
435 id = id.substring(id.lastIndexOf("|") + 1);
\r
441 id = seq.getName();
\r
445 // Should try to call a general feature fetcher that
\r
446 // queries many sources with name to discover applicable ID references
\r
447 createFeatureFetcher(seq, dasSource, id);
\r
454 * fetch and add das features to a sequence using the given source URL and
\r
455 * compatible DbRef id. new features are mapped using the DbRef mapping to the
\r
456 * local coordinate system.
\r
462 protected void createFeatureFetcher(final SequenceI seq,
\r
463 final DasSource dasSource, final DBRefEntry dbref)
\r
467 // / fetch DAS features
\r
468 final Das1Source source = new Das1Source();
\r
469 source.setUrl(dasSource.getUrl());
\r
470 source.setNickname(dasSource.getNickname());
\r
471 if (dbref == null || dbref.getAccessionId() == null
\r
472 || dbref.getAccessionId().length() < 1)
\r
474 responseComplete(dasSource, seq); // reduce thread count anyhow
\r
477 debug("new Das Feature Fetcher for " + dbref.getSource() + ":"
\r
478 + dbref.getAccessionId() + " querying " + dasSource.getUrl());
\r
479 FeatureThread fetcher = new FeatureThread(dbref.getAccessionId()
\r
480 // + ":" + start + "," + end,
\r
483 fetcher.addFeatureListener(new FeatureListener()
\r
485 public void comeBackLater(FeatureEvent e)
\r
487 responseComplete(dasSource, seq);
\r
488 debug("das source " + e.getSource().getNickname()
\r
489 + " asked us to come back in " + e.getComeBackLater()
\r
493 public void newFeatures(FeatureEvent e)
\r
496 Das1Source ds = e.getSource();
\r
498 Map[] features = e.getFeatures();
\r
499 // add features to sequence
\r
500 debug("das source " + ds.getUrl() + " returned " + features.length
\r
503 if (features.length > 0)
\r
505 for (int i = 0; i < features.length; i++)
\r
507 // standard DAS feature-> jalview sequence feature transformation
\r
508 SequenceFeature f = newSequenceFeature(features[i], source
\r
510 if (!parseSeqFeature(seq, f, features[i], source))
\r
512 if (dbref.getMap() != null && f.getBegin() > 0
\r
515 debug("mapping from " + f.getBegin() + " - " + f.getEnd());
\r
516 SequenceFeature vf[] = null;
\r
520 vf = dbref.getMap().locateFeature(f);
\r
521 } catch (Exception ex)
\r
524 .info("Error in 'experimental' mapping of features. Please try to reproduce and then report info to jalview-discuss@jalview.org.");
\r
525 Cache.log.info("Mapping feature from " + f.getBegin()
\r
526 + " to " + f.getEnd() + " in dbref "
\r
527 + dbref.getAccessionId() + " in "
\r
528 + dbref.getSource());
\r
529 Cache.log.info("using das Source " + ds.getUrl());
\r
530 Cache.log.info("Exception", ex);
\r
535 for (int v = 0; v < vf.length; v++)
\r
537 debug("mapping to " + v + ": " + vf[v].getBegin()
\r
538 + " - " + vf[v].getEnd());
\r
539 seq.addSequenceFeature(vf[v]);
\r
545 seq.addSequenceFeature(f);
\r
549 featuresAdded(seq);
\r
553 // System.out.println("No features found for " + seq.getName()
\r
554 // + " from: " + e.getDasSource().getNickname());
\r
556 responseComplete(dasSource, seq);
\r
566 protected void createFeatureFetcher(final SequenceI seq,
\r
567 final DasSource dasSource, String id)
\r
570 // / fetch DAS features
\r
571 final Das1Source source = new Das1Source();
\r
572 source.setUrl(dasSource.getUrl());
\r
573 source.setNickname(dasSource.getNickname());
\r
579 if (id != null && id.length() > 0)
\r
581 debug("new Das Feature Fetcher for " + id + " querying "
\r
582 + dasSource.getUrl());
\r
583 FeatureThread fetcher = new FeatureThread(id
\r
584 // + ":" + start + "," + end,
\r
587 fetcher.addFeatureListener(new FeatureListener()
\r
589 public void comeBackLater(FeatureEvent e)
\r
591 responseComplete(dasSource, seq);
\r
592 debug("das source " + e.getSource().getNickname()
\r
593 + " asked us to come back in " + e.getComeBackLater()
\r
597 public void newFeatures(FeatureEvent e)
\r
600 Das1Source ds = e.getSource();
\r
602 Map[] features = e.getFeatures();
\r
603 // add features to sequence
\r
604 debug("das source " + ds.getUrl() + " returned "
\r
605 + features.length + " features");
\r
607 if (features.length > 0)
\r
609 for (int i = 0; i < features.length; i++)
\r
611 // standard DAS feature-> jalview sequence feature transformation
\r
612 SequenceFeature f = newSequenceFeature(features[i], source
\r
614 if (!parseSeqFeature(seq, f, features[i], source))
\r
616 // just add as a simple sequence feature
\r
617 seq.addSequenceFeature(f);
\r
621 featuresAdded(seq);
\r
625 // System.out.println("No features found for " + seq.getName()
\r
626 // + " from: " + e.getDasSource().getNickname());
\r
628 responseComplete(dasSource, seq);
\r
639 // invalid fetch - indicate it is finished.
\r
640 debug("Skipping empty ID for querying " + dasSource.getUrl());
\r
641 responseComplete(dasSource, seq);
\r
646 * examine the given sequence feature to determine if it should actually be
\r
647 * turned into sequence annotation or database cross references rather than a
\r
648 * simple sequence feature.
\r
651 * the sequence to annotate
\r
653 * the jalview sequence feature generated from the DAS feature
\r
655 * the sequence feature attributes
\r
657 * the source that emitted the feature
\r
658 * @return true if feature was consumed as another kind of annotation.
\r
660 protected boolean parseSeqFeature(SequenceI seq, SequenceFeature f,
\r
661 Map map, Das1Source source)
\r
663 SequenceI mseq = seq;
\r
664 while (seq.getDatasetSequence()!=null)
\r
666 seq = seq.getDatasetSequence();
\r
668 if (f.getType() != null)
\r
670 String type = f.getType();
\r
671 if (type.equalsIgnoreCase("protein_name"))
\r
673 // parse name onto the alignment sequence or the dataset sequence.
\r
674 if (seq.getDescription()==null || seq.getDescription().trim().length() == 0)
\r
676 // could look at the note series to pick out the first long name, for the moment just use the whole description string
\r
677 seq.setDescription(f.getDescription());
\r
679 if (mseq.getDescription()==null || mseq.getDescription().trim().length() == 0)
\r
681 // could look at the note series to pick out the first long name, for the moment just use the whole description string
\r
682 mseq.setDescription(f.getDescription());
\r
686 // check if source has biosapiens or other sequence ontology label
\r
687 if (type.equalsIgnoreCase("DBXREF") || type.equalsIgnoreCase("DBREF"))
\r
689 // try to parse the accession out
\r
691 DBRefEntry dbr = new DBRefEntry();
\r
692 dbr.setVersion(source.getNickname());
\r
693 StringTokenizer st = new StringTokenizer(f.getDescription(), ":");
\r
694 if (st.hasMoreTokens())
\r
696 dbr.setSource(st.nextToken());
\r
698 if (st.hasMoreTokens())
\r
700 dbr.setAccessionId(st.nextToken());
\r
704 if (f.links != null && f.links.size() > 0)
\r
706 // feature is also appended to enable links to be seen.
\r
707 // TODO: consider extending dbrefs to have their own links ?
\r
708 // 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
709 // for the moment - just fix up the existing feature so it displays correctly.
\r
710 // f.setType(dbr.getSource());
\r
711 //f.setDescription();
\r
712 f.setValue("linkonly", Boolean.TRUE);
\r
713 //f.setDescription("");
\r
714 Vector newlinks = new Vector();
\r
715 Enumeration it = f.links.elements();
\r
716 while (it.hasMoreElements())
\r
719 UrlLink urllink = new UrlLink(elm = (String)it.nextElement());
\r
720 if (urllink.isValid()) {
\r
721 urllink.setLabel(f.getDescription());
\r
722 newlinks.addElement(urllink.toString());
\r
724 // couldn't parse the link properly. Keep it anyway - just in case.
\r
725 debug("couldn't parse link string - "+elm);
\r
726 newlinks.addElement(elm);
\r
729 f.links = newlinks;
\r
730 seq.addSequenceFeature(f);
\r
739 * creates a jalview sequence feature from a das feature document
\r
741 * @param dasfeature
\r
742 * @return sequence feature object created using dasfeature information
\r
744 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
746 if (dasfeature == null)
\r
753 * Different qNames for a DAS Feature - are string keys to the HashMaps in
\r
754 * features "METHOD") || qName.equals("TYPE") || qName.equals("START") ||
\r
755 * qName.equals("END") || qName.equals("NOTE") || qName.equals("LINK") ||
\r
756 * qName.equals("SCORE")
\r
758 String desc = new String();
\r
759 if (dasfeature.containsKey("NOTE"))
\r
761 desc += (String) dasfeature.get("NOTE");
\r
764 int start = 0, end = 0;
\r
769 start = Integer.parseInt(dasfeature.get("START").toString());
\r
770 } catch (Exception ex)
\r
775 end = Integer.parseInt(dasfeature.get("END").toString());
\r
776 } catch (Exception ex)
\r
781 Object scr = dasfeature.get("SCORE");
\r
784 score = (float) Double.parseDouble(scr.toString());
\r
787 } catch (Exception ex)
\r
791 SequenceFeature f = new SequenceFeature((String) dasfeature
\r
792 .get("TYPE"), desc, start, end, score, nickname);
\r
794 if (dasfeature.containsKey("LINK"))
\r
796 // Do not put feature extent in link text for non-positional features
\r
797 if (f.begin == 0 && f.end == 0)
\r
799 f.addLink(f.getType() + "|" + dasfeature.get("LINK"));
\r
803 f.addLink(f.getType() + " " + f.begin + "_" + f.end + "|"
\r
804 + dasfeature.get("LINK"));
\r
809 } catch (Exception e)
\r
811 System.out.println("ERRR " + e);
\r
812 e.printStackTrace();
\r
813 System.out.println("############");
\r
814 debug("Failed to parse " + dasfeature.toString(), e);
\r
820 * query the default DAS Source Registry for sources. Uses value of jalview
\r
821 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
\r
824 * @return list of sources
\r
826 public static DasSource[] getDASSources()
\r
829 String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
830 DasSourceBrowser.DEFAULT_REGISTRY);
\r
831 return getDASSources(registryURL);
\r
835 * query the given URL for DasSources.
\r
837 * @param registryURL
\r
838 * return sources from registryURL
\r
840 public static DasSource[] getDASSources(String registryURL)
\r
842 DasSourceReaderImpl reader = new DasSourceReaderImpl();
\r
846 URL url = new URL(registryURL);
\r
848 DasSource[] sources = reader.readDasSource(url);
\r
850 List das1sources = new ArrayList();
\r
851 for (int i = 0; i < sources.length; i++)
\r
853 DasSource ds = sources[i];
\r
854 if (ds instanceof Das2Source)
\r
856 Das2Source d2s = (Das2Source) ds;
\r
857 if (d2s.hasDas1Capabilities())
\r
859 Das1Source d1s = DasSourceConverter.toDas1Source(d2s);
\r
860 das1sources.add(d1s);
\r
864 else if (ds instanceof Das1Source)
\r
866 das1sources.add((Das1Source) ds);
\r
870 return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources
\r
872 } catch (Exception ex)
\r
874 ex.printStackTrace();
\r