2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
\r
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
20 import jalview.datamodel.Alignment;
\r
21 import jalview.datamodel.AlignmentI;
\r
22 import jalview.datamodel.DBRefSource;
\r
23 import jalview.datamodel.SequenceI;
\r
24 import jalview.ws.dbsources.das.api.jalviewSourceI;
\r
25 import jalview.ws.dbsources.das.datamodel.DasSequenceSource;
\r
26 import jalview.ws.seqfetcher.ASequenceFetcher;
\r
27 import jalview.ws.seqfetcher.DbSourceProxy;
\r
29 import java.util.ArrayList;
\r
30 import java.util.Enumeration;
\r
31 import java.util.List;
\r
32 import java.util.Vector;
\r
35 * This is the the concrete implementation of the sequence retrieval interface
\r
36 * and abstract class in jalview.ws.seqfetcher. This implements the run-time
\r
37 * discovery of sequence database clients, and provides a hardwired main for
\r
38 * testing all registered handlers.
\r
41 public class SequenceFetcher extends ASequenceFetcher
\r
44 * Thread safe construction of database proxies TODO: extend to a configurable
\r
45 * database plugin mechanism where classes are instantiated by reflection and
\r
46 * queried for their DbRefSource and version association.
\r
49 public SequenceFetcher()
\r
53 public SequenceFetcher(boolean addDas)
\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 addDBRefSourceImpl(jalview.ws.dbsources.RfamFull.class);
\r
66 addDBRefSourceImpl(jalview.ws.dbsources.RfamSeed.class);
\r
68 registerDasSequenceSources();
\r
73 * return an ordered list of database sources where non-das database classes
\r
74 * appear before das database classes
\r
76 public String[] getOrderedSupportedSources()
\r
78 String[] srcs = this.getSupportedDb();
\r
79 ArrayList<String> dassrc = new ArrayList<String>(), nondas = new ArrayList<String>();
\r
80 for (int i = 0; i < srcs.length; i++)
\r
82 boolean das = false,skip=false;
\r
84 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
\r
86 // Skip the alignment databases for the moment - they're not useful for verifying a single sequence against its reference source
\r
87 if (dbs.isA(DBRefSource.ALIGNMENTDB))
\r
91 nm = dbs.getDbName();
\r
92 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
\r
94 if (nm.startsWith("das:"))
\r
96 nm = nm.substring(4);
\r
109 dassrc.add(srcs[i]);
\r
113 nondas.add(srcs[i]);
\r
116 String[] tosort = nondas.toArray(new String[0]), sorted = nondas
\r
117 .toArray(new String[0]);
\r
118 for (int j = 0, jSize = sorted.length; j < jSize; j++)
\r
120 tosort[j] = tosort[j].toLowerCase();
\r
122 jalview.util.QuickSort.sort(tosort, sorted);
\r
123 // construct array with all sources listed
\r
125 srcs = new String[sorted.length + dassrc.size()];
\r
127 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
129 srcs[i] = sorted[j];
\r
133 sorted = dassrc.toArray(new String[0]);
\r
134 tosort = dassrc.toArray(new String[0]);
\r
135 for (int j = 0, jSize = sorted.length; j < jSize; j++)
\r
137 tosort[j] = tosort[j].toLowerCase();
\r
139 jalview.util.QuickSort.sort(tosort, sorted);
\r
140 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
142 srcs[i] = sorted[j];
\r
148 * return plaintext databse list suitable for using in a GUI element
\r
150 public String[] _getOrderedSupportedSources()
\r
152 String[] srcs = this.getSupportedDb();
\r
153 ArrayList dassrc = new ArrayList(), nondas = new ArrayList();
\r
154 for (int i = 0; i < srcs.length; i++)
\r
156 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
\r
158 String nm = dbs.getDbName();
\r
159 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
\r
161 if (nm.startsWith("das:"))
\r
163 nm = nm.substring(4);
\r
165 dassrc.add(new String[]
\r
166 { srcs[i], nm.toUpperCase() });
\r
170 nondas.add(new String[]
\r
171 { srcs[i], nm.toUpperCase() });
\r
175 Object[] sorted = nondas.toArray();
\r
176 String[] tosort = new String[sorted.length];
\r
178 for (int j = 0; j < sorted.length; j++)
\r
180 tosort[j] = ((String[]) sorted[j])[1];
\r
182 jalview.util.QuickSort.sort(tosort, sorted);
\r
184 // construct array with all sources listed
\r
185 srcs = new String[sorted.length + dassrc.size()];
\r
186 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
188 srcs[i] = ((String[]) sorted[j])[0];
\r
192 sorted = dassrc.toArray();
\r
193 tosort = new String[sorted.length];
\r
195 for (int j = 0; j < sorted.length; j++)
\r
197 tosort[j] = ((String[]) sorted[j])[1];
\r
199 jalview.util.QuickSort.sort(tosort, sorted);
\r
200 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
202 srcs[i] = ((String[]) sorted[j])[0];
\r
209 * simple run method to test dbsources.
\r
213 public static void main(String[] argv)
\r
215 AlignmentI ds = null;
\r
216 Vector noProds = new Vector();
\r
217 String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
\r
218 + "With no arguments, all DbSources will be queried with their test Accession number.\n"
\r
219 + "With one argument, the argument will be resolved to one or more db sources and each will be queried with their test accession only.\n"
\r
220 + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"
\r
221 + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";
\r
222 boolean withDas=true;
\r
223 if (argv!=null && argv.length>0 && argv[0].toLowerCase().startsWith("-nodas"))
\r
226 String targs[] = new String[argv.length-1];
\r
227 System.arraycopy(argv, 1, targs, 0, targs.length);
\r
230 if (argv != null && argv.length > 0)
\r
232 List<DbSourceProxy> sps = new SequenceFetcher(withDas)
\r
233 .getSourceProxy(argv[0]);
\r
237 for (DbSourceProxy sp : sps)
\r
239 AlignmentI al = null;
\r
242 al = sp.getSequenceRecords(argv.length>1 ? argv[1] : sp.getTestQuery());
\r
243 } catch (Exception e)
\r
245 e.printStackTrace();
\r
246 System.err.println("Error when retrieving " + (argv.length>1 ? argv[1] : sp.getTestQuery())
\r
247 + " from " + argv[0] + "\nUsage: " + usage);
\r
249 SequenceI[] prod = al.getSequencesArray();
\r
252 for (int p = 0; p < prod.length; p++)
\r
254 System.out.println("Prod " + p + ": "
\r
255 + prod[p].getDisplayId(true) + " : "
\r
256 + prod[p].getDescription());
\r
264 System.err.println("Can't resolve " + argv[0]
\r
265 + " as a database name. Allowed values are :\n"
\r
266 + new SequenceFetcher().getSupportedDb());
\r
268 System.out.println(usage);
\r
271 ASequenceFetcher sfetcher = new SequenceFetcher(withDas);
\r
272 String[] dbSources = sfetcher.getSupportedDb();
\r
273 for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
\r
275 String db = dbSources[dbsource];
\r
277 if (db.equals(DBRefSource.PDB))
\r
279 for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
\r
281 System.out.println("Source: " + sp.getDbName() + " (" + db
\r
282 + "): retrieving test:" + sp.getTestQuery());
\r
283 AlignmentI al = null;
\r
286 al = sp.getSequenceRecords(sp.getTestQuery());
\r
287 if (al != null && al.getHeight() > 0
\r
288 && sp.getDbSourceProperties() != null)
\r
290 boolean dna = sp.getDbSourceProperties().containsKey(
\r
291 DBRefSource.DNACODINGSEQDB)
\r
292 || sp.getDbSourceProperties().containsKey(
\r
293 DBRefSource.DNASEQDB)
\r
294 || sp.getDbSourceProperties().containsKey(
\r
295 DBRefSource.CODINGSEQDB);
\r
296 // try and find products
\r
297 String types[] = jalview.analysis.CrossRef
\r
298 .findSequenceXrefTypes(dna, al.getSequencesArray());
\r
301 System.out.println("Xref Types for: "
\r
302 + (dna ? "dna" : "prot"));
\r
303 for (int t = 0; t < types.length; t++)
\r
305 System.out.println("Type: " + types[t]);
\r
306 SequenceI[] prod = jalview.analysis.CrossRef
\r
307 .findXrefSequences(al.getSequencesArray(), dna,
\r
308 types[t]).getSequencesArray();
\r
309 System.out.println("Found "
\r
310 + ((prod == null) ? "no" : "" + prod.length)
\r
314 for (int p = 0; p < prod.length; p++)
\r
316 System.out.println("Prod " + p + ": "
\r
317 + prod[p].getDisplayId(true));
\r
324 noProds.addElement((dna ? new Object[]
\r
325 { al, al } : new Object[]
\r
330 } catch (Exception ex)
\r
332 System.out.println("ERROR:Failed to retrieve test query.");
\r
333 ex.printStackTrace(System.out);
\r
338 System.out.println("ERROR:No alignment retrieved.");
\r
339 StringBuffer raw = sp.getRawRecords();
\r
341 System.out.println(raw.toString());
\r
343 System.out.println("ERROR:No Raw results.");
\r
347 System.out.println("Retrieved " + al.getHeight() + " sequences.");
\r
348 for (int s = 0; s < al.getHeight(); s++)
\r
350 SequenceI sq = al.getSequenceAt(s);
\r
351 while (sq.getDatasetSequence() != null)
\r
353 sq = sq.getDatasetSequence();
\r
358 ds = new Alignment(new SequenceI[]
\r
364 ds.addSequence(sq);
\r
368 System.out.flush();
\r
369 System.err.flush();
\r
372 if (noProds.size() > 0)
\r
374 Enumeration ts = noProds.elements();
\r
375 while (ts.hasMoreElements())
\r
378 Object[] typeSq = (Object[]) ts.nextElement();
\r
379 boolean dna = (typeSq.length > 1);
\r
380 AlignmentI al = (AlignmentI) typeSq[0];
\r
381 System.out.println("Trying getProducts for "
\r
382 + al.getSequenceAt(0).getDisplayId(true));
\r
383 System.out.println("Search DS Xref for: "
\r
384 + (dna ? "dna" : "prot"));
\r
385 // have a bash at finding the products amongst all the retrieved
\r
387 SequenceI[] seqs = al.getSequencesArray();
\r
388 Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
\r
389 seqs, dna, null, ds);
\r
390 System.out.println("Found "
\r
391 + ((prodal == null) ? "no" : "" + prodal.getHeight())
\r
393 if (prodal != null)
\r
395 SequenceI[] prod = prodal.getSequencesArray(); // note
\r
406 for (int p = 0; p < prod.length; p++)
\r
408 System.out.println("Prod " + p + ": "
\r
409 + prod[p].getDisplayId(true));
\r
420 * query the currently defined DAS source registry for sequence sources and
\r
421 * add a DasSequenceSource instance for each source to the SequenceFetcher
\r
424 public void registerDasSequenceSources()
\r
426 // TODO: define a context as a registry provider (either desktop,
\r
427 // jalview.bin.cache, or something else).
\r
428 for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry()
\r
431 if (source.isSequenceSource())
\r
433 List<DbSourceProxy> dassources = source.getSequenceSourceProxies();
\r
434 for (DbSourceProxy seqsrc : dassources)
\r
436 addDbRefSourceImpl(seqsrc);
\r