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