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