2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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
21 import jalview.datamodel.*;
\r
23 import jalview.gui.*;
\r
27 import java.net.URL;
\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
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
42 import jalview.bin.Cache;
\r
43 import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
\r
45 import javax.swing.*;
\r
51 * @version $Revision$
\r
53 public class DasSequenceFeatureFetcher implements Runnable
\r
55 SequenceI [] sequences;
\r
57 StringBuffer sbuffer = new StringBuffer();
\r
58 Vector selectedSources;
\r
61 int threadsRunning = 0;
\r
65 * Creates a new SequenceFeatureFetcher object.
\r
68 * @param align DOCUMENT ME!
\r
69 * @param ap DOCUMENT ME!
\r
71 public DasSequenceFeatureFetcher(SequenceI [] sequences,
\r
72 final AlignFrame af,
\r
73 Vector selectedSources)
\r
75 this.selectedSources = selectedSources;
\r
76 this.sequences = sequences;
\r
79 int uniprotCount = 0;
\r
80 for (int i = 0; i < selectedSources.size(); i++)
\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
86 if (coords[c].getName().equalsIgnoreCase("UniProt"))
\r
95 for(int i=0; i<sequences.length; i++)
\r
97 if(sequences[i].getDBRef()!=null)
\r
99 for(int j=0; j<sequences[i].getDBRef().size(); j++)
\r
101 System.out.println(sequences[i].getDBRef().elementAt(j)
\r
102 +" "+jalview.datamodel.DBRefSource.UNIPROT);
\r
103 if(sequences[i].getDBRef().elementAt(j)
\r
104 .equals(jalview.datamodel.DBRefSource.UNIPROT))
\r
106 System.out.println("got a match");
\r
114 System.out.println(refCount+" "+uniprotCount+" "+sequences.length);
\r
115 if(refCount<sequences.length && uniprotCount>0)
\r
118 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
\r
119 "Do you want Jalview to find\n"
\r
120 +"Uniprot Accession ids for given sequence names?",
\r
121 "Find Uniprot Accession Ids",
\r
122 JOptionPane.YES_NO_OPTION,
\r
123 JOptionPane.QUESTION_MESSAGE);
\r
125 if(reply == JOptionPane.YES_OPTION)
\r
128 af.getViewport().getAlignment(), af).fetchDBRefs(true);
\r
133 System.out.println("User selection is "
\r
135 ( ( (float) uniprotCount / (float) selectedSources.size()) * 100)
\r
136 + " % Uniprot, and "+refCount+" / " +sequences.length+" have uniprot accession");
\r
139 Thread thread = new Thread(this);
\r
143 * creates a jalview sequence feature from a das feature document
\r
144 * @param dasfeature
\r
145 * @return sequence feature object created using dasfeature information
\r
147 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
151 * Different qNames for a DAS Feature - are string keys to the HashMaps in features
\r
153 qName.equals("TYPE") ||
\r
154 qName.equals("START") ||
\r
155 qName.equals("END") ||
\r
156 qName.equals("NOTE") ||
\r
157 qName.equals("LINK") ||
\r
158 qName.equals("SCORE")
\r
160 String desc = new String();
\r
161 if (dasfeature.containsKey("NOTE"))
\r
162 desc+=(String) dasfeature.get("NOTE");
\r
165 int start = 0, end = 0;
\r
168 try{ start = Integer.parseInt( dasfeature.get("START").toString()); }
\r
169 catch( Exception ex){}
\r
170 try{ end = Integer.parseInt( dasfeature.get("END").toString()); }
\r
171 catch (Exception ex){}
\r
172 try{ score = Integer.parseInt( dasfeature.get("SCORE").toString()); }
\r
173 catch (Exception ex){}
\r
176 SequenceFeature f = new SequenceFeature(
\r
177 (String) dasfeature.get("TYPE"),
\r
184 if (dasfeature.containsKey("LINK"))
\r
186 f.addLink(f.getType()+" "+f.begin+"_"+f.end
\r
187 +"|"+ dasfeature.get("LINK"));
\r
189 // (String) dasfeature.get("ID"),
\r
190 //// (String) dasfeature.get("METHOD"),
\r
191 // (String) dasfeature.get("SCORE"),
\r
195 // System.out.println(nickname+" "+f.getType()+" "+f.begin+" "+f.end);
\r
198 catch (Exception e) {
\r
199 e.printStackTrace();
\r
200 Cache.log.debug("Failed to parse "+dasfeature.toString(), e);
\r
205 * fetch and add das features to a sequence using the given source URL and Id to create a feature request
\r
210 protected void createFeatureFetcher(final SequenceI seq,
\r
211 final String sourceUrl,
\r
215 /// fetch DAS features
\r
216 final Das1Source source = new Das1Source();
\r
217 source.setUrl(sourceUrl);
\r
218 source.setNickname(nickname);
\r
221 Cache.log.debug("new Das Feature Fetcher for " + id + " querying " +
\r
223 if (id != null && id.length() > 0)
\r
225 setThreadsRunning(+1);
\r
227 FeatureThread fetcher = new FeatureThread(id
\r
228 + ":" + seq.getStart() + "," +seq.getEnd()
\r
231 fetcher.addFeatureListener(new FeatureListener()
\r
233 public void comeBackLater(FeatureEvent e)
\r
235 setThreadsRunning(-1);
\r
236 Cache.log.debug("das source " + e.getDasSource().getNickname() +
\r
237 " asked us to come back in " + e.getComeBackLater() +
\r
241 public void newFeatures(FeatureEvent e)
\r
243 setThreadsRunning(-1);
\r
244 Das1Source ds = e.getDasSource();
\r
246 Map[] features = e.getFeatures();
\r
247 // add features to sequence
\r
248 Cache.log.debug("das source " + ds.getUrl() + " returned " +
\r
249 features.length + " features");
\r
251 if (features.length > 0)
\r
253 for (int i = 0; i < features.length; i++)
\r
255 SequenceFeature f = newSequenceFeature(features[i],
\r
256 source.getNickname());
\r
258 seq.addSequenceFeature(f);
\r
263 System.out.println("No features found for sequence " +seq.getName()
\r
264 +" from: "+ e.getDasSource().getNickname());
\r
272 //NOTE alignPanel listener will be called after the previous
\r
273 //anonymous listener!!!
\r
274 fetcher.addFeatureListener(af);
\r
280 synchronized void setThreadsRunning(int i)
\r
282 threadsRunning += i;
\r
283 if(threadsRunning<1)
\r
284 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
288 * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset
\r
292 startTime = System.currentTimeMillis();
\r
293 af.setProgressBar("Fetching DAS Sequence Features", startTime);
\r
295 DasSource [] sources = new jalview.gui.DasSourceBrowser().getDASSource();
\r
297 if(selectedSources==null || selectedSources.size()==0)
\r
299 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE", "uniprot");
\r
300 StringTokenizer st = new StringTokenizer(active, "\t");
\r
301 Vector selectedSources = new Vector();
\r
303 while (st.hasMoreTokens())
\r
305 token = st.nextToken();
\r
306 for (int i = 0; i < sources.length; i++)
\r
308 if (sources[i].getNickname().equals(token))
\r
310 selectedSources.addElement(sources[i]);
\r
317 if(selectedSources == null || selectedSources.size()==0)
\r
319 System.out.println("No DAS Sources active");
\r
320 af.setProgressBar("No DAS Sources Active", startTime);
\r
327 while (seqIndex < sequences.length)
\r
329 Vector uprefs = jalview.util.DBRefUtils.selectRefs(sequences[seqIndex].getDBRef(),
\r
331 jalview.datamodel.DBRefSource.PDB,
\r
332 jalview.datamodel.DBRefSource.UNIPROT});
\r
334 for(int sourceIndex=0; sourceIndex<selectedSources.size(); sourceIndex++)
\r
336 DasSource dasSource = (DasSource)selectedSources.elementAt(sourceIndex);
\r
338 if (uprefs != null)
\r
340 // we know the id for this entry, so don't note its ID in the unknownSequences list
\r
341 for (int j = 0; j < uprefs.size(); j++)
\r
344 // 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
345 org.biojava.dasobert.dasregistry.DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();
\r
346 for (int l=0; l<cs.length; l++)
\r
348 if (jalview.util.DBRefUtils.isDasCoordinateSystem(cs[l].getName(), (DBRefEntry)
\r
351 Cache.log.debug("Launched fetcher for coordinate system " +
\r
355 createFeatureFetcher(sequences[seqIndex],
\r
356 dasSource.getUrl(),
\r
357 ( (DBRefEntry) uprefs.get(j)).
\r
359 dasSource.getNickname());
\r
367 // try and use the name as the sequence id
\r
368 if (sequences[seqIndex].getName().indexOf("|") > -1)
\r
370 id = sequences[seqIndex].getName().substring(
\r
371 sequences[seqIndex].getName().lastIndexOf("|") + 1);
\r
375 id = sequences[seqIndex].getName();
\r
379 // Should try to call a general feature fetcher that queries many sources with name to discover applicable ID references
\r
380 createFeatureFetcher(sequences[seqIndex],
\r
381 dasSource.getUrl(),
\r
383 dasSource.getNickname());
\r
391 catch (Exception ex)
\r
393 ex.printStackTrace();
\r
398 public static DasSource[] getDASSources()
\r
400 DasSourceReaderImpl reader = new DasSourceReaderImpl();
\r
402 String registryURL =jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
403 "http://das.sanger.ac.uk/registry/das1/sources/"
\r
408 URL url = new URL(registryURL);
\r
410 DasSource[] sources = reader.readDasSource(url);
\r
412 List das1sources = new ArrayList();
\r
413 for (int i = 0; i < sources.length; i++)
\r
415 DasSource ds = sources[i];
\r
416 if (ds instanceof Das2Source)
\r
418 Das2Source d2s = (Das2Source) ds;
\r
419 if (d2s.hasDas1Capabilities())
\r
421 Das1Source d1s = DasSourceConverter.toDas1Source(d2s);
\r
422 das1sources.add(d1s);
\r
426 else if (ds instanceof Das1Source)
\r
428 das1sources.add( (Das1Source) ds);
\r
432 return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources.size()]);
\r
434 catch (Exception ex)
\r
436 ex.printStackTrace();
\r