2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
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.seqfetcher.ASequenceFetcher;
\r
26 import jalview.ws.seqfetcher.DbSourceProxy;
\r
28 import java.util.ArrayList;
\r
29 import java.util.Enumeration;
\r
30 import java.util.List;
\r
31 import java.util.Vector;
\r
34 * This is the the concrete implementation of the sequence retrieval interface
\r
35 * and abstract class in jalview.ws.seqfetcher. This implements the run-time
\r
36 * discovery of sequence database clients, and provides a hardwired main for
\r
37 * testing all registered handlers.
\r
40 public class SequenceFetcher extends ASequenceFetcher
\r
43 * Thread safe construction of database proxies TODO: extend to a configurable
\r
44 * database plugin mechanism where classes are instantiated by reflection and
\r
45 * queried for their DbRefSource and version association.
\r
48 public SequenceFetcher()
\r
52 public SequenceFetcher(boolean addDas)
\r
54 addDBRefSourceImpl(jalview.ws.dbsources.EmblSource.class);
\r
55 addDBRefSourceImpl(jalview.ws.dbsources.EmblCdsSouce.class);
\r
56 addDBRefSourceImpl(jalview.ws.dbsources.Uniprot.class);
\r
57 addDBRefSourceImpl(jalview.ws.dbsources.UnprotName.class);
\r
58 addDBRefSourceImpl(jalview.ws.dbsources.Pdb.class);
\r
59 addDBRefSourceImpl(jalview.ws.dbsources.PfamFull.class);
\r
60 addDBRefSourceImpl(jalview.ws.dbsources.PfamSeed.class); // ensures Seed
\r
64 addDBRefSourceImpl(jalview.ws.dbsources.RfamFull.class);
\r
65 addDBRefSourceImpl(jalview.ws.dbsources.RfamSeed.class);
\r
67 registerDasSequenceSources();
\r
72 * return an ordered list of database sources where non-das database classes
\r
73 * appear before das database classes
\r
75 public String[] getOrderedSupportedSources()
\r
77 String[] srcs = this.getSupportedDb();
\r
78 ArrayList<String> dassrc = new ArrayList<String>(), nondas = new ArrayList<String>();
\r
79 for (int i = 0; i < srcs.length; i++)
\r
81 boolean das = false, skip = false;
\r
83 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
\r
85 // Skip the alignment databases for the moment - they're not useful for
\r
86 // verifying a single sequence against its reference source
\r
87 if (dbs.isA(DBRefSource.ALIGNMENTDB))
\r
93 nm = dbs.getDbName();
\r
94 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
\r
96 if (nm.startsWith("das:"))
\r
98 nm = nm.substring(4);
\r
111 dassrc.add(srcs[i]);
\r
115 nondas.add(srcs[i]);
\r
118 String[] tosort = nondas.toArray(new String[0]), sorted = nondas
\r
119 .toArray(new String[0]);
\r
120 for (int j = 0, jSize = sorted.length; j < jSize; j++)
\r
122 tosort[j] = tosort[j].toLowerCase();
\r
124 jalview.util.QuickSort.sort(tosort, sorted);
\r
125 // construct array with all sources listed
\r
127 srcs = new String[sorted.length + dassrc.size()];
\r
129 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
131 srcs[i] = sorted[j];
\r
135 sorted = dassrc.toArray(new String[0]);
\r
136 tosort = dassrc.toArray(new String[0]);
\r
137 for (int j = 0, jSize = sorted.length; j < jSize; j++)
\r
139 tosort[j] = tosort[j].toLowerCase();
\r
141 jalview.util.QuickSort.sort(tosort, sorted);
\r
142 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
144 srcs[i] = sorted[j];
\r
150 * return plaintext databse list suitable for using in a GUI element
\r
152 public String[] _getOrderedSupportedSources()
\r
154 String[] srcs = this.getSupportedDb();
\r
155 ArrayList dassrc = new ArrayList(), nondas = new ArrayList();
\r
156 for (int i = 0; i < srcs.length; i++)
\r
158 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
\r
160 String nm = dbs.getDbName();
\r
161 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
\r
163 if (nm.startsWith("das:"))
\r
165 nm = nm.substring(4);
\r
167 dassrc.add(new String[]
\r
168 { srcs[i], nm.toUpperCase() });
\r
172 nondas.add(new String[]
\r
173 { srcs[i], nm.toUpperCase() });
\r
177 Object[] sorted = nondas.toArray();
\r
178 String[] tosort = new String[sorted.length];
\r
180 for (int j = 0; j < sorted.length; j++)
\r
182 tosort[j] = ((String[]) sorted[j])[1];
\r
184 jalview.util.QuickSort.sort(tosort, sorted);
\r
186 // construct array with all sources listed
\r
187 srcs = new String[sorted.length + dassrc.size()];
\r
188 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
190 srcs[i] = ((String[]) sorted[j])[0];
\r
194 sorted = dassrc.toArray();
\r
195 tosort = new String[sorted.length];
\r
197 for (int j = 0; j < sorted.length; j++)
\r
199 tosort[j] = ((String[]) sorted[j])[1];
\r
201 jalview.util.QuickSort.sort(tosort, sorted);
\r
202 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
204 srcs[i] = ((String[]) sorted[j])[0];
\r
211 * simple run method to test dbsources.
\r
215 public static void main(String[] argv)
\r
217 AlignmentI ds = null;
\r
218 Vector noProds = new Vector();
\r
219 String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
\r
220 + "With no arguments, all DbSources will be queried with their test Accession number.\n"
\r
221 + "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
222 + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"
\r
223 + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";
\r
224 boolean withDas=true;
\r
225 if (argv!=null && argv.length>0 && argv[0].toLowerCase().startsWith("-nodas"))
\r
228 String targs[] = new String[argv.length-1];
\r
229 System.arraycopy(argv, 1, targs, 0, targs.length);
\r
232 if (argv != null && argv.length > 0)
\r
234 List<DbSourceProxy> sps = new SequenceFetcher(withDas)
\r
235 .getSourceProxy(argv[0]);
\r
239 for (DbSourceProxy sp : sps)
\r
241 AlignmentI al = null;
\r
244 al = sp.getSequenceRecords(argv.length>1 ? argv[1] : sp.getTestQuery());
\r
245 } catch (Exception e)
\r
247 e.printStackTrace();
\r
248 System.err.println("Error when retrieving " + (argv.length>1 ? argv[1] : sp.getTestQuery())
\r
249 + " from " + argv[0] + "\nUsage: " + usage);
\r
251 SequenceI[] prod = al.getSequencesArray();
\r
254 for (int p = 0; p < prod.length; p++)
\r
256 System.out.println("Prod " + p + ": "
\r
257 + prod[p].getDisplayId(true) + " : "
\r
258 + prod[p].getDescription());
\r
266 System.err.println("Can't resolve " + argv[0]
\r
267 + " as a database name. Allowed values are :\n"
\r
268 + new SequenceFetcher().getSupportedDb());
\r
270 System.out.println(usage);
\r
273 ASequenceFetcher sfetcher = new SequenceFetcher(withDas);
\r
274 String[] dbSources = sfetcher.getSupportedDb();
\r
275 for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
\r
277 String db = dbSources[dbsource];
\r
279 if (db.equals(DBRefSource.PDB))
\r
281 for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
\r
283 System.out.println("Source: " + sp.getDbName() + " (" + db
\r
284 + "): retrieving test:" + sp.getTestQuery());
\r
285 AlignmentI al = null;
\r
288 al = sp.getSequenceRecords(sp.getTestQuery());
\r
289 if (al != null && al.getHeight() > 0
\r
290 && sp.getDbSourceProperties() != null)
\r
292 boolean dna = sp.getDbSourceProperties().containsKey(
\r
293 DBRefSource.DNACODINGSEQDB)
\r
294 || sp.getDbSourceProperties().containsKey(
\r
295 DBRefSource.DNASEQDB)
\r
296 || sp.getDbSourceProperties().containsKey(
\r
297 DBRefSource.CODINGSEQDB);
\r
298 // try and find products
\r
299 String types[] = jalview.analysis.CrossRef
\r
300 .findSequenceXrefTypes(dna, al.getSequencesArray());
\r
303 System.out.println("Xref Types for: "
\r
304 + (dna ? "dna" : "prot"));
\r
305 for (int t = 0; t < types.length; t++)
\r
307 System.out.println("Type: " + types[t]);
\r
308 SequenceI[] prod = jalview.analysis.CrossRef
\r
309 .findXrefSequences(al.getSequencesArray(), dna,
\r
310 types[t]).getSequencesArray();
\r
311 System.out.println("Found "
\r
312 + ((prod == null) ? "no" : "" + prod.length)
\r
316 for (int p = 0; p < prod.length; p++)
\r
318 System.out.println("Prod " + p + ": "
\r
319 + prod[p].getDisplayId(true));
\r
326 noProds.addElement((dna ? new Object[]
\r
327 { al, al } : new Object[]
\r
332 } catch (Exception ex)
\r
334 System.out.println("ERROR:Failed to retrieve test query.");
\r
335 ex.printStackTrace(System.out);
\r
340 System.out.println("ERROR:No alignment retrieved.");
\r
341 StringBuffer raw = sp.getRawRecords();
\r
343 System.out.println(raw.toString());
\r
345 System.out.println("ERROR:No Raw results.");
\r
349 System.out.println("Retrieved " + al.getHeight() + " sequences.");
\r
350 for (int s = 0; s < al.getHeight(); s++)
\r
352 SequenceI sq = al.getSequenceAt(s);
\r
353 while (sq.getDatasetSequence() != null)
\r
355 sq = sq.getDatasetSequence();
\r
360 ds = new Alignment(new SequenceI[]
\r
366 ds.addSequence(sq);
\r
370 System.out.flush();
\r
371 System.err.flush();
\r
374 if (noProds.size() > 0)
\r
376 Enumeration ts = noProds.elements();
\r
377 while (ts.hasMoreElements())
\r
380 Object[] typeSq = (Object[]) ts.nextElement();
\r
381 boolean dna = (typeSq.length > 1);
\r
382 AlignmentI al = (AlignmentI) typeSq[0];
\r
383 System.out.println("Trying getProducts for "
\r
384 + al.getSequenceAt(0).getDisplayId(true));
\r
385 System.out.println("Search DS Xref for: "
\r
386 + (dna ? "dna" : "prot"));
\r
387 // have a bash at finding the products amongst all the retrieved
\r
389 SequenceI[] seqs = al.getSequencesArray();
\r
390 Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
\r
391 seqs, dna, null, ds);
\r
392 System.out.println("Found "
\r
393 + ((prodal == null) ? "no" : "" + prodal.getHeight())
\r
395 if (prodal != null)
\r
397 SequenceI[] prod = prodal.getSequencesArray(); // note
\r
408 for (int p = 0; p < prod.length; p++)
\r
410 System.out.println("Prod " + p + ": "
\r
411 + prod[p].getDisplayId(true));
\r
422 * query the currently defined DAS source registry for sequence sources and
\r
423 * add a DasSequenceSource instance for each source to the SequenceFetcher
\r
426 public void registerDasSequenceSources()
\r
428 // TODO: define a context as a registry provider (either desktop,
\r
429 // jalview.bin.cache, or something else).
\r
430 for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry()
\r
433 if (source.isSequenceSource())
\r
435 List<DbSourceProxy> dassources = source.getSequenceSourceProxies();
\r
436 for (DbSourceProxy seqsrc : dassources)
\r
438 addDbRefSourceImpl(seqsrc);
\r