Check for all gapped seqs
[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 import org.biojava.dasobert.dasregistry.DasCoordinateSystem;\r
44 \r
45 import javax.swing.*;\r
46 \r
47 /**\r
48  * DOCUMENT ME!\r
49  *\r
50  * @author $author$\r
51  * @version $Revision$\r
52  */\r
53 public class DasSequenceFeatureFetcher implements Runnable\r
54 {\r
55   SequenceI [] sequences;\r
56   AlignFrame af;\r
57   StringBuffer sbuffer = new StringBuffer();\r
58   Vector selectedSources;\r
59 \r
60   long startTime;\r
61   int threadsRunning = 0;\r
62 \r
63 \r
64   /**\r
65    * Creates a new SequenceFeatureFetcher object.\r
66    * Uses default\r
67    *\r
68    * @param align DOCUMENT ME!\r
69    * @param ap DOCUMENT ME!\r
70    */\r
71   public DasSequenceFeatureFetcher(SequenceI [] sequences,\r
72                                    final AlignFrame af,\r
73                                    Vector selectedSources)\r
74   {\r
75     this.selectedSources = selectedSources;\r
76     this.sequences = sequences;\r
77     this.af = af;\r
78 \r
79     int uniprotCount = 0;\r
80     for (int i = 0; i < selectedSources.size(); i++)\r
81     {\r
82       DasSource source = (DasSource) selectedSources.elementAt(i);\r
83       DasCoordinateSystem[] coords = source.getCoordinateSystem();\r
84       for (int c = 0; c < coords.length; c++)\r
85       {\r
86         if (coords[c].getName().equalsIgnoreCase("UniProt"))\r
87         {\r
88           uniprotCount++;\r
89           break;\r
90         }\r
91       }\r
92     }\r
93 \r
94     int refCount = 0;\r
95     for(int i=0; i<sequences.length; i++)\r
96     {\r
97       Vector dbref = sequences[i].getDBRef();\r
98       if(dbref!=null)\r
99       {\r
100         for(int j=0; j<dbref.size(); j++)\r
101         {\r
102           System.out.println(dbref.elementAt(j)\r
103                              +" "+jalview.datamodel.DBRefSource.UNIPROT);\r
104           if(dbref.elementAt(j)\r
105              .equals(jalview.datamodel.DBRefSource.UNIPROT))\r
106           {\r
107             System.out.println("got a match");\r
108             refCount++;\r
109             break;\r
110           }\r
111         }\r
112       }\r
113     }\r
114 \r
115    System.out.println(refCount+" "+uniprotCount+" "+sequences.length);\r
116     if(refCount<sequences.length && uniprotCount>0)\r
117     {\r
118 \r
119      int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,\r
120           "Do you want Jalview to find\n"\r
121          +"Uniprot Accession ids for given sequence names?",\r
122          "Find Uniprot Accession Ids",\r
123          JOptionPane.YES_NO_OPTION,\r
124          JOptionPane.QUESTION_MESSAGE);\r
125 \r
126      if(reply == JOptionPane.YES_OPTION)\r
127      {\r
128        new DBRefFetcher(\r
129              af.getViewport().getAlignment(), af).fetchDBRefs(true);\r
130 \r
131      }\r
132     }\r
133 \r
134    System.out.println("User selection is "\r
135                       +\r
136                        ( ( (float) uniprotCount / (float) selectedSources.size()) * 100)\r
137                       + " % Uniprot,  and "+refCount+" / " +sequences.length+" have uniprot accession");\r
138 \r
139 \r
140     Thread thread = new Thread(this);\r
141     thread.start();\r
142   }\r
143   /**\r
144    * creates a jalview sequence feature from a das feature document\r
145    * @param dasfeature\r
146    * @return sequence feature object created using dasfeature information\r
147    */\r
148   SequenceFeature newSequenceFeature(Map dasfeature, String nickname)\r
149   {\r
150           try {\r
151                 /**\r
152            * Different qNames for a DAS Feature - are string keys to the HashMaps in features\r
153            * "METHOD") ||\r
154            qName.equals("TYPE") ||\r
155            qName.equals("START") ||\r
156            qName.equals("END") ||\r
157            qName.equals("NOTE") ||\r
158            qName.equals("LINK") ||\r
159            qName.equals("SCORE")\r
160            */\r
161                   String desc = new String();\r
162                   if (dasfeature.containsKey("NOTE"))\r
163                                 desc+=(String) dasfeature.get("NOTE");\r
164 \r
165 \r
166                   int start = 0, end = 0;\r
167                   float score = 0f;\r
168 \r
169                   try{ start = Integer.parseInt( dasfeature.get("START").toString()); }\r
170                   catch( Exception ex){}\r
171                   try{ end = Integer.parseInt( dasfeature.get("END").toString()); }\r
172                   catch (Exception ex){}\r
173                   try{ score = Integer.parseInt( dasfeature.get("SCORE").toString()); }\r
174                   catch (Exception ex){}\r
175 \r
176 \r
177                   SequenceFeature f = new SequenceFeature(\r
178                                   (String) dasfeature.get("TYPE"),\r
179                                   desc,\r
180                                   start,\r
181                                   end,\r
182                                   score,\r
183                                   nickname);\r
184 \r
185                   if (dasfeature.containsKey("LINK"))\r
186                  {\r
187                       f.addLink(f.getType()+" "+f.begin+"_"+f.end\r
188                                 +"|"+ dasfeature.get("LINK"));\r
189                   }\r
190                   // (String) dasfeature.get("ID"),\r
191                   ////  (String) dasfeature.get("METHOD"),\r
192                                 //  (String) dasfeature.get("SCORE"),\r
193                                 //  null\r
194                         // );\r
195 \r
196                      // System.out.println(nickname+" "+f.getType()+" "+f.begin+" "+f.end);\r
197           return f;\r
198           }\r
199           catch (Exception e) {\r
200             e.printStackTrace();\r
201                   Cache.log.debug("Failed to parse "+dasfeature.toString(), e);\r
202                   return null;\r
203           }\r
204   }\r
205   /**\r
206    * fetch and add das features to a sequence using the given source URL and Id to create a feature request\r
207    * @param seq\r
208    * @param SourceUrl\r
209    * @param id\r
210    */\r
211   protected void createFeatureFetcher(final SequenceI seq,\r
212                                       final String sourceUrl,\r
213                                       String id,\r
214                                       String nickname)\r
215   {\r
216           //////////////\r
217           /// fetch DAS features\r
218           final Das1Source source = new Das1Source();\r
219           source.setUrl(sourceUrl);\r
220           source.setNickname(nickname);\r
221 \r
222 \r
223           Cache.log.debug("new Das Feature Fetcher for " + id + " querying " +\r
224                           sourceUrl);\r
225 \r
226           if (id != null && id.length() > 0)\r
227           {\r
228             setThreadsRunning(+1);\r
229 \r
230             int start=seq.getStart(), end = seq.getEnd();\r
231 \r
232             if(af.getViewport().getSelectionGroup()!=null)\r
233             {\r
234               SequenceI tmp = af.getViewport().getAlignment().findName(seq.getName());\r
235               start = tmp.findPosition(\r
236               af.getViewport().getSelectionGroup().getStartRes()\r
237                   );\r
238 \r
239               end = tmp.findPosition(\r
240                  af.getViewport().getSelectionGroup().getEndRes()\r
241                  );\r
242             }\r
243 \r
244             FeatureThread fetcher = new FeatureThread(id\r
245                +  ":" + start + "," + end, source);\r
246 \r
247             fetcher.addFeatureListener(new FeatureListener()\r
248             {\r
249               public void comeBackLater(FeatureEvent e)\r
250               {\r
251                 setThreadsRunning(-1);\r
252                 Cache.log.debug("das source " + e.getDasSource().getNickname() +\r
253                                 " asked us to come back in " + e.getComeBackLater() +\r
254                                 " secs.");\r
255               }\r
256 \r
257               public void newFeatures(FeatureEvent e)\r
258               {\r
259                 setThreadsRunning(-1);\r
260                 Das1Source ds = e.getDasSource();\r
261 \r
262                 Map[] features = e.getFeatures();\r
263                 // add features to sequence\r
264                 Cache.log.debug("das source " + ds.getUrl() + " returned " +\r
265                                 features.length + " features");\r
266 \r
267                 if (features.length > 0)\r
268                 {\r
269                   for (int i = 0; i < features.length; i++)\r
270                   {\r
271                       SequenceFeature f = newSequenceFeature(features[i],\r
272                         source.getNickname());\r
273 \r
274                       seq.addSequenceFeature(f);\r
275                   }\r
276                 }\r
277                 else\r
278                 {\r
279                   System.out.println("No features found for sequence " +seq.getName()\r
280                                      +" from: "+ e.getDasSource().getNickname());\r
281                 }\r
282 \r
283               }\r
284             }\r
285 \r
286             );\r
287 \r
288             //NOTE alignPanel listener will be called after the previous\r
289             //anonymous listener!!!\r
290             fetcher.addFeatureListener(af);\r
291 \r
292             fetcher.start();\r
293           }\r
294   }\r
295 \r
296   synchronized void setThreadsRunning(int i)\r
297   {\r
298     threadsRunning += i;\r
299     if(threadsRunning<1)\r
300       af.setProgressBar("DAS Feature Fetching Complete", startTime);\r
301   }\r
302 \r
303   /**\r
304    * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset\r
305    */\r
306   public void run()\r
307   {\r
308     startTime = System.currentTimeMillis();\r
309     af.setProgressBar("Fetching DAS Sequence Features", startTime);\r
310 \r
311     DasSource [] sources = new jalview.gui.DasSourceBrowser().getDASSource();\r
312 \r
313     if(selectedSources==null || selectedSources.size()==0)\r
314     {\r
315       String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE", "uniprot");\r
316       StringTokenizer st = new StringTokenizer(active, "\t");\r
317       Vector selectedSources = new Vector();\r
318       String token;\r
319       while (st.hasMoreTokens())\r
320       {\r
321         token = st.nextToken();\r
322         for (int i = 0; i < sources.length; i++)\r
323         {\r
324           if (sources[i].getNickname().equals(token))\r
325           {\r
326             selectedSources.addElement(sources[i]);\r
327             break;\r
328           }\r
329         }\r
330       }\r
331     }\r
332 \r
333     if(selectedSources == null || selectedSources.size()==0)\r
334     {\r
335       System.out.println("No DAS Sources active");\r
336       af.setProgressBar("No DAS Sources Active", startTime);\r
337       return;\r
338     }\r
339 \r
340     try\r
341     {\r
342       int seqIndex = 0;\r
343       while (seqIndex < sequences.length)\r
344       {\r
345           Vector uprefs = jalview.util.DBRefUtils.selectRefs(sequences[seqIndex].getDBRef(),\r
346               new String[]  {\r
347               jalview.datamodel.DBRefSource.PDB,\r
348               jalview.datamodel.DBRefSource.UNIPROT});\r
349 \r
350           for(int sourceIndex=0; sourceIndex<selectedSources.size(); sourceIndex++)\r
351           {\r
352             DasSource dasSource = (DasSource)selectedSources.elementAt(sourceIndex);\r
353 \r
354             if (uprefs != null)\r
355             {\r
356               // we know the id for this entry, so don't note its ID in the unknownSequences list\r
357               for (int j = 0;  j < uprefs.size(); j++)\r
358               {\r
359 \r
360                 // 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
361                 org.biojava.dasobert.dasregistry.DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();\r
362                 for (int l=0; l<cs.length; l++)\r
363                 {\r
364                   if (jalview.util.DBRefUtils.isDasCoordinateSystem(cs[l].getName(), (DBRefEntry)\r
365                       uprefs.get(j)))\r
366                   {\r
367                     Cache.log.debug("Launched fetcher for coordinate system " +\r
368                                     cs[l].getName());\r
369 \r
370 \r
371                     createFeatureFetcher(sequences[seqIndex],\r
372                                          dasSource.getUrl(),\r
373                                          ( (DBRefEntry) uprefs.get(j)).\r
374                                          getAccessionId(),\r
375                                          dasSource.getNickname());\r
376                   }\r
377                 }\r
378               }\r
379             }\r
380             else\r
381             {\r
382               String id = null;\r
383               // try and use the name as the sequence id\r
384               if (sequences[seqIndex].getName().indexOf("|") > -1)\r
385               {\r
386                 id = sequences[seqIndex].getName().substring(\r
387                     sequences[seqIndex].getName().lastIndexOf("|") + 1);\r
388               }\r
389               else\r
390               {\r
391                 id = sequences[seqIndex].getName();\r
392               }\r
393               if (id != null)\r
394               {\r
395                 // Should try to call a general feature fetcher that queries many sources with name to discover applicable ID references\r
396                 createFeatureFetcher(sequences[seqIndex],\r
397                                      dasSource.getUrl(),\r
398                                      id,\r
399                                      dasSource.getNickname());\r
400               }\r
401             }\r
402           }\r
403 \r
404           seqIndex++;\r
405     }\r
406     }\r
407     catch (Exception ex)\r
408     {\r
409       ex.printStackTrace();\r
410     }\r
411   }\r
412 \r
413 \r
414   public static DasSource[] getDASSources()\r
415   {\r
416     DasSourceReaderImpl reader = new DasSourceReaderImpl();\r
417 \r
418     String registryURL =jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",\r
419         "http://das.sanger.ac.uk/registry/das1/sources/"\r
420       );\r
421 \r
422     try\r
423     {\r
424       URL url = new URL(registryURL);\r
425 \r
426       DasSource[] sources = reader.readDasSource(url);\r
427 \r
428       List das1sources = new ArrayList();\r
429       for (int i = 0; i < sources.length; i++)\r
430       {\r
431         DasSource ds = sources[i];\r
432         if (ds instanceof Das2Source)\r
433         {\r
434           Das2Source d2s = (Das2Source) ds;\r
435           if (d2s.hasDas1Capabilities())\r
436           {\r
437             Das1Source d1s = DasSourceConverter.toDas1Source(d2s);\r
438             das1sources.add(d1s);\r
439           }\r
440 \r
441         }\r
442         else if (ds instanceof Das1Source)\r
443         {\r
444           das1sources.add( (Das1Source) ds);\r
445         }\r
446       }\r
447 \r
448       return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources.size()]);\r
449     }\r
450     catch (Exception ex)\r
451     {\r
452       ex.printStackTrace();\r
453       return null;\r
454     }\r
455 \r
456   }\r
457 }\r
458 \r
459 \r