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