2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
\r
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.ws.seqfetcher;
\r
21 import jalview.datamodel.AlignmentI;
\r
22 import jalview.datamodel.SequenceI;
\r
24 import java.util.Enumeration;
\r
25 import java.util.Hashtable;
\r
26 import java.util.Vector;
\r
28 public class ASequenceFetcher
\r
32 * set of databases we can retrieve entries from
\r
34 protected Hashtable FETCHABLEDBS;
\r
36 public ASequenceFetcher()
\r
42 * get list of supported Databases
\r
44 * @return database source string for each database - only the latest version
\r
45 * of a source db is bound to each source.
\r
47 public String[] getSupportedDb()
\r
49 if (FETCHABLEDBS == null)
\r
51 String[] sf = new String[FETCHABLEDBS.size()];
\r
52 Enumeration e = FETCHABLEDBS.keys();
\r
54 while (e.hasMoreElements())
\r
56 sf[i++] = (String) e.nextElement();
\r
62 public boolean isFetchable(String source)
\r
64 Enumeration e = FETCHABLEDBS.keys();
\r
65 while (e.hasMoreElements())
\r
67 String db = (String) e.nextElement();
\r
68 if (source.compareToIgnoreCase(db) == 0)
\r
71 jalview.bin.Cache.log.warn("isFetchable doesn't know about '" + source
\r
76 public SequenceI[] getSequences(jalview.datamodel.DBRefEntry[] refs)
\r
78 SequenceI[] ret = null;
\r
79 Vector rseqs = new Vector();
\r
80 Hashtable queries = new Hashtable();
\r
81 for (int r = 0; r < refs.length; r++)
\r
83 if (!queries.containsKey(refs[r].getSource()))
\r
85 queries.put(refs[r].getSource(), new Vector());
\r
87 Vector qset = (Vector) queries.get(refs[r].getSource());
\r
88 if (!qset.contains(refs[r].getAccessionId()))
\r
90 qset.addElement(refs[r].getAccessionId());
\r
93 Enumeration e = queries.keys();
\r
94 while (e.hasMoreElements())
\r
96 Vector query = null;
\r
100 db = (String) e.nextElement();
\r
101 query = (Vector) queries.get(db);
\r
102 if (!isFetchable(db))
\r
103 throw new Exception(
\r
104 "Don't know how to fetch from this database :" + db);
\r
105 DbSourceProxy fetcher = getSourceProxy(db);
\r
106 boolean doMultiple = fetcher.getAccessionSeparator() != null; // No
\r
111 Enumeration qs = query.elements();
\r
112 while (qs.hasMoreElements())
\r
114 StringBuffer qsb = new StringBuffer();
\r
117 qsb.append((String) qs.nextElement());
\r
118 if (qs.hasMoreElements() && doMultiple) // and not reached limit for
\r
119 // multiple queries at one
\r
120 // time for this source
\r
122 qsb.append(fetcher.getAccessionSeparator());
\r
124 } while (doMultiple && qs.hasMoreElements());
\r
126 AlignmentI seqset = null;
\r
129 // create a fetcher and go to it
\r
130 seqset = fetcher.getSequenceRecords(qsb.toString());
\r
131 } catch (Exception ex)
\r
133 System.err.println("Failed to retrieve the following from "
\r
135 System.err.println(qsb);
\r
136 ex.printStackTrace(System.err);
\r
138 // TODO: Merge alignment together - perhaps
\r
139 if (seqset != null)
\r
141 SequenceI seqs[] = seqset.getSequencesArray();
\r
144 for (int is = 0; is < seqs.length; is++)
\r
146 rseqs.addElement(seqs[is]);
\r
152 if (fetcher.getRawRecords() != null)
\r
154 System.out.println("# Retrieved from " + db + ":"
\r
156 StringBuffer rrb = fetcher.getRawRecords();
\r
158 * for (int rr = 0; rr<rrb.length; rr++) {
\r
161 // if (rr<qs.length)
\r
163 hdr = "# " + db + ":" + qsb.toString();
\r
165 * } else { hdr = "# part "+rr; }
\r
167 System.out.println(hdr);
\r
169 System.out.println(rrb);
\r
170 System.out.println("# end of " + hdr);
\r
175 } catch (Exception ex)
\r
178 .println("Failed to retrieve the following references from "
\r
180 Enumeration qv = query.elements();
\r
182 while (qv.hasMoreElements())
\r
184 System.err.print(" " + qv.nextElement() + ";");
\r
187 System.err.println();
\r
191 System.err.println();
\r
192 ex.printStackTrace();
\r
195 if (rseqs.size() > 0)
\r
197 ret = new SequenceI[rseqs.size()];
\r
198 Enumeration sqs = rseqs.elements();
\r
200 while (sqs.hasMoreElements())
\r
202 SequenceI s = (SequenceI) sqs.nextElement();
\r
211 * Retrieve an instance of the proxy for the given source
\r
214 * database source string TODO: add version string/wildcard for
\r
215 * retrieval of specific DB source/version combinations.
\r
216 * @return an instance of DbSourceProxy for that db.
\r
218 public DbSourceProxy getSourceProxy(String db)
\r
220 DbSourceProxy dbs = (DbSourceProxy) FETCHABLEDBS.get(db);
\r
225 * constructs and instance of the proxy and registers it as a valid
\r
228 * @param dbSourceProxy
\r
229 * reference for class implementing
\r
230 * jalview.ws.seqfetcher.DbSourceProxy
\r
231 * @throws java.lang.IllegalArgumentException
\r
232 * if class does not implement
\r
233 * jalview.ws.seqfetcher.DbSourceProxy
\r
235 protected void addDBRefSourceImpl(Class dbSourceProxy)
\r
236 throws java.lang.IllegalArgumentException
\r
238 DbSourceProxy proxy = null;
\r
241 Object proxyObj = dbSourceProxy.getConstructor(null)
\r
242 .newInstance(null);
\r
243 if (!DbSourceProxy.class.isInstance(proxyObj))
\r
245 throw new IllegalArgumentException(
\r
246 dbSourceProxy.toString()
\r
247 + " does not implement the jalview.ws.seqfetcher.DbSourceProxy");
\r
249 proxy = (DbSourceProxy) proxyObj;
\r
250 } catch (IllegalArgumentException e)
\r
253 } catch (Exception e)
\r
255 // Serious problems if this happens.
\r
256 throw new Error("DBRefSource Implementation Exception", e);
\r
258 addDbRefSourceImpl(proxy);
\r
262 * add the properly initialised DbSourceProxy object 'proxy' to the list of
\r
263 * sequence fetchers
\r
267 protected void addDbRefSourceImpl(DbSourceProxy proxy)
\r
271 if (FETCHABLEDBS == null)
\r
273 FETCHABLEDBS = new Hashtable();
\r
275 FETCHABLEDBS.put(proxy.getDbSource(), proxy);
\r
280 * test if the database handler for dbName contains the given dbProperty
\r
283 * @param dbProperty
\r
284 * @return true if proxy has the given property
\r
286 public boolean hasDbSourceProperty(String dbName, String dbProperty)
\r
288 // TODO: decide if invalidDbName exception is thrown here.
\r
289 DbSourceProxy proxy = getSourceProxy(dbName);
\r
292 if (proxy.getDbSourceProperties() != null)
\r
294 return proxy.getDbSourceProperties().containsKey(dbProperty);
\r