JAL-636 tweak main() based test to allow DAS sources to be excluded from basic Sequen...
[jalview.git] / src / jalview / ws / SequenceFetcher.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
4  * \r
5  * This file is part of Jalview.\r
6  * \r
7  * Jalview is free software: you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License \r
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
10  * \r
11  * Jalview is distributed in the hope that it will be useful, but \r
12  * WITHOUT ANY WARRANTY; without even the implied warranty \r
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
14  * PURPOSE.  See the GNU General Public License for more details.\r
15  * \r
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
17  */\r
18 package jalview.ws;\r
19 \r
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.dbsources.das.datamodel.DasSequenceSource;\r
26 import jalview.ws.seqfetcher.ASequenceFetcher;\r
27 import jalview.ws.seqfetcher.DbSourceProxy;\r
28 \r
29 import java.util.ArrayList;\r
30 import java.util.Enumeration;\r
31 import java.util.List;\r
32 import java.util.Vector;\r
33 \r
34 /**\r
35  * This is the the concrete implementation of the sequence retrieval interface\r
36  * and abstract class in jalview.ws.seqfetcher. This implements the run-time\r
37  * discovery of sequence database clients, and provides a hardwired main for\r
38  * testing all registered handlers.\r
39  * \r
40  */\r
41 public class SequenceFetcher extends ASequenceFetcher\r
42 {\r
43   /**\r
44    * Thread safe construction of database proxies TODO: extend to a configurable\r
45    * database plugin mechanism where classes are instantiated by reflection and\r
46    * queried for their DbRefSource and version association.\r
47    * \r
48    */\r
49   public SequenceFetcher()\r
50   {\r
51     this(true);\r
52   }\r
53   public SequenceFetcher(boolean addDas)\r
54   {\r
55     addDBRefSourceImpl(jalview.ws.dbsources.EmblSource.class);\r
56     addDBRefSourceImpl(jalview.ws.dbsources.EmblCdsSouce.class);\r
57     addDBRefSourceImpl(jalview.ws.dbsources.Uniprot.class);\r
58     addDBRefSourceImpl(jalview.ws.dbsources.UnprotName.class);\r
59     addDBRefSourceImpl(jalview.ws.dbsources.Pdb.class);\r
60     addDBRefSourceImpl(jalview.ws.dbsources.PfamFull.class);\r
61     addDBRefSourceImpl(jalview.ws.dbsources.PfamSeed.class); // ensures Seed\r
62     // alignment is\r
63     // 'default' for\r
64     // PFAM\r
65     addDBRefSourceImpl(jalview.ws.dbsources.RfamFull.class);\r
66     addDBRefSourceImpl(jalview.ws.dbsources.RfamSeed.class);\r
67     if (addDas) {\r
68       registerDasSequenceSources();\r
69     }\r
70   }\r
71 \r
72   /**\r
73    * return an ordered list of database sources where non-das database classes\r
74    * appear before das database classes\r
75    */\r
76   public String[] getOrderedSupportedSources()\r
77   {\r
78     String[] srcs = this.getSupportedDb();\r
79     ArrayList<String> dassrc = new ArrayList<String>(), nondas = new ArrayList<String>();\r
80     for (int i = 0; i < srcs.length; i++)\r
81     {\r
82       boolean das = false,skip=false;\r
83       String nm;\r
84       for (DbSourceProxy dbs : getSourceProxy(srcs[i]))\r
85       {\r
86         // Skip the alignment databases for the moment - they're not useful for verifying a single sequence against its reference source\r
87         if (dbs.isA(DBRefSource.ALIGNMENTDB))\r
88         {\r
89           skip=true;\r
90         } else {\r
91           nm = dbs.getDbName();\r
92           if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)\r
93           {\r
94             if (nm.startsWith("das:"))\r
95             {\r
96               nm = nm.substring(4);\r
97               das = true;\r
98             }\r
99             break;\r
100           }\r
101         }\r
102       }\r
103       if (skip)\r
104       {\r
105         continue;\r
106       }\r
107       if (das)\r
108       {\r
109         dassrc.add(srcs[i]);\r
110       }\r
111       else\r
112       {\r
113         nondas.add(srcs[i]);\r
114       }\r
115     }\r
116     String[] tosort = nondas.toArray(new String[0]), sorted = nondas\r
117             .toArray(new String[0]);\r
118     for (int j = 0, jSize = sorted.length; j < jSize; j++)\r
119     {\r
120       tosort[j] = tosort[j].toLowerCase();\r
121     }\r
122     jalview.util.QuickSort.sort(tosort, sorted);\r
123     // construct array with all sources listed\r
124 \r
125     srcs = new String[sorted.length + dassrc.size()];\r
126     int i = 0;\r
127     for (int j = sorted.length - 1; j >= 0; j--, i++)\r
128     {\r
129       srcs[i] = sorted[j];\r
130       sorted[j] = null;\r
131     }\r
132 \r
133     sorted = dassrc.toArray(new String[0]);\r
134     tosort = dassrc.toArray(new String[0]);\r
135     for (int j = 0, jSize = sorted.length; j < jSize; j++)\r
136     {\r
137       tosort[j] = tosort[j].toLowerCase();\r
138     }\r
139     jalview.util.QuickSort.sort(tosort, sorted);\r
140     for (int j = sorted.length - 1; j >= 0; j--, i++)\r
141     {\r
142       srcs[i] = sorted[j];\r
143     }\r
144     return srcs;\r
145   }\r
146 \r
147   /**\r
148    * return plaintext databse list suitable for using in a GUI element\r
149    */\r
150   public String[] _getOrderedSupportedSources()\r
151   {\r
152     String[] srcs = this.getSupportedDb();\r
153     ArrayList dassrc = new ArrayList(), nondas = new ArrayList();\r
154     for (int i = 0; i < srcs.length; i++)\r
155     {\r
156       for (DbSourceProxy dbs : getSourceProxy(srcs[i]))\r
157       {\r
158         String nm = dbs.getDbName();\r
159         if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)\r
160         {\r
161           if (nm.startsWith("das:"))\r
162           {\r
163             nm = nm.substring(4);\r
164           }\r
165           dassrc.add(new String[]\r
166           { srcs[i], nm.toUpperCase() });\r
167         }\r
168         else\r
169         {\r
170           nondas.add(new String[]\r
171           { srcs[i], nm.toUpperCase() });\r
172         }\r
173       }\r
174     }\r
175     Object[] sorted = nondas.toArray();\r
176     String[] tosort = new String[sorted.length];\r
177     nondas.clear();\r
178     for (int j = 0; j < sorted.length; j++)\r
179     {\r
180       tosort[j] = ((String[]) sorted[j])[1];\r
181     }\r
182     jalview.util.QuickSort.sort(tosort, sorted);\r
183     int i = 0;\r
184     // construct array with all sources listed\r
185     srcs = new String[sorted.length + dassrc.size()];\r
186     for (int j = sorted.length - 1; j >= 0; j--, i++)\r
187     {\r
188       srcs[i] = ((String[]) sorted[j])[0];\r
189       sorted[j] = null;\r
190     }\r
191 \r
192     sorted = dassrc.toArray();\r
193     tosort = new String[sorted.length];\r
194     dassrc.clear();\r
195     for (int j = 0; j < sorted.length; j++)\r
196     {\r
197       tosort[j] = ((String[]) sorted[j])[1];\r
198     }\r
199     jalview.util.QuickSort.sort(tosort, sorted);\r
200     for (int j = sorted.length - 1; j >= 0; j--, i++)\r
201     {\r
202       srcs[i] = ((String[]) sorted[j])[0];\r
203       sorted[j] = null;\r
204     }\r
205     return srcs;\r
206   }\r
207 \r
208   /**\r
209    * simple run method to test dbsources.\r
210    * \r
211    * @param argv\r
212    */\r
213   public static void main(String[] argv)\r
214   {\r
215     AlignmentI ds = null;\r
216     Vector noProds = new Vector();\r
217     String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"\r
218             + "With no arguments, all DbSources will be queried with their test Accession number.\n"\r
219             + "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
220             + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"\r
221             + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";\r
222     boolean withDas=true;\r
223     if (argv!=null && argv.length>0 && argv[0].toLowerCase().startsWith("-nodas"))\r
224     {\r
225       withDas=false;\r
226       String targs[] = new String[argv.length-1];\r
227       System.arraycopy(argv, 1, targs, 0, targs.length);\r
228       argv=targs;\r
229     }\r
230     if (argv != null && argv.length > 0)\r
231     {\r
232       List<DbSourceProxy> sps = new SequenceFetcher(withDas)\r
233               .getSourceProxy(argv[0]);\r
234 \r
235       if (sps != null)\r
236       {\r
237         for (DbSourceProxy sp : sps)\r
238         {\r
239           AlignmentI al = null;\r
240           try\r
241           {\r
242             al = sp.getSequenceRecords(argv.length>1 ? argv[1] : sp.getTestQuery());\r
243           } catch (Exception e)\r
244           {\r
245             e.printStackTrace();\r
246             System.err.println("Error when retrieving " + (argv.length>1 ? argv[1] : sp.getTestQuery())\r
247                     + " from " + argv[0] + "\nUsage: " + usage);\r
248           }\r
249           SequenceI[] prod = al.getSequencesArray();\r
250           if (al != null)\r
251           {\r
252             for (int p = 0; p < prod.length; p++)\r
253             {\r
254               System.out.println("Prod " + p + ": "\r
255                       + prod[p].getDisplayId(true) + " : "\r
256                       + prod[p].getDescription());\r
257             }\r
258           }\r
259         }\r
260         return;\r
261       }\r
262       else\r
263       {\r
264         System.err.println("Can't resolve " + argv[0]\r
265                 + " as a database name. Allowed values are :\n"\r
266                 + new SequenceFetcher().getSupportedDb());\r
267       }\r
268       System.out.println(usage);\r
269       return;\r
270     }\r
271     ASequenceFetcher sfetcher = new SequenceFetcher(withDas);\r
272     String[] dbSources = sfetcher.getSupportedDb();\r
273     for (int dbsource = 0; dbsource < dbSources.length; dbsource++)\r
274     {\r
275       String db = dbSources[dbsource];\r
276       // skip me\r
277       if (db.equals(DBRefSource.PDB))\r
278         continue;\r
279       for (DbSourceProxy sp : sfetcher.getSourceProxy(db))\r
280       {\r
281         System.out.println("Source: " + sp.getDbName() + " (" + db\r
282                 + "): retrieving test:" + sp.getTestQuery());\r
283         AlignmentI al = null;\r
284         try\r
285         {\r
286           al = sp.getSequenceRecords(sp.getTestQuery());\r
287           if (al != null && al.getHeight() > 0\r
288                   && sp.getDbSourceProperties() != null)\r
289           {\r
290             boolean dna = sp.getDbSourceProperties().containsKey(\r
291                     DBRefSource.DNACODINGSEQDB)\r
292                     || sp.getDbSourceProperties().containsKey(\r
293                             DBRefSource.DNASEQDB)\r
294                     || sp.getDbSourceProperties().containsKey(\r
295                             DBRefSource.CODINGSEQDB);\r
296             // try and find products\r
297             String types[] = jalview.analysis.CrossRef\r
298                     .findSequenceXrefTypes(dna, al.getSequencesArray());\r
299             if (types != null)\r
300             {\r
301               System.out.println("Xref Types for: "\r
302                       + (dna ? "dna" : "prot"));\r
303               for (int t = 0; t < types.length; t++)\r
304               {\r
305                 System.out.println("Type: " + types[t]);\r
306                 SequenceI[] prod = jalview.analysis.CrossRef\r
307                         .findXrefSequences(al.getSequencesArray(), dna,\r
308                                 types[t]).getSequencesArray();\r
309                 System.out.println("Found "\r
310                         + ((prod == null) ? "no" : "" + prod.length)\r
311                         + " products");\r
312                 if (prod != null)\r
313                 {\r
314                   for (int p = 0; p < prod.length; p++)\r
315                   {\r
316                     System.out.println("Prod " + p + ": "\r
317                             + prod[p].getDisplayId(true));\r
318                   }\r
319                 }\r
320               }\r
321             }\r
322             else\r
323             {\r
324               noProds.addElement((dna ? new Object[]\r
325               { al, al } : new Object[]\r
326               { al }));\r
327             }\r
328 \r
329           }\r
330         } catch (Exception ex)\r
331         {\r
332           System.out.println("ERROR:Failed to retrieve test query.");\r
333           ex.printStackTrace(System.out);\r
334         }\r
335 \r
336         if (al == null)\r
337         {\r
338           System.out.println("ERROR:No alignment retrieved.");\r
339           StringBuffer raw = sp.getRawRecords();\r
340           if (raw != null)\r
341             System.out.println(raw.toString());\r
342           else\r
343             System.out.println("ERROR:No Raw results.");\r
344         }\r
345         else\r
346         {\r
347           System.out.println("Retrieved " + al.getHeight() + " sequences.");\r
348           for (int s = 0; s < al.getHeight(); s++)\r
349           {\r
350             SequenceI sq = al.getSequenceAt(s);\r
351             while (sq.getDatasetSequence() != null)\r
352             {\r
353               sq = sq.getDatasetSequence();\r
354 \r
355             }\r
356             if (ds == null)\r
357             {\r
358               ds = new Alignment(new SequenceI[]\r
359               { sq });\r
360 \r
361             }\r
362             else\r
363             {\r
364               ds.addSequence(sq);\r
365             }\r
366           }\r
367         }\r
368         System.out.flush();\r
369         System.err.flush();\r
370 \r
371       }\r
372       if (noProds.size() > 0)\r
373       {\r
374         Enumeration ts = noProds.elements();\r
375         while (ts.hasMoreElements())\r
376 \r
377         {\r
378           Object[] typeSq = (Object[]) ts.nextElement();\r
379           boolean dna = (typeSq.length > 1);\r
380           AlignmentI al = (AlignmentI) typeSq[0];\r
381           System.out.println("Trying getProducts for "\r
382                   + al.getSequenceAt(0).getDisplayId(true));\r
383           System.out.println("Search DS Xref for: "\r
384                   + (dna ? "dna" : "prot"));\r
385           // have a bash at finding the products amongst all the retrieved\r
386           // sequences.\r
387           SequenceI[] seqs = al.getSequencesArray();\r
388           Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(\r
389                   seqs, dna, null, ds);\r
390           System.out.println("Found "\r
391                   + ((prodal == null) ? "no" : "" + prodal.getHeight())\r
392                   + " products");\r
393           if (prodal != null)\r
394           {\r
395             SequenceI[] prod = prodal.getSequencesArray(); // note\r
396             // should\r
397             // test\r
398             // rather\r
399             // than\r
400             // throw\r
401             // away\r
402             // codon\r
403             // mapping\r
404             // (if\r
405             // present)\r
406             for (int p = 0; p < prod.length; p++)\r
407             {\r
408               System.out.println("Prod " + p + ": "\r
409                       + prod[p].getDisplayId(true));\r
410             }\r
411           }\r
412         }\r
413 \r
414       }\r
415 \r
416     }\r
417   }\r
418 \r
419   /**\r
420    * query the currently defined DAS source registry for sequence sources and\r
421    * add a DasSequenceSource instance for each source to the SequenceFetcher\r
422    * source list.\r
423    */\r
424   public void registerDasSequenceSources()\r
425   {\r
426     // TODO: define a context as a registry provider (either desktop,\r
427     // jalview.bin.cache, or something else).\r
428     for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry()\r
429             .getSources())\r
430     {\r
431       if (source.isSequenceSource())\r
432       {\r
433         List<DbSourceProxy> dassources = source.getSequenceSourceProxies();\r
434         for (DbSourceProxy seqsrc : dassources)\r
435         {\r
436           addDbRefSourceImpl(seqsrc);\r
437         }\r
438       }\r
439     }\r
440   }\r
441 \r
442 }\r