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 Vector dbref = sequences[i].getDBRef();
\r
100 for(int j=0; j<dbref.size(); j++)
\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
107 System.out.println("got a match");
\r
115 System.out.println(refCount+" "+uniprotCount+" "+sequences.length);
\r
116 if(refCount<sequences.length && uniprotCount>0)
\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
126 if(reply == JOptionPane.YES_OPTION)
\r
129 af.getViewport().getAlignment(), af).fetchDBRefs(true);
\r
134 System.out.println("User selection is "
\r
136 ( ( (float) uniprotCount / (float) selectedSources.size()) * 100)
\r
137 + " % Uniprot, and "+refCount+" / " +sequences.length+" have uniprot accession");
\r
140 Thread thread = new Thread(this);
\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
148 SequenceFeature newSequenceFeature(Map dasfeature, String nickname)
\r
152 * Different qNames for a DAS Feature - are string keys to the HashMaps in features
\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
161 String desc = new String();
\r
162 if (dasfeature.containsKey("NOTE"))
\r
163 desc+=(String) dasfeature.get("NOTE");
\r
166 int start = 0, end = 0;
\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
177 SequenceFeature f = new SequenceFeature(
\r
178 (String) dasfeature.get("TYPE"),
\r
185 if (dasfeature.containsKey("LINK"))
\r
187 f.addLink(f.getType()+" "+f.begin+"_"+f.end
\r
188 +"|"+ dasfeature.get("LINK"));
\r
190 // (String) dasfeature.get("ID"),
\r
191 //// (String) dasfeature.get("METHOD"),
\r
192 // (String) dasfeature.get("SCORE"),
\r
196 // System.out.println(nickname+" "+f.getType()+" "+f.begin+" "+f.end);
\r
199 catch (Exception e) {
\r
200 e.printStackTrace();
\r
201 Cache.log.debug("Failed to parse "+dasfeature.toString(), e);
\r
206 * fetch and add das features to a sequence using the given source URL and Id to create a feature request
\r
211 protected void createFeatureFetcher(final SequenceI seq,
\r
212 final String sourceUrl,
\r
217 /// fetch DAS features
\r
218 final Das1Source source = new Das1Source();
\r
219 source.setUrl(sourceUrl);
\r
220 source.setNickname(nickname);
\r
223 Cache.log.debug("new Das Feature Fetcher for " + id + " querying " +
\r
226 if (id != null && id.length() > 0)
\r
228 setThreadsRunning(+1);
\r
230 int start=seq.getStart(), end = seq.getEnd();
\r
232 if(af.getViewport().getSelectionGroup()!=null)
\r
234 SequenceI tmp = af.getViewport().getAlignment().findName(seq.getName());
\r
235 start = tmp.findPosition(
\r
236 af.getViewport().getSelectionGroup().getStartRes()
\r
239 end = tmp.findPosition(
\r
240 af.getViewport().getSelectionGroup().getEndRes()
\r
244 FeatureThread fetcher = new FeatureThread(id
\r
245 + ":" + start + "," + end, source);
\r
247 fetcher.addFeatureListener(new FeatureListener()
\r
249 public void comeBackLater(FeatureEvent e)
\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
257 public void newFeatures(FeatureEvent e)
\r
259 setThreadsRunning(-1);
\r
260 Das1Source ds = e.getDasSource();
\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
267 if (features.length > 0)
\r
269 for (int i = 0; i < features.length; i++)
\r
271 SequenceFeature f = newSequenceFeature(features[i],
\r
272 source.getNickname());
\r
274 seq.addSequenceFeature(f);
\r
279 System.out.println("No features found for sequence " +seq.getName()
\r
280 +" from: "+ e.getDasSource().getNickname());
\r
288 //NOTE alignPanel listener will be called after the previous
\r
289 //anonymous listener!!!
\r
290 fetcher.addFeatureListener(af);
\r
296 synchronized void setThreadsRunning(int i)
\r
298 threadsRunning += i;
\r
299 if(threadsRunning<1)
\r
300 af.setProgressBar("DAS Feature Fetching Complete", startTime);
\r
304 * Spawns a number of dasobert Fetcher threads to add features to sequences in the dataset
\r
308 startTime = System.currentTimeMillis();
\r
309 af.setProgressBar("Fetching DAS Sequence Features", startTime);
\r
311 DasSource [] sources = new jalview.gui.DasSourceBrowser().getDASSource();
\r
313 if(selectedSources==null || selectedSources.size()==0)
\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
319 while (st.hasMoreTokens())
\r
321 token = st.nextToken();
\r
322 for (int i = 0; i < sources.length; i++)
\r
324 if (sources[i].getNickname().equals(token))
\r
326 selectedSources.addElement(sources[i]);
\r
333 if(selectedSources == null || selectedSources.size()==0)
\r
335 System.out.println("No DAS Sources active");
\r
336 af.setProgressBar("No DAS Sources Active", startTime);
\r
343 while (seqIndex < sequences.length)
\r
345 Vector uprefs = jalview.util.DBRefUtils.selectRefs(sequences[seqIndex].getDBRef(),
\r
347 jalview.datamodel.DBRefSource.PDB,
\r
348 jalview.datamodel.DBRefSource.UNIPROT});
\r
350 for(int sourceIndex=0; sourceIndex<selectedSources.size(); sourceIndex++)
\r
352 DasSource dasSource = (DasSource)selectedSources.elementAt(sourceIndex);
\r
354 if (uprefs != null)
\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
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
364 if (jalview.util.DBRefUtils.isDasCoordinateSystem(cs[l].getName(), (DBRefEntry)
\r
367 Cache.log.debug("Launched fetcher for coordinate system " +
\r
371 createFeatureFetcher(sequences[seqIndex],
\r
372 dasSource.getUrl(),
\r
373 ( (DBRefEntry) uprefs.get(j)).
\r
375 dasSource.getNickname());
\r
383 // try and use the name as the sequence id
\r
384 if (sequences[seqIndex].getName().indexOf("|") > -1)
\r
386 id = sequences[seqIndex].getName().substring(
\r
387 sequences[seqIndex].getName().lastIndexOf("|") + 1);
\r
391 id = sequences[seqIndex].getName();
\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
399 dasSource.getNickname());
\r
407 catch (Exception ex)
\r
409 ex.printStackTrace();
\r
414 public static DasSource[] getDASSources()
\r
416 DasSourceReaderImpl reader = new DasSourceReaderImpl();
\r
418 String registryURL =jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
\r
419 "http://das.sanger.ac.uk/registry/das1/sources/"
\r
424 URL url = new URL(registryURL);
\r
426 DasSource[] sources = reader.readDasSource(url);
\r
428 List das1sources = new ArrayList();
\r
429 for (int i = 0; i < sources.length; i++)
\r
431 DasSource ds = sources[i];
\r
432 if (ds instanceof Das2Source)
\r
434 Das2Source d2s = (Das2Source) ds;
\r
435 if (d2s.hasDas1Capabilities())
\r
437 Das1Source d1s = DasSourceConverter.toDas1Source(d2s);
\r
438 das1sources.add(d1s);
\r
442 else if (ds instanceof Das1Source)
\r
444 das1sources.add( (Das1Source) ds);
\r
448 return (Das1Source[]) das1sources.toArray(new Das1Source[das1sources.size()]);
\r
450 catch (Exception ex)
\r
452 ex.printStackTrace();
\r