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