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
21 import java.util.ArrayList;
\r
22 import java.util.Enumeration;
\r
23 import java.util.Vector;
\r
25 import org.biojava.dasobert.das2.Das2Source;
\r
26 import org.biojava.dasobert.dasregistry.Das1Source;
\r
27 import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
\r
28 import org.biojava.dasobert.dasregistry.DasSource;
\r
30 import jalview.datamodel.Alignment;
\r
31 import jalview.datamodel.AlignmentI;
\r
32 import jalview.datamodel.DBRefSource;
\r
33 import jalview.datamodel.SequenceI;
\r
34 import jalview.ws.seqfetcher.ASequenceFetcher;
\r
35 import jalview.ws.seqfetcher.DbSourceProxy;
\r
38 * This is the the concrete implementation of the sequence retrieval interface
\r
39 * and abstract class in jalview.ws.seqfetcher. This implements the run-time
\r
40 * discovery of sequence database clients, and provides a hardwired main for testing all registered handlers.
\r
43 public class SequenceFetcher extends ASequenceFetcher
\r
46 * Thread safe construction of database proxies TODO: extend to a configurable
\r
47 * database plugin mechanism where classes are instantiated by reflection and
\r
48 * queried for their DbRefSource and version association.
\r
51 public SequenceFetcher()
\r
53 addDBRefSourceImpl(jalview.ws.dbsources.EmblSource.class);
\r
54 addDBRefSourceImpl(jalview.ws.dbsources.EmblCdsSouce.class);
\r
55 addDBRefSourceImpl(jalview.ws.dbsources.Uniprot.class);
\r
56 addDBRefSourceImpl(jalview.ws.dbsources.UnprotName.class);
\r
57 addDBRefSourceImpl(jalview.ws.dbsources.Pdb.class);
\r
58 addDBRefSourceImpl(jalview.ws.dbsources.PfamFull.class);
\r
59 addDBRefSourceImpl(jalview.ws.dbsources.PfamSeed.class); // ensures Seed alignment is 'default' for PFAM
\r
60 registerDasSequenceSources();
\r
63 * return an ordered list of database sources suitable for using in a GUI element
\r
65 public String[] getOrderedSupportedSources() {
\r
66 String[] srcs = this.getSupportedDb();
\r
67 ArrayList dassrc = new ArrayList(),
\r
68 nondas=new ArrayList();
\r
69 for (int i=0;i<srcs.length;i++)
\r
71 String nm = getSourceProxy(srcs[i]).getDbName();
\r
72 if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.DasSequenceSource)
\r
74 if (nm.startsWith("das:"))
\r
76 nm = nm.substring(4);
\r
78 dassrc.add(new String[] { srcs[i], nm.toUpperCase()} );
\r
80 nondas.add(new String[] { srcs[i], nm.toUpperCase()} );
\r
83 Object[] sorted = nondas.toArray();
\r
84 String[] tosort = new String[sorted.length];
\r
86 for (int j=0;j<sorted.length;j++)
\r
88 tosort[j] = ((String[]) sorted[j])[1];
\r
90 jalview.util.QuickSort.sort(tosort, sorted);
\r
92 for (int j=sorted.length-1;j>=0; j--,i++)
\r
94 srcs[i] = ((String[]) sorted[j])[0];
\r
98 sorted = dassrc.toArray();
\r
99 tosort = new String[sorted.length];
\r
101 for (int j=0;j<sorted.length;j++)
\r
103 tosort[j] = ((String[]) sorted[j])[1];
\r
105 jalview.util.QuickSort.sort(tosort, sorted);
\r
106 for (int j=sorted.length-1;j>=0; j--,i++)
\r
108 srcs[i] = ((String[]) sorted[j])[0];
\r
114 * simple run method to test dbsources.
\r
117 public static void main(String[] argv)
\r
119 AlignmentI ds = null;
\r
120 Vector noProds = new Vector();
\r
121 String usage = "SequenceFetcher.main [<DBNAME> <ACCNO>]\n"
\r
122 +"With no arguments, all DbSources will be queried with their test Accession number.\n"
\r
123 +"If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.";
\r
124 if (argv != null && argv.length > 0)
\r
126 DbSourceProxy sp = new SequenceFetcher().getSourceProxy(argv[0]);
\r
129 AlignmentI al = null;
\r
132 al = sp.getSequenceRecords(argv[1]);
\r
133 } catch (Exception e)
\r
135 e.printStackTrace();
\r
136 System.err.println("Error when retrieving "+argv[1]+" from "+argv[0]+"\nUsage: "+usage);
\r
138 SequenceI[] prod = al.getSequencesArray();
\r
141 for (int p = 0; p < prod.length; p++)
\r
143 System.out.println("Prod " + p + ": "
\r
144 + prod[p].getDisplayId(true) + " : "+prod[p].getDescription());
\r
149 System.err.println("Can't resolve "+argv[0]+" as a database name. Allowed values are :\n"+new SequenceFetcher().getSupportedDb());
\r
154 ASequenceFetcher sfetcher = new SequenceFetcher();
\r
155 String[] dbSources = sfetcher.getSupportedDb();
\r
156 for (int dbsource=0; dbsource<dbSources.length;dbsource++)
\r
158 String db = dbSources[dbsource];
\r
160 if (db.equals(DBRefSource.PDB))
\r
162 DbSourceProxy sp = sfetcher.getSourceProxy(db);
\r
164 .println("Source: "+sp.getDbName()+" (" + db + "): retrieving test:" + sp.getTestQuery());
\r
165 AlignmentI al = null;
\r
168 al = sp.getSequenceRecords(sp.getTestQuery());
\r
169 if (al != null && al.getHeight() > 0 && sp.getDbSourceProperties()!=null)
\r
171 boolean dna = sp.getDbSourceProperties().containsKey(
\r
172 DBRefSource.DNACODINGSEQDB)
\r
173 || sp.getDbSourceProperties().containsKey(
\r
174 DBRefSource.DNASEQDB)
\r
175 || sp.getDbSourceProperties().containsKey(
\r
176 DBRefSource.CODINGSEQDB);
\r
177 // try and find products
\r
178 String types[] = jalview.analysis.CrossRef.findSequenceXrefTypes(
\r
179 dna, al.getSequencesArray());
\r
182 System.out.println("Xref Types for: " + (dna ? "dna" : "prot"));
\r
183 for (int t = 0; t < types.length; t++)
\r
185 System.out.println("Type: " + types[t]);
\r
186 SequenceI[] prod = jalview.analysis.CrossRef
\r
187 .findXrefSequences(al.getSequencesArray(), dna,
\r
188 types[t]).getSequencesArray();
\r
189 System.out.println("Found "
\r
190 + ((prod == null) ? "no" : "" + prod.length)
\r
194 for (int p = 0; p < prod.length; p++)
\r
196 System.out.println("Prod " + p + ": "
\r
197 + prod[p].getDisplayId(true));
\r
204 noProds.addElement((dna ? new Object[]
\r
205 { al, al } : new Object[]
\r
210 } catch (Exception ex)
\r
212 System.out.println("ERROR:Failed to retrieve test query.");
\r
213 ex.printStackTrace(System.out);
\r
217 System.out.println("ERROR:No alignment retrieved.");
\r
218 StringBuffer raw = sp.getRawRecords();
\r
220 System.out.println(raw.toString());
\r
222 System.out.println("ERROR:No Raw results.");
\r
226 System.out.println("Retrieved " + al.getHeight() + " sequences.");
\r
227 for (int s = 0; s < al.getHeight(); s++)
\r
229 SequenceI sq = al.getSequenceAt(s);
\r
230 while (sq.getDatasetSequence() != null)
\r
232 sq = sq.getDatasetSequence();
\r
237 ds = new Alignment(new SequenceI[]
\r
243 ds.addSequence(sq);
\r
247 System.out.flush();
\r
248 System.err.flush();
\r
251 if (noProds.size() > 0)
\r
253 Enumeration ts = noProds.elements();
\r
254 while (ts.hasMoreElements())
\r
257 Object[] typeSq = (Object[]) ts.nextElement();
\r
258 boolean dna = (typeSq.length > 1);
\r
259 AlignmentI al = (AlignmentI) typeSq[0];
\r
260 System.out.println("Trying getProducts for "
\r
261 + al.getSequenceAt(0).getDisplayId(true));
\r
262 System.out.println("Search DS Xref for: " + (dna ? "dna" : "prot"));
\r
263 // have a bash at finding the products amongst all the retrieved
\r
265 SequenceI[] seqs = al.getSequencesArray();
\r
266 Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
\r
267 seqs, dna, null, ds);
\r
268 System.out.println("Found "
\r
269 + ((prodal == null) ? "no" : "" + prodal.getHeight()) + " products");
\r
270 if (prodal != null)
\r
272 SequenceI[] prod = prodal.getSequencesArray(); // note
\r
283 for (int p = 0; p < prod.length; p++)
\r
285 System.out.println("Prod " + p + ": "
\r
286 + prod[p].getDisplayId(true));
\r
294 * query the currently defined DAS source registry for sequence sources and add a DasSequenceSource instance for each source to the SequenceFetcher source list.
\r
296 public void registerDasSequenceSources() {
\r
297 DasSource[] sources = jalview.ws.DasSequenceFeatureFetcher.getDASSources();
\r
298 for (int s=0;s<sources.length; s++)
\r
300 Das1Source d1s=null;
\r
301 if (sources[s].hasCapability("sequence"))
\r
303 if (sources[s] instanceof Das2Source)
\r
305 if (((Das2Source)sources[s]).hasDas1Capabilities()) {
\r
307 d1s = org.biojava.dasobert.das2.DasSourceConverter.toDas1Source((Das2Source) sources[s]);
\r
308 } catch (Exception e)
\r
310 System.err.println("Ignoring DAS2 sequence source "+sources[s].getNickname()+" - couldn't map to Das1Source.\n");
\r
311 e.printStackTrace();
\r
315 if (sources[s] instanceof Das1Source)
\r
317 d1s = (Das1Source) sources[s];
\r
323 DasCoordinateSystem[] css = d1s.getCoordinateSystem();
\r
324 for (int c=0;c<css.length;c++)
\r
327 addDbRefSourceImpl(
\r
328 new jalview.ws.dbsources.DasSequenceSource("das:"+d1s.getNickname()+" ("+css[c].getName()+")",
\r
329 css[c].getName(),d1s, css[c]));
\r
330 } catch (Exception e)
\r
332 System.err.println("Ignoring sequence coord system "+c+" ("+
\r
333 css[c].getName()+") for source "+d1s.getNickname()
\r
334 + "- threw exception when constructing fetcher.\n");
\r
335 e.printStackTrace();
\r