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