2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 import java.awt.event.*;
30 import jalview.datamodel.*;
31 import jalview.datamodel.xdb.embl.*;
34 import jalview.ws.DBRefFetcher;
35 import jalview.ws.ebi.EBIFetchClient;
36 import jalview.ws.seqfetcher.ASequenceFetcher;
37 import jalview.ws.seqfetcher.DbSourceProxy;
39 import java.awt.Rectangle;
40 import java.awt.BorderLayout;
41 import java.awt.Dimension;
43 public class SequenceFetcher extends JPanel implements Runnable
45 // ASequenceFetcher sfetch;
48 IProgressIndicator guiWindow;
50 AlignFrame alignFrame;
54 final String noDbSelected = "-- Select Database --";
56 Hashtable sources = new Hashtable();
58 private static jalview.ws.SequenceFetcher sfetch = null;
60 private static String dasRegistry = null;
62 * Blocking method that initialises and returns the shared instance of the SequenceFetcher client
63 * @param guiWindow - where the initialisation delay message should be shown
64 * @return the singleton instance of the sequence fetcher client
66 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(final IProgressIndicator guiWindow) {
68 || dasRegistry != DasSourceBrowser.getDasRegistryURL())
71 * give a visual indication that sequence fetcher construction is
74 if (guiWindow != null)
76 guiWindow.setProgressBar(
77 "Initialising Sequence Database Fetchers", Thread.currentThread()
80 dasRegistry = DasSourceBrowser.getDasRegistryURL();
81 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
82 if (guiWindow != null)
84 guiWindow.setProgressBar(
85 "Initialising Sequence Database Fetchers", Thread.currentThread().hashCode());
92 public SequenceFetcher(IProgressIndicator guiIndic)
94 final IProgressIndicator guiWindow = guiIndic;
95 final SequenceFetcher us = this;
96 // launch initialiser thread
97 Thread sf = new Thread(new Runnable()
102 if (getSequenceFetcherSingleton(guiWindow)!=null)
104 us.initGui(guiWindow);
106 javax.swing.SwingUtilities.invokeLater(new Runnable()
110 JOptionPane.showInternalMessageDialog(Desktop.desktop,
111 "Could not create the sequence fetcher client. Check error logs for details.",
112 "Couldn't create SequenceFetcher", JOptionPane.ERROR_MESSAGE);
116 // raise warning dialog
124 * called by thread spawned by constructor
128 private void initGui(IProgressIndicator guiWindow)
130 this.guiWindow = guiWindow;
131 if (guiWindow instanceof AlignFrame)
133 alignFrame = (AlignFrame) guiWindow;
136 database.addItem(noDbSelected);
138 * Dynamically generated database list will need a translation function from
139 * internal source to externally distinct names. UNIPROT and UP_NAME are
140 * identical DB sources, and should be collapsed.
143 String dbs[] = sfetch.getOrderedSupportedSources();
144 for (int i = 0; i < dbs.length; i++)
146 if (!sources.containsValue(dbs[i]))
148 String name = sfetch.getSourceProxy(dbs[i]).getDbName();
149 // duplicate source names are thrown away, here.
150 if (!sources.containsKey(name))
152 database.addItem(name);
154 // overwrite with latest version of the retriever for this source
155 sources.put(name, dbs[i]);
161 } catch (Exception ex)
163 ex.printStackTrace();
166 frame = new JInternalFrame();
167 frame.setContentPane(this);
168 if (new jalview.util.Platform().isAMac())
170 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
174 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 140);
178 private String getFrameTitle()
180 return ((alignFrame == null) ? "New " : "Additional ")
181 + "Sequence Fetcher";
184 private void jbInit() throws Exception
186 this.setLayout(borderLayout2);
188 database.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
189 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
190 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
191 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
193 .setText("Separate multiple accession ids with semi colon \";\"");
195 ok.addActionListener(new ActionListener()
197 public void actionPerformed(ActionEvent e)
199 ok_actionPerformed();
202 clear.setText("Clear");
203 clear.addActionListener(new ActionListener()
205 public void actionPerformed(ActionEvent e)
207 clear_actionPerformed();
211 example.setText("Example");
212 example.addActionListener(new ActionListener()
214 public void actionPerformed(ActionEvent e)
216 example_actionPerformed();
219 close.setText("Close");
220 close.addActionListener(new ActionListener()
222 public void actionPerformed(ActionEvent e)
224 close_actionPerformed(e);
227 textArea.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
228 textArea.setLineWrap(true);
229 textArea.addKeyListener(new KeyAdapter()
231 public void keyPressed(KeyEvent e)
233 if (e.getKeyCode() == KeyEvent.VK_ENTER)
234 ok_actionPerformed();
237 jPanel3.setLayout(borderLayout1);
238 borderLayout1.setVgap(5);
240 jPanel1.add(example);
243 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
244 jPanel2.setLayout(borderLayout3);
246 database.addActionListener(new ActionListener()
249 public void actionPerformed(ActionEvent e)
251 DbSourceProxy db = null;
254 db = sfetch.getSourceProxy((String) sources.get(database
255 .getSelectedItem()));
256 dbeg.setText("Example query: " + db.getTestQuery());
257 } catch (Exception ex)
265 jPanel2.add(database, java.awt.BorderLayout.NORTH);
266 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
267 jPanel2.add(jLabel1, java.awt.BorderLayout.SOUTH);
268 // jPanel2.setPreferredSize(new Dimension())
269 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
270 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
271 this.add(jPanel3, java.awt.BorderLayout.CENTER);
272 this.add(jPanel2, java.awt.BorderLayout.NORTH);
273 jScrollPane1.getViewport().add(textArea);
277 protected void example_actionPerformed()
279 DbSourceProxy db = null;
282 db = sfetch.getSourceProxy((String) sources.get(database
283 .getSelectedItem()));
284 textArea.setText(db.getTestQuery());
285 } catch (Exception ex)
291 protected void clear_actionPerformed()
293 textArea.setText("");
297 JLabel dbeg = new JLabel();
299 JComboBox database = new JComboBox();
301 JLabel jLabel1 = new JLabel();
303 JButton ok = new JButton();
305 JButton clear = new JButton();
307 JButton example = new JButton();
309 JButton close = new JButton();
311 JPanel jPanel1 = new JPanel();
313 JTextArea textArea = new JTextArea();
315 JScrollPane jScrollPane1 = new JScrollPane();
317 JPanel jPanel2 = new JPanel();
319 JPanel jPanel3 = new JPanel();
321 JPanel jPanel4 = new JPanel();
323 BorderLayout borderLayout1 = new BorderLayout();
325 BorderLayout borderLayout2 = new BorderLayout();
327 BorderLayout borderLayout3 = new BorderLayout();
329 public void close_actionPerformed(ActionEvent e)
333 frame.setClosed(true);
334 } catch (Exception ex)
339 public void ok_actionPerformed()
341 database.setEnabled(false);
342 textArea.setEnabled(false);
343 ok.setEnabled(false);
344 close.setEnabled(false);
346 Thread worker = new Thread(this);
350 private void resetDialog()
352 database.setEnabled(true);
353 textArea.setEnabled(true);
355 close.setEnabled(true);
361 if (database.getSelectedItem().equals(noDbSelected))
363 error += "Please select the source database\n";
365 com.stevesoft.pat.Regex empty = new com.stevesoft.pat.Regex("\\s+", "");
366 textArea.setText(empty.replaceAll(textArea.getText()));
367 if (textArea.getText().length() == 0)
369 error += "Please enter a (semi-colon separated list of) database id(s)";
371 if (error.length() > 0)
373 showErrorMessage(error);
377 AlignmentI aresult = null;
380 guiWindow.setProgressBar("Fetching Sequences from "
381 + database.getSelectedItem(), Thread.currentThread()
383 aresult = sfetch.getSourceProxy(
384 (String) sources.get(database.getSelectedItem()))
385 .getSequenceRecords(textArea.getText());
387 } catch (Exception e)
389 showErrorMessage("Error retrieving " + textArea.getText() + " from "
390 + database.getSelectedItem());
391 // error +="Couldn't retrieve sequences from "+database.getSelectedItem();
392 System.err.println("Retrieval failed for source ='"
393 + database.getSelectedItem() + "' and query\n'"
394 + textArea.getText() + "'\n");
396 } catch (OutOfMemoryError e)
398 showErrorMessage("Out of Memory when retrieving "
401 + database.getSelectedItem()
402 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
406 showErrorMessage("Serious Error retrieving " + textArea.getText()
407 + " from " + database.getSelectedItem());
410 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
413 parseResult(aresult, null, null);
419 * result = new StringBuffer(); if
420 * (database.getSelectedItem().equals("Uniprot")) {
421 * getUniprotFile(textArea.getText()); } else if
422 * (database.getSelectedItem().equals("EMBL") ||
423 * database.getSelectedItem().equals("EMBLCDS")) { String DBRefSource =
424 * database.getSelectedItem().equals("EMBLCDS") ?
425 * jalview.datamodel.DBRefSource.EMBLCDS : jalview.datamodel.DBRefSource.EMBL;
427 * StringTokenizer st = new StringTokenizer(textArea.getText(), ";");
428 * SequenceI[] seqs = null; while(st.hasMoreTokens()) { EBIFetchClient dbFetch =
429 * new EBIFetchClient(); String qry =
430 * database.getSelectedItem().toString().toLowerCase( ) + ":" +
431 * st.nextToken(); File reply = dbFetch.fetchDataAsFile( qry, "emblxml",null);
433 * jalview.datamodel.xdb.embl.EmblFile efile=null; if (reply != null &&
434 * reply.exists()) { efile =
435 * jalview.datamodel.xdb.embl.EmblFile.getEmblFile(reply); } if (efile!=null) {
436 * for (Iterator i=efile.getEntries().iterator(); i.hasNext(); ) { EmblEntry
437 * entry = (EmblEntry) i.next(); SequenceI[] seqparts =
438 * entry.getSequences(false,true, DBRefSource); if (seqparts!=null) {
439 * SequenceI[] newseqs = null; int si=0; if (seqs==null) { newseqs = new
440 * SequenceI[seqparts.length]; } else { newseqs = new
441 * SequenceI[seqs.length+seqparts.length];
443 * for (;si<seqs.length; si++) { newseqs[si] = seqs[si]; seqs[si] = null; } }
444 * for (int j=0;j<seqparts.length; si++, j++) { newseqs[si] =
445 * seqparts[j].deriveSequence(); // place DBReferences on dataset and refer }
447 * } } } else { result.append("# no response for "+qry); } } if (seqs!=null &&
448 * seqs.length>0) { if (parseResult(new Alignment(seqs), null, null)!=null) {
449 * result.append("# Successfully parsed the "+database.getSelectedItem()+"
450 * Queries into an Alignment"); } } } else if
451 * (database.getSelectedItem().equals("PDB")) { StringTokenizer qset = new
452 * StringTokenizer(textArea.getText(), ";"); String query; SequenceI[] seqs =
453 * null; while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null)) {
454 * SequenceI[] seqparts = getPDBFile(query.toUpperCase()); if (seqparts !=
455 * null) { if (seqs == null) { seqs = seqparts; } else { SequenceI[] newseqs =
456 * new SequenceI[seqs.length+seqparts.length]; int i=0; for (; i <
457 * seqs.length; i++) { newseqs[i] = seqs[i]; seqs[i] = null; } for (int j=0;j<seqparts.length;
458 * i++, j++) { newseqs[i] = seqparts[j]; } seqs=newseqs; } result.append("#
459 * Success for "+query.toUpperCase()+"\n"); } } if (seqs != null &&
460 * seqs.length > 0) { if (parseResult(new Alignment(seqs), null, null)!=null) {
461 * result.append( "# Successfully parsed the PDB File Queries into an
462 * Alignment"); } } } else if( database.getSelectedItem().equals("PFAM")) {
463 * try { result.append(new FastaFile(
464 * "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc=" +
465 * textArea.getText().toUpperCase(), "URL").print() );
467 * if(result.length()>0) { parseResult( result.toString(),
468 * textArea.getText().toUpperCase() ); }
469 * } catch (java.io.IOException ex) { result = null; } }
471 * if (result == null || result.length() == 0) { showErrorMessage("Error
472 * retrieving " + textArea.getText() + " from " + database.getSelectedItem()); }
474 * resetDialog(); return; }
476 * void getUniprotFile(String id) { EBIFetchClient ebi = new EBIFetchClient();
477 * File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
479 * DBRefFetcher dbref = new DBRefFetcher(); Vector entries =
480 * dbref.getUniprotEntries(file);
482 * if (entries != null) { //First, make the new sequences Enumeration en =
483 * entries.elements(); while (en.hasMoreElements()) { UniprotEntry entry =
484 * (UniprotEntry) en.nextElement();
486 * StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot"); Enumeration
487 * en2 = entry.getAccession().elements(); while (en2.hasMoreElements()) {
488 * name.append("|"); name.append(en2.nextElement()); } en2 =
489 * entry.getName().elements(); while (en2.hasMoreElements()) {
490 * name.append("|"); name.append(en2.nextElement()); }
492 * if (entry.getProtein() != null) { name.append(" " +
493 * entry.getProtein().getName().elementAt(0)); }
495 * result.append(name + "\n" + entry.getUniprotSequence().getContent() +
499 * //Then read in the features and apply them to the dataset Alignment al =
500 * parseResult(result.toString(), null); for (int i = 0; i < entries.size();
501 * i++) { UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
502 * Enumeration e = entry.getDbReference().elements(); Vector onlyPdbEntries =
503 * new Vector(); while (e.hasMoreElements()) { PDBEntry pdb = (PDBEntry)
504 * e.nextElement(); if (!pdb.getType().equals("PDB")) { continue; }
506 * onlyPdbEntries.addElement(pdb); }
508 * Enumeration en2 = entry.getAccession().elements(); while
509 * (en2.hasMoreElements()) {
510 * al.getSequenceAt(i).getDatasetSequence().addDBRef(new DBRefEntry(
511 * DBRefSource.UNIPROT, "0", en2.nextElement().toString())); }
516 * al.getSequenceAt(i).getDatasetSequence().setPDBId(onlyPdbEntries); if
517 * (entry.getFeature() != null) { e = entry.getFeature().elements(); while
518 * (e.hasMoreElements()) { SequenceFeature sf = (SequenceFeature)
519 * e.nextElement(); sf.setFeatureGroup("Uniprot");
520 * al.getSequenceAt(i).getDatasetSequence().addSequenceFeature( sf ); } } } } }
522 * SequenceI[] getPDBFile(String id) { Vector result = new Vector(); String
523 * chain = null; if (id.indexOf(":") > -1) { chain =
524 * id.substring(id.indexOf(":") + 1); id = id.substring(0, id.indexOf(":")); }
526 * EBIFetchClient ebi = new EBIFetchClient(); String file =
527 * ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw"). getAbsolutePath(); if (file ==
528 * null) { return null; } try { PDBfile pdbfile = new PDBfile(file,
529 * jalview.io.AppletFormatAdapter.FILE); for (int i = 0; i <
530 * pdbfile.chains.size(); i++) { if (chain == null || ( (PDBChain)
531 * pdbfile.chains.elementAt(i)).id. toUpperCase().equals(chain)) { PDBChain
532 * pdbchain = (PDBChain) pdbfile.chains.elementAt(i); // Get the Chain's
533 * Sequence - who's dataset includes any special features added from the PDB
534 * file SequenceI sq = pdbchain.sequence; // Specially formatted name for the
535 * PDB chain sequences retrieved from the PDB
536 * sq.setName("PDB|"+id+"|"+sq.getName()); // Might need to add more metadata
537 * to the PDBEntry object // like below /* PDBEntry entry = new PDBEntry(); //
538 * Construct the PDBEntry entry.setId(id); if (entry.getProperty() == null)
539 * entry.setProperty(new Hashtable()); entry.getProperty().put("chains",
540 * pdbchain.id + "=" + sq.getStart() + "-" + sq.getEnd());
541 * sq.getDatasetSequence().addPDBId(entry);
542 * // Add PDB DB Refs // We make a DBRefEtntry because we have obtained the
543 * PDB file from a verifiable source // JBPNote - PDB DBRefEntry should also
544 * carry the chain and mapping information DBRefEntry dbentry = new
545 * DBRefEntry(jalview.datamodel.DBRefSource.PDB, "0", id + pdbchain.id);
546 * sq.addDBRef(dbentry); // and add seuqence to the retrieved set
547 * result.addElement(sq.deriveSequence()); } }
549 * if (result.size() < 1) { throw new Exception("WsDBFetch for PDB id resulted
550 * in zero result size"); } } catch (Exception ex) // Problem parsing PDB file {
551 * jalview.bin.Cache.log.warn("Exception when retrieving " +
552 * textArea.getText() + " from " + database.getSelectedItem(), ex); return
556 * SequenceI[] results = new SequenceI[result.size()]; for (int i = 0, j =
557 * result.size(); i < j; i++) { results[i] = (SequenceI) result.elementAt(i);
558 * result.setElementAt(null,i); } return results; }
560 AlignmentI parseResult(String result, String title)
562 String format = new IdentifyFile().Identify(result, "Paste");
563 Alignment sequences = null;
564 if (FormatAdapter.isValidFormat(format))
569 sequences = new FormatAdapter().readFile(result.toString(),
571 } catch (Exception ex)
575 if (sequences != null)
577 return parseResult(sequences, title, format);
582 showErrorMessage("Error retrieving " + textArea.getText() + " from "
583 + database.getSelectedItem());
589 AlignmentI parseResult(AlignmentI al, String title,
590 String currentFileFormat)
593 if (al != null && al.getHeight() > 0)
595 if (alignFrame == null)
597 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
598 AlignFrame.DEFAULT_HEIGHT);
599 if (currentFileFormat != null)
601 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
603 // NON-FormatAdapter Sourced
609 title = "Retrieved from " + database.getSelectedItem();
612 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
613 AlignFrame.DEFAULT_HEIGHT);
615 af.statusBar.setText("Successfully pasted alignment file");
619 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
621 } catch (Exception ex)
627 for (int i = 0; i < al.getHeight(); i++)
629 alignFrame.viewport.alignment.addSequence(al.getSequenceAt(i)); // this
636 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment
638 alignFrame.viewport.alignment.getWidth();
639 alignFrame.viewport.firePropertyChange("alignment", null,
640 alignFrame.viewport.getAlignment().getSequences());
646 void showErrorMessage(final String error)
649 javax.swing.SwingUtilities.invokeLater(new Runnable()
653 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
654 "Error Retrieving Data", JOptionPane.WARNING_MESSAGE);