temp push
[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     addAllDatabases();
86   }
87
88   public void addAllDatabases()
89   {
90     addDBRefSourceImpl(EnsemblGene.class); // includes EnsemblGenomes.class
91     addDBRefSourceImpl(Uniprot.class); // includes UniprotName.class
92     // addDBRefSourceImpl(EmblSource.class);
93     // addDBRefSourceImpl(EmblCdsSource.class);
94     // addDBRefSourceImpl(Pdb.class);
95     // addDBRefSourceImpl(PfamFull.class);
96     // addDBRefSourceImpl(PfamSeed.class);
97     // addDBRefSourceImpl(RfamSeed.class);
98     addDBRefSourceImpl(DBRefSource.EMBL,
99             "jalview.ws.dbsources.EmblSource");
100     addDBRefSourceImpl(DBRefSource.EMBLCDS,
101             "jalview.ws.dbsources.EmblCdsSource");
102     addDBRefSourceImpl(DBRefSource.PDB, "jalview.ws.dbsources.Pdb");
103     addDBRefSourceImpl(DBRefSource.PFAM_FULL,
104             "jalview.ws.dbsources.PfamFull");
105     addDBRefSourceImpl(DBRefSource.PFAM_SEED,
106             "jalview.ws.dbsources.PfamSeed");
107     addDBRefSourceImpl(DBRefSource.RFAM_SEED,
108             "jalview.ws.dbsources.RfamSeed");
109   }
110
111   /**
112    * return an ordered list of database sources excluding alignment only
113    * databases
114    */
115   public String[] getNonAlignmentSources()
116   {
117     String[] srcs = this.getSupportedDb();
118     List<String> src = new ArrayList<>();
119     outer: for (int i = 0; i < srcs.length; i++)
120     {
121       for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
122       {
123         // Skip the alignment databases for the moment - they're not useful for
124         // verifying a single sequence against its reference source
125         if (dbs.isAlignmentSource())
126         {
127           continue outer;
128         }
129       }
130       src.add(srcs[i]);
131     }
132     Collections.sort(src, String.CASE_INSENSITIVE_ORDER);
133     return src.toArray(new String[src.size()]);
134   }
135 }