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.*;
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;
37 import java.awt.Rectangle;
38 import java.awt.BorderLayout;
39 import java.awt.Dimension;
41 public class SequenceFetcher
42 extends JPanel implements Runnable
44 ASequenceFetcher sfetch;
46 AlignFrame alignFrame;
48 final String noDbSelected = "-- Select Database --";
49 Hashtable sources = new Hashtable();
50 public SequenceFetcher(AlignFrame af)
53 sfetch = new jalview.ws.SequenceFetcher();
54 database.addItem(noDbSelected);
56 * Dynamically generated database list
57 * will need a translation function from
58 * internal source to externally distinct names.
59 * UNIPROT and UP_NAME are identical DB sources,
60 * and should be collapsed.
63 String dbs[] = sfetch.getSupportedDb();
64 for (int i=0; i<dbs.length;i++)
66 if (!sources.containsValue(dbs[i]))
68 String name = sfetch.getSourceProxy(dbs[i]).getDbName();
69 // duplicate source names are thrown away, here.
70 if (!sources.containsKey(name))
72 database.addItem(name);
74 // overwrite with latest version of the retriever for this source
75 sources.put(name, dbs[i]);
87 frame = new JInternalFrame();
88 frame.setContentPane(this);
89 if (new jalview.util.Platform().isAMac())
91 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 140);
95 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 125);
99 private String getFrameTitle()
101 return ( (alignFrame == null) ? "New " : "Additional ") +
105 private void jbInit()
108 this.setLayout(borderLayout2);
110 database.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
111 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
112 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
114 "Separate multiple accession ids with semi colon \";\"");
116 ok.addActionListener(new ActionListener()
118 public void actionPerformed(ActionEvent e)
120 ok_actionPerformed();
123 close.setText("Close");
124 close.addActionListener(new ActionListener()
126 public void actionPerformed(ActionEvent e)
128 close_actionPerformed(e);
131 textArea.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
132 textArea.setLineWrap(true);
133 textArea.addKeyListener(new KeyAdapter()
135 public void keyPressed(KeyEvent e)
137 if(e.getKeyCode()==KeyEvent.VK_ENTER)
138 ok_actionPerformed();
141 jPanel3.setLayout(borderLayout1);
142 borderLayout1.setVgap(5);
145 jPanel3.add(jPanel2, java.awt.BorderLayout.WEST);
146 jPanel2.add(database);
147 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
148 jPanel3.add(jLabel1, java.awt.BorderLayout.NORTH);
149 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
150 this.add(jPanel3, java.awt.BorderLayout.CENTER);
151 jScrollPane1.getViewport().add(textArea);
155 JComboBox database = new JComboBox();
156 JLabel jLabel1 = new JLabel();
157 JButton ok = new JButton();
158 JButton close = new JButton();
159 JPanel jPanel1 = new JPanel();
160 JTextArea textArea = new JTextArea();
161 JScrollPane jScrollPane1 = new JScrollPane();
162 JPanel jPanel2 = new JPanel();
163 JPanel jPanel3 = new JPanel();
164 BorderLayout borderLayout1 = new BorderLayout();
165 BorderLayout borderLayout2 = new BorderLayout();
166 public void close_actionPerformed(ActionEvent e)
170 frame.setClosed(true);
176 public void ok_actionPerformed()
178 database.setEnabled(false);
179 textArea.setEnabled(false);
180 ok.setEnabled(false);
181 close.setEnabled(false);
183 Thread worker = new Thread(this);
187 private void resetDialog()
189 database.setEnabled(true);
190 textArea.setEnabled(true);
192 close.setEnabled(true);
198 if (database.getSelectedItem().equals(noDbSelected))
200 error += "Please select the source database\n";
202 com.stevesoft.pat.Regex empty = new com.stevesoft.pat.Regex("\\s+", "");
203 textArea.setText(empty.replaceAll(textArea.getText()));
204 if (textArea.getText().length() == 0)
206 error += "Please enter a (semi-colon separated list of) database id(s)";
208 if (error.length() > 0)
210 showErrorMessage(error);
214 AlignmentI aresult=null;
216 aresult = sfetch.getSourceProxy((String)sources.get(database.getSelectedItem()))
217 .getSequenceRecords(textArea.getText());
221 showErrorMessage("Error retrieving " + textArea.getText()
222 + " from " + database.getSelectedItem());
223 //error +="Couldn't retrieve sequences from "+database.getSelectedItem();
224 System.err.println("Retrieval failed for source ='"+database.getSelectedItem()+"' and query\n'"+textArea.getText()+"'\n");
229 parseResult(aresult, null, null);
234 result = new StringBuffer();
235 if (database.getSelectedItem().equals("Uniprot"))
237 getUniprotFile(textArea.getText());
239 else if (database.getSelectedItem().equals("EMBL")
240 || database.getSelectedItem().equals("EMBLCDS"))
242 String DBRefSource = database.getSelectedItem().equals("EMBLCDS")
243 ? jalview.datamodel.DBRefSource.EMBLCDS
244 : jalview.datamodel.DBRefSource.EMBL;
246 StringTokenizer st = new StringTokenizer(textArea.getText(), ";");
247 SequenceI[] seqs = null;
248 while(st.hasMoreTokens())
250 EBIFetchClient dbFetch = new EBIFetchClient();
251 String qry = database.getSelectedItem().toString().toLowerCase(
252 ) + ":" + st.nextToken();
253 File reply = dbFetch.fetchDataAsFile(
257 jalview.datamodel.xdb.embl.EmblFile efile=null;
258 if (reply != null && reply.exists())
260 efile = jalview.datamodel.xdb.embl.EmblFile.getEmblFile(reply);
263 for (Iterator i=efile.getEntries().iterator(); i.hasNext(); ) {
264 EmblEntry entry = (EmblEntry) i.next();
265 SequenceI[] seqparts = entry.getSequences(false,true, DBRefSource);
266 if (seqparts!=null) {
267 SequenceI[] newseqs = null;
270 newseqs = new SequenceI[seqparts.length];
272 newseqs = new SequenceI[seqs.length+seqparts.length];
274 for (;si<seqs.length; si++) {
275 newseqs[si] = seqs[si];
279 for (int j=0;j<seqparts.length; si++, j++) {
280 newseqs[si] = seqparts[j].deriveSequence(); // place DBReferences on dataset and refer
287 result.append("# no response for "+qry);
290 if (seqs!=null && seqs.length>0) {
291 if (parseResult(new Alignment(seqs), null, null)!=null)
293 result.append("# Successfully parsed the "+database.getSelectedItem()+" Queries into an Alignment");
297 else if (database.getSelectedItem().equals("PDB"))
299 StringTokenizer qset = new StringTokenizer(textArea.getText(), ";");
301 SequenceI[] seqs = null;
302 while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null))
304 SequenceI[] seqparts = getPDBFile(query.toUpperCase());
305 if (seqparts != null)
313 SequenceI[] newseqs = new SequenceI[seqs.length+seqparts.length];
315 for (; i < seqs.length; i++)
317 newseqs[i] = seqs[i];
320 for (int j=0;j<seqparts.length; i++, j++)
322 newseqs[i] = seqparts[j];
326 result.append("# Success for "+query.toUpperCase()+"\n");
329 if (seqs != null && seqs.length > 0)
331 if (parseResult(new Alignment(seqs), null, null)!=null)
334 "# Successfully parsed the PDB File Queries into an Alignment");
338 else if( database.getSelectedItem().equals("PFAM"))
342 result.append(new FastaFile(
343 "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc="
344 + textArea.getText().toUpperCase(), "URL").print()
347 if(result.length()>0)
349 parseResult( result.toString(), textArea.getText().toUpperCase() );
353 catch (java.io.IOException ex)
359 if (result == null || result.length() == 0)
361 showErrorMessage("Error retrieving " + textArea.getText()
362 + " from " + database.getSelectedItem());
369 void getUniprotFile(String id)
371 EBIFetchClient ebi = new EBIFetchClient();
372 File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
374 DBRefFetcher dbref = new DBRefFetcher();
375 Vector entries = dbref.getUniprotEntries(file);
379 //First, make the new sequences
380 Enumeration en = entries.elements();
381 while (en.hasMoreElements())
383 UniprotEntry entry = (UniprotEntry) en.nextElement();
385 StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
386 Enumeration en2 = entry.getAccession().elements();
387 while (en2.hasMoreElements())
390 name.append(en2.nextElement());
392 en2 = entry.getName().elements();
393 while (en2.hasMoreElements())
396 name.append(en2.nextElement());
399 if (entry.getProtein() != null)
401 name.append(" " + entry.getProtein().getName().elementAt(0));
404 result.append(name + "\n" + entry.getUniprotSequence().getContent() +
409 //Then read in the features and apply them to the dataset
410 Alignment al = parseResult(result.toString(), null);
411 for (int i = 0; i < entries.size(); i++)
413 UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
414 Enumeration e = entry.getDbReference().elements();
415 Vector onlyPdbEntries = new Vector();
416 while (e.hasMoreElements())
418 PDBEntry pdb = (PDBEntry) e.nextElement();
419 if (!pdb.getType().equals("PDB"))
424 onlyPdbEntries.addElement(pdb);
427 Enumeration en2 = entry.getAccession().elements();
428 while (en2.hasMoreElements())
430 al.getSequenceAt(i).getDatasetSequence().addDBRef(new DBRefEntry(
433 en2.nextElement().toString()));
439 al.getSequenceAt(i).getDatasetSequence().setPDBId(onlyPdbEntries);
440 if (entry.getFeature() != null)
442 e = entry.getFeature().elements();
443 while (e.hasMoreElements())
445 SequenceFeature sf = (SequenceFeature) e.nextElement();
446 sf.setFeatureGroup("Uniprot");
447 al.getSequenceAt(i).getDatasetSequence().addSequenceFeature( sf );
454 SequenceI[] getPDBFile(String id)
456 Vector result = new Vector();
458 if (id.indexOf(":") > -1)
460 chain = id.substring(id.indexOf(":") + 1);
461 id = id.substring(0, id.indexOf(":"));
464 EBIFetchClient ebi = new EBIFetchClient();
465 String file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").
473 PDBfile pdbfile = new PDBfile(file, jalview.io.AppletFormatAdapter.FILE);
474 for (int i = 0; i < pdbfile.chains.size(); i++)
477 ( (PDBChain) pdbfile.chains.elementAt(i)).id.
478 toUpperCase().equals(chain))
480 PDBChain pdbchain = (PDBChain) pdbfile.chains.elementAt(i);
481 // Get the Chain's Sequence - who's dataset includes any special features added from the PDB file
482 SequenceI sq = pdbchain.sequence;
483 // Specially formatted name for the PDB chain sequences retrieved from the PDB
484 sq.setName("PDB|"+id+"|"+sq.getName());
485 // Might need to add more metadata to the PDBEntry object
488 * PDBEntry entry = new PDBEntry();
489 // Construct the PDBEntry
491 if (entry.getProperty() == null)
492 entry.setProperty(new Hashtable());
493 entry.getProperty().put("chains",
495 + "=" + sq.getStart()
496 + "-" + sq.getEnd());
497 sq.getDatasetSequence().addPDBId(entry);
500 // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source
501 // JBPNote - PDB DBRefEntry should also carry the chain and mapping information
502 DBRefEntry dbentry = new DBRefEntry(jalview.datamodel.DBRefSource.PDB,
503 "0", id + pdbchain.id);
504 sq.addDBRef(dbentry);
505 // and add seuqence to the retrieved set
506 result.addElement(sq.deriveSequence());
510 if (result.size() < 1)
512 throw new Exception("WsDBFetch for PDB id resulted in zero result size");
515 catch (Exception ex) // Problem parsing PDB file
517 jalview.bin.Cache.log.warn("Exception when retrieving " +
518 textArea.getText() + " from " +
519 database.getSelectedItem(), ex);
524 SequenceI[] results = new SequenceI[result.size()];
525 for (int i = 0, j = result.size(); i < j; i++)
527 results[i] = (SequenceI) result.elementAt(i);
528 result.setElementAt(null,i);
532 AlignmentI parseResult(String result, String title)
534 String format = new IdentifyFile().Identify(result, "Paste");
535 Alignment sequences = null;
536 if (FormatAdapter.isValidFormat(format))
541 sequences = new FormatAdapter().readFile(result.toString(), "Paste",
549 return parseResult(sequences, title, format);
554 showErrorMessage("Error retrieving " + textArea.getText()
555 + " from " + database.getSelectedItem());
561 AlignmentI parseResult(AlignmentI al, String title, String currentFileFormat)
564 if (al != null && al.getHeight() > 0)
566 if (alignFrame == null)
568 AlignFrame af = new AlignFrame(al,
569 AlignFrame.DEFAULT_WIDTH,
570 AlignFrame.DEFAULT_HEIGHT);
571 if (currentFileFormat!=null)
573 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT FORMAT FOR NON-FormatAdapter Sourced Alignments?
578 title = "Retrieved from " + database.getSelectedItem();
581 Desktop.addInternalFrame(af,
583 AlignFrame.DEFAULT_WIDTH,
584 AlignFrame.DEFAULT_HEIGHT);
586 af.statusBar.setText("Successfully pasted alignment file");
590 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
597 for (int i = 0; i < al.getHeight(); i++)
599 alignFrame.viewport.alignment.addSequence(al.getSequenceAt(i)); // this also creates dataset sequence entries
601 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.
603 alignFrame.viewport.alignment.getWidth();
604 alignFrame.viewport.firePropertyChange("alignment", null,
606 getAlignment().getSequences());
612 void showErrorMessage(final String error)
615 javax.swing.SwingUtilities.invokeLater(new Runnable()
619 JOptionPane.showInternalMessageDialog(Desktop.desktop,
620 error, "Error Retrieving Data",
621 JOptionPane.WARNING_MESSAGE);