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