2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
24 import java.awt.event.*;
29 import jalview.datamodel.*;
30 import jalview.datamodel.xdb.embl.*;
33 import jalview.ws.DBRefFetcher;
34 import jalview.ws.ebi.EBIFetchClient;
35 import jalview.ws.seqfetcher.ASequenceFetcher;
36 import jalview.ws.seqfetcher.DbSourceProxy;
38 import java.awt.Rectangle;
39 import java.awt.BorderLayout;
40 import java.awt.Dimension;
42 public class SequenceFetcher extends JPanel implements Runnable
44 // ASequenceFetcher sfetch;
47 IProgressIndicator guiWindow;
49 AlignFrame alignFrame;
53 final String noDbSelected = "-- Select Database --";
55 Hashtable sources = new Hashtable();
57 private static jalview.ws.SequenceFetcher sfetch = null;
59 private static String dasRegistry = null;
62 * Blocking method that initialises and returns the shared instance of the
63 * SequenceFetcher client
66 * - where the initialisation delay message should be shown
67 * @return the singleton instance of the sequence fetcher client
69 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
70 final IProgressIndicator guiWindow)
73 || dasRegistry != DasSourceBrowser.getDasRegistryURL())
76 * give a visual indication that sequence fetcher construction is occuring
78 if (guiWindow != null)
80 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
81 Thread.currentThread().hashCode());
83 dasRegistry = DasSourceBrowser.getDasRegistryURL();
84 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
85 if (guiWindow != null)
87 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
88 Thread.currentThread().hashCode());
96 public SequenceFetcher(IProgressIndicator guiIndic)
98 final IProgressIndicator guiWindow = guiIndic;
99 final SequenceFetcher us = this;
100 // launch initialiser thread
101 Thread sf = new Thread(new Runnable()
106 if (getSequenceFetcherSingleton(guiWindow) != null)
108 us.initGui(guiWindow);
112 javax.swing.SwingUtilities.invokeLater(new Runnable()
117 .showInternalMessageDialog(
119 "Could not create the sequence fetcher client. Check error logs for details.",
120 "Couldn't create SequenceFetcher",
121 JOptionPane.ERROR_MESSAGE);
125 // raise warning dialog
133 * called by thread spawned by constructor
137 private void initGui(IProgressIndicator guiWindow)
139 this.guiWindow = guiWindow;
140 if (guiWindow instanceof AlignFrame)
142 alignFrame = (AlignFrame) guiWindow;
145 database.addItem(noDbSelected);
147 * Dynamically generated database list will need a translation function from
148 * internal source to externally distinct names. UNIPROT and UP_NAME are
149 * identical DB sources, and should be collapsed.
152 String dbs[] = sfetch.getOrderedSupportedSources();
153 for (int i = 0; i < dbs.length; i++)
155 if (!sources.containsValue(dbs[i]))
157 String name = sfetch.getSourceProxy(dbs[i]).getDbName();
158 // duplicate source names are thrown away, here.
159 if (!sources.containsKey(name))
161 database.addItem(name);
163 // overwrite with latest version of the retriever for this source
164 sources.put(name, dbs[i]);
170 } catch (Exception ex)
172 ex.printStackTrace();
175 frame = new JInternalFrame();
176 frame.setContentPane(this);
177 if (new jalview.util.Platform().isAMac())
179 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
183 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 140);
187 private String getFrameTitle()
189 return ((alignFrame == null) ? "New " : "Additional ")
190 + "Sequence Fetcher";
193 private void jbInit() throws Exception
195 this.setLayout(borderLayout2);
197 database.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
198 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
199 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
200 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
202 .setText("Separate multiple accession ids with semi colon \";\"");
204 ok.addActionListener(new ActionListener()
206 public void actionPerformed(ActionEvent e)
208 ok_actionPerformed();
211 clear.setText("Clear");
212 clear.addActionListener(new ActionListener()
214 public void actionPerformed(ActionEvent e)
216 clear_actionPerformed();
220 example.setText("Example");
221 example.addActionListener(new ActionListener()
223 public void actionPerformed(ActionEvent e)
225 example_actionPerformed();
228 close.setText("Close");
229 close.addActionListener(new ActionListener()
231 public void actionPerformed(ActionEvent e)
233 close_actionPerformed(e);
236 textArea.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
237 textArea.setLineWrap(true);
238 textArea.addKeyListener(new KeyAdapter()
240 public void keyPressed(KeyEvent e)
242 if (e.getKeyCode() == KeyEvent.VK_ENTER)
243 ok_actionPerformed();
246 jPanel3.setLayout(borderLayout1);
247 borderLayout1.setVgap(5);
249 jPanel1.add(example);
252 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
253 jPanel2.setLayout(borderLayout3);
255 database.addActionListener(new ActionListener()
258 public void actionPerformed(ActionEvent e)
260 DbSourceProxy db = null;
263 db = sfetch.getSourceProxy((String) sources.get(database
264 .getSelectedItem()));
265 dbeg.setText("Example query: " + db.getTestQuery());
266 } catch (Exception ex)
274 jPanel2.add(database, java.awt.BorderLayout.NORTH);
275 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
276 jPanel2.add(jLabel1, java.awt.BorderLayout.SOUTH);
277 // jPanel2.setPreferredSize(new Dimension())
278 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
279 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
280 this.add(jPanel3, java.awt.BorderLayout.CENTER);
281 this.add(jPanel2, java.awt.BorderLayout.NORTH);
282 jScrollPane1.getViewport().add(textArea);
286 protected void example_actionPerformed()
288 DbSourceProxy db = null;
291 db = sfetch.getSourceProxy((String) sources.get(database
292 .getSelectedItem()));
293 textArea.setText(db.getTestQuery());
294 } catch (Exception ex)
300 protected void clear_actionPerformed()
302 textArea.setText("");
306 JLabel dbeg = new JLabel();
308 JComboBox database = new JComboBox();
310 JLabel jLabel1 = new JLabel();
312 JButton ok = new JButton();
314 JButton clear = new JButton();
316 JButton example = new JButton();
318 JButton close = new JButton();
320 JPanel jPanel1 = new JPanel();
322 JTextArea textArea = new JTextArea();
324 JScrollPane jScrollPane1 = new JScrollPane();
326 JPanel jPanel2 = new JPanel();
328 JPanel jPanel3 = new JPanel();
330 JPanel jPanel4 = new JPanel();
332 BorderLayout borderLayout1 = new BorderLayout();
334 BorderLayout borderLayout2 = new BorderLayout();
336 BorderLayout borderLayout3 = new BorderLayout();
338 public void close_actionPerformed(ActionEvent e)
342 frame.setClosed(true);
343 } catch (Exception ex)
348 public void ok_actionPerformed()
350 database.setEnabled(false);
351 textArea.setEnabled(false);
352 ok.setEnabled(false);
353 close.setEnabled(false);
355 Thread worker = new Thread(this);
359 private void resetDialog()
361 database.setEnabled(true);
362 textArea.setEnabled(true);
364 close.setEnabled(true);
370 if (database.getSelectedItem().equals(noDbSelected))
372 error += "Please select the source database\n";
374 // TODO: make this transformation optional and configurable
375 com.stevesoft.pat.Regex empty = new com.stevesoft.pat.Regex(
376 "(\\s|[,; ])+", ";"); // \\s+", "");
377 textArea.setText(empty.replaceAll(textArea.getText()));
378 // see if there's anthing to search with
379 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
382 error += "Please enter a (semi-colon separated list of) database id(s)";
384 if (error.length() > 0)
386 showErrorMessage(error);
390 AlignmentI aresult = null;
391 Object source = database.getSelectedItem();
392 Enumeration en = new StringTokenizer(textArea.getText(), ";");
395 guiWindow.setProgressBar("Fetching Sequences from "
396 + database.getSelectedItem(), Thread.currentThread()
398 DbSourceProxy proxy = sfetch.getSourceProxy((String) sources
400 if (proxy.getAccessionSeparator() == null)
402 while (en.hasMoreElements())
404 String item = (String) en.nextElement();
411 // give the server a chance to breathe
413 } catch (Exception e)
420 AlignmentI indres = null;
423 indres = proxy.getSequenceRecords(item);
424 } catch (OutOfMemoryError oome)
427 "fetching " + item + " from "
428 + database.getSelectedItem(),oome,
439 aresult.append(indres);
442 } catch (Exception e)
444 jalview.bin.Cache.log.info("Error retrieving " + item
445 + " from " + source, e);
451 StringBuffer multiacc = new StringBuffer();
452 while (en.hasMoreElements())
454 multiacc.append(en.nextElement());
455 if (en.hasMoreElements())
457 multiacc.append(proxy.getAccessionSeparator());
462 aresult = proxy.getSequenceRecords(multiacc.toString());
463 } catch (OutOfMemoryError oome)
466 "fetching " + multiacc + " from "
467 + database.getSelectedItem(),oome,
474 } catch (Exception e)
476 showErrorMessage("Error retrieving " + textArea.getText() + " from "
477 + database.getSelectedItem());
478 // error +="Couldn't retrieve sequences from "+database.getSelectedItem();
479 System.err.println("Retrieval failed for source ='"
480 + database.getSelectedItem() + "' and query\n'"
481 + textArea.getText() + "'\n");
483 } catch (OutOfMemoryError e)
485 // resets dialog box - so we don't use OOMwarning here.
486 showErrorMessage("Out of Memory when retrieving "
489 + database.getSelectedItem()
490 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
494 showErrorMessage("Serious Error retrieving " + textArea.getText()
495 + " from " + database.getSelectedItem());
500 parseResult(aresult, null, null);
502 // only remove visual delay after we finished parsing.
503 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
508 * result = new StringBuffer(); if
509 * (database.getSelectedItem().equals("Uniprot")) {
510 * getUniprotFile(textArea.getText()); } else if
511 * (database.getSelectedItem().equals("EMBL") ||
512 * database.getSelectedItem().equals("EMBLCDS")) { String DBRefSource =
513 * database.getSelectedItem().equals("EMBLCDS") ?
514 * jalview.datamodel.DBRefSource.EMBLCDS : jalview.datamodel.DBRefSource.EMBL;
516 * StringTokenizer st = new StringTokenizer(textArea.getText(), ";");
517 * SequenceI[] seqs = null; while(st.hasMoreTokens()) { EBIFetchClient dbFetch
518 * = new EBIFetchClient(); String qry =
519 * database.getSelectedItem().toString().toLowerCase( ) + ":" +
520 * st.nextToken(); File reply = dbFetch.fetchDataAsFile( qry, "emblxml",null);
522 * jalview.datamodel.xdb.embl.EmblFile efile=null; if (reply != null &&
523 * reply.exists()) { efile =
524 * jalview.datamodel.xdb.embl.EmblFile.getEmblFile(reply); } if (efile!=null)
525 * { for (Iterator i=efile.getEntries().iterator(); i.hasNext(); ) { EmblEntry
526 * entry = (EmblEntry) i.next(); SequenceI[] seqparts =
527 * entry.getSequences(false,true, DBRefSource); if (seqparts!=null) {
528 * SequenceI[] newseqs = null; int si=0; if (seqs==null) { newseqs = new
529 * SequenceI[seqparts.length]; } else { newseqs = new
530 * SequenceI[seqs.length+seqparts.length];
532 * for (;si<seqs.length; si++) { newseqs[si] = seqs[si]; seqs[si] = null; } }
533 * for (int j=0;j<seqparts.length; si++, j++) { newseqs[si] =
534 * seqparts[j].deriveSequence(); // place DBReferences on dataset and refer }
535 * seqs=newseqs; } } } else { result.append("# no response for "+qry); } } if
536 * (seqs!=null && seqs.length>0) { if (parseResult(new Alignment(seqs), null,
537 * null)!=null) { result.append("# Successfully parsed the
538 * "+database.getSelectedItem()+" Queries into an Alignment"); } } } else if
539 * (database.getSelectedItem().equals("PDB")) { StringTokenizer qset = new
540 * StringTokenizer(textArea.getText(), ";"); String query; SequenceI[] seqs =
541 * null; while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null)) {
542 * SequenceI[] seqparts = getPDBFile(query.toUpperCase()); if (seqparts !=
543 * null) { if (seqs == null) { seqs = seqparts; } else { SequenceI[] newseqs =
544 * new SequenceI[seqs.length+seqparts.length]; int i=0; for (; i <
545 * seqs.length; i++) { newseqs[i] = seqs[i]; seqs[i] = null; } for (int
546 * j=0;j<seqparts.length; i++, j++) { newseqs[i] = seqparts[j]; }
547 * seqs=newseqs; } result.append("# Success for "+query.toUpperCase()+"\n"); }
548 * } if (seqs != null && seqs.length > 0) { if (parseResult(new
549 * Alignment(seqs), null, null)!=null) { result.append( "# Successfully parsed
550 * the PDB File Queries into an
551 * Alignment"); } } } else if( database.getSelectedItem().equals("PFAM")) {
552 * try { result.append(new FastaFile(
553 * "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc=" +
554 * textArea.getText().toUpperCase(), "URL").print() );
556 * if(result.length()>0) { parseResult( result.toString(),
557 * textArea.getText().toUpperCase() ); } } catch (java.io.IOException ex) {
560 * if (result == null || result.length() == 0) { showErrorMessage("Error
561 * retrieving " + textArea.getText() + " from " + database.getSelectedItem());
564 * resetDialog(); return; }
566 * void getUniprotFile(String id) { EBIFetchClient ebi = new EBIFetchClient();
567 * File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
569 * DBRefFetcher dbref = new DBRefFetcher(); Vector entries =
570 * dbref.getUniprotEntries(file);
572 * if (entries != null) { //First, make the new sequences Enumeration en =
573 * entries.elements(); while (en.hasMoreElements()) { UniprotEntry entry =
574 * (UniprotEntry) en.nextElement();
576 * StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot"); Enumeration
577 * en2 = entry.getAccession().elements(); while (en2.hasMoreElements()) {
578 * name.append("|"); name.append(en2.nextElement()); } en2 =
579 * entry.getName().elements(); while (en2.hasMoreElements()) {
580 * name.append("|"); name.append(en2.nextElement()); }
582 * if (entry.getProtein() != null) { name.append(" " +
583 * entry.getProtein().getName().elementAt(0)); }
585 * result.append(name + "\n" + entry.getUniprotSequence().getContent() +
588 * //Then read in the features and apply them to the dataset Alignment al =
589 * parseResult(result.toString(), null); for (int i = 0; i < entries.size();
590 * i++) { UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
591 * Enumeration e = entry.getDbReference().elements(); Vector onlyPdbEntries =
592 * new Vector(); while (e.hasMoreElements()) { PDBEntry pdb = (PDBEntry)
593 * e.nextElement(); if (!pdb.getType().equals("PDB")) { continue; }
595 * onlyPdbEntries.addElement(pdb); }
597 * Enumeration en2 = entry.getAccession().elements(); while
598 * (en2.hasMoreElements()) {
599 * al.getSequenceAt(i).getDatasetSequence().addDBRef(new DBRefEntry(
600 * DBRefSource.UNIPROT, "0", en2.nextElement().toString())); }
605 * al.getSequenceAt(i).getDatasetSequence().setPDBId(onlyPdbEntries); if
606 * (entry.getFeature() != null) { e = entry.getFeature().elements(); while
607 * (e.hasMoreElements()) { SequenceFeature sf = (SequenceFeature)
608 * e.nextElement(); sf.setFeatureGroup("Uniprot");
609 * al.getSequenceAt(i).getDatasetSequence().addSequenceFeature( sf ); } } } }
612 * SequenceI[] getPDBFile(String id) { Vector result = new Vector(); String
613 * chain = null; if (id.indexOf(":") > -1) { chain =
614 * id.substring(id.indexOf(":") + 1); id = id.substring(0, id.indexOf(":")); }
616 * EBIFetchClient ebi = new EBIFetchClient(); String file =
617 * ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw"). getAbsolutePath(); if (file
618 * == null) { return null; } try { PDBfile pdbfile = new PDBfile(file,
619 * jalview.io.AppletFormatAdapter.FILE); for (int i = 0; i <
620 * pdbfile.chains.size(); i++) { if (chain == null || ( (PDBChain)
621 * pdbfile.chains.elementAt(i)).id. toUpperCase().equals(chain)) { PDBChain
622 * pdbchain = (PDBChain) pdbfile.chains.elementAt(i); // Get the Chain's
623 * Sequence - who's dataset includes any special features added from the PDB
624 * file SequenceI sq = pdbchain.sequence; // Specially formatted name for the
625 * PDB chain sequences retrieved from the PDB
626 * sq.setName("PDB|"+id+"|"+sq.getName()); // Might need to add more metadata
627 * to the PDBEntry object // like below /* PDBEntry entry = new PDBEntry(); //
628 * Construct the PDBEntry entry.setId(id); if (entry.getProperty() == null)
629 * entry.setProperty(new Hashtable()); entry.getProperty().put("chains",
630 * pdbchain.id + "=" + sq.getStart() + "-" + sq.getEnd());
631 * sq.getDatasetSequence().addPDBId(entry); // Add PDB DB Refs // We make a
632 * DBRefEtntry because we have obtained the PDB file from a verifiable source
633 * // JBPNote - PDB DBRefEntry should also carry the chain and mapping
634 * information DBRefEntry dbentry = new
635 * DBRefEntry(jalview.datamodel.DBRefSource.PDB, "0", id + pdbchain.id);
636 * sq.addDBRef(dbentry); // and add seuqence to the retrieved set
637 * result.addElement(sq.deriveSequence()); } }
639 * if (result.size() < 1) { throw new Exception("WsDBFetch for PDB id resulted
640 * in zero result size"); } } catch (Exception ex) // Problem parsing PDB file
641 * { jalview.bin.Cache.log.warn("Exception when retrieving " +
642 * textArea.getText() + " from " + database.getSelectedItem(), ex); return
646 * SequenceI[] results = new SequenceI[result.size()]; for (int i = 0, j =
647 * result.size(); i < j; i++) { results[i] = (SequenceI) result.elementAt(i);
648 * result.setElementAt(null,i); } return results; }
650 AlignmentI parseResult(String result, String title)
652 String format = new IdentifyFile().Identify(result, "Paste");
653 Alignment sequences = null;
654 if (FormatAdapter.isValidFormat(format))
659 sequences = new FormatAdapter().readFile(result.toString(),
661 } catch (Exception ex)
665 if (sequences != null)
667 return parseResult(sequences, title, format);
672 showErrorMessage("Error retrieving " + textArea.getText() + " from "
673 + database.getSelectedItem());
679 AlignmentI parseResult(AlignmentI al, String title,
680 String currentFileFormat)
683 if (al != null && al.getHeight() > 0)
685 if (alignFrame == null)
687 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
688 AlignFrame.DEFAULT_HEIGHT);
689 if (currentFileFormat != null)
691 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
693 // NON-FormatAdapter Sourced
699 title = "Retrieved from " + database.getSelectedItem();
701 SequenceFeature[] sfs = null;
702 for (Enumeration sq = al.getSequences().elements(); sq
705 if ((sfs = ((SequenceI) sq.nextElement()).getDatasetSequence()
706 .getSequenceFeatures()) != null)
710 af.setShowSeqFeatures(true);
716 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
717 AlignFrame.DEFAULT_HEIGHT);
719 af.statusBar.setText("Successfully pasted alignment file");
723 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
725 } catch (Exception ex)
731 for (int i = 0; i < al.getHeight(); i++)
733 alignFrame.viewport.alignment.addSequence(al.getSequenceAt(i)); // this
740 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment
742 alignFrame.viewport.alignment.getWidth();
743 alignFrame.viewport.firePropertyChange("alignment", null,
744 alignFrame.viewport.getAlignment().getSequences());
750 void showErrorMessage(final String error)
753 javax.swing.SwingUtilities.invokeLater(new Runnable()
757 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
758 "Error Retrieving Data", JOptionPane.WARNING_MESSAGE);