Merge commit 'alpha/update_2_12_for_2_11_2_series_merge^2' into HEAD
[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.bin.ApplicationSingletonProvider;
24 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
25 import jalview.ext.ensembl.EnsemblGene;
26 import jalview.ws.dbsources.EmblCdsSource;
27 import jalview.ws.dbsources.EmblSource;
28 import jalview.ws.dbsources.Pdb;
29 import jalview.ws.dbsources.PfamFull;
30 import jalview.ws.dbsources.PfamSeed;
31 import jalview.ws.dbsources.RfamSeed;
32 import jalview.ws.dbsources.TDBeacons;
33 import jalview.ws.dbsources.Uniprot;
34 import jalview.ws.seqfetcher.ASequenceFetcher;
35 import jalview.ws.seqfetcher.DbSourceProxy;
36
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40
41 /**
42  * This implements the run-time discovery of sequence database clients.
43  * 
44  */
45 public class SequenceFetcher extends ASequenceFetcher implements ApplicationSingletonI
46 {
47   /*
48    * set a mock fetcher here for testing only - reset to null afterwards
49    */
50   private static SequenceFetcher mockFetcher;
51
52   /**
53    * Set the instance object to use (intended for unit testing with mock
54    * objects).
55    * 
56    * Be sure to reset to null in the tearDown method of any tests!
57    * 
58    * @param sf
59    */
60   public static void setMockFetcher(SequenceFetcher sf)
61   {
62     mockFetcher = sf;
63   }
64   
65   /**
66    * Returns a new SequenceFetcher singleton, or a mock object if one has been
67    * set.
68    * 
69    * @return
70    */
71   public static SequenceFetcher getInstance()
72   {
73     return mockFetcher != null ? mockFetcher
74             : (SequenceFetcher) ApplicationSingletonProvider
75                     .getInstance(SequenceFetcher.class);
76   }
77
78   /**
79    * Thread safe construction of database proxies TODO: extend to a configurable
80    * database plugin mechanism where classes are instantiated by reflection and
81    * queried for their DbRefSource and version association.
82    * 
83    */
84   public SequenceFetcher()
85   {
86     addDBRefSourceImpl(EnsemblGene.class);
87     // addDBRefSourceImpl(EnsemblGenomes.class);
88     addDBRefSourceImpl(EmblSource.class);
89     addDBRefSourceImpl(EmblCdsSource.class);
90     addDBRefSourceImpl(Uniprot.class);
91     // not a sequence source yet
92     // addDBRefSourceImpl(TDBeacons.class);
93     addDBRefSourceImpl(Pdb.class);
94     addDBRefSourceImpl(PfamFull.class);
95     addDBRefSourceImpl(PfamSeed.class);
96     addDBRefSourceImpl(RfamSeed.class);
97   }
98
99   /**
100    * return an ordered list of database sources excluding alignment only databases
101    */
102   public String[] getNonAlignmentSources()
103   {
104     String[] srcs = this.getSupportedDb();
105     List<String> src = new ArrayList<>();
106
107     for (int i = 0; i < srcs.length; i++)
108     {
109       boolean accept = true;
110       for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
111       {
112         // Skip the alignment databases for the moment - they're not useful for
113         // verifying a single sequence against its reference source
114         if (dbs.isAlignmentSource())
115         {
116           accept = false;
117           break;
118         }
119       }
120       if (accept)
121       {
122         src.add(srcs[i]);
123       }
124     }
125
126     Collections.sort(src, String.CASE_INSENSITIVE_ORDER);
127     return src.toArray(new String[src.size()]);
128   }
129 }