JAL-1705 DbSourceProxy properties converted to methods, tidy/format code
[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.ext.ensembl.EnsemblCdna;
24 import jalview.ext.ensembl.EnsemblCds;
25 import jalview.ext.ensembl.EnsemblGenome;
26 import jalview.ext.ensembl.EnsemblProtein;
27 import jalview.ws.dbsources.EmblCdsSource;
28 import jalview.ws.dbsources.EmblSource;
29 import jalview.ws.dbsources.Pdb;
30 import jalview.ws.dbsources.PfamFull;
31 import jalview.ws.dbsources.PfamSeed;
32 import jalview.ws.dbsources.RfamFull;
33 import jalview.ws.dbsources.RfamSeed;
34 import jalview.ws.dbsources.Uniprot;
35 import jalview.ws.dbsources.UniprotName;
36 import jalview.ws.dbsources.das.api.jalviewSourceI;
37 import jalview.ws.seqfetcher.ASequenceFetcher;
38 import jalview.ws.seqfetcher.DbSourceProxy;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 /**
44  * This is the the concrete implementation of the sequence retrieval interface
45  * and abstract class in jalview.ws.seqfetcher. This implements the run-time
46  * discovery of sequence database clientss.
47  * 
48  */
49 public class SequenceFetcher extends ASequenceFetcher
50 {
51   /**
52    * Thread safe construction of database proxies TODO: extend to a configurable
53    * database plugin mechanism where classes are instantiated by reflection and
54    * queried for their DbRefSource and version association.
55    * 
56    */
57   public SequenceFetcher()
58   {
59     this(true);
60   }
61
62   public SequenceFetcher(boolean addDas)
63   {
64     addDBRefSourceImpl(EnsemblProtein.class);
65     // EnsemblTranscript would just replace EnsemblProtein as the proxy for
66     // { DbSource="ENSEMBL", DbName="ENSEMBL (Protein)" }
67     // addDBRefSourceImpl(EnsemblTranscript.class);
68     addDBRefSourceImpl(EnsemblCds.class);
69     addDBRefSourceImpl(EnsemblGenome.class);
70     addDBRefSourceImpl(EnsemblCdna.class);
71
72     addDBRefSourceImpl(EmblSource.class);
73     addDBRefSourceImpl(EmblCdsSource.class);
74     addDBRefSourceImpl(Uniprot.class);
75     addDBRefSourceImpl(UniprotName.class);
76     addDBRefSourceImpl(Pdb.class);
77     addDBRefSourceImpl(PfamFull.class);
78     addDBRefSourceImpl(PfamSeed.class);
79     // ensures Seed alignment is 'default' for PFAM
80     addDBRefSourceImpl(RfamFull.class);
81     addDBRefSourceImpl(RfamSeed.class);
82     if (addDas)
83     {
84       registerDasSequenceSources();
85     }
86   }
87
88   /**
89    * return an ordered list of database sources where non-das database classes
90    * appear before das database classes
91    */
92   public String[] getOrderedSupportedSources()
93   {
94     String[] srcs = this.getSupportedDb();
95     ArrayList<String> dassrc = new ArrayList<String>(), nondas = new ArrayList<String>();
96     for (int i = 0; i < srcs.length; i++)
97     {
98       boolean das = false, skip = false;
99       String nm;
100       for (DbSourceProxy dbs : getSourceProxy(srcs[i]))
101       {
102         // Skip the alignment databases for the moment - they're not useful for
103         // verifying a single sequence against its reference source
104         if (dbs.isAlignmentSource())
105         {
106           skip = true;
107         }
108         else
109         {
110           nm = dbs.getDbName();
111           if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
112           {
113             if (nm.startsWith("das:"))
114             {
115               nm = nm.substring(4);
116               das = true;
117             }
118             break;
119           }
120         }
121       }
122       if (skip)
123       {
124         continue;
125       }
126       if (das)
127       {
128         dassrc.add(srcs[i]);
129       }
130       else
131       {
132         nondas.add(srcs[i]);
133       }
134     }
135     String[] tosort = nondas.toArray(new String[0]), sorted = nondas
136             .toArray(new String[0]);
137     for (int j = 0, jSize = sorted.length; j < jSize; j++)
138     {
139       tosort[j] = tosort[j].toLowerCase();
140     }
141     jalview.util.QuickSort.sort(tosort, sorted);
142     // construct array with all sources listed
143
144     srcs = new String[sorted.length + dassrc.size()];
145     int i = 0;
146     for (int j = sorted.length - 1; j >= 0; j--, i++)
147     {
148       srcs[i] = sorted[j];
149       sorted[j] = null;
150     }
151
152     sorted = dassrc.toArray(new String[0]);
153     tosort = dassrc.toArray(new String[0]);
154     for (int j = 0, jSize = sorted.length; j < jSize; j++)
155     {
156       tosort[j] = tosort[j].toLowerCase();
157     }
158     jalview.util.QuickSort.sort(tosort, sorted);
159     for (int j = sorted.length - 1; j >= 0; j--, i++)
160     {
161       srcs[i] = sorted[j];
162     }
163     return srcs;
164   }
165
166   /**
167    * query the currently defined DAS source registry for sequence sources and
168    * add a DasSequenceSource instance for each source to the SequenceFetcher
169    * source list.
170    */
171   public void registerDasSequenceSources()
172   {
173     // TODO: define a context as a registry provider (either desktop,
174     // jalview.bin.cache, or something else).
175     for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry()
176             .getSources())
177     {
178       if (source.isSequenceSource())
179       {
180         List<DbSourceProxy> dassources = source.getSequenceSourceProxies();
181         for (DbSourceProxy seqsrc : dassources)
182         {
183           addDbRefSourceImpl(seqsrc);
184         }
185       }
186     }
187   }
188
189 }