JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / ws / seqfetcher / ASequenceFetcher.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.seqfetcher;
22
23 import jalview.api.FeatureSettingsModelI;
24 import jalview.bin.Console;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.DBRefUtils;
29 import jalview.util.MessageManager;
30
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.Enumeration;
35 import java.util.HashSet;
36 import java.util.Hashtable;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Stack;
40 import java.util.Vector;
41
42 public class ASequenceFetcher
43 {
44
45   /*
46    * set of databases we can retrieve entries from
47    */
48   protected Hashtable<String, Map<String, DbSourceProxy>> fetchableDbs;
49
50   /*
51    * comparator to sort by tier (0/1/2) and name
52    */
53   private Comparator<DbSourceProxy> proxyComparator;
54
55   /**
56    * Constructor
57    */
58   protected ASequenceFetcher()
59   {
60     super();
61
62     /*
63      * comparator to sort proxies by tier and name
64      */
65     proxyComparator = new Comparator<DbSourceProxy>()
66     {
67       @Override
68       public int compare(DbSourceProxy o1, DbSourceProxy o2)
69       {
70         /*
71          * Tier 0 precedes 1 precedes 2
72          */
73         int compared = Integer.compare(o1.getTier(), o2.getTier());
74         if (compared == 0)
75         {
76           // defend against NullPointer - should never happen
77           String o1Name = o1.getDbName();
78           String o2Name = o2.getDbName();
79           if (o1Name != null && o2Name != null)
80           {
81             compared = o1Name.compareToIgnoreCase(o2Name);
82           }
83         }
84         return compared;
85       }
86     };
87   }
88
89   /**
90    * get array of supported Databases
91    * 
92    * @return database source string for each database - only the latest version
93    *         of a source db is bound to each source.
94    */
95   public String[] getSupportedDb()
96   {
97     if (fetchableDbs == null)
98     {
99       return null;
100     }
101     String[] sf = fetchableDbs.keySet()
102             .toArray(new String[fetchableDbs.size()]);
103     return sf;
104   }
105
106   public boolean isFetchable(String source)
107   {
108     for (String db : fetchableDbs.keySet())
109     {
110       if (source.equalsIgnoreCase(db))
111       {
112         return true;
113       }
114     }
115     Console.warn("isFetchable doesn't know about '" + source + "'");
116     return false;
117   }
118
119   /**
120    * Fetch sequences for the given cross-references
121    * 
122    * @param refs
123    * @param dna
124    *          if true, only fetch from nucleotide data sources, else peptide
125    * @return
126    */
127   public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
128   {
129     Vector<SequenceI> rseqs = new Vector<>();
130     Hashtable<String, List<String>> queries = new Hashtable<>();
131     for (DBRefEntry ref : refs)
132     {
133       String canonical = DBRefUtils.getCanonicalName(ref.getSource());
134       if (!queries.containsKey(canonical))
135       {
136         queries.put(canonical, new ArrayList<String>());
137       }
138       List<String> qset = queries.get(canonical);
139       if (!qset.contains(ref.getAccessionId()))
140       {
141         qset.add(ref.getAccessionId());
142       }
143     }
144     Enumeration<String> e = queries.keys();
145     while (e.hasMoreElements())
146     {
147       List<String> query = null;
148       String db = null;
149       db = e.nextElement();
150       query = queries.get(db);
151       if (!isFetchable(db))
152       {
153         reportStdError(db, query, new Exception(
154                 "Don't know how to fetch from this database :" + db));
155         continue;
156       }
157
158       Stack<String> queriesLeft = new Stack<>();
159       queriesLeft.addAll(query);
160
161       List<DbSourceProxy> proxies = getSourceProxy(db);
162       for (DbSourceProxy fetcher : proxies)
163       {
164         List<String> queriesMade = new ArrayList<>();
165         HashSet<String> queriesFound = new HashSet<>();
166         try
167         {
168           if (fetcher.isDnaCoding() != dna)
169           {
170             continue; // wrong sort of data
171           }
172           boolean doMultiple = fetcher.getMaximumQueryCount() > 1;
173           while (!queriesLeft.isEmpty())
174           {
175             StringBuffer qsb = new StringBuffer();
176             do
177             {
178               if (qsb.length() > 0)
179               {
180                 qsb.append(fetcher.getAccessionSeparator());
181               }
182               String q = queriesLeft.pop();
183               queriesMade.add(q);
184               qsb.append(q);
185             } while (doMultiple && !queriesLeft.isEmpty());
186
187             AlignmentI seqset = null;
188             try
189             {
190               // create a fetcher and go to it
191               seqset = fetcher.getSequenceRecords(qsb.toString());
192             } catch (Exception ex)
193             {
194               System.err.println(
195                       "Failed to retrieve the following from " + db);
196               System.err.println(qsb);
197               ex.printStackTrace(System.err);
198             }
199             // TODO: Merge alignment together - perhaps
200             if (seqset != null)
201             {
202               SequenceI seqs[] = seqset.getSequencesArray();
203               if (seqs != null)
204               {
205                 for (int is = 0; is < seqs.length; is++)
206                 {
207                   rseqs.addElement(seqs[is]);
208                   // BH 2015.01.25 check about version/accessid being null here
209                   List<DBRefEntry> frefs = DBRefUtils.searchRefs(
210                           seqs[is].getDBRefs(),
211                           new DBRefEntry(db, null, null),
212                           DBRefUtils.SEARCH_MODE_FULL);
213                   for (DBRefEntry dbr : frefs)
214                   {
215                     queriesFound.add(dbr.getAccessionId());
216                     queriesMade.remove(dbr.getAccessionId());
217                   }
218                   seqs[is] = null;
219                 }
220               }
221               else
222               {
223                 if (fetcher.getRawRecords() != null)
224                 {
225                   System.out.println(
226                           "# Retrieved from " + db + ":" + qsb.toString());
227                   StringBuffer rrb = fetcher.getRawRecords();
228                   /*
229                    * for (int rr = 0; rr<rrb.length; rr++) {
230                    */
231                   String hdr;
232                   // if (rr<qs.length)
233                   // {
234                   hdr = "# " + db + ":" + qsb.toString();
235                   /*
236                    * } else { hdr = "# part "+rr; }
237                    */
238                   System.out.println(hdr);
239                   if (rrb != null)
240                   {
241                     System.out.println(rrb);
242                   }
243                   System.out.println("# end of " + hdr);
244                 }
245
246               }
247             }
248
249           }
250         } catch (Exception ex)
251         {
252           reportStdError(db, queriesMade, ex);
253         }
254         if (queriesMade.size() > 0)
255         {
256           System.out.println("# Adding " + queriesMade.size()
257                   + " ids back to queries list for searching again (" + db
258                   + ")");
259           queriesLeft.addAll(queriesMade);
260         }
261       }
262     }
263
264     SequenceI[] result = null;
265     if (rseqs.size() > 0)
266     {
267       result = new SequenceI[rseqs.size()];
268       int si = 0;
269       for (SequenceI s : rseqs)
270       {
271         result[si++] = s;
272         s.updatePDBIds();
273       }
274     }
275     return result;
276   }
277
278   public void reportStdError(String db, List<String> queriesMade,
279           Exception ex)
280   {
281
282     System.err.println(
283             "Failed to retrieve the following references from " + db);
284     int n = 0;
285     for (String qv : queriesMade)
286     {
287       System.err.print(" " + qv + ";");
288       if (n++ > 10)
289       {
290         System.err.println();
291         n = 0;
292       }
293     }
294     System.err.println();
295     ex.printStackTrace();
296   }
297
298   /**
299    * Returns a list of proxies for the given source
300    * 
301    * @param db
302    *          database source string TODO: add version string/wildcard for
303    *          retrieval of specific DB source/version combinations.
304    * @return a list of DbSourceProxy for the db
305    */
306   public List<DbSourceProxy> getSourceProxy(String db)
307   {
308     db = DBRefUtils.getCanonicalName(db);
309     Map<String, DbSourceProxy> dblist = fetchableDbs.get(db);
310     if (dblist == null)
311     {
312       return new ArrayList<>();
313     }
314
315     /*
316      * sort so that primary sources precede secondary
317      */
318     List<DbSourceProxy> dbs = new ArrayList<>(dblist.values());
319     Collections.sort(dbs, proxyComparator);
320     return dbs;
321   }
322
323   /**
324    * constructs an instance of the proxy and registers it as a valid dbrefsource
325    * 
326    * @param dbSourceProxy
327    *          reference for class implementing
328    *          jalview.ws.seqfetcher.DbSourceProxy
329    */
330   protected void addDBRefSourceImpl(
331           Class<? extends DbSourceProxy> dbSourceProxy)
332           throws IllegalArgumentException
333   {
334     DbSourceProxy proxy = null;
335     try
336     {
337       DbSourceProxy proxyObj = dbSourceProxy.getConstructor().newInstance();
338       proxy = proxyObj;
339     } catch (IllegalArgumentException e)
340     {
341       throw e;
342     } catch (Exception e)
343     {
344       // Serious problems if this happens.
345       throw new Error(MessageManager
346               .getString("error.dbrefsource_implementation_exception"), e);
347     }
348     addDbRefSourceImpl(proxy);
349   }
350
351   /**
352    * add the properly initialised DbSourceProxy object 'proxy' to the list of
353    * sequence fetchers
354    * 
355    * @param proxy
356    */
357   protected void addDbRefSourceImpl(DbSourceProxy proxy)
358   {
359     if (proxy != null)
360     {
361       if (fetchableDbs == null)
362       {
363         fetchableDbs = new Hashtable<>();
364       }
365       Map<String, DbSourceProxy> slist = fetchableDbs
366               .get(proxy.getDbSource());
367       if (slist == null)
368       {
369         fetchableDbs.put(proxy.getDbSource(), slist = new Hashtable<>());
370       }
371       slist.put(proxy.getDbName(), proxy);
372     }
373   }
374
375   /**
376    * select sources which are implemented by instances of the given class
377    * 
378    * @param class
379    *          that implements DbSourceProxy
380    * @return null or vector of source names for fetchers
381    */
382   public String[] getDbInstances(Class class1)
383   {
384     if (!DbSourceProxy.class.isAssignableFrom(class1))
385     {
386       throw new Error(MessageManager.formatMessage(
387               "error.implementation_error_dbinstance_must_implement_interface",
388               new String[]
389               { class1.toString() }));
390     }
391     if (fetchableDbs == null)
392     {
393       return null;
394     }
395     String[] sources = null;
396     Vector<String> src = new Vector<>();
397     Enumeration<String> dbs = fetchableDbs.keys();
398     while (dbs.hasMoreElements())
399     {
400       String dbn = dbs.nextElement();
401       for (DbSourceProxy dbp : fetchableDbs.get(dbn).values())
402       {
403         if (class1.isAssignableFrom(dbp.getClass()))
404         {
405           src.addElement(dbn);
406         }
407       }
408     }
409     if (src.size() > 0)
410     {
411       src.copyInto(sources = new String[src.size()]);
412     }
413     return sources;
414   }
415
416   public DbSourceProxy[] getDbSourceProxyInstances(Class class1)
417   {
418     List<DbSourceProxy> prlist = new ArrayList<>();
419     for (String fetchable : getSupportedDb())
420     {
421       for (DbSourceProxy pr : getSourceProxy(fetchable))
422       {
423         if (class1.isInstance(pr))
424         {
425           prlist.add(pr);
426         }
427       }
428     }
429     if (prlist.size() == 0)
430     {
431       return null;
432     }
433     return prlist.toArray(new DbSourceProxy[0]);
434   }
435
436   /**
437    * Returns a preferred feature colouring scheme for the given source, or null
438    * if none is defined.
439    * 
440    * @param source
441    * @return
442    */
443   public FeatureSettingsModelI getFeatureColourScheme(String source)
444   {
445     /*
446      * return the first non-null colour scheme for any proxy for
447      * this database source
448      */
449     for (DbSourceProxy proxy : getSourceProxy(source))
450     {
451       FeatureSettingsModelI preferredColours = proxy
452               .getFeatureColourScheme();
453       if (preferredColours != null)
454       {
455         return preferredColours;
456       }
457     }
458     return null;
459   }
460 }