Merge branch 'develop' into features/JAL-1705_ensembl
[jalview.git] / src / jalview / ws / SequenceFetcher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ws;
22
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;
30
31 import java.util.ArrayList;
32 import java.util.Enumeration;
33 import java.util.List;
34 import java.util.Vector;
35
36 /**
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.
41  * 
42  */
43 public class SequenceFetcher extends ASequenceFetcher
44 {
45   /**
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.
49    * 
50    */
51   public SequenceFetcher()
52   {
53     this(true);
54   }
55
56   public SequenceFetcher(boolean addDas)
57   {
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);
63
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);
74     if (addDas)
75     {
76       registerDasSequenceSources();
77     }
78   }
79
80   /**
81    * return an ordered list of database sources where non-das database classes
82    * appear before das database classes
83    */
84   public String[] getOrderedSupportedSources()
85   {
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++)
89     {
90       boolean das = false, skip = false;
91       String nm;
92       for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
93       {
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))
97         {
98           skip = true;
99         }
100         else
101         {
102           nm = dbs.getDbName();
103           if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
104           {
105             if (nm.startsWith("das:"))
106             {
107               nm = nm.substring(4);
108               das = true;
109             }
110             break;
111           }
112         }
113       }
114       if (skip)
115       {
116         continue;
117       }
118       if (das)
119       {
120         dassrc.add(srcs[i]);
121       }
122       else
123       {
124         nondas.add(srcs[i]);
125       }
126     }
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++)
130     {
131       tosort[j] = tosort[j].toLowerCase();
132     }
133     jalview.util.QuickSort.sort(tosort, sorted);
134     // construct array with all sources listed
135
136     srcs = new String[sorted.length + dassrc.size()];
137     int i = 0;
138     for (int j = sorted.length - 1; j >= 0; j--, i++)
139     {
140       srcs[i] = sorted[j];
141       sorted[j] = null;
142     }
143
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++)
147     {
148       tosort[j] = tosort[j].toLowerCase();
149     }
150     jalview.util.QuickSort.sort(tosort, sorted);
151     for (int j = sorted.length - 1; j >= 0; j--, i++)
152     {
153       srcs[i] = sorted[j];
154     }
155     return srcs;
156   }
157
158   /**
159    * return plaintext databse list suitable for using in a GUI element
160    */
161   public String[] _getOrderedSupportedSources()
162   {
163     String[] srcs = this.getSupportedDb();
164     ArrayList dassrc = new ArrayList(), nondas = new ArrayList();
165     for (int i = 0; i < srcs.length; i++)
166     {
167       for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
168       {
169         String nm = dbs.getDbName();
170         if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
171         {
172           if (nm.startsWith("das:"))
173           {
174             nm = nm.substring(4);
175           }
176           dassrc.add(new String[] { srcs[i], nm.toUpperCase() });
177         }
178         else
179         {
180           nondas.add(new String[] { srcs[i], nm.toUpperCase() });
181         }
182       }
183     }
184     Object[] sorted = nondas.toArray();
185     String[] tosort = new String[sorted.length];
186     nondas.clear();
187     for (int j = 0; j < sorted.length; j++)
188     {
189       tosort[j] = ((String[]) sorted[j])[1];
190     }
191     jalview.util.QuickSort.sort(tosort, sorted);
192     int i = 0;
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++)
196     {
197       srcs[i] = ((String[]) sorted[j])[0];
198       sorted[j] = null;
199     }
200
201     sorted = dassrc.toArray();
202     tosort = new String[sorted.length];
203     dassrc.clear();
204     for (int j = 0; j < sorted.length; j++)
205     {
206       tosort[j] = ((String[]) sorted[j])[1];
207     }
208     jalview.util.QuickSort.sort(tosort, sorted);
209     for (int j = sorted.length - 1; j >= 0; j--, i++)
210     {
211       srcs[i] = ((String[]) sorted[j])[0];
212       sorted[j] = null;
213     }
214     return srcs;
215   }
216
217   /**
218    * simple run method to test dbsources.
219    * 
220    * @param argv
221    */
222   public static void main(String[] argv)
223   {
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"))
234     {
235       withDas = false;
236       String targs[] = new String[argv.length - 1];
237       System.arraycopy(argv, 1, targs, 0, targs.length);
238       argv = targs;
239     }
240     if (argv != null && argv.length > 0)
241     {
242       List<DbSourceProxy> sps = new SequenceFetcher(withDas)
243               .getSourceProxy(argv[0]);
244
245       if (sps != null)
246       {
247         for (DbSourceProxy sp : sps)
248         {
249           AlignmentI al = null;
250           try
251           {
252             al = sp.getSequenceRecords(argv.length > 1 ? argv[1] : sp
253                     .getTestQuery());
254           } catch (Exception e)
255           {
256             e.printStackTrace();
257             System.err.println("Error when retrieving "
258                     + (argv.length > 1 ? argv[1] : sp.getTestQuery())
259                     + " from " + argv[0] + "\nUsage: " + usage);
260           }
261           SequenceI[] prod = al.getSequencesArray();
262           if (al != null)
263           {
264             for (int p = 0; p < prod.length; p++)
265             {
266               System.out.println("Prod " + p + ": "
267                       + prod[p].getDisplayId(true) + " : "
268                       + prod[p].getDescription());
269             }
270           }
271         }
272         return;
273       }
274       else
275       {
276         System.err.println("Can't resolve " + argv[0]
277                 + " as a database name. Allowed values are :\n"
278                 + new SequenceFetcher().getSupportedDb());
279       }
280       System.out.println(usage);
281       return;
282     }
283     ASequenceFetcher sfetcher = new SequenceFetcher(withDas);
284     String[] dbSources = sfetcher.getSupportedDb();
285     for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
286     {
287       String db = dbSources[dbsource];
288       // skip me
289       if (db.equals(DBRefSource.PDB))
290       {
291         continue;
292       }
293       for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
294       {
295         System.out.println("Source: " + sp.getDbName() + " (" + db
296                 + "): retrieving test:" + sp.getTestQuery());
297         AlignmentI al = null;
298         try
299         {
300           al = sp.getSequenceRecords(sp.getTestQuery());
301           if (al != null && al.getHeight() > 0
302                   && sp.getDbSourceProperties() != null)
303           {
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());
313             if (types != null)
314             {
315               System.out.println("Xref Types for: "
316                       + (dna ? "dna" : "prot"));
317               for (int t = 0; t < types.length; t++)
318               {
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)
325                         + " products");
326                 if (prod != null)
327                 {
328                   for (int p = 0; p < prod.length; p++)
329                   {
330                     System.out.println("Prod " + p + ": "
331                             + prod[p].getDisplayId(true));
332                   }
333                 }
334               }
335             }
336             else
337             {
338               noProds.addElement((dna ? new Object[] { al, al }
339                       : new Object[] { al }));
340             }
341
342           }
343         } catch (Exception ex)
344         {
345           System.out.println("ERROR:Failed to retrieve test query.");
346           ex.printStackTrace(System.out);
347         }
348
349         if (al == null)
350         {
351           System.out.println("ERROR:No alignment retrieved.");
352           StringBuffer raw = sp.getRawRecords();
353           if (raw != null)
354           {
355             System.out.println(raw.toString());
356           }
357           else
358           {
359             System.out.println("ERROR:No Raw results.");
360           }
361         }
362         else
363         {
364           System.out.println("Retrieved " + al.getHeight() + " sequences.");
365           for (int s = 0; s < al.getHeight(); s++)
366           {
367             SequenceI sq = al.getSequenceAt(s);
368             while (sq.getDatasetSequence() != null)
369             {
370               sq = sq.getDatasetSequence();
371
372             }
373             if (ds == null)
374             {
375               ds = new Alignment(new SequenceI[] { sq });
376
377             }
378             else
379             {
380               ds.addSequence(sq);
381             }
382           }
383         }
384         System.out.flush();
385         System.err.flush();
386
387       }
388       if (noProds.size() > 0)
389       {
390         Enumeration ts = noProds.elements();
391         while (ts.hasMoreElements())
392
393         {
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
402           // sequences.
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())
408                   + " products");
409           if (prodal != null)
410           {
411             SequenceI[] prod = prodal.getSequencesArray(); // note
412             // should
413             // test
414             // rather
415             // than
416             // throw
417             // away
418             // codon
419             // mapping
420             // (if
421             // present)
422             for (int p = 0; p < prod.length; p++)
423             {
424               System.out.println("Prod " + p + ": "
425                       + prod[p].getDisplayId(true));
426             }
427           }
428         }
429
430       }
431
432     }
433   }
434
435   /**
436    * query the currently defined DAS source registry for sequence sources and
437    * add a DasSequenceSource instance for each source to the SequenceFetcher
438    * source list.
439    */
440   public void registerDasSequenceSources()
441   {
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()
445             .getSources())
446     {
447       if (source.isSequenceSource())
448       {
449         List<DbSourceProxy> dassources = source.getSequenceSourceProxies();
450         for (DbSourceProxy seqsrc : dassources)
451         {
452           addDbRefSourceImpl(seqsrc);
453         }
454       }
455     }
456   }
457
458 }