2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
3 * Copyright (C) 2015 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);
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[] { srcs[i], nm.toUpperCase() });
174 nondas.add(new String[] { srcs[i], nm.toUpperCase() });
178 Object[] sorted = nondas.toArray();
179 String[] tosort = new String[sorted.length];
181 for (int j = 0; j < sorted.length; j++)
183 tosort[j] = ((String[]) sorted[j])[1];
185 jalview.util.QuickSort.sort(tosort, sorted);
187 // construct array with all sources listed
188 srcs = new String[sorted.length + dassrc.size()];
189 for (int j = sorted.length - 1; j >= 0; j--, i++)
191 srcs[i] = ((String[]) sorted[j])[0];
195 sorted = dassrc.toArray();
196 tosort = new String[sorted.length];
198 for (int j = 0; j < sorted.length; j++)
200 tosort[j] = ((String[]) sorted[j])[1];
202 jalview.util.QuickSort.sort(tosort, sorted);
203 for (int j = sorted.length - 1; j >= 0; j--, i++)
205 srcs[i] = ((String[]) sorted[j])[0];
212 * simple run method to test dbsources.
216 public static void main(String[] argv)
218 AlignmentI ds = null;
219 Vector noProds = new Vector();
220 String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
221 + "With no arguments, all DbSources will be queried with their test Accession number.\n"
222 + "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"
223 + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"
224 + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";
225 boolean withDas = true;
226 if (argv != null && argv.length > 0
227 && argv[0].toLowerCase().startsWith("-nodas"))
230 String targs[] = new String[argv.length - 1];
231 System.arraycopy(argv, 1, targs, 0, targs.length);
234 if (argv != null && argv.length > 0)
236 List<DbSourceProxy> sps = new SequenceFetcher(withDas)
237 .getSourceProxy(argv[0]);
241 for (DbSourceProxy sp : sps)
243 AlignmentI al = null;
246 al = sp.getSequenceRecords(argv.length > 1 ? argv[1] : sp
248 } catch (Exception e)
251 System.err.println("Error when retrieving "
252 + (argv.length > 1 ? argv[1] : sp.getTestQuery())
253 + " from " + argv[0] + "\nUsage: " + usage);
255 SequenceI[] prod = al.getSequencesArray();
258 for (int p = 0; p < prod.length; p++)
260 System.out.println("Prod " + p + ": "
261 + prod[p].getDisplayId(true) + " : "
262 + prod[p].getDescription());
270 System.err.println("Can't resolve " + argv[0]
271 + " as a database name. Allowed values are :\n"
272 + new SequenceFetcher().getSupportedDb());
274 System.out.println(usage);
277 ASequenceFetcher sfetcher = new SequenceFetcher(withDas);
278 String[] dbSources = sfetcher.getSupportedDb();
279 for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
281 String db = dbSources[dbsource];
283 if (db.equals(DBRefSource.PDB))
287 for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
289 System.out.println("Source: " + sp.getDbName() + " (" + db
290 + "): retrieving test:" + sp.getTestQuery());
291 AlignmentI al = null;
294 al = sp.getSequenceRecords(sp.getTestQuery());
295 if (al != null && al.getHeight() > 0
296 && sp.getDbSourceProperties() != null)
298 boolean dna = sp.getDbSourceProperties().containsKey(
299 DBRefSource.DNACODINGSEQDB)
300 || sp.getDbSourceProperties().containsKey(
301 DBRefSource.DNASEQDB)
302 || sp.getDbSourceProperties().containsKey(
303 DBRefSource.CODINGSEQDB);
304 // try and find products
305 String types[] = jalview.analysis.CrossRef
306 .findSequenceXrefTypes(dna, al.getSequencesArray());
309 System.out.println("Xref Types for: "
310 + (dna ? "dna" : "prot"));
311 for (int t = 0; t < types.length; t++)
313 System.out.println("Type: " + types[t]);
314 SequenceI[] prod = jalview.analysis.CrossRef
315 .findXrefSequences(al.getSequencesArray(), dna,
316 types[t]).getSequencesArray();
317 System.out.println("Found "
318 + ((prod == null) ? "no" : "" + prod.length)
322 for (int p = 0; p < prod.length; p++)
324 System.out.println("Prod " + p + ": "
325 + prod[p].getDisplayId(true));
332 noProds.addElement((dna ? new Object[] { al, al }
333 : new Object[] { al }));
337 } catch (Exception ex)
339 System.out.println("ERROR:Failed to retrieve test query.");
340 ex.printStackTrace(System.out);
345 System.out.println("ERROR:No alignment retrieved.");
346 StringBuffer raw = sp.getRawRecords();
349 System.out.println(raw.toString());
353 System.out.println("ERROR:No Raw results.");
358 System.out.println("Retrieved " + al.getHeight() + " sequences.");
359 for (int s = 0; s < al.getHeight(); s++)
361 SequenceI sq = al.getSequenceAt(s);
362 while (sq.getDatasetSequence() != null)
364 sq = sq.getDatasetSequence();
369 ds = new Alignment(new SequenceI[] { sq });
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);