2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
\r
3 * Copyright (C) 2009 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 java.util.ArrayList;
\r
22 import java.util.Enumeration;
\r
23 import java.util.Vector;
\r
25 import org.biojava.dasobert.das2.Das2Source;
\r
26 import org.biojava.dasobert.dasregistry.Das1Source;
\r
27 import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
\r
28 import org.biojava.dasobert.dasregistry.DasSource;
\r
30 import jalview.datamodel.Alignment;
\r
31 import jalview.datamodel.AlignmentI;
\r
32 import jalview.datamodel.DBRefSource;
\r
33 import jalview.datamodel.SequenceI;
\r
34 import jalview.ws.dbsources.DasSequenceSource;
\r
35 import jalview.ws.seqfetcher.ASequenceFetcher;
\r
36 import jalview.ws.seqfetcher.DbSourceProxy;
\r
39 * This is the the concrete implementation of the sequence retrieval interface
\r
40 * and abstract class in jalview.ws.seqfetcher. This implements the run-time
\r
41 * discovery of sequence database clients, and provides a hardwired main for
\r
42 * testing all registered handlers.
\r
45 public class SequenceFetcher extends ASequenceFetcher
\r
48 * Thread safe construction of database proxies TODO: extend to a configurable
\r
49 * database plugin mechanism where classes are instantiated by reflection and
\r
50 * queried for their DbRefSource and version association.
\r
53 public SequenceFetcher()
\r
55 addDBRefSourceImpl(jalview.ws.dbsources.EmblSource.class);
\r
56 addDBRefSourceImpl(jalview.ws.dbsources.EmblCdsSouce.class);
\r
57 addDBRefSourceImpl(jalview.ws.dbsources.Uniprot.class);
\r
58 addDBRefSourceImpl(jalview.ws.dbsources.UnprotName.class);
\r
59 addDBRefSourceImpl(jalview.ws.dbsources.Pdb.class);
\r
60 addDBRefSourceImpl(jalview.ws.dbsources.PfamFull.class);
\r
61 addDBRefSourceImpl(jalview.ws.dbsources.PfamSeed.class); // ensures Seed
\r
65 registerDasSequenceSources();
\r
69 * return an ordered list of database sources suitable for using in a GUI
\r
72 public String[] getOrderedSupportedSources()
\r
74 String[] srcs = this.getSupportedDb();
\r
75 ArrayList dassrc = new ArrayList(), nondas = new ArrayList();
\r
76 for (int i = 0; i < srcs.length; i++)
\r
78 String nm = getSourceProxy(srcs[i]).getDbName();
\r
79 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.DasSequenceSource)
\r
81 if (nm.startsWith("das:"))
\r
83 nm = nm.substring(4);
\r
85 dassrc.add(new String[]
\r
86 { srcs[i], nm.toUpperCase() });
\r
90 nondas.add(new String[]
\r
91 { srcs[i], nm.toUpperCase() });
\r
94 Object[] sorted = nondas.toArray();
\r
95 String[] tosort = new String[sorted.length];
\r
97 for (int j = 0; j < sorted.length; j++)
\r
99 tosort[j] = ((String[]) sorted[j])[1];
\r
101 jalview.util.QuickSort.sort(tosort, sorted);
\r
103 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
105 srcs[i] = ((String[]) sorted[j])[0];
\r
109 sorted = dassrc.toArray();
\r
110 tosort = new String[sorted.length];
\r
112 for (int j = 0; j < sorted.length; j++)
\r
114 tosort[j] = ((String[]) sorted[j])[1];
\r
116 jalview.util.QuickSort.sort(tosort, sorted);
\r
117 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
119 srcs[i] = ((String[]) sorted[j])[0];
\r
126 * simple run method to test dbsources.
\r
130 public static void main(String[] argv)
\r
132 AlignmentI ds = null;
\r
133 Vector noProds = new Vector();
\r
134 String usage = "SequenceFetcher.main [<DBNAME> <ACCNO>]\n"
\r
135 + "With no arguments, all DbSources will be queried with their test Accession number.\n"
\r
136 + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.";
\r
137 if (argv != null && argv.length > 0)
\r
139 DbSourceProxy sp = new SequenceFetcher().getSourceProxy(argv[0]);
\r
142 AlignmentI al = null;
\r
145 al = sp.getSequenceRecords(argv[1]);
\r
146 } catch (Exception e)
\r
148 e.printStackTrace();
\r
149 System.err.println("Error when retrieving " + argv[1] + " from "
\r
150 + argv[0] + "\nUsage: " + usage);
\r
152 SequenceI[] prod = al.getSequencesArray();
\r
155 for (int p = 0; p < prod.length; p++)
\r
157 System.out.println("Prod " + p + ": "
\r
158 + prod[p].getDisplayId(true) + " : "
\r
159 + prod[p].getDescription());
\r
166 System.err.println("Can't resolve " + argv[0]
\r
167 + " as a database name. Allowed values are :\n"
\r
168 + new SequenceFetcher().getSupportedDb());
\r
170 System.out.println(usage);
\r
172 ASequenceFetcher sfetcher = new SequenceFetcher();
\r
173 String[] dbSources = sfetcher.getSupportedDb();
\r
174 for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
\r
176 String db = dbSources[dbsource];
\r
178 if (db.equals(DBRefSource.PDB))
\r
180 DbSourceProxy sp = sfetcher.getSourceProxy(db);
\r
181 System.out.println("Source: " + sp.getDbName() + " (" + db
\r
182 + "): retrieving test:" + sp.getTestQuery());
\r
183 AlignmentI al = null;
\r
186 al = sp.getSequenceRecords(sp.getTestQuery());
\r
187 if (al != null && al.getHeight() > 0
\r
188 && sp.getDbSourceProperties() != null)
\r
190 boolean dna = sp.getDbSourceProperties().containsKey(
\r
191 DBRefSource.DNACODINGSEQDB)
\r
192 || sp.getDbSourceProperties().containsKey(
\r
193 DBRefSource.DNASEQDB)
\r
194 || sp.getDbSourceProperties().containsKey(
\r
195 DBRefSource.CODINGSEQDB);
\r
196 // try and find products
\r
197 String types[] = jalview.analysis.CrossRef.findSequenceXrefTypes(
\r
198 dna, al.getSequencesArray());
\r
201 System.out.println("Xref Types for: " + (dna ? "dna" : "prot"));
\r
202 for (int t = 0; t < types.length; t++)
\r
204 System.out.println("Type: " + types[t]);
\r
205 SequenceI[] prod = jalview.analysis.CrossRef
\r
206 .findXrefSequences(al.getSequencesArray(), dna,
\r
207 types[t]).getSequencesArray();
\r
208 System.out.println("Found "
\r
209 + ((prod == null) ? "no" : "" + prod.length)
\r
213 for (int p = 0; p < prod.length; p++)
\r
215 System.out.println("Prod " + p + ": "
\r
216 + prod[p].getDisplayId(true));
\r
223 noProds.addElement((dna ? new Object[]
\r
224 { al, al } : new Object[]
\r
229 } catch (Exception ex)
\r
231 System.out.println("ERROR:Failed to retrieve test query.");
\r
232 ex.printStackTrace(System.out);
\r
236 System.out.println("ERROR:No alignment retrieved.");
\r
237 StringBuffer raw = sp.getRawRecords();
\r
239 System.out.println(raw.toString());
\r
241 System.out.println("ERROR:No Raw results.");
\r
245 System.out.println("Retrieved " + al.getHeight() + " sequences.");
\r
246 for (int s = 0; s < al.getHeight(); s++)
\r
248 SequenceI sq = al.getSequenceAt(s);
\r
249 while (sq.getDatasetSequence() != null)
\r
251 sq = sq.getDatasetSequence();
\r
256 ds = new Alignment(new SequenceI[]
\r
262 ds.addSequence(sq);
\r
266 System.out.flush();
\r
267 System.err.flush();
\r
270 if (noProds.size() > 0)
\r
272 Enumeration ts = noProds.elements();
\r
273 while (ts.hasMoreElements())
\r
276 Object[] typeSq = (Object[]) ts.nextElement();
\r
277 boolean dna = (typeSq.length > 1);
\r
278 AlignmentI al = (AlignmentI) typeSq[0];
\r
279 System.out.println("Trying getProducts for "
\r
280 + al.getSequenceAt(0).getDisplayId(true));
\r
281 System.out.println("Search DS Xref for: " + (dna ? "dna" : "prot"));
\r
282 // have a bash at finding the products amongst all the retrieved
\r
284 SequenceI[] seqs = al.getSequencesArray();
\r
285 Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
\r
286 seqs, dna, null, ds);
\r
287 System.out.println("Found "
\r
288 + ((prodal == null) ? "no" : "" + prodal.getHeight())
\r
290 if (prodal != null)
\r
292 SequenceI[] prod = prodal.getSequencesArray(); // note
\r
303 for (int p = 0; p < prod.length; p++)
\r
305 System.out.println("Prod " + p + ": "
\r
306 + prod[p].getDisplayId(true));
\r
315 * query the currently defined DAS source registry for sequence sources and
\r
316 * add a DasSequenceSource instance for each source to the SequenceFetcher
\r
319 public void registerDasSequenceSources()
\r
321 DasSource[] sources = jalview.ws.DasSequenceFeatureFetcher
\r
325 for (int s=0; sources!=null && s<sources.length; s++)
\r
327 addDasSequenceSource(sources[s]);
\r
331 Vector localsources = jalview.bin.Cache.getLocalDasSources();
\r
332 if (localsources!=null)
\r
334 for (Enumeration ls = localsources.elements(); ls.hasMoreElements(); )
\r
336 addDasSequenceSource((DasSource) ls.nextElement());
\r
341 * Try to create and add a DasSequenceSource to the list of sources.
\r
343 * @return null if no source was added, or the new DasSequenceSource created
\r
345 DasSequenceSource addDasSequenceSource(DasSource source) {
\r
346 DasSequenceSource ds=null;
\r
347 Das1Source d1s = null;
\r
348 if (source.hasCapability("sequence"))
\r
350 if (source instanceof Das2Source)
\r
352 if (((Das2Source) source).hasDas1Capabilities())
\r
356 d1s = org.biojava.dasobert.das2.DasSourceConverter
\r
357 .toDas1Source((Das2Source) source);
\r
358 } catch (Exception e)
\r
360 System.err.println("Ignoring DAS2 sequence source "
\r
361 + source.getNickname()
\r
362 + " - couldn't map to Das1Source.\n");
\r
363 e.printStackTrace();
\r
369 if (source instanceof Das1Source)
\r
371 d1s = (Das1Source) source;
\r
377 DasCoordinateSystem[] css = d1s.getCoordinateSystem();
\r
378 if (css==null || css.length==0)
\r
380 // TODO: query das source directly to identify coordinate system... or
\r
381 // have to make up a coordinate system
\r
382 css = new DasCoordinateSystem[] { new DasCoordinateSystem()};
\r
383 css[0].setName(d1s.getNickname());
\r
384 css[0].setUniqueId(d1s.getNickname());
\r
386 for (int c = 0; c < css.length; c++)
\r
390 addDbRefSourceImpl(ds=new DasSequenceSource(
\r
391 "das:" + d1s.getNickname() + " (" + css[c].getName()
\r
392 + ")", css[c].getName(), d1s, css[c]));
\r
393 } catch (Exception e)
\r
395 System.err.println("Ignoring sequence coord system " + c + " ("
\r
396 + css[c].getName() + ") for source "
\r
397 + d1s.getNickname()
\r
398 + "- threw exception when constructing fetcher.\n");
\r
399 e.printStackTrace();
\r