2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
\r
3 * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
20 import jalview.datamodel.Alignment;
\r
21 import jalview.datamodel.AlignmentI;
\r
22 import jalview.datamodel.DBRefSource;
\r
23 import jalview.datamodel.SequenceI;
\r
24 import jalview.ws.dbsources.das.api.jalviewSourceI;
\r
25 import jalview.ws.seqfetcher.ASequenceFetcher;
\r
26 import jalview.ws.seqfetcher.DbSourceProxy;
\r
28 import java.util.ArrayList;
\r
29 import java.util.Enumeration;
\r
30 import java.util.List;
\r
31 import java.util.Vector;
\r
34 * This is the the concrete implementation of the sequence retrieval interface
\r
35 * and abstract class in jalview.ws.seqfetcher. This implements the run-time
\r
36 * discovery of sequence database clients, and provides a hardwired main for
\r
37 * testing all registered handlers.
\r
40 public class SequenceFetcher extends ASequenceFetcher
\r
43 * Thread safe construction of database proxies TODO: extend to a configurable
\r
44 * database plugin mechanism where classes are instantiated by reflection and
\r
45 * queried for their DbRefSource and version association.
\r
48 public SequenceFetcher()
\r
50 addDBRefSourceImpl(jalview.ws.dbsources.EmblSource.class);
\r
51 addDBRefSourceImpl(jalview.ws.dbsources.EmblCdsSouce.class);
\r
52 addDBRefSourceImpl(jalview.ws.dbsources.Uniprot.class);
\r
53 addDBRefSourceImpl(jalview.ws.dbsources.UnprotName.class);
\r
54 addDBRefSourceImpl(jalview.ws.dbsources.Pdb.class);
\r
55 addDBRefSourceImpl(jalview.ws.dbsources.PfamFull.class);
\r
56 addDBRefSourceImpl(jalview.ws.dbsources.PfamSeed.class); // ensures Seed
\r
60 registerDasSequenceSources();
\r
64 * return an ordered list of database sources suitable for using in a GUI
\r
67 public String[] getOrderedSupportedSources()
\r
69 String[] srcs = this.getSupportedDb();
\r
70 ArrayList dassrc = new ArrayList(), nondas = new ArrayList();
\r
71 for (int i = 0; i < srcs.length; i++)
\r
73 String nm = getSourceProxy(srcs[i]).getDbName();
\r
74 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
\r
76 if (nm.startsWith("das:"))
\r
78 nm = nm.substring(4);
\r
80 dassrc.add(new String[]
\r
81 { srcs[i], nm.toUpperCase() });
\r
85 nondas.add(new String[]
\r
86 { srcs[i], nm.toUpperCase() });
\r
89 Object[] sorted = nondas.toArray();
\r
90 String[] tosort = new String[sorted.length];
\r
92 for (int j = 0; j < sorted.length; j++)
\r
94 tosort[j] = ((String[]) sorted[j])[1];
\r
96 jalview.util.QuickSort.sort(tosort, sorted);
\r
98 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
100 srcs[i] = ((String[]) sorted[j])[0];
\r
104 sorted = dassrc.toArray();
\r
105 tosort = new String[sorted.length];
\r
107 for (int j = 0; j < sorted.length; j++)
\r
109 tosort[j] = ((String[]) sorted[j])[1];
\r
111 jalview.util.QuickSort.sort(tosort, sorted);
\r
112 for (int j = sorted.length - 1; j >= 0; j--, i++)
\r
114 srcs[i] = ((String[]) sorted[j])[0];
\r
121 * simple run method to test dbsources.
\r
125 public static void main(String[] argv)
\r
127 AlignmentI ds = null;
\r
128 Vector noProds = new Vector();
\r
129 String usage = "SequenceFetcher.main [<DBNAME> <ACCNO>]\n"
\r
130 + "With no arguments, all DbSources will be queried with their test Accession number.\n"
\r
131 + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.";
\r
132 if (argv != null && argv.length > 0)
\r
134 DbSourceProxy sp = new SequenceFetcher().getSourceProxy(argv[0]);
\r
137 AlignmentI al = null;
\r
140 al = sp.getSequenceRecords(argv[1]);
\r
141 } catch (Exception e)
\r
143 e.printStackTrace();
\r
144 System.err.println("Error when retrieving " + argv[1] + " from "
\r
145 + argv[0] + "\nUsage: " + usage);
\r
147 SequenceI[] prod = al.getSequencesArray();
\r
150 for (int p = 0; p < prod.length; p++)
\r
152 System.out.println("Prod " + p + ": "
\r
153 + prod[p].getDisplayId(true) + " : "
\r
154 + prod[p].getDescription());
\r
161 System.err.println("Can't resolve " + argv[0]
\r
162 + " as a database name. Allowed values are :\n"
\r
163 + new SequenceFetcher().getSupportedDb());
\r
165 System.out.println(usage);
\r
167 ASequenceFetcher sfetcher = new SequenceFetcher();
\r
168 String[] dbSources = sfetcher.getSupportedDb();
\r
169 for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
\r
171 String db = dbSources[dbsource];
\r
173 if (db.equals(DBRefSource.PDB))
\r
175 DbSourceProxy sp = sfetcher.getSourceProxy(db);
\r
176 System.out.println("Source: " + sp.getDbName() + " (" + db
\r
177 + "): retrieving test:" + sp.getTestQuery());
\r
178 AlignmentI al = null;
\r
181 al = sp.getSequenceRecords(sp.getTestQuery());
\r
182 if (al != null && al.getHeight() > 0
\r
183 && sp.getDbSourceProperties() != null)
\r
185 boolean dna = sp.getDbSourceProperties().containsKey(
\r
186 DBRefSource.DNACODINGSEQDB)
\r
187 || sp.getDbSourceProperties().containsKey(
\r
188 DBRefSource.DNASEQDB)
\r
189 || sp.getDbSourceProperties().containsKey(
\r
190 DBRefSource.CODINGSEQDB);
\r
191 // try and find products
\r
192 String types[] = jalview.analysis.CrossRef.findSequenceXrefTypes(
\r
193 dna, al.getSequencesArray());
\r
196 System.out.println("Xref Types for: " + (dna ? "dna" : "prot"));
\r
197 for (int t = 0; t < types.length; t++)
\r
199 System.out.println("Type: " + types[t]);
\r
200 SequenceI[] prod = jalview.analysis.CrossRef
\r
201 .findXrefSequences(al.getSequencesArray(), dna,
\r
202 types[t]).getSequencesArray();
\r
203 System.out.println("Found "
\r
204 + ((prod == null) ? "no" : "" + prod.length)
\r
208 for (int p = 0; p < prod.length; p++)
\r
210 System.out.println("Prod " + p + ": "
\r
211 + prod[p].getDisplayId(true));
\r
218 noProds.addElement((dna ? new Object[]
\r
219 { al, al } : new Object[]
\r
224 } catch (Exception ex)
\r
226 System.out.println("ERROR:Failed to retrieve test query.");
\r
227 ex.printStackTrace(System.out);
\r
231 System.out.println("ERROR:No alignment retrieved.");
\r
232 StringBuffer raw = sp.getRawRecords();
\r
234 System.out.println(raw.toString());
\r
236 System.out.println("ERROR:No Raw results.");
\r
240 System.out.println("Retrieved " + al.getHeight() + " sequences.");
\r
241 for (int s = 0; s < al.getHeight(); s++)
\r
243 SequenceI sq = al.getSequenceAt(s);
\r
244 while (sq.getDatasetSequence() != null)
\r
246 sq = sq.getDatasetSequence();
\r
251 ds = new Alignment(new SequenceI[]
\r
257 ds.addSequence(sq);
\r
261 System.out.flush();
\r
262 System.err.flush();
\r
265 if (noProds.size() > 0)
\r
267 Enumeration ts = noProds.elements();
\r
268 while (ts.hasMoreElements())
\r
271 Object[] typeSq = (Object[]) ts.nextElement();
\r
272 boolean dna = (typeSq.length > 1);
\r
273 AlignmentI al = (AlignmentI) typeSq[0];
\r
274 System.out.println("Trying getProducts for "
\r
275 + al.getSequenceAt(0).getDisplayId(true));
\r
276 System.out.println("Search DS Xref for: " + (dna ? "dna" : "prot"));
\r
277 // have a bash at finding the products amongst all the retrieved
\r
279 SequenceI[] seqs = al.getSequencesArray();
\r
280 Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
\r
281 seqs, dna, null, ds);
\r
282 System.out.println("Found "
\r
283 + ((prodal == null) ? "no" : "" + prodal.getHeight())
\r
285 if (prodal != null)
\r
287 SequenceI[] prod = prodal.getSequencesArray(); // note
\r
298 for (int p = 0; p < prod.length; p++)
\r
300 System.out.println("Prod " + p + ": "
\r
301 + prod[p].getDisplayId(true));
\r
310 * query the currently defined DAS source registry for sequence sources and
\r
311 * add a DasSequenceSource instance for each source to the SequenceFetcher
\r
314 public void registerDasSequenceSources()
\r
316 // TODO: define a context as a registry provider (either desktop,
\r
317 // jalview.bin.cache, or something else).
\r
318 for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry().getSources())
\r
320 if (source.isSequenceSource())
\r
322 List<DbSourceProxy> dassources = source.getSequenceSourceProxies();
\r
323 for (DbSourceProxy seqsrc : dassources)
\r
325 addDbRefSourceImpl(seqsrc);
\r