2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.List;
26 import java.util.Vector;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.DBRefSource;
31 import jalview.datamodel.SequenceI;
32 import jalview.ws.dbsources.das.api.jalviewSourceI;
33 import jalview.ws.seqfetcher.ASequenceFetcher;
34 import jalview.ws.seqfetcher.DbSourceProxy;
37 * This is the the concrete implementation of the sequence retrieval interface
38 * and abstract class in jalview.ws.seqfetcher. This implements the run-time
39 * discovery of sequence database clients, and provides a hardwired main for
40 * testing all registered handlers.
43 public class SequenceFetcher extends ASequenceFetcher
46 * Thread safe construction of database proxies TODO: extend to a configurable
47 * database plugin mechanism where classes are instantiated by reflection and
48 * queried for their DbRefSource and version association.
51 public SequenceFetcher()
56 public SequenceFetcher(boolean addDas)
58 addDBRefSourceImpl(jalview.ws.dbsources.EmblSource.class);
59 addDBRefSourceImpl(jalview.ws.dbsources.EmblCdsSouce.class);
60 addDBRefSourceImpl(jalview.ws.dbsources.Uniprot.class);
61 addDBRefSourceImpl(jalview.ws.dbsources.UnprotName.class);
62 addDBRefSourceImpl(jalview.ws.dbsources.Pdb.class);
63 addDBRefSourceImpl(jalview.ws.dbsources.PfamFull.class);
64 addDBRefSourceImpl(jalview.ws.dbsources.PfamSeed.class);
65 // ensures Seed alignment is 'default' for PFAM
66 addDBRefSourceImpl(jalview.ws.dbsources.RfamFull.class);
67 addDBRefSourceImpl(jalview.ws.dbsources.RfamSeed.class);
70 registerDasSequenceSources();
75 * return an ordered list of database sources where non-das database classes
76 * appear before das database classes
78 public String[] getOrderedSupportedSources()
80 String[] srcs = this.getSupportedDb();
81 ArrayList<String> dassrc = new ArrayList<String>(), nondas = new ArrayList<String>();
82 for (int i = 0; i < srcs.length; i++)
84 boolean das = false, skip = false;
86 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
88 // Skip the alignment databases for the moment - they're not useful for
89 // verifying a single sequence against its reference source
90 if (dbs.isA(DBRefSource.ALIGNMENTDB))
97 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
99 if (nm.startsWith("das:"))
101 nm = nm.substring(4);
121 String[] tosort = nondas.toArray(new String[0]), sorted = nondas
122 .toArray(new String[0]);
123 for (int j = 0, jSize = sorted.length; j < jSize; j++)
125 tosort[j] = tosort[j].toLowerCase();
127 jalview.util.QuickSort.sort(tosort, sorted);
128 // construct array with all sources listed
130 srcs = new String[sorted.length + dassrc.size()];
132 for (int j = sorted.length - 1; j >= 0; j--, i++)
138 sorted = dassrc.toArray(new String[0]);
139 tosort = dassrc.toArray(new String[0]);
140 for (int j = 0, jSize = sorted.length; j < jSize; j++)
142 tosort[j] = tosort[j].toLowerCase();
144 jalview.util.QuickSort.sort(tosort, sorted);
145 for (int j = sorted.length - 1; j >= 0; j--, i++)
153 * return plaintext databse list suitable for using in a GUI element
155 public String[] _getOrderedSupportedSources()
157 String[] srcs = this.getSupportedDb();
158 ArrayList dassrc = new ArrayList(), nondas = new ArrayList();
159 for (int i = 0; i < srcs.length; i++)
161 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
163 String nm = dbs.getDbName();
164 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
166 if (nm.startsWith("das:"))
168 nm = nm.substring(4);
170 dassrc.add(new String[]
171 { srcs[i], nm.toUpperCase() });
175 nondas.add(new String[]
176 { srcs[i], nm.toUpperCase() });
180 Object[] sorted = nondas.toArray();
181 String[] tosort = new String[sorted.length];
183 for (int j = 0; j < sorted.length; j++)
185 tosort[j] = ((String[]) sorted[j])[1];
187 jalview.util.QuickSort.sort(tosort, sorted);
189 // construct array with all sources listed
190 srcs = new String[sorted.length + dassrc.size()];
191 for (int j = sorted.length - 1; j >= 0; j--, i++)
193 srcs[i] = ((String[]) sorted[j])[0];
197 sorted = dassrc.toArray();
198 tosort = new String[sorted.length];
200 for (int j = 0; j < sorted.length; j++)
202 tosort[j] = ((String[]) sorted[j])[1];
204 jalview.util.QuickSort.sort(tosort, sorted);
205 for (int j = sorted.length - 1; j >= 0; j--, i++)
207 srcs[i] = ((String[]) sorted[j])[0];
214 * simple run method to test dbsources.
218 public static void main(String[] argv)
220 AlignmentI ds = null;
221 Vector noProds = new Vector();
222 String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
223 + "With no arguments, all DbSources will be queried with their test Accession number.\n"
224 + "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"
225 + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"
226 + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";
227 boolean withDas = true;
228 if (argv != null && argv.length > 0
229 && argv[0].toLowerCase().startsWith("-nodas"))
232 String targs[] = new String[argv.length - 1];
233 System.arraycopy(argv, 1, targs, 0, targs.length);
236 if (argv != null && argv.length > 0)
238 List<DbSourceProxy> sps = new SequenceFetcher(withDas)
239 .getSourceProxy(argv[0]);
243 for (DbSourceProxy sp : sps)
245 AlignmentI al = null;
248 al = sp.getSequenceRecords(argv.length > 1 ? argv[1] : sp
250 } catch (Exception e)
253 System.err.println("Error when retrieving "
254 + (argv.length > 1 ? argv[1] : sp.getTestQuery())
255 + " from " + argv[0] + "\nUsage: " + usage);
257 SequenceI[] prod = al.getSequencesArray();
260 for (int p = 0; p < prod.length; p++)
262 System.out.println("Prod " + p + ": "
263 + prod[p].getDisplayId(true) + " : "
264 + prod[p].getDescription());
272 System.err.println("Can't resolve " + argv[0]
273 + " as a database name. Allowed values are :\n"
274 + new SequenceFetcher().getSupportedDb());
276 System.out.println(usage);
279 ASequenceFetcher sfetcher = new SequenceFetcher(withDas);
280 String[] dbSources = sfetcher.getSupportedDb();
281 for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
283 String db = dbSources[dbsource];
285 if (db.equals(DBRefSource.PDB))
289 for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
291 System.out.println("Source: " + sp.getDbName() + " (" + db
292 + "): retrieving test:" + sp.getTestQuery());
293 AlignmentI al = null;
296 al = sp.getSequenceRecords(sp.getTestQuery());
297 if (al != null && al.getHeight() > 0
298 && sp.getDbSourceProperties() != null)
300 boolean dna = sp.getDbSourceProperties().containsKey(
301 DBRefSource.DNACODINGSEQDB)
302 || sp.getDbSourceProperties().containsKey(
303 DBRefSource.DNASEQDB)
304 || sp.getDbSourceProperties().containsKey(
305 DBRefSource.CODINGSEQDB);
306 // try and find products
307 String types[] = jalview.analysis.CrossRef
308 .findSequenceXrefTypes(dna, al.getSequencesArray());
311 System.out.println("Xref Types for: "
312 + (dna ? "dna" : "prot"));
313 for (int t = 0; t < types.length; t++)
315 System.out.println("Type: " + types[t]);
316 SequenceI[] prod = jalview.analysis.CrossRef
317 .findXrefSequences(al.getSequencesArray(), dna,
318 types[t]).getSequencesArray();
319 System.out.println("Found "
320 + ((prod == null) ? "no" : "" + prod.length)
324 for (int p = 0; p < prod.length; p++)
326 System.out.println("Prod " + p + ": "
327 + prod[p].getDisplayId(true));
334 noProds.addElement((dna ? new Object[]
335 { al, al } : new Object[]
340 } catch (Exception ex)
342 System.out.println("ERROR:Failed to retrieve test query.");
343 ex.printStackTrace(System.out);
348 System.out.println("ERROR:No alignment retrieved.");
349 StringBuffer raw = sp.getRawRecords();
352 System.out.println(raw.toString());
356 System.out.println("ERROR:No Raw results.");
361 System.out.println("Retrieved " + al.getHeight() + " sequences.");
362 for (int s = 0; s < al.getHeight(); s++)
364 SequenceI sq = al.getSequenceAt(s);
365 while (sq.getDatasetSequence() != null)
367 sq = sq.getDatasetSequence();
372 ds = new Alignment(new SequenceI[]
386 if (noProds.size() > 0)
388 Enumeration ts = noProds.elements();
389 while (ts.hasMoreElements())
392 Object[] typeSq = (Object[]) ts.nextElement();
393 boolean dna = (typeSq.length > 1);
394 AlignmentI al = (AlignmentI) typeSq[0];
395 System.out.println("Trying getProducts for "
396 + al.getSequenceAt(0).getDisplayId(true));
397 System.out.println("Search DS Xref for: "
398 + (dna ? "dna" : "prot"));
399 // have a bash at finding the products amongst all the retrieved
401 SequenceI[] seqs = al.getSequencesArray();
402 Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
403 seqs, dna, null, ds);
404 System.out.println("Found "
405 + ((prodal == null) ? "no" : "" + prodal.getHeight())
409 SequenceI[] prod = prodal.getSequencesArray(); // note
420 for (int p = 0; p < prod.length; p++)
422 System.out.println("Prod " + p + ": "
423 + prod[p].getDisplayId(true));
434 * query the currently defined DAS source registry for sequence sources and
435 * add a DasSequenceSource instance for each source to the SequenceFetcher
438 public void registerDasSequenceSources()
440 // TODO: define a context as a registry provider (either desktop,
441 // jalview.bin.cache, or something else).
442 for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry()
445 if (source.isSequenceSource())
447 List<DbSourceProxy> dassources = source.getSequenceSourceProxies();
448 for (DbSourceProxy seqsrc : dassources)
450 addDbRefSourceImpl(seqsrc);