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