Jalview 2.6 source licence
[jalview.git] / src / jalview / ws / DasSequenceFeatureFetcher.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)\r
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle\r
4  * \r
5  * This file is part of Jalview.\r
6  * \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
10  * \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
15  * \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
17  */\r
18 package jalview.ws;\r
19 \r
20 import java.net.*;\r
21 import java.util.*;\r
22 \r
23 import javax.swing.*;\r
24 \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
34 \r
35 /**\r
36  * DOCUMENT ME!\r
37  * \r
38  * @author $author$\r
39  * @version $Revision$\r
40  */\r
41 public class DasSequenceFeatureFetcher\r
42 {\r
43   SequenceI[] sequences;\r
44 \r
45   AlignFrame af;\r
46 \r
47   FeatureSettings fsettings;\r
48 \r
49   StringBuffer sbuffer = new StringBuffer();\r
50 \r
51   Vector selectedSources;\r
52 \r
53   boolean cancelled = false;\r
54 \r
55   private void debug(String mesg)\r
56   {\r
57     debug(mesg, null);\r
58   }\r
59 \r
60   private void debug(String mesg, Exception e)\r
61   {\r
62     if (Cache.log != null)\r
63     {\r
64       Cache.log.debug(mesg, e);\r
65     }\r
66     else\r
67     {\r
68       System.err.println(mesg);\r
69       if (e != null)\r
70       {\r
71         e.printStackTrace();\r
72       }\r
73     }\r
74   }\r
75 \r
76   long startTime;\r
77 \r
78   /**\r
79    * Creates a new SequenceFeatureFetcher object. Uses default\r
80    * \r
81    * @param align\r
82    *          DOCUMENT ME!\r
83    * @param ap\r
84    *          DOCUMENT ME!\r
85    */\r
86   public DasSequenceFeatureFetcher(SequenceI[] sequences,\r
87           FeatureSettings fsettings, Vector selectedSources)\r
88   {\r
89     this(sequences, fsettings, selectedSources, true, true);\r
90   }\r
91 \r
92   public DasSequenceFeatureFetcher(SequenceI[] oursequences,\r
93           FeatureSettings fsettings, Vector ourselectedSources,\r
94           boolean checkDbrefs, boolean promptFetchDbrefs)\r
95   {\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
100     {\r
101       Object src = sources.nextElement();\r
102       if (!selectedSources.contains(src))\r
103       {\r
104         selectedSources.addElement(src);\r
105       }\r
106       ;\r
107     }\r
108     Vector sqs = new Vector();\r
109     for (int i = 0; i < oursequences.length; i++)\r
110     {\r
111       if (!sqs.contains(oursequences[i]))\r
112       {\r
113         sqs.addElement(oursequences[i]);\r
114       }\r
115     }\r
116     sequences = new SequenceI[sqs.size()];\r
117     for (int i = 0; i < sequences.length; i++)\r
118     {\r
119       sequences[i] = (SequenceI) sqs.elementAt(i);\r
120     }\r
121     if (fsettings != null)\r
122     {\r
123       this.fsettings = fsettings;\r
124       this.af = fsettings.af;\r
125       af.setShowSeqFeatures(true);\r
126     }\r
127     int uniprotCount = 0;\r
128     for (int i = 0; i < selectedSources.size(); i++)\r
129     {\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
133       {\r
134         // TODO: match UniProt coord system canonically (?) - does\r
135         // UniProt==uniprot==UNIPROT ?\r
136         if (coords[c].getName().indexOf("UniProt") > -1)\r
137         {\r
138           uniprotCount++;\r
139           break;\r
140         }\r
141       }\r
142     }\r
143 \r
144     int refCount = 0;\r
145     for (int i = 0; i < sequences.length; i++)\r
146     {\r
147       DBRefEntry[] dbref = sequences[i].getDBRef();\r
148       if (dbref != null)\r
149       {\r
150         for (int j = 0; j < dbref.length; j++)\r
151         {\r
152           if (dbref[j].getSource().equals(\r
153                   jalview.datamodel.DBRefSource.UNIPROT))\r
154           {\r
155             refCount++;\r
156             break;\r
157           }\r
158         }\r
159       }\r
160     }\r
161 \r
162     if (checkDbrefs && refCount < sequences.length && uniprotCount > 0)\r
163     {\r
164 \r
165       int reply = JOptionPane.YES_OPTION;\r
166       if (promptFetchDbrefs)\r
167       {\r
168         reply = JOptionPane\r
169                 .showInternalConfirmDialog(\r
170                         Desktop.desktop,\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
176       }\r
177 \r
178       if (reply == JOptionPane.YES_OPTION)\r
179       {\r
180         Thread thread = new Thread(new FetchDBRefs());\r
181         thread.start();\r
182       }\r
183       else\r
184       {\r
185         startFetching();\r
186       }\r
187     }\r
188     else\r
189     {\r
190       startFetching();\r
191     }\r
192 \r
193   }\r
194 \r
195   class FetchDBRefs implements Runnable\r
196   {\r
197     public void run()\r
198     {\r
199       new DBRefFetcher(sequences, af).fetchDBRefs(true);\r
200       startFetching();\r
201     }\r
202   }\r
203 \r
204   /**\r
205    * Spawns a number of dasobert Fetcher threads to add features to sequences in\r
206    * the dataset\r
207    */\r
208   void startFetching()\r
209   {\r
210     cancelled = false;\r
211     startTime = System.currentTimeMillis();\r
212     if (af != null)\r
213     {\r
214       af.setProgressBar("Fetching DAS Sequence Features", startTime);\r
215     }\r
216 \r
217     if (selectedSources == null || selectedSources.size() == 0)\r
218     {\r
219       try\r
220       {\r
221         DasSource[] sources = new jalview.gui.DasSourceBrowser()\r
222                 .getDASSource();\r
223 \r
224         String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",\r
225                 "uniprot");\r
226         StringTokenizer st = new StringTokenizer(active, "\t");\r
227         selectedSources = new Vector();\r
228         String token;\r
229         while (st.hasMoreTokens())\r
230         {\r
231           token = st.nextToken();\r
232           for (int i = 0; i < sources.length; i++)\r
233           {\r
234             if (sources[i].getNickname().equals(token))\r
235             {\r
236               selectedSources.addElement(sources[i]);\r
237               break;\r
238             }\r
239           }\r
240         }\r
241       } catch (Exception ex)\r
242       {\r
243         debug(\r
244                 "Exception whilst setting default feature sources from registry and local preferences.",\r
245                 ex);\r
246       }\r
247     }\r
248 \r
249     if (selectedSources == null || selectedSources.size() == 0)\r
250     {\r
251       System.out.println("No DAS Sources active");\r
252       cancelled = true;\r
253       setGuiNoDassourceActive();\r
254       return;\r
255     }\r
256 \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
261     {\r
262       DasSource dasSource = (DasSource) selectedSources\r
263               .elementAt(sourceIndex);\r
264 \r
265       nextSequence(dasSource, sequences[0]);\r
266     }\r
267   }\r
268 \r
269   private void setGuiNoDassourceActive()\r
270   {\r
271 \r
272     if (af != null)\r
273     {\r
274       af.setProgressBar("No DAS Sources Active", startTime);\r
275     }\r
276     if (getFeatSettings() != null)\r
277     {\r
278       fsettings.noDasSourceActive();\r
279     }\r
280   }\r
281 \r
282   /**\r
283    * Update our fsettings dialog reference if we didn't have one when we were\r
284    * first initialised.\r
285    * \r
286    * @return fsettings\r
287    */\r
288   private FeatureSettings getFeatSettings()\r
289   {\r
290     if (fsettings == null)\r
291     {\r
292       if (af != null)\r
293       {\r
294         fsettings = af.featureSettings;\r
295       }\r
296     }\r
297     return fsettings;\r
298   }\r
299 \r
300   public void cancel()\r
301   {\r
302     if (af != null)\r
303     {\r
304       af.setProgressBar("DAS Feature Fetching Cancelled", startTime);\r
305     }\r
306     cancelled = true;\r
307   }\r
308 \r
309   int sourcesRemaining = 0;\r
310 \r
311   void responseComplete(DasSource dasSource, SequenceI seq)\r
312   {\r
313     if (seq != null)\r
314     {\r
315       for (int seqIndex = 0; seqIndex < sequences.length - 1 && !cancelled; seqIndex++)\r
316       {\r
317         if (sequences[seqIndex] == seq)\r
318         {\r
319           nextSequence(dasSource, sequences[++seqIndex]);\r
320           return;\r
321         }\r
322       }\r
323     }\r
324 \r
325     sourcesRemaining--;\r
326 \r
327     if (sourcesRemaining == 0)\r
328     {\r
329       System.err.println("Fetching Complete.");\r
330       setGuiFetchComplete();\r
331     }\r
332 \r
333   }\r
334 \r
335   private void setGuiFetchComplete()\r
336   {\r
337 \r
338     if (!cancelled && af != null)\r
339     {\r
340       // only update the progress bar if we've completed the fetch normally\r
341       af.setProgressBar("DAS Feature Fetching Complete", startTime);\r
342     }\r
343 \r
344     if (af != null && af.featureSettings != null)\r
345     {\r
346       af.featureSettings.setTableData();\r
347     }\r
348 \r
349     if (getFeatSettings() != null)\r
350     {\r
351       fsettings.complete();\r
352     }\r
353   }\r
354 \r
355   void featuresAdded(SequenceI seq)\r
356   {\r
357     if (af == null)\r
358     {\r
359       // no gui to update with features.\r
360       return;\r
361     }\r
362     af.getFeatureRenderer().featuresAdded();\r
363 \r
364     int start = af.getViewport().getStartSeq();\r
365     int end = af.getViewport().getEndSeq();\r
366     int index;\r
367     for (index = start; index < end; index++)\r
368     {\r
369       if (seq == af.getViewport().getAlignment().getSequenceAt(index)\r
370               .getDatasetSequence())\r
371       {\r
372         af.alignPanel.paintAlignment(true);\r
373         break;\r
374       }\r
375     }\r
376   }\r
377 \r
378   void nextSequence(DasSource dasSource, SequenceI seq)\r
379   {\r
380     if (cancelled)\r
381       return;\r
382     DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(\r
383             seq.getDBRef(), new String[]\r
384             {\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
388             // sys sources\r
389             });\r
390     // TODO: minimal list of DAS queries to make by querying with untyped ID if\r
391     // distinct from any typed IDs\r
392 \r
393     boolean dasCoordSysFound = false;\r
394 \r
395     if (uprefs != null)\r
396     {\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
399       {\r
400         DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();\r
401 \r
402         for (int csIndex = 0; csIndex < cs.length && !dasCoordSysFound; csIndex++)\r
403         {\r
404           if (cs.length > 0\r
405                   && jalview.util.DBRefUtils.isDasCoordinateSystem(\r
406                           cs[csIndex].getName(), uprefs[j]))\r
407           {\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
413 \r
414             System.out.println(seq.getName() + " "\r
415                     + (seq.getDatasetSequence() == null) + " "\r
416                     + dasSource.getUrl());\r
417 \r
418             dasCoordSysFound = true; // break's out of the loop\r
419             createFeatureFetcher(seq, dasSource, uprefs[j]);\r
420           }\r
421           else\r
422             System.out.println("IGNORE " + cs[csIndex].getName());\r
423         }\r
424       }\r
425     }\r
426 \r
427     if (!dasCoordSysFound)\r
428     {\r
429       String id = null;\r
430       // try and use the name as the sequence id\r
431       if (seq.getName().indexOf("|") > -1)\r
432       {\r
433         id = seq.getName().substring(seq.getName().lastIndexOf("|") + 1);\r
434         if (id.trim().length() < 4)\r
435         {\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
440           {\r
441             id = id.substring(id.lastIndexOf("|") + 1);\r
442           }\r
443         }\r
444       }\r
445       else\r
446       {\r
447         id = seq.getName();\r
448       }\r
449       if (id != null)\r
450       {\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
454       }\r
455     }\r
456 \r
457   }\r
458 \r
459   /**\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
463    * \r
464    * @param seq\r
465    * @param SourceUrl\r
466    * @param dbref\r
467    */\r
468   protected void createFeatureFetcher(final SequenceI seq,\r
469           final DasSource dasSource, final DBRefEntry dbref)\r
470   {\r
471 \r
472     // ////////////\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
479     {\r
480       responseComplete(dasSource, seq); // reduce thread count anyhow\r
481       return;\r
482     }\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
487             , source);\r
488 \r
489     fetcher.addFeatureListener(new FeatureListener()\r
490     {\r
491       public void comeBackLater(FeatureEvent e)\r
492       {\r
493         responseComplete(dasSource, seq);\r
494         debug("das source " + e.getSource().getNickname()\r
495                 + " asked us to come back in " + e.getComeBackLater()\r
496                 + " secs.");\r
497       }\r
498 \r
499       public void newFeatures(FeatureEvent e)\r
500       {\r
501 \r
502         Das1Source ds = e.getSource();\r
503 \r
504         Map[] features = e.getFeatures();\r
505         // add features to sequence\r
506         debug("das source " + ds.getUrl() + " returned " + features.length\r
507                 + " features");\r
508 \r
509         if (features.length > 0)\r
510         {\r
511           for (int i = 0; i < features.length; i++)\r
512           {\r
513             // standard DAS feature-> jalview sequence feature transformation\r
514             SequenceFeature f = newSequenceFeature(features[i], source\r
515                     .getNickname());\r
516             if (!parseSeqFeature(seq, f, features[i], source))\r
517             {\r
518               if (dbref.getMap() != null && f.getBegin() > 0\r
519                       && f.getEnd() > 0)\r
520               {\r
521                 debug("mapping from " + f.getBegin() + " - " + f.getEnd());\r
522                 SequenceFeature vf[] = null;\r
523 \r
524                 try\r
525                 {\r
526                   vf = dbref.getMap().locateFeature(f);\r
527                 } catch (Exception ex)\r
528                 {\r
529                   Cache.log\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
537                 }\r
538 \r
539                 if (vf != null)\r
540                 {\r
541                   for (int v = 0; v < vf.length; v++)\r
542                   {\r
543                     debug("mapping to " + v + ": " + vf[v].getBegin()\r
544                             + " - " + vf[v].getEnd());\r
545                     seq.addSequenceFeature(vf[v]);\r
546                   }\r
547                 }\r
548               }\r
549               else\r
550               {\r
551                 seq.addSequenceFeature(f);\r
552               }\r
553             }\r
554           }\r
555           featuresAdded(seq);\r
556         }\r
557         else\r
558         {\r
559           // System.out.println("No features found for " + seq.getName()\r
560           // + " from: " + e.getDasSource().getNickname());\r
561         }\r
562         responseComplete(dasSource, seq);\r
563 \r
564       }\r
565     }\r
566 \r
567     );\r
568 \r
569     fetcher.start();\r
570   }\r
571 \r
572   protected void createFeatureFetcher(final SequenceI seq,\r
573           final DasSource dasSource, String id)\r
574   {\r
575     // ////////////\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
580 \r
581     if (id != null)\r
582     {\r
583       id = id.trim();\r
584     }\r
585     if (id != null && id.length() > 0)\r
586     {\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
591               , source);\r
592 \r
593       fetcher.addFeatureListener(new FeatureListener()\r
594       {\r
595         public void comeBackLater(FeatureEvent e)\r
596         {\r
597           responseComplete(dasSource, seq);\r
598           debug("das source " + e.getSource().getNickname()\r
599                   + " asked us to come back in " + e.getComeBackLater()\r
600                   + " secs.");\r
601         }\r
602 \r
603         public void newFeatures(FeatureEvent e)\r
604         {\r
605 \r
606           Das1Source ds = e.getSource();\r
607 \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
612 \r
613           if (features.length > 0)\r
614           {\r
615             for (int i = 0; i < features.length; i++)\r
616             {\r
617               // standard DAS feature-> jalview sequence feature transformation\r
618               SequenceFeature f = newSequenceFeature(features[i], source\r
619                       .getNickname());\r
620               if (!parseSeqFeature(seq, f, features[i], source))\r
621               {\r
622                 // just add as a simple sequence feature\r
623                 seq.addSequenceFeature(f);\r
624               }\r
625             }\r
626 \r
627             featuresAdded(seq);\r
628           }\r
629           else\r
630           {\r
631             // System.out.println("No features found for " + seq.getName()\r
632             // + " from: " + e.getDasSource().getNickname());\r
633           }\r
634           responseComplete(dasSource, seq);\r
635 \r
636         }\r
637       }\r
638 \r
639       );\r
640 \r
641       fetcher.start();\r
642     }\r
643     else\r
644     {\r
645       // invalid fetch - indicate it is finished.\r
646       debug("Skipping empty ID for querying " + dasSource.getUrl());\r
647       responseComplete(dasSource, seq);\r
648     }\r
649   }\r
650 \r
651   /**\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
655    * \r
656    * @param seq\r
657    *          the sequence to annotate\r
658    * @param f\r
659    *          the jalview sequence feature generated from the DAS feature\r
660    * @param map\r
661    *          the sequence feature attributes\r
662    * @param source\r
663    *          the source that emitted the feature\r
664    * @return true if feature was consumed as another kind of annotation.\r
665    */\r
666   protected boolean parseSeqFeature(SequenceI seq, SequenceFeature f,\r
667           Map map, Das1Source source)\r
668   {\r
669     SequenceI mseq = seq;\r
670     while (seq.getDatasetSequence() != null)\r
671     {\r
672       seq = seq.getDatasetSequence();\r
673     }\r
674     if (f.getType() != null)\r
675     {\r
676       String type = f.getType();\r
677       if (type.equalsIgnoreCase("protein_name"))\r
678       {\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
682         {\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
686         }\r
687         if (mseq.getDescription() == null\r
688                 || mseq.getDescription().trim().length() == 0)\r
689         {\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
693         }\r
694         return true;\r
695       }\r
696       // check if source has biosapiens or other sequence ontology label\r
697       if (type.equalsIgnoreCase("DBXREF") || type.equalsIgnoreCase("DBREF"))\r
698       {\r
699         // try to parse the accession out\r
700 \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
705         {\r
706           dbr.setSource(st.nextToken());\r
707         }\r
708         if (st.hasMoreTokens())\r
709         {\r
710           dbr.setAccessionId(st.nextToken());\r
711         }\r
712         seq.addDBRef(dbr);\r
713 \r
714         if (f.links != null && f.links.size() > 0)\r
715         {\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
720           // preferences ?\r
721           // for the moment - just fix up the existing feature so it displays\r
722           // correctly.\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
730           {\r
731             String elm;\r
732             UrlLink urllink = new UrlLink(elm = (String) it.nextElement());\r
733             if (urllink.isValid())\r
734             {\r
735               urllink.setLabel(f.getDescription());\r
736               newlinks.addElement(urllink.toString());\r
737             }\r
738             else\r
739             {\r
740               // couldn't parse the link properly. Keep it anyway - just in\r
741               // case.\r
742               debug("couldn't parse link string - " + elm);\r
743               newlinks.addElement(elm);\r
744             }\r
745           }\r
746           f.links = newlinks;\r
747           seq.addSequenceFeature(f);\r
748         }\r
749         return true;\r
750       }\r
751     }\r
752     return false;\r
753   }\r
754 \r
755   /**\r
756    * creates a jalview sequence feature from a das feature document\r
757    * \r
758    * @param dasfeature\r
759    * @return sequence feature object created using dasfeature information\r
760    */\r
761   SequenceFeature newSequenceFeature(Map dasfeature, String nickname)\r
762   {\r
763     if (dasfeature == null)\r
764     {\r
765       return null;\r
766     }\r
767     try\r
768     {\r
769       /**\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
774        */\r
775       String desc = new String();\r
776       if (dasfeature.containsKey("NOTE"))\r
777       {\r
778         desc += (String) dasfeature.get("NOTE");\r
779       }\r
780 \r
781       int start = 0, end = 0;\r
782       float score = 0f;\r
783 \r
784       try\r
785       {\r
786         start = Integer.parseInt(dasfeature.get("START").toString());\r
787       } catch (Exception ex)\r
788       {\r
789       }\r
790       try\r
791       {\r
792         end = Integer.parseInt(dasfeature.get("END").toString());\r
793       } catch (Exception ex)\r
794       {\r
795       }\r
796       try\r
797       {\r
798         Object scr = dasfeature.get("SCORE");\r
799         if (scr != null)\r
800         {\r
801           score = (float) Double.parseDouble(scr.toString());\r
802 \r
803         }\r
804       } catch (Exception ex)\r
805       {\r
806       }\r
807 \r
808       SequenceFeature f = new SequenceFeature((String) dasfeature\r
809               .get("TYPE"), desc, start, end, score, nickname);\r
810 \r
811       if (dasfeature.containsKey("LINK"))\r
812       {\r
813         // Do not put feature extent in link text for non-positional features\r
814         if (f.begin == 0 && f.end == 0)\r
815         {\r
816           f.addLink(f.getType() + "|" + dasfeature.get("LINK"));\r
817         }\r
818         else\r
819         {\r
820           f.addLink(f.getType() + " " + f.begin + "_" + f.end + "|"\r
821                   + dasfeature.get("LINK"));\r
822         }\r
823       }\r
824 \r
825       return f;\r
826     } catch (Exception e)\r
827     {\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
832       return null;\r
833     }\r
834   }\r
835 \r
836   /**\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
839    * doesn't exist.\r
840    * \r
841    * @return list of sources\r
842    */\r
843   public static DasSource[] getDASSources()\r
844   {\r
845 \r
846     String registryURL = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",\r
847             DasSourceBrowser.DEFAULT_REGISTRY);\r
848     return getDASSources(registryURL);\r
849   }\r
850 \r
851   /**\r
852    * query the given URL for DasSources.\r
853    * \r
854    * @param registryURL\r
855    *          return sources from registryURL\r
856    */\r
857   public static DasSource[] getDASSources(String registryURL)\r
858   {\r
859     DasSourceReaderImpl reader = new DasSourceReaderImpl();\r
860 \r
861     try\r
862     {\r
863       URL url = new URL(registryURL);\r
864 \r
865       DasSource[] sources = reader.readDasSource(url);\r
866 \r
867       List das1sources = new ArrayList();\r
868       for (int i = 0; i < sources.length; i++)\r
869       {\r
870         DasSource ds = sources[i];\r
871         if (ds instanceof Das2Source)\r
872         {\r
873           Das2Source d2s = (Das2Source) ds;\r
874           if (d2s.hasDas1Capabilities())\r
875           {\r
876             Das1Source d1s = DasSourceConverter.toDas1Source(d2s);\r
877             das1sources.add(d1s);\r
878           }\r
879 \r
880         }\r
881         else if (ds instanceof Das1Source)\r
882         {\r
883           das1sources.add((Das1Source) ds);\r
884         }\r
885       }\r
886 \r
887       return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources\r
888               .size()]);\r
889     } catch (Exception ex)\r
890     {\r
891       System.err.println("Failed to contact DAS1 registry at "\r
892               + registryURL);\r
893       ex.printStackTrace();\r
894       return null;\r
895     }\r
896   }\r
897 \r
898 }\r