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