2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.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); // ensures Seed
68 addDBRefSourceImpl(jalview.ws.dbsources.RfamFull.class);
69 addDBRefSourceImpl(jalview.ws.dbsources.RfamSeed.class);
72 registerDasSequenceSources();
77 * return an ordered list of database sources where non-das database classes
78 * appear before das database classes
80 public String[] getOrderedSupportedSources()
82 String[] srcs = this.getSupportedDb();
83 ArrayList<String> dassrc = new ArrayList<String>(), nondas = new ArrayList<String>();
84 for (int i = 0; i < srcs.length; i++)
86 boolean das = false, skip = false;
88 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
90 // Skip the alignment databases for the moment - they're not useful for
91 // verifying a single sequence against its reference source
92 if (dbs.isA(DBRefSource.ALIGNMENTDB))
99 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
101 if (nm.startsWith("das:"))
103 nm = nm.substring(4);
123 String[] tosort = nondas.toArray(new String[0]), sorted = nondas
124 .toArray(new String[0]);
125 for (int j = 0, jSize = sorted.length; j < jSize; j++)
127 tosort[j] = tosort[j].toLowerCase();
129 jalview.util.QuickSort.sort(tosort, sorted);
130 // construct array with all sources listed
132 srcs = new String[sorted.length + dassrc.size()];
134 for (int j = sorted.length - 1; j >= 0; j--, i++)
140 sorted = dassrc.toArray(new String[0]);
141 tosort = dassrc.toArray(new String[0]);
142 for (int j = 0, jSize = sorted.length; j < jSize; j++)
144 tosort[j] = tosort[j].toLowerCase();
146 jalview.util.QuickSort.sort(tosort, sorted);
147 for (int j = sorted.length - 1; j >= 0; j--, i++)
155 * return plaintext databse list suitable for using in a GUI element
157 public String[] _getOrderedSupportedSources()
159 String[] srcs = this.getSupportedDb();
160 ArrayList dassrc = new ArrayList(), nondas = new ArrayList();
161 for (int i = 0; i < srcs.length; i++)
163 for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
165 String nm = dbs.getDbName();
166 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
168 if (nm.startsWith("das:"))
170 nm = nm.substring(4);
172 dassrc.add(new String[]
173 { srcs[i], nm.toUpperCase() });
177 nondas.add(new String[]
178 { srcs[i], nm.toUpperCase() });
182 Object[] sorted = nondas.toArray();
183 String[] tosort = new String[sorted.length];
185 for (int j = 0; j < sorted.length; j++)
187 tosort[j] = ((String[]) sorted[j])[1];
189 jalview.util.QuickSort.sort(tosort, sorted);
191 // construct array with all sources listed
192 srcs = new String[sorted.length + dassrc.size()];
193 for (int j = sorted.length - 1; j >= 0; j--, i++)
195 srcs[i] = ((String[]) sorted[j])[0];
199 sorted = dassrc.toArray();
200 tosort = new String[sorted.length];
202 for (int j = 0; j < sorted.length; j++)
204 tosort[j] = ((String[]) sorted[j])[1];
206 jalview.util.QuickSort.sort(tosort, sorted);
207 for (int j = sorted.length - 1; j >= 0; j--, i++)
209 srcs[i] = ((String[]) sorted[j])[0];
216 * simple run method to test dbsources.
220 public static void main(String[] argv)
222 AlignmentI ds = null;
223 Vector noProds = new Vector();
224 String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
225 + "With no arguments, all DbSources will be queried with their test Accession number.\n"
226 + "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"
227 + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"
228 + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";
229 boolean withDas = true;
230 if (argv != null && argv.length > 0
231 && argv[0].toLowerCase().startsWith("-nodas"))
234 String targs[] = new String[argv.length - 1];
235 System.arraycopy(argv, 1, targs, 0, targs.length);
238 if (argv != null && argv.length > 0)
240 List<DbSourceProxy> sps = new SequenceFetcher(withDas)
241 .getSourceProxy(argv[0]);
245 for (DbSourceProxy sp : sps)
247 AlignmentI al = null;
250 al = sp.getSequenceRecords(argv.length > 1 ? argv[1] : sp
252 } catch (Exception e)
255 System.err.println("Error when retrieving "
256 + (argv.length > 1 ? argv[1] : sp.getTestQuery())
257 + " from " + argv[0] + "\nUsage: " + usage);
259 SequenceI[] prod = al.getSequencesArray();
262 for (int p = 0; p < prod.length; p++)
264 System.out.println("Prod " + p + ": "
265 + prod[p].getDisplayId(true) + " : "
266 + prod[p].getDescription());
274 System.err.println("Can't resolve " + argv[0]
275 + " as a database name. Allowed values are :\n"
276 + new SequenceFetcher().getSupportedDb());
278 System.out.println(usage);
281 ASequenceFetcher sfetcher = new SequenceFetcher(withDas);
282 String[] dbSources = sfetcher.getSupportedDb();
283 for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
285 String db = dbSources[dbsource];
287 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();
351 System.out.println(raw.toString());
353 System.out.println("ERROR:No Raw results.");
357 System.out.println("Retrieved " + al.getHeight() + " sequences.");
358 for (int s = 0; s < al.getHeight(); s++)
360 SequenceI sq = al.getSequenceAt(s);
361 while (sq.getDatasetSequence() != null)
363 sq = sq.getDatasetSequence();
368 ds = new Alignment(new SequenceI[]
382 if (noProds.size() > 0)
384 Enumeration ts = noProds.elements();
385 while (ts.hasMoreElements())
388 Object[] typeSq = (Object[]) ts.nextElement();
389 boolean dna = (typeSq.length > 1);
390 AlignmentI al = (AlignmentI) typeSq[0];
391 System.out.println("Trying getProducts for "
392 + al.getSequenceAt(0).getDisplayId(true));
393 System.out.println("Search DS Xref for: "
394 + (dna ? "dna" : "prot"));
395 // have a bash at finding the products amongst all the retrieved
397 SequenceI[] seqs = al.getSequencesArray();
398 Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
399 seqs, dna, null, ds);
400 System.out.println("Found "
401 + ((prodal == null) ? "no" : "" + prodal.getHeight())
405 SequenceI[] prod = prodal.getSequencesArray(); // note
416 for (int p = 0; p < prod.length; p++)
418 System.out.println("Prod " + p + ": "
419 + prod[p].getDisplayId(true));
430 * query the currently defined DAS source registry for sequence sources and
431 * add a DasSequenceSource instance for each source to the SequenceFetcher
434 public void registerDasSequenceSources()
436 // TODO: define a context as a registry provider (either desktop,
437 // jalview.bin.cache, or something else).
438 for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry()
441 if (source.isSequenceSource())
443 List<DbSourceProxy> dassources = source.getSequenceSourceProxies();
444 for (DbSourceProxy seqsrc : dassources)
446 addDbRefSourceImpl(seqsrc);