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