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.eventmodel.FeatureEvent;
\r
31 import org.biojava.dasobert.eventmodel.FeatureListener;
\r
32 import org.biojava.dasobert.dasregistry.DasSource;
\r
34 import org.biojava.dasobert.das2.io.DasSourceReaderImpl;
\r
35 import org.biojava.dasobert.dasregistry.Das1Source;
\r
36 import org.biojava.dasobert.dasregistry.DasSource;
\r
37 import org.biojava.dasobert.das2.Das2Source;
\r
38 import org.biojava.dasobert.das2.DasSourceConverter;
\r
41 import jalview.bin.Cache;
\r
42 import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
\r
44 import javax.swing.*;
\r
50 * @version $Revision$
\r
52 public class DasSequenceFeatureFetcher implements Runnable
\r
54 SequenceI [] sequences;
\r
56 StringBuffer sbuffer = new StringBuffer();
\r
57 Vector selectedSources;
\r
60 int threadsRunning = 0;
\r
64 * Creates a new SequenceFeatureFetcher object.
\r
67 * @param align DOCUMENT ME!
\r
68 * @param ap DOCUMENT ME!
\r
70 public DasSequenceFeatureFetcher(SequenceI [] sequences,
\r
71 final AlignFrame af,
\r
72 Vector selectedSources)
\r
74 this.selectedSources = selectedSources;
\r
75 this.sequences = sequences;
\r
78 int uniprotCount = 0;
\r
79 for (int i = 0; i < selectedSources.size(); i++)
\r
81 DasSource source = (DasSource) selectedSources.elementAt(i);
\r
82 DasCoordinateSystem[] coords = source.getCoordinateSystem();
\r
83 for (int c = 0; c < coords.length; c++)
\r
85 if (coords[c].getName().indexOf("UniProt")>-1)
\r
94 for(int i=0; i<sequences.length; i++)
\r
96 DBRefEntry [] dbref = sequences[i].getDBRef();
\r
99 for(int j=0; j<dbref.length; j++)
\r
101 if(dbref[j].getSource()
\r
102 .equals(jalview.datamodel.DBRefSource.UNIPROT))
\r
111 if(refCount<sequences.length && uniprotCount>0)
\r
114 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
\r
115 "Do you want Jalview to find\n"
\r
116 +"Uniprot Accession ids for given sequence names?",
\r
117 "Find Uniprot Accession Ids",
\r
118 JOptionPane.YES_NO_OPTION,
\r
119 JOptionPane.QUESTION_MESSAGE);
\r
121 if(reply == JOptionPane.YES_OPTION)
\r
123 Thread thread = new Thread(new FetchDBRefs());
\r
132 /* System.out.println("User selection is " +
\r
133 ( ( (float) uniprotCount / (float) selectedSources.size()) * 100)
\r
134 + " % Uniprot, and "+refCount+" / " +sequences.length+" have uniprot accession");
\r
138 void startFetching()
\r
140 Thread thread = new Thread(this);
\r
144 class FetchDBRefs implements Runnable
\r
149 af.getViewport().getAlignment(), af).fetchDBRefs(true);
\r
156 * creates a jalview sequence feature from a das feature document
\r
157 * @param dasfeature
\r
158 * @return sequence feature object created using dasfeature information
\r
160 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
164 * Different qNames for a DAS Feature - are string keys to the HashMaps in features
\r
166 qName.equals("TYPE") ||
\r
167 qName.equals("START") ||
\r
168 qName.equals("END") ||
\r
169 qName.equals("NOTE") ||
\r
170 qName.equals("LINK") ||
\r
171 qName.equals("SCORE")
\r
173 String desc = new String();
\r
174 if (dasfeature.containsKey("NOTE"))
\r
175 desc+=(String) dasfeature.get("NOTE");
\r
178 int start = 0, end = 0;
\r
181 try{ start = Integer.parseInt( dasfeature.get("START").toString()); }
\r
182 catch( Exception ex){}
\r
183 try{ end = Integer.parseInt( dasfeature.get("END").toString()); }
\r
184 catch (Exception ex){}
\r
185 try{ score = Integer.parseInt( dasfeature.get("SCORE").toString()); }
\r
186 catch (Exception ex){}
\r
189 SequenceFeature f = new SequenceFeature(
\r
190 (String) dasfeature.get("TYPE"),
\r
197 if (dasfeature.containsKey("LINK"))
\r
199 f.addLink(f.getType()+" "+f.begin+"_"+f.end
\r
200 +"|"+ dasfeature.get("LINK"));
\r
202 // (String) dasfeature.get("ID"),
\r
203 //// (String) dasfeature.get("METHOD"),
\r
204 // (String) dasfeature.get("SCORE"),
\r
208 // System.out.println(nickname+" "+f.getType()+" "+f.begin+" "+f.end);
\r
211 catch (Exception e) {
\r
212 e.printStackTrace();
\r
213 Cache.log.debug("Failed to parse "+dasfeature.toString(), e);
\r
218 * fetch and add das features to a sequence using the given source URL and Id to create a feature request
\r
223 protected void createFeatureFetcher(final SequenceI seq,
\r
224 final String sourceUrl,
\r
229 /// fetch DAS features
\r
230 final Das1Source source = new Das1Source();
\r
231 source.setUrl(sourceUrl);
\r
232 source.setNickname(nickname);
\r
235 Cache.log.debug("new Das Feature Fetcher for " + id + " querying " +
\r
238 if (id != null && id.length() > 0)
\r
240 setThreadsRunning(+1);
\r
242 int start=seq.getStart(), end = seq.getEnd();
\r
244 if(af.getViewport().getSelectionGroup()!=null)
\r
246 SequenceI tmp = af.getViewport().getAlignment().findName(seq.getName());
\r
247 start = tmp.findPosition(
\r
248 af.getViewport().getSelectionGroup().getStartRes()
\r
251 end = tmp.findPosition(
\r
252 af.getViewport().getSelectionGroup().getEndRes()
\r
256 FeatureThread fetcher = new FeatureThread(id
\r
257 // + ":" + start + "," + end,
\r
261 fetcher.addFeatureListener(new FeatureListener()
\r
263 public void comeBackLater(FeatureEvent e)
\r
265 setThreadsRunning(-1);
\r
266 Cache.log.debug("das source " + e.getDasSource().getNickname() +
\r
267 " asked us to come back in " + e.getComeBackLater() +
\r
271 public void newFeatures(FeatureEvent e)
\r
273 setThreadsRunning(-1);
\r
274 Das1Source ds = e.getDasSource();
\r
276 Map[] features = e.getFeatures();
\r
277 // add features to sequence
\r
278 Cache.log.debug("das source " + ds.getUrl() + " returned " +
\r
279 features.length + " features");
\r
281 if (features.length > 0)
\r
283 for (int i = 0; i < features.length; i++)
\r
285 SequenceFeature f = newSequenceFeature(features[i],
\r
286 source.getNickname());
\r
288 seq.addSequenceFeature(f);
\r
293 System.out.println("No features found for sequence " +seq.getName()
\r
294 +" from: "+ e.getDasSource().getNickname());
\r
302 //NOTE alignPanel listener will be called after the previous
\r
303 //anonymous listener!!!
\r
304 fetcher.addFeatureListener(af);
\r
310 synchronized void setThreadsRunning(int i)
\r
312 threadsRunning += i;
\r
313 if(threadsRunning<1)
\r
314 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
318 * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset
\r
322 startTime = System.currentTimeMillis();
\r
323 af.setProgressBar("Fetching DAS Sequence Features", startTime);
\r
325 DasSource [] sources = new jalview.gui.DasSourceBrowser().getDASSource();
\r
327 if(selectedSources==null || selectedSources.size()==0)
\r
329 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE", "uniprot");
\r
330 StringTokenizer st = new StringTokenizer(active, "\t");
\r
331 Vector selectedSources = new Vector();
\r
333 while (st.hasMoreTokens())
\r
335 token = st.nextToken();
\r
336 for (int i = 0; i < sources.length; i++)
\r
338 if (sources[i].getNickname().equals(token))
\r
340 selectedSources.addElement(sources[i]);
\r
347 if(selectedSources == null || selectedSources.size()==0)
\r
349 System.out.println("No DAS Sources active");
\r
350 af.setProgressBar("No DAS Sources Active", startTime);
\r
357 while (seqIndex < sequences.length)
\r
359 DBRefEntry [] uprefs = jalview.util.DBRefUtils.selectRefs(sequences[seqIndex].getDBRef(),
\r
361 jalview.datamodel.DBRefSource.PDB,
\r
362 jalview.datamodel.DBRefSource.UNIPROT});
\r
364 for(int sourceIndex=0; sourceIndex<selectedSources.size(); sourceIndex++)
\r
366 DasSource dasSource = (DasSource)selectedSources.elementAt(sourceIndex);
\r
368 if (uprefs != null)
\r
370 // we know the id for this entry, so don't note its ID in the unknownSequences list
\r
371 for (int j = 0; j < uprefs.length; j++)
\r
374 // 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
375 DasCoordinateSystem cs[] = dasSource.getCoordinateSystem();
\r
376 for (int l=0; l<cs.length; l++)
\r
378 if (jalview.util.DBRefUtils.isDasCoordinateSystem(cs[l].getName(), uprefs[j]))
\r
380 Cache.log.debug("Launched fetcher for coordinate system " +
\r
384 createFeatureFetcher(sequences[seqIndex],
\r
385 dasSource.getUrl(),
\r
386 uprefs[j].getAccessionId(),
\r
387 dasSource.getNickname());
\r
395 // try and use the name as the sequence id
\r
396 if (sequences[seqIndex].getName().indexOf("|") > -1)
\r
398 id = sequences[seqIndex].getName().substring(
\r
399 sequences[seqIndex].getName().lastIndexOf("|") + 1);
\r
403 id = sequences[seqIndex].getName();
\r
407 // Should try to call a general feature fetcher that queries many sources with name to discover applicable ID references
\r
408 createFeatureFetcher(sequences[seqIndex],
\r
409 dasSource.getUrl(),
\r
411 dasSource.getNickname());
\r
419 catch (Exception ex)
\r
421 ex.printStackTrace();
\r
426 public static DasSource[] getDASSources()
\r
428 DasSourceReaderImpl reader = new DasSourceReaderImpl();
\r
430 String registryURL =jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
431 "http://das.sanger.ac.uk/registry/das1/sources/"
\r
436 URL url = new URL(registryURL);
\r
438 DasSource[] sources = reader.readDasSource(url);
\r
440 List das1sources = new ArrayList();
\r
441 for (int i = 0; i < sources.length; i++)
\r
443 DasSource ds = sources[i];
\r
444 if (ds instanceof Das2Source)
\r
446 Das2Source d2s = (Das2Source) ds;
\r
447 if (d2s.hasDas1Capabilities())
\r
449 Das1Source d1s = DasSourceConverter.toDas1Source(d2s);
\r
450 das1sources.add(d1s);
\r
454 else if (ds instanceof Das1Source)
\r
456 das1sources.add( (Das1Source) ds);
\r
460 return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources.size()]);
\r
462 catch (Exception ex)
\r
464 ex.printStackTrace();
\r