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 jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.DBRefSource;
26 import jalview.datamodel.SequenceI;
27 import jalview.ws.dbsources.das.api.jalviewSourceI;
28 import jalview.ws.seqfetcher.ASequenceFetcher;
29 import jalview.ws.seqfetcher.DbSourceProxy;
31 import java.util.ArrayList;
32 import java.util.Enumeration;
33 import java.util.List;
34 import java.util.Vector;
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.ext.ensembl.EnsemblProtein.class);
59 addDBRefSourceImpl(jalview.ext.ensembl.EnsemblTranscript.class);
60 addDBRefSourceImpl(jalview.ext.ensembl.EnsemblCds.class);
61 addDBRefSourceImpl(jalview.ext.ensembl.EnsemblGenome.class);
62 addDBRefSourceImpl(jalview.ext.ensembl.EnsemblCdna.class);
64 addDBRefSourceImpl(jalview.ws.dbsources.EmblSource.class);
65 addDBRefSourceImpl(jalview.ws.dbsources.EmblCdsSouce.class);
66 addDBRefSourceImpl(jalview.ws.dbsources.Uniprot.class);
67 addDBRefSourceImpl(jalview.ws.dbsources.UnprotName.class);
68 addDBRefSourceImpl(jalview.ws.dbsources.Pdb.class);
69 addDBRefSourceImpl(jalview.ws.dbsources.PfamFull.class);
70 addDBRefSourceImpl(jalview.ws.dbsources.PfamSeed.class);
71 // ensures Seed alignment is 'default' for PFAM
72 addDBRefSourceImpl(jalview.ws.dbsources.RfamFull.class);
73 addDBRefSourceImpl(jalview.ws.dbsources.RfamSeed.class);
76 registerDasSequenceSources();
81 * return an ordered list of database sources where non-das database classes
82 * appear before das database classes
84 public String[] getOrderedSupportedSources()
86 String[] srcs = this.getSupportedDb();
87 ArrayList<String> dassrc = new ArrayList<String>(), nondas = new ArrayList<String>();
88 for (int i = 0; i < srcs.length; i++)
90 boolean das = false, skip = false;
92 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
94 // Skip the alignment databases for the moment - they're not useful for
95 // verifying a single sequence against its reference source
96 if (dbs.isA(DBRefSource.ALIGNMENTDB))
102 nm = dbs.getDbName();
103 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
105 if (nm.startsWith("das:"))
107 nm = nm.substring(4);
127 String[] tosort = nondas.toArray(new String[0]), sorted = nondas
128 .toArray(new String[0]);
129 for (int j = 0, jSize = sorted.length; j < jSize; j++)
131 tosort[j] = tosort[j].toLowerCase();
133 jalview.util.QuickSort.sort(tosort, sorted);
134 // construct array with all sources listed
136 srcs = new String[sorted.length + dassrc.size()];
138 for (int j = sorted.length - 1; j >= 0; j--, i++)
144 sorted = dassrc.toArray(new String[0]);
145 tosort = dassrc.toArray(new String[0]);
146 for (int j = 0, jSize = sorted.length; j < jSize; j++)
148 tosort[j] = tosort[j].toLowerCase();
150 jalview.util.QuickSort.sort(tosort, sorted);
151 for (int j = sorted.length - 1; j >= 0; j--, i++)
159 * return plaintext databse list suitable for using in a GUI element
161 public String[] _getOrderedSupportedSources()
163 String[] srcs = this.getSupportedDb();
164 ArrayList dassrc = new ArrayList(), nondas = new ArrayList();
165 for (int i = 0; i < srcs.length; i++)
167 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
169 String nm = dbs.getDbName();
170 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
172 if (nm.startsWith("das:"))
174 nm = nm.substring(4);
176 dassrc.add(new String[] { srcs[i], nm.toUpperCase() });
180 nondas.add(new String[] { srcs[i], nm.toUpperCase() });
184 Object[] sorted = nondas.toArray();
185 String[] tosort = new String[sorted.length];
187 for (int j = 0; j < sorted.length; j++)
189 tosort[j] = ((String[]) sorted[j])[1];
191 jalview.util.QuickSort.sort(tosort, sorted);
193 // construct array with all sources listed
194 srcs = new String[sorted.length + dassrc.size()];
195 for (int j = sorted.length - 1; j >= 0; j--, i++)
197 srcs[i] = ((String[]) sorted[j])[0];
201 sorted = dassrc.toArray();
202 tosort = new String[sorted.length];
204 for (int j = 0; j < sorted.length; j++)
206 tosort[j] = ((String[]) sorted[j])[1];
208 jalview.util.QuickSort.sort(tosort, sorted);
209 for (int j = sorted.length - 1; j >= 0; j--, i++)
211 srcs[i] = ((String[]) sorted[j])[0];
218 * simple run method to test dbsources.
222 public static void main(String[] argv)
224 AlignmentI ds = null;
225 Vector noProds = new Vector();
226 String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
227 + "With no arguments, all DbSources will be queried with their test Accession number.\n"
228 + "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"
229 + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"
230 + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";
231 boolean withDas = true;
232 if (argv != null && argv.length > 0
233 && argv[0].toLowerCase().startsWith("-nodas"))
236 String targs[] = new String[argv.length - 1];
237 System.arraycopy(argv, 1, targs, 0, targs.length);
240 if (argv != null && argv.length > 0)
242 List<DbSourceProxy> sps = new SequenceFetcher(withDas)
243 .getSourceProxy(argv[0]);
247 for (DbSourceProxy sp : sps)
249 AlignmentI al = null;
252 al = sp.getSequenceRecords(argv.length > 1 ? argv[1] : sp
254 } catch (Exception e)
257 System.err.println("Error when retrieving "
258 + (argv.length > 1 ? argv[1] : sp.getTestQuery())
259 + " from " + argv[0] + "\nUsage: " + usage);
261 SequenceI[] prod = al.getSequencesArray();
264 for (int p = 0; p < prod.length; p++)
266 System.out.println("Prod " + p + ": "
267 + prod[p].getDisplayId(true) + " : "
268 + prod[p].getDescription());
276 System.err.println("Can't resolve " + argv[0]
277 + " as a database name. Allowed values are :\n"
278 + new SequenceFetcher().getSupportedDb());
280 System.out.println(usage);
283 ASequenceFetcher sfetcher = new SequenceFetcher(withDas);
284 String[] dbSources = sfetcher.getSupportedDb();
285 for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
287 String db = dbSources[dbsource];
289 if (db.equals(DBRefSource.PDB))
293 for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
295 System.out.println("Source: " + sp.getDbName() + " (" + db
296 + "): retrieving test:" + sp.getTestQuery());
297 AlignmentI al = null;
300 al = sp.getSequenceRecords(sp.getTestQuery());
301 if (al != null && al.getHeight() > 0
302 && sp.getDbSourceProperties() != null)
304 boolean dna = sp.getDbSourceProperties().containsKey(
305 DBRefSource.DNACODINGSEQDB)
306 || sp.getDbSourceProperties().containsKey(
307 DBRefSource.DNASEQDB)
308 || sp.getDbSourceProperties().containsKey(
309 DBRefSource.CODINGSEQDB);
310 // try and find products
311 String types[] = jalview.analysis.CrossRef
312 .findSequenceXrefTypes(dna, al.getSequencesArray());
315 System.out.println("Xref Types for: "
316 + (dna ? "dna" : "prot"));
317 for (int t = 0; t < types.length; t++)
319 System.out.println("Type: " + types[t]);
320 SequenceI[] prod = jalview.analysis.CrossRef
321 .findXrefSequences(al.getSequencesArray(), dna,
322 types[t]).getSequencesArray();
323 System.out.println("Found "
324 + ((prod == null) ? "no" : "" + prod.length)
328 for (int p = 0; p < prod.length; p++)
330 System.out.println("Prod " + p + ": "
331 + prod[p].getDisplayId(true));
338 noProds.addElement((dna ? new Object[] { al, al }
339 : new Object[] { al }));
343 } catch (Exception ex)
345 System.out.println("ERROR:Failed to retrieve test query.");
346 ex.printStackTrace(System.out);
351 System.out.println("ERROR:No alignment retrieved.");
352 StringBuffer raw = sp.getRawRecords();
355 System.out.println(raw.toString());
359 System.out.println("ERROR:No Raw results.");
364 System.out.println("Retrieved " + al.getHeight() + " sequences.");
365 for (int s = 0; s < al.getHeight(); s++)
367 SequenceI sq = al.getSequenceAt(s);
368 while (sq.getDatasetSequence() != null)
370 sq = sq.getDatasetSequence();
375 ds = new Alignment(new SequenceI[] { sq });
388 if (noProds.size() > 0)
390 Enumeration ts = noProds.elements();
391 while (ts.hasMoreElements())
394 Object[] typeSq = (Object[]) ts.nextElement();
395 boolean dna = (typeSq.length > 1);
396 AlignmentI al = (AlignmentI) typeSq[0];
397 System.out.println("Trying getProducts for "
398 + al.getSequenceAt(0).getDisplayId(true));
399 System.out.println("Search DS Xref for: "
400 + (dna ? "dna" : "prot"));
401 // have a bash at finding the products amongst all the retrieved
403 SequenceI[] seqs = al.getSequencesArray();
404 Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
405 seqs, dna, null, ds);
406 System.out.println("Found "
407 + ((prodal == null) ? "no" : "" + prodal.getHeight())
411 SequenceI[] prod = prodal.getSequencesArray(); // note
422 for (int p = 0; p < prod.length; p++)
424 System.out.println("Prod " + p + ": "
425 + prod[p].getDisplayId(true));
436 * query the currently defined DAS source registry for sequence sources and
437 * add a DasSequenceSource instance for each source to the SequenceFetcher
440 public void registerDasSequenceSources()
442 // TODO: define a context as a registry provider (either desktop,
443 // jalview.bin.cache, or something else).
444 for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry()
447 if (source.isSequenceSource())
449 List<DbSourceProxy> dassources = source.getSequenceSourceProxies();
450 for (DbSourceProxy seqsrc : dassources)
452 addDbRefSourceImpl(seqsrc);