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
44 extends JPanel implements Runnable
46 // ASequenceFetcher sfetch;
48 IProgressIndicator guiWindow;
49 AlignFrame alignFrame;
51 final String noDbSelected = "-- Select Database --";
52 Hashtable sources = new Hashtable();
53 private static jalview.ws.SequenceFetcher sfetch=null;
54 public SequenceFetcher(IProgressIndicator guiIndic)
56 final IProgressIndicator guiWindow = guiIndic;
57 final SequenceFetcher us = this;
58 // launch initialiser thread
59 Thread sf = new Thread(new Runnable() {
66 * give a visual indication that sequence fetcher construction is occuring
68 if ( guiWindow!=null )
70 guiWindow.setProgressBar("Initialising Sequence Database Fetchers", this.hashCode());
72 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
73 if ( guiWindow!=null )
75 guiWindow.setProgressBar("Initialising Sequence Database Fetchers", this.hashCode());
80 us.initGui(guiWindow);
86 * called by thread spawned by constructor
89 private void initGui(IProgressIndicator guiWindow) {
90 this.guiWindow = guiWindow;
91 if (guiWindow instanceof AlignFrame)
93 alignFrame = (AlignFrame) guiWindow;
96 database.addItem(noDbSelected);
98 * Dynamically generated database list
99 * will need a translation function from
100 * internal source to externally distinct names.
101 * UNIPROT and UP_NAME are identical DB sources,
102 * and should be collapsed.
105 String dbs[] = sfetch.getOrderedSupportedSources();
106 for (int i=0; i<dbs.length;i++)
108 if (!sources.containsValue(dbs[i]))
110 String name = sfetch.getSourceProxy(dbs[i]).getDbName();
111 // duplicate source names are thrown away, here.
112 if (!sources.containsKey(name))
114 database.addItem(name);
116 // overwrite with latest version of the retriever for this source
117 sources.put(name, dbs[i]);
126 ex.printStackTrace();
129 frame = new JInternalFrame();
130 frame.setContentPane(this);
131 if (new jalview.util.Platform().isAMac())
133 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 140);
137 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 125);
141 private String getFrameTitle()
143 return ( (alignFrame == null) ? "New " : "Additional ") +
147 private void jbInit()
150 this.setLayout(borderLayout2);
152 database.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
153 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
154 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
155 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
157 "Separate multiple accession ids with semi colon \";\"");
159 ok.addActionListener(new ActionListener()
161 public void actionPerformed(ActionEvent e)
163 ok_actionPerformed();
166 clear.setText("Clear");
167 clear.addActionListener(new ActionListener()
169 public void actionPerformed(ActionEvent e)
171 clear_actionPerformed();
175 example.setText("Example");
176 example.addActionListener(new ActionListener()
178 public void actionPerformed(ActionEvent e)
180 example_actionPerformed();
183 close.setText("Close");
184 close.addActionListener(new ActionListener()
186 public void actionPerformed(ActionEvent e)
188 close_actionPerformed(e);
191 textArea.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
192 textArea.setLineWrap(true);
193 textArea.addKeyListener(new KeyAdapter()
195 public void keyPressed(KeyEvent e)
197 if(e.getKeyCode()==KeyEvent.VK_ENTER)
198 ok_actionPerformed();
201 jPanel3.setLayout(borderLayout1);
202 borderLayout1.setVgap(5);
204 jPanel1.add(example);
207 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
208 jPanel2.setLayout(borderLayout3);
210 database.addActionListener(new ActionListener() {
212 public void actionPerformed(ActionEvent e)
214 DbSourceProxy db=null;
216 db = sfetch.getSourceProxy((String)sources.get(database.getSelectedItem()));
217 dbeg.setText("Example query: "+db.getTestQuery());
218 } catch (Exception ex)
226 jPanel2.add(database, java.awt.BorderLayout.NORTH);
227 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
228 jPanel2.add(jLabel1, java.awt.BorderLayout.SOUTH);
229 // jPanel2.setPreferredSize(new Dimension())
230 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
231 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
232 this.add(jPanel3, java.awt.BorderLayout.CENTER);
233 this.add(jPanel2,java.awt.BorderLayout.NORTH);
234 jScrollPane1.getViewport().add(textArea);
238 protected void example_actionPerformed()
240 DbSourceProxy db=null;
242 db = sfetch.getSourceProxy((String)sources.get(database.getSelectedItem()));
243 textArea.setText(db.getTestQuery());
244 } catch (Exception ex)
250 protected void clear_actionPerformed()
252 textArea.setText("");
255 JLabel dbeg = new JLabel();
256 JComboBox database = new JComboBox();
257 JLabel jLabel1 = new JLabel();
258 JButton ok = new JButton();
259 JButton clear = new JButton();
260 JButton example = new JButton();
261 JButton close = new JButton();
262 JPanel jPanel1 = new JPanel();
263 JTextArea textArea = new JTextArea();
264 JScrollPane jScrollPane1 = new JScrollPane();
265 JPanel jPanel2 = new JPanel();
266 JPanel jPanel3 = new JPanel();
267 JPanel jPanel4 = new JPanel();
269 BorderLayout borderLayout1 = new BorderLayout();
270 BorderLayout borderLayout2 = new BorderLayout();
271 BorderLayout borderLayout3 = new BorderLayout();
272 public void close_actionPerformed(ActionEvent e)
276 frame.setClosed(true);
282 public void ok_actionPerformed()
284 database.setEnabled(false);
285 textArea.setEnabled(false);
286 ok.setEnabled(false);
287 close.setEnabled(false);
289 Thread worker = new Thread(this);
293 private void resetDialog()
295 database.setEnabled(true);
296 textArea.setEnabled(true);
298 close.setEnabled(true);
304 if (database.getSelectedItem().equals(noDbSelected))
306 error += "Please select the source database\n";
308 com.stevesoft.pat.Regex empty = new com.stevesoft.pat.Regex("\\s+", "");
309 textArea.setText(empty.replaceAll(textArea.getText()));
310 if (textArea.getText().length() == 0)
312 error += "Please enter a (semi-colon separated list of) database id(s)";
314 if (error.length() > 0)
316 showErrorMessage(error);
320 AlignmentI aresult=null;
322 guiWindow.setProgressBar("Fetching Sequences from "+database.getSelectedItem(), Thread.currentThread().hashCode());
323 aresult = sfetch.getSourceProxy((String)sources.get(database.getSelectedItem()))
324 .getSequenceRecords(textArea.getText());
329 showErrorMessage("Error retrieving " + textArea.getText()
330 + " from " + database.getSelectedItem());
331 //error +="Couldn't retrieve sequences from "+database.getSelectedItem();
332 System.err.println("Retrieval failed for source ='"+database.getSelectedItem()+"' and query\n'"+textArea.getText()+"'\n");
335 catch (OutOfMemoryError e)
337 showErrorMessage("Out of Memory when retrieving " + textArea.getText()
338 + " from " + database.getSelectedItem()+"\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
343 showErrorMessage("Serious Error retrieving " + textArea.getText()
344 + " from " + database.getSelectedItem());
347 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
350 parseResult(aresult, null, null);
355 result = new StringBuffer();
356 if (database.getSelectedItem().equals("Uniprot"))
358 getUniprotFile(textArea.getText());
360 else if (database.getSelectedItem().equals("EMBL")
361 || database.getSelectedItem().equals("EMBLCDS"))
363 String DBRefSource = database.getSelectedItem().equals("EMBLCDS")
364 ? jalview.datamodel.DBRefSource.EMBLCDS
365 : jalview.datamodel.DBRefSource.EMBL;
367 StringTokenizer st = new StringTokenizer(textArea.getText(), ";");
368 SequenceI[] seqs = null;
369 while(st.hasMoreTokens())
371 EBIFetchClient dbFetch = new EBIFetchClient();
372 String qry = database.getSelectedItem().toString().toLowerCase(
373 ) + ":" + st.nextToken();
374 File reply = dbFetch.fetchDataAsFile(
378 jalview.datamodel.xdb.embl.EmblFile efile=null;
379 if (reply != null && reply.exists())
381 efile = jalview.datamodel.xdb.embl.EmblFile.getEmblFile(reply);
384 for (Iterator i=efile.getEntries().iterator(); i.hasNext(); ) {
385 EmblEntry entry = (EmblEntry) i.next();
386 SequenceI[] seqparts = entry.getSequences(false,true, DBRefSource);
387 if (seqparts!=null) {
388 SequenceI[] newseqs = null;
391 newseqs = new SequenceI[seqparts.length];
393 newseqs = new SequenceI[seqs.length+seqparts.length];
395 for (;si<seqs.length; si++) {
396 newseqs[si] = seqs[si];
400 for (int j=0;j<seqparts.length; si++, j++) {
401 newseqs[si] = seqparts[j].deriveSequence(); // place DBReferences on dataset and refer
408 result.append("# no response for "+qry);
411 if (seqs!=null && seqs.length>0) {
412 if (parseResult(new Alignment(seqs), null, null)!=null)
414 result.append("# Successfully parsed the "+database.getSelectedItem()+" Queries into an Alignment");
418 else if (database.getSelectedItem().equals("PDB"))
420 StringTokenizer qset = new StringTokenizer(textArea.getText(), ";");
422 SequenceI[] seqs = null;
423 while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null))
425 SequenceI[] seqparts = getPDBFile(query.toUpperCase());
426 if (seqparts != null)
434 SequenceI[] newseqs = new SequenceI[seqs.length+seqparts.length];
436 for (; i < seqs.length; i++)
438 newseqs[i] = seqs[i];
441 for (int j=0;j<seqparts.length; i++, j++)
443 newseqs[i] = seqparts[j];
447 result.append("# Success for "+query.toUpperCase()+"\n");
450 if (seqs != null && seqs.length > 0)
452 if (parseResult(new Alignment(seqs), null, null)!=null)
455 "# Successfully parsed the PDB File Queries into an Alignment");
459 else if( database.getSelectedItem().equals("PFAM"))
463 result.append(new FastaFile(
464 "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc="
465 + textArea.getText().toUpperCase(), "URL").print()
468 if(result.length()>0)
470 parseResult( result.toString(), textArea.getText().toUpperCase() );
474 catch (java.io.IOException ex)
480 if (result == null || result.length() == 0)
482 showErrorMessage("Error retrieving " + textArea.getText()
483 + " from " + database.getSelectedItem());
490 void getUniprotFile(String id)
492 EBIFetchClient ebi = new EBIFetchClient();
493 File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
495 DBRefFetcher dbref = new DBRefFetcher();
496 Vector entries = dbref.getUniprotEntries(file);
500 //First, make the new sequences
501 Enumeration en = entries.elements();
502 while (en.hasMoreElements())
504 UniprotEntry entry = (UniprotEntry) en.nextElement();
506 StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
507 Enumeration en2 = entry.getAccession().elements();
508 while (en2.hasMoreElements())
511 name.append(en2.nextElement());
513 en2 = entry.getName().elements();
514 while (en2.hasMoreElements())
517 name.append(en2.nextElement());
520 if (entry.getProtein() != null)
522 name.append(" " + entry.getProtein().getName().elementAt(0));
525 result.append(name + "\n" + entry.getUniprotSequence().getContent() +
530 //Then read in the features and apply them to the dataset
531 Alignment al = parseResult(result.toString(), null);
532 for (int i = 0; i < entries.size(); i++)
534 UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
535 Enumeration e = entry.getDbReference().elements();
536 Vector onlyPdbEntries = new Vector();
537 while (e.hasMoreElements())
539 PDBEntry pdb = (PDBEntry) e.nextElement();
540 if (!pdb.getType().equals("PDB"))
545 onlyPdbEntries.addElement(pdb);
548 Enumeration en2 = entry.getAccession().elements();
549 while (en2.hasMoreElements())
551 al.getSequenceAt(i).getDatasetSequence().addDBRef(new DBRefEntry(
554 en2.nextElement().toString()));
560 al.getSequenceAt(i).getDatasetSequence().setPDBId(onlyPdbEntries);
561 if (entry.getFeature() != null)
563 e = entry.getFeature().elements();
564 while (e.hasMoreElements())
566 SequenceFeature sf = (SequenceFeature) e.nextElement();
567 sf.setFeatureGroup("Uniprot");
568 al.getSequenceAt(i).getDatasetSequence().addSequenceFeature( sf );
575 SequenceI[] getPDBFile(String id)
577 Vector result = new Vector();
579 if (id.indexOf(":") > -1)
581 chain = id.substring(id.indexOf(":") + 1);
582 id = id.substring(0, id.indexOf(":"));
585 EBIFetchClient ebi = new EBIFetchClient();
586 String file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").
594 PDBfile pdbfile = new PDBfile(file, jalview.io.AppletFormatAdapter.FILE);
595 for (int i = 0; i < pdbfile.chains.size(); i++)
598 ( (PDBChain) pdbfile.chains.elementAt(i)).id.
599 toUpperCase().equals(chain))
601 PDBChain pdbchain = (PDBChain) pdbfile.chains.elementAt(i);
602 // Get the Chain's Sequence - who's dataset includes any special features added from the PDB file
603 SequenceI sq = pdbchain.sequence;
604 // Specially formatted name for the PDB chain sequences retrieved from the PDB
605 sq.setName("PDB|"+id+"|"+sq.getName());
606 // Might need to add more metadata to the PDBEntry object
609 * PDBEntry entry = new PDBEntry();
610 // Construct the PDBEntry
612 if (entry.getProperty() == null)
613 entry.setProperty(new Hashtable());
614 entry.getProperty().put("chains",
616 + "=" + sq.getStart()
617 + "-" + sq.getEnd());
618 sq.getDatasetSequence().addPDBId(entry);
621 // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source
622 // JBPNote - PDB DBRefEntry should also carry the chain and mapping information
623 DBRefEntry dbentry = new DBRefEntry(jalview.datamodel.DBRefSource.PDB,
624 "0", id + pdbchain.id);
625 sq.addDBRef(dbentry);
626 // and add seuqence to the retrieved set
627 result.addElement(sq.deriveSequence());
631 if (result.size() < 1)
633 throw new Exception("WsDBFetch for PDB id resulted in zero result size");
636 catch (Exception ex) // Problem parsing PDB file
638 jalview.bin.Cache.log.warn("Exception when retrieving " +
639 textArea.getText() + " from " +
640 database.getSelectedItem(), ex);
645 SequenceI[] results = new SequenceI[result.size()];
646 for (int i = 0, j = result.size(); i < j; i++)
648 results[i] = (SequenceI) result.elementAt(i);
649 result.setElementAt(null,i);
653 AlignmentI parseResult(String result, String title)
655 String format = new IdentifyFile().Identify(result, "Paste");
656 Alignment sequences = null;
657 if (FormatAdapter.isValidFormat(format))
662 sequences = new FormatAdapter().readFile(result.toString(), "Paste",
670 return parseResult(sequences, title, format);
675 showErrorMessage("Error retrieving " + textArea.getText()
676 + " from " + database.getSelectedItem());
682 AlignmentI parseResult(AlignmentI al, String title, String currentFileFormat)
685 if (al != null && al.getHeight() > 0)
687 if (alignFrame == null)
689 AlignFrame af = new AlignFrame(al,
690 AlignFrame.DEFAULT_WIDTH,
691 AlignFrame.DEFAULT_HEIGHT);
692 if (currentFileFormat!=null)
694 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT FORMAT FOR NON-FormatAdapter Sourced Alignments?
699 title = "Retrieved from " + database.getSelectedItem();
702 Desktop.addInternalFrame(af,
704 AlignFrame.DEFAULT_WIDTH,
705 AlignFrame.DEFAULT_HEIGHT);
707 af.statusBar.setText("Successfully pasted alignment file");
711 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
718 for (int i = 0; i < al.getHeight(); i++)
720 alignFrame.viewport.alignment.addSequence(al.getSequenceAt(i)); // this also creates dataset sequence entries
722 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.
724 alignFrame.viewport.alignment.getWidth();
725 alignFrame.viewport.firePropertyChange("alignment", null,
727 getAlignment().getSequences());
733 void showErrorMessage(final String error)
736 javax.swing.SwingUtilities.invokeLater(new Runnable()
740 JOptionPane.showInternalMessageDialog(Desktop.desktop,
741 error, "Error Retrieving Data",
742 JOptionPane.WARNING_MESSAGE);