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)
419 AlignmentI indres = proxy.getSequenceRecords(item);
428 aresult.append(indres);
431 } catch (Exception e)
433 jalview.bin.Cache.log.info("Error retrieving " + item
434 + " from " + source, e);
440 StringBuffer multiacc = new StringBuffer();
441 while (en.hasMoreElements())
443 multiacc.append(en.nextElement());
444 if (en.hasMoreElements())
446 multiacc.append(proxy.getAccessionSeparator());
449 aresult = proxy.getSequenceRecords(multiacc.toString());
452 } catch (Exception e)
454 showErrorMessage("Error retrieving " + textArea.getText() + " from "
455 + database.getSelectedItem());
456 // error +="Couldn't retrieve sequences from "+database.getSelectedItem();
457 System.err.println("Retrieval failed for source ='"
458 + database.getSelectedItem() + "' and query\n'"
459 + textArea.getText() + "'\n");
461 } catch (OutOfMemoryError e)
463 // resets dialog box - so we don't use OOMwarning here.
464 showErrorMessage("Out of Memory when retrieving "
467 + database.getSelectedItem()
468 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
472 showErrorMessage("Serious Error retrieving " + textArea.getText()
473 + " from " + database.getSelectedItem());
478 parseResult(aresult, null, null);
480 // only remove visual delay after we finished parsing.
481 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
486 * result = new StringBuffer(); if
487 * (database.getSelectedItem().equals("Uniprot")) {
488 * getUniprotFile(textArea.getText()); } else if
489 * (database.getSelectedItem().equals("EMBL") ||
490 * database.getSelectedItem().equals("EMBLCDS")) { String DBRefSource =
491 * database.getSelectedItem().equals("EMBLCDS") ?
492 * jalview.datamodel.DBRefSource.EMBLCDS : jalview.datamodel.DBRefSource.EMBL;
494 * StringTokenizer st = new StringTokenizer(textArea.getText(), ";");
495 * SequenceI[] seqs = null; while(st.hasMoreTokens()) { EBIFetchClient dbFetch
496 * = new EBIFetchClient(); String qry =
497 * database.getSelectedItem().toString().toLowerCase( ) + ":" +
498 * st.nextToken(); File reply = dbFetch.fetchDataAsFile( qry, "emblxml",null);
500 * jalview.datamodel.xdb.embl.EmblFile efile=null; if (reply != null &&
501 * reply.exists()) { efile =
502 * jalview.datamodel.xdb.embl.EmblFile.getEmblFile(reply); } if (efile!=null)
503 * { for (Iterator i=efile.getEntries().iterator(); i.hasNext(); ) { EmblEntry
504 * entry = (EmblEntry) i.next(); SequenceI[] seqparts =
505 * entry.getSequences(false,true, DBRefSource); if (seqparts!=null) {
506 * SequenceI[] newseqs = null; int si=0; if (seqs==null) { newseqs = new
507 * SequenceI[seqparts.length]; } else { newseqs = new
508 * SequenceI[seqs.length+seqparts.length];
510 * for (;si<seqs.length; si++) { newseqs[si] = seqs[si]; seqs[si] = null; } }
511 * for (int j=0;j<seqparts.length; si++, j++) { newseqs[si] =
512 * seqparts[j].deriveSequence(); // place DBReferences on dataset and refer }
513 * seqs=newseqs; } } } else { result.append("# no response for "+qry); } } if
514 * (seqs!=null && seqs.length>0) { if (parseResult(new Alignment(seqs), null,
515 * null)!=null) { result.append("# Successfully parsed the
516 * "+database.getSelectedItem()+" Queries into an Alignment"); } } } else if
517 * (database.getSelectedItem().equals("PDB")) { StringTokenizer qset = new
518 * StringTokenizer(textArea.getText(), ";"); String query; SequenceI[] seqs =
519 * null; while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null)) {
520 * SequenceI[] seqparts = getPDBFile(query.toUpperCase()); if (seqparts !=
521 * null) { if (seqs == null) { seqs = seqparts; } else { SequenceI[] newseqs =
522 * new SequenceI[seqs.length+seqparts.length]; int i=0; for (; i <
523 * seqs.length; i++) { newseqs[i] = seqs[i]; seqs[i] = null; } for (int
524 * j=0;j<seqparts.length; i++, j++) { newseqs[i] = seqparts[j]; }
525 * seqs=newseqs; } result.append("# Success for "+query.toUpperCase()+"\n"); }
526 * } if (seqs != null && seqs.length > 0) { if (parseResult(new
527 * Alignment(seqs), null, null)!=null) { result.append( "# Successfully parsed
528 * the PDB File Queries into an
529 * Alignment"); } } } else if( database.getSelectedItem().equals("PFAM")) {
530 * try { result.append(new FastaFile(
531 * "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc=" +
532 * textArea.getText().toUpperCase(), "URL").print() );
534 * if(result.length()>0) { parseResult( result.toString(),
535 * textArea.getText().toUpperCase() ); } } catch (java.io.IOException ex) {
538 * if (result == null || result.length() == 0) { showErrorMessage("Error
539 * retrieving " + textArea.getText() + " from " + database.getSelectedItem());
542 * resetDialog(); return; }
544 * void getUniprotFile(String id) { EBIFetchClient ebi = new EBIFetchClient();
545 * File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
547 * DBRefFetcher dbref = new DBRefFetcher(); Vector entries =
548 * dbref.getUniprotEntries(file);
550 * if (entries != null) { //First, make the new sequences Enumeration en =
551 * entries.elements(); while (en.hasMoreElements()) { UniprotEntry entry =
552 * (UniprotEntry) en.nextElement();
554 * StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot"); Enumeration
555 * en2 = entry.getAccession().elements(); while (en2.hasMoreElements()) {
556 * name.append("|"); name.append(en2.nextElement()); } en2 =
557 * entry.getName().elements(); while (en2.hasMoreElements()) {
558 * name.append("|"); name.append(en2.nextElement()); }
560 * if (entry.getProtein() != null) { name.append(" " +
561 * entry.getProtein().getName().elementAt(0)); }
563 * result.append(name + "\n" + entry.getUniprotSequence().getContent() +
566 * //Then read in the features and apply them to the dataset Alignment al =
567 * parseResult(result.toString(), null); for (int i = 0; i < entries.size();
568 * i++) { UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
569 * Enumeration e = entry.getDbReference().elements(); Vector onlyPdbEntries =
570 * new Vector(); while (e.hasMoreElements()) { PDBEntry pdb = (PDBEntry)
571 * e.nextElement(); if (!pdb.getType().equals("PDB")) { continue; }
573 * onlyPdbEntries.addElement(pdb); }
575 * Enumeration en2 = entry.getAccession().elements(); while
576 * (en2.hasMoreElements()) {
577 * al.getSequenceAt(i).getDatasetSequence().addDBRef(new DBRefEntry(
578 * DBRefSource.UNIPROT, "0", en2.nextElement().toString())); }
583 * al.getSequenceAt(i).getDatasetSequence().setPDBId(onlyPdbEntries); if
584 * (entry.getFeature() != null) { e = entry.getFeature().elements(); while
585 * (e.hasMoreElements()) { SequenceFeature sf = (SequenceFeature)
586 * e.nextElement(); sf.setFeatureGroup("Uniprot");
587 * al.getSequenceAt(i).getDatasetSequence().addSequenceFeature( sf ); } } } }
590 * SequenceI[] getPDBFile(String id) { Vector result = new Vector(); String
591 * chain = null; if (id.indexOf(":") > -1) { chain =
592 * id.substring(id.indexOf(":") + 1); id = id.substring(0, id.indexOf(":")); }
594 * EBIFetchClient ebi = new EBIFetchClient(); String file =
595 * ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw"). getAbsolutePath(); if (file
596 * == null) { return null; } try { PDBfile pdbfile = new PDBfile(file,
597 * jalview.io.AppletFormatAdapter.FILE); for (int i = 0; i <
598 * pdbfile.chains.size(); i++) { if (chain == null || ( (PDBChain)
599 * pdbfile.chains.elementAt(i)).id. toUpperCase().equals(chain)) { PDBChain
600 * pdbchain = (PDBChain) pdbfile.chains.elementAt(i); // Get the Chain's
601 * Sequence - who's dataset includes any special features added from the PDB
602 * file SequenceI sq = pdbchain.sequence; // Specially formatted name for the
603 * PDB chain sequences retrieved from the PDB
604 * sq.setName("PDB|"+id+"|"+sq.getName()); // Might need to add more metadata
605 * to the PDBEntry object // like below /* PDBEntry entry = new PDBEntry(); //
606 * Construct the PDBEntry entry.setId(id); if (entry.getProperty() == null)
607 * entry.setProperty(new Hashtable()); entry.getProperty().put("chains",
608 * pdbchain.id + "=" + sq.getStart() + "-" + sq.getEnd());
609 * sq.getDatasetSequence().addPDBId(entry); // Add PDB DB Refs // We make a
610 * DBRefEtntry because we have obtained the PDB file from a verifiable source
611 * // JBPNote - PDB DBRefEntry should also carry the chain and mapping
612 * information DBRefEntry dbentry = new
613 * DBRefEntry(jalview.datamodel.DBRefSource.PDB, "0", id + pdbchain.id);
614 * sq.addDBRef(dbentry); // and add seuqence to the retrieved set
615 * result.addElement(sq.deriveSequence()); } }
617 * if (result.size() < 1) { throw new Exception("WsDBFetch for PDB id resulted
618 * in zero result size"); } } catch (Exception ex) // Problem parsing PDB file
619 * { jalview.bin.Cache.log.warn("Exception when retrieving " +
620 * textArea.getText() + " from " + database.getSelectedItem(), ex); return
624 * SequenceI[] results = new SequenceI[result.size()]; for (int i = 0, j =
625 * result.size(); i < j; i++) { results[i] = (SequenceI) result.elementAt(i);
626 * result.setElementAt(null,i); } return results; }
628 AlignmentI parseResult(String result, String title)
630 String format = new IdentifyFile().Identify(result, "Paste");
631 Alignment sequences = null;
632 if (FormatAdapter.isValidFormat(format))
637 sequences = new FormatAdapter().readFile(result.toString(),
639 } catch (Exception ex)
643 if (sequences != null)
645 return parseResult(sequences, title, format);
650 showErrorMessage("Error retrieving " + textArea.getText() + " from "
651 + database.getSelectedItem());
657 AlignmentI parseResult(AlignmentI al, String title,
658 String currentFileFormat)
661 if (al != null && al.getHeight() > 0)
663 if (alignFrame == null)
665 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
666 AlignFrame.DEFAULT_HEIGHT);
667 if (currentFileFormat != null)
669 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
671 // NON-FormatAdapter Sourced
677 title = "Retrieved from " + database.getSelectedItem();
679 SequenceFeature[] sfs = null;
680 for (Enumeration sq = al.getSequences().elements(); sq
683 if ((sfs = ((SequenceI) sq.nextElement()).getDatasetSequence()
684 .getSequenceFeatures()) != null)
688 af.setShowSeqFeatures(true);
694 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
695 AlignFrame.DEFAULT_HEIGHT);
697 af.statusBar.setText("Successfully pasted alignment file");
701 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
703 } catch (Exception ex)
709 for (int i = 0; i < al.getHeight(); i++)
711 alignFrame.viewport.alignment.addSequence(al.getSequenceAt(i)); // this
718 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment
720 alignFrame.viewport.alignment.getWidth();
721 alignFrame.viewport.firePropertyChange("alignment", null,
722 alignFrame.viewport.getAlignment().getSequences());
728 void showErrorMessage(final String error)
731 javax.swing.SwingUtilities.invokeLater(new Runnable()
735 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
736 "Error Retrieving Data", JOptionPane.WARNING_MESSAGE);