Fixed to trial registry at the moment
[jalview.git] / src / jalview / io / DasSequenceFeatureFetcher.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\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
9 *\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
14 *\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
18 */\r
19 package jalview.io;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import jalview.gui.*;\r
24 \r
25 import java.util.*;\r
26 \r
27 import java.net.URL;\r
28 \r
29 import org.biojava.dasobert.das.FeatureThread;\r
30 import org.biojava.dasobert.dasregistry.Das1Source;\r
31 import org.biojava.dasobert.eventmodel.FeatureEvent;\r
32 import org.biojava.dasobert.eventmodel.FeatureListener;\r
33 import org.biojava.dasobert.dasregistry.DasSource;\r
34 \r
35 import org.biojava.dasobert.das2.io.DasSourceReaderImpl;\r
36 import org.biojava.dasobert.dasregistry.Das1Source;\r
37 import org.biojava.dasobert.dasregistry.DasSource;\r
38 import org.biojava.dasobert.das2.Das2Source;\r
39 import org.biojava.dasobert.das2.DasSourceConverter;\r
40 \r
41 \r
42 import jalview.bin.Cache;\r
43 \r
44 \r
45 /**\r
46  * DOCUMENT ME!\r
47  *\r
48  * @author $author$\r
49  * @version $Revision$\r
50  */\r
51 public class DasSequenceFeatureFetcher implements Runnable\r
52 {\r
53   final AlignmentI dataset;\r
54   final AlignmentPanel ap;\r
55   StringBuffer sbuffer = new StringBuffer();\r
56 \r
57 \r
58 \r
59   /**\r
60    * Creates a new SequenceFeatureFetcher object.\r
61    *\r
62    * @param align DOCUMENT ME!\r
63    * @param ap DOCUMENT ME!\r
64    */\r
65   public DasSequenceFeatureFetcher(AlignmentI align, AlignmentPanel ap)\r
66   {\r
67     this.dataset = align.getDataset();\r
68     this.ap = ap;\r
69 \r
70     Thread thread = new Thread(this);\r
71     thread.start();\r
72   }\r
73   /**\r
74    * creates a jalview sequence feature from a das feature document\r
75    * @param dasfeature\r
76    * @return sequence feature object created using dasfeature information\r
77    */\r
78   SequenceFeature newSequenceFeature(Map dasfeature, String nickname)\r
79   {\r
80           try {\r
81                 /**\r
82            * Different qNames for a DAS Feature - are string keys to the HashMaps in features\r
83            * "METHOD") ||\r
84            qName.equals("TYPE") ||\r
85            qName.equals("START") ||\r
86            qName.equals("END") ||\r
87            qName.equals("NOTE") ||\r
88            qName.equals("LINK") ||\r
89            qName.equals("SCORE")\r
90            */\r
91                   String desc = new String();\r
92                   if (dasfeature.containsKey("NOTE"))\r
93                                 desc+=(String) dasfeature.get("NOTE");\r
94 \r
95 \r
96                   int start = 0, end = 0;\r
97                   float score = 0f;\r
98 \r
99                   try{ start = Integer.parseInt( dasfeature.get("START").toString()); }\r
100                   catch( Exception ex){}\r
101                   try{ end = Integer.parseInt( dasfeature.get("END").toString()); }\r
102                   catch (Exception ex){}\r
103                   try{ score = Integer.parseInt( dasfeature.get("SCORE").toString()); }\r
104                   catch (Exception ex){}\r
105 \r
106 \r
107                   SequenceFeature f = new SequenceFeature(\r
108                                   (String) dasfeature.get("TYPE"),\r
109                                   desc,\r
110                                   start,\r
111                                   end,\r
112                                   score,\r
113                                   nickname);\r
114 \r
115                   if (dasfeature.containsKey("LINK"))\r
116                  {\r
117                       f.addLink(f.getType()+" "+f.begin+"_"+f.end\r
118                                 +"|"+ dasfeature.get("LINK"));\r
119                   }\r
120                   // (String) dasfeature.get("ID"),\r
121                   ////  (String) dasfeature.get("METHOD"),\r
122                                 //  (String) dasfeature.get("SCORE"),\r
123                                 //  null\r
124                         // );\r
125 \r
126                      // System.out.println(nickname+" "+f.getType()+" "+f.begin+" "+f.end);\r
127           return f;\r
128           }\r
129           catch (Exception e) {\r
130             e.printStackTrace();\r
131                   Cache.log.debug("Failed to parse "+dasfeature.toString(), e);\r
132                   return null;\r
133           }\r
134   }\r
135   /**\r
136    * fetch and add das features to a sequence using the given source URL and Id to create a feature request\r
137    * @param seq\r
138    * @param SourceUrl\r
139    * @param id\r
140    */\r
141   protected void createFeatureFetcher(final Sequence seq,\r
142                                       final String sourceUrl,\r
143                                       String id,\r
144                                       String nickname)  {\r
145           //////////////\r
146           /// fetch DAS features\r
147           final Das1Source source = new Das1Source();\r
148           source.setUrl(sourceUrl);\r
149           source.setNickname(nickname);\r
150 \r
151 \r
152           Cache.log.debug("new Das Feature Fetcher for " + id + " querying " +\r
153                           sourceUrl);\r
154           if (id != null && id.length() > 0)\r
155           {\r
156             FeatureThread fetcher = new FeatureThread(id\r
157                +  ":" + seq.getStart() + "," +seq.getEnd()\r
158                  , source);\r
159 \r
160             fetcher.addFeatureListener(new FeatureListener()\r
161             {\r
162               public void comeBackLater(FeatureEvent e)\r
163               {\r
164                 Cache.log.debug("das source " + e.getDasSource().getNickname() +\r
165                                 " asked us to come back in " + e.getComeBackLater() +\r
166                                 " secs.");\r
167               }\r
168 \r
169               public void newFeatures(FeatureEvent e)\r
170               {\r
171                 Das1Source ds = e.getDasSource();\r
172 \r
173                 Map[] features = e.getFeatures();\r
174                 // add features to sequence\r
175                 Cache.log.debug("das source " + ds.getUrl() + " returned " +\r
176                                 features.length + " features");\r
177 \r
178                 if (features.length > 0)\r
179                 {\r
180                   for (int i = 0; i < features.length; i++)\r
181                   {\r
182                       SequenceFeature f = newSequenceFeature(features[i],\r
183                         source.getNickname());\r
184 \r
185                       seq.addSequenceFeature(f);\r
186                   }\r
187                 }\r
188               }\r
189 \r
190             }\r
191 \r
192             );\r
193 \r
194             //NOTE alignPanel listener will be called after the previous\r
195             //anonymous listener!!!\r
196             fetcher.addFeatureListener(ap);\r
197 \r
198             fetcher.start();\r
199           }\r
200   }\r
201   /**\r
202    * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset\r
203    */\r
204   public void run()\r
205   {\r
206     DasSource [] sources = new jalview.gui.DasSourceBrowser().getDASSource();\r
207 \r
208     String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE", "uniprot");\r
209     StringTokenizer st = new StringTokenizer(active, "\t");\r
210     Vector selectedSources = new Vector();\r
211     String token;\r
212     while (st.hasMoreTokens())\r
213     {\r
214       token = st.nextToken();\r
215       for(int i=0; i<sources.length; i++)\r
216       {\r
217         if(sources[i].getNickname().equals(token))\r
218         {\r
219           selectedSources.addElement(sources[i]);\r
220           break;\r
221         }\r
222       }\r
223     }\r
224 \r
225 \r
226     if(selectedSources == null || selectedSources.size()==0)\r
227     {\r
228       System.out.println("No DAS Sources active");\r
229       return;\r
230     }\r
231 \r
232     try\r
233     {\r
234       int seqIndex = 0;\r
235       Vector sequences = dataset.getSequences();\r
236       while (seqIndex < sequences.size())\r
237       {\r
238           Sequence sequence = (Sequence) sequences.get(seqIndex);\r
239           Vector uprefs = jalview.util.DBRefUtils.selectRefs(sequence.getDBRef(),\r
240               new String[]  {\r
241               jalview.datamodel.DBRefSource.PDB,\r
242               jalview.datamodel.DBRefSource.UNIPROT});\r
243 \r
244           for(int sourceIndex=0; sourceIndex<selectedSources.size(); sourceIndex++)\r
245           {\r
246             DasSource dasSource = (DasSource)selectedSources.elementAt(sourceIndex);\r
247 \r
248             if (uprefs != null)\r
249             {\r
250               // we know the id for this entry, so don't note its ID in the unknownSequences list\r
251               for (int j = 0, k = uprefs.size(); j < k; j++)\r
252               {\r
253                 // Will have to pass any mapping information to the fetcher - the start/end for the DBRefEntry may not be the same as the sequence's start/end\r
254                 org.biojava.dasobert.dasregistry.DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();\r
255                 for (int l=0; l<cs.length; l++) {\r
256                   if (jalview.util.DBRefUtils.isDasCoordinateSystem(cs[l].getName(), (DBRefEntry)\r
257                       uprefs.get(j)))\r
258                   {\r
259                     Cache.log.debug("Launched fetcher for coordinate system " +\r
260                                     cs[l].getName());\r
261                     createFeatureFetcher(sequence,\r
262                                          dasSource.getUrl(),\r
263                                          ( (DBRefEntry) uprefs.get(j)).\r
264                                          getAccessionId(),\r
265                                          dasSource.getNickname());\r
266                   }\r
267                 }\r
268               }\r
269             }\r
270             else\r
271             {\r
272               String id = null;\r
273               // try and use the name as the sequence id\r
274               if (sequence.getName().indexOf("|") > -1)\r
275               {\r
276                 id = sequence.getName().substring(\r
277                     sequence.getName().lastIndexOf("|") + 1);\r
278               }\r
279               else\r
280               {\r
281                 id = sequence.getName();\r
282               }\r
283               if (id != null)\r
284               {\r
285                 // Should try to call a general feature fetcher that queries many sources with name to discover applicable ID references\r
286                 createFeatureFetcher(sequence,\r
287                                      dasSource.getUrl(),\r
288                                      id,\r
289                                      dasSource.getNickname());\r
290               }\r
291             }\r
292           }\r
293 \r
294           seqIndex++;\r
295     }\r
296     }\r
297     catch (Exception ex)\r
298     {\r
299       ex.printStackTrace();\r
300     }\r
301   }\r
302 \r
303 \r
304   public static DasSource[] getDASSources()\r
305   {\r
306     DasSourceReaderImpl reader = new DasSourceReaderImpl();\r
307 \r
308     String registryURL = //jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",\r
309        // "http://servlet.sanger.ac.uk/dasregistry/services/das_registry"\r
310         "http://www.spice-3d.org/dasregistry/das1/sources";\r
311      //  );\r
312 \r
313     try\r
314     {\r
315       URL url = new URL(registryURL);\r
316 \r
317       DasSource[] sources = reader.readDasSource(url);\r
318 \r
319       List das1sources = new ArrayList();\r
320       for (int i = 0; i < sources.length; i++)\r
321       {\r
322         DasSource ds = sources[i];\r
323         if (ds instanceof Das2Source)\r
324         {\r
325           Das2Source d2s = (Das2Source) ds;\r
326           if (d2s.hasDas1Capabilities())\r
327           {\r
328             Das1Source d1s = DasSourceConverter.toDas1Source(d2s);\r
329             das1sources.add(d1s);\r
330           }\r
331 \r
332         }\r
333         else if (ds instanceof Das1Source)\r
334         {\r
335           das1sources.add( (Das1Source) ds);\r
336         }\r
337       }\r
338 \r
339       return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources.size()]);\r
340     }\r
341     catch (Exception ex)\r
342     {\r
343       ex.printStackTrace();\r
344       return null;\r
345     }\r
346 \r
347   }\r
348 }\r
349 \r
350 \r