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.*;
32 public class SequenceFetcher
33 extends JPanel implements Runnable
36 AlignFrame alignFrame;
38 final String noDbSelected = "-- Select Database --";
39 public SequenceFetcher(AlignFrame af)
42 database.addItem(noDbSelected);
43 database.addItem("Uniprot");
44 database.addItem("EMBL");
45 database.addItem("EMBLCDS");
46 database.addItem("PDB");
47 database.addItem("PFAM");
58 frame = new JInternalFrame();
59 frame.setContentPane(this);
60 if (System.getProperty("os.name").startsWith("Mac"))
62 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 140);
66 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 125);
70 private String getFrameTitle()
72 return ( (alignFrame == null) ? "New " : "Additional ") +
79 this.setLayout(gridBagLayout1);
81 database.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
82 database.setMinimumSize(new Dimension(160, 21));
83 database.setPreferredSize(new Dimension(160, 21));
84 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
86 "Separate multiple accession ids with semi colon \";\"");
88 ok.addActionListener(new ActionListener()
90 public void actionPerformed(ActionEvent e)
92 ok_actionPerformed(e);
95 close.setText("Close");
96 close.addActionListener(new ActionListener()
98 public void actionPerformed(ActionEvent e)
100 close_actionPerformed(e);
103 textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
104 textfield.addActionListener(new ActionListener()
106 public void actionPerformed(ActionEvent e)
108 ok_actionPerformed(e);
113 this.add(jLabel1, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0
114 , GridBagConstraints.WEST,
115 GridBagConstraints.NONE,
116 new Insets(7, 4, 0, 6), 77, 6));
117 this.add(jPanel1, new GridBagConstraints(0, 2, 2, 1, 1.0, 1.0
118 , GridBagConstraints.WEST,
119 GridBagConstraints.BOTH,
120 new Insets(7, -2, 7, 12), 241, -2));
121 this.add(database, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
122 , GridBagConstraints.WEST,
123 GridBagConstraints.NONE,
124 new Insets(0, 4, 0, 0), 1, 0));
125 this.add(textfield, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
126 , GridBagConstraints.CENTER,
127 GridBagConstraints.NONE,
128 new Insets(0, 0, 0, 6), 211, 1));
131 JComboBox database = new JComboBox();
132 JLabel jLabel1 = new JLabel();
133 JButton ok = new JButton();
134 JButton close = new JButton();
135 JPanel jPanel1 = new JPanel();
136 JTextField textfield = new JTextField();
137 GridBagLayout gridBagLayout1 = new GridBagLayout();
138 public void close_actionPerformed(ActionEvent e)
142 frame.setClosed(true);
148 public void ok_actionPerformed(ActionEvent e)
150 database.setEnabled(false);
151 textfield.setEnabled(false);
152 ok.setEnabled(false);
153 close.setEnabled(false);
155 Thread worker = new Thread(this);
159 private void resetDialog()
161 database.setEnabled(true);
162 textfield.setEnabled(true);
164 close.setEnabled(true);
170 if (database.getSelectedItem().equals(noDbSelected))
172 error += "Please select the source database\n";
174 com.stevesoft.pat.Regex empty = new com.stevesoft.pat.Regex("\\s+", "");
175 textfield.setText(empty.replaceAll(textfield.getText()));
176 if (textfield.getText().length() == 0)
178 error += "Please enter a (semi-colon separated list of) database id(s)";
180 if (error.length() > 0)
182 showErrorMessage(error);
187 result = new StringBuffer();
188 if (database.getSelectedItem().equals("Uniprot"))
190 getUniprotFile(textfield.getText());
192 else if (database.getSelectedItem().equals("EMBL")
193 || database.getSelectedItem().equals("EMBLCDS"))
195 StringTokenizer st = new StringTokenizer(textfield.getText(), ";");
196 while (st.hasMoreTokens())
198 EBIFetchClient dbFetch = new EBIFetchClient();
200 String[] reply = dbFetch.fetchData(
201 database.getSelectedItem().toString().toLowerCase(
202 ) + ":" + st.nextToken(),
207 for (int i = 0; i < reply.length; i++)
209 result.append(reply[i] + "\n");
214 if (result != null && result.length() > 1) // arbitrary minimum length for a seuqence file
216 System.out.println(result.toString());
218 parseResult(result.toString(), null);
221 else if (database.getSelectedItem().equals("PDB"))
223 StringTokenizer qset = new StringTokenizer(textfield.getText(), ";");
225 SequenceI[] seqs = null;
226 while (qset.hasMoreTokens() && ( (query = qset.nextToken()) != null))
228 SequenceI[] seqparts = getPDBFile(query.toUpperCase());
229 if (seqparts != null)
237 SequenceI[] newseqs = new SequenceI[seqs.length + seqparts.length];
239 for (; i < seqs.length; i++)
241 newseqs[i] = seqs[i];
244 for (int j = 0; j < seqparts.length; i++, j++)
246 newseqs[i] = seqparts[j];
250 result.append("# Success for " + query.toUpperCase() + "\n");
253 if (seqs != null && seqs.length > 0)
255 if (parseResult(new Alignment(seqs), null, null) != null)
258 "# Successfully parsed the PDB File Queries into an Alignment");
262 else if (database.getSelectedItem().equals("PFAM"))
266 result.append(new FastaFile(
267 "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc="
268 + textfield.getText().toUpperCase(), "URL").print()
271 if (result.length() > 0)
273 parseResult(result.toString(), textfield.getText().toUpperCase());
277 catch (java.io.IOException ex)
283 if (result == null || result.length() == 0)
285 showErrorMessage("Error retrieving " + textfield.getText()
286 + " from " + database.getSelectedItem());
293 void getUniprotFile(String id)
295 EBIFetchClient ebi = new EBIFetchClient();
296 File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
298 DBRefFetcher dbref = new DBRefFetcher();
299 Vector entries = dbref.getUniprotEntries(file);
303 //First, make the new sequences
304 Enumeration en = entries.elements();
305 while (en.hasMoreElements())
307 UniprotEntry entry = (UniprotEntry) en.nextElement();
309 StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
310 Enumeration en2 = entry.getAccession().elements();
311 while (en2.hasMoreElements())
314 name.append(en2.nextElement());
316 en2 = entry.getName().elements();
317 while (en2.hasMoreElements())
320 name.append(en2.nextElement());
323 if (entry.getProtein() != null)
325 name.append(" " + entry.getProtein().getName().elementAt(0));
328 result.append(name + "\n" + entry.getUniprotSequence().getContent() +
333 //Then read in the features and apply them to the dataset
334 Alignment al = parseResult(result.toString(), null);
335 for (int i = 0; i < entries.size(); i++)
337 UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
338 Enumeration e = entry.getDbReference().elements();
339 Vector onlyPdbEntries = new Vector();
340 while (e.hasMoreElements())
342 PDBEntry pdb = (PDBEntry) e.nextElement();
343 if (!pdb.getType().equals("PDB"))
348 onlyPdbEntries.addElement(pdb);
351 Enumeration en2 = entry.getAccession().elements();
352 while (en2.hasMoreElements())
354 al.getSequenceAt(i).getDatasetSequence().addDBRef(new DBRefEntry(
357 en2.nextElement().toString()));
360 al.getSequenceAt(i).getDatasetSequence().setPDBId(onlyPdbEntries);
361 if (entry.getFeature() != null)
363 e = entry.getFeature().elements();
364 while (e.hasMoreElements())
366 SequenceFeature sf = (SequenceFeature) e.nextElement();
367 sf.setFeatureGroup("Uniprot");
368 al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
375 SequenceI[] getPDBFile(String id)
377 Vector result = new Vector();
379 if (id.indexOf(":") > -1)
381 chain = id.substring(id.indexOf(":") + 1);
382 id = id.substring(0, id.indexOf(":"));
385 EBIFetchClient ebi = new EBIFetchClient();
386 String file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").
394 PDBfile pdbfile = new PDBfile(file, jalview.io.AppletFormatAdapter.FILE);
395 for (int i = 0; i < pdbfile.chains.size(); i++)
398 ( (PDBChain) pdbfile.chains.elementAt(i)).id.
399 toUpperCase().equals(chain))
401 PDBChain pdbchain = (PDBChain) pdbfile.chains.elementAt(i);
402 // Get the Chain's Sequence - who's dataset includes any special features added from the PDB file
403 SequenceI sq = pdbchain.sequence;
404 // Specially formatted name for the PDB chain sequences retrieved from the PDB
405 sq.setName("PDB|" + id + "|" + sq.getName());
406 // Might need to add more metadata to the PDBEntry object
409 * PDBEntry entry = new PDBEntry();
410 // Construct the PDBEntry
412 if (entry.getProperty() == null)
413 entry.setProperty(new Hashtable());
414 entry.getProperty().put("chains",
416 + "=" + sq.getStart()
417 + "-" + sq.getEnd());
418 sq.getDatasetSequence().addPDBId(entry);
421 // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source
422 // JBPNote - PDB DBRefEntry should also carry the chain and mapping information
423 DBRefEntry dbentry = new DBRefEntry(jalview.datamodel.DBRefSource.PDB,
424 "0", id + pdbchain.id);
425 sq.addDBRef(dbentry);
426 // and add seuqence to the retrieved set
427 result.addElement(sq.deriveSequence());
431 catch (Exception ex) // Problem parsing PDB file
433 jalview.bin.Cache.log.warn("Exception when retrieving " +
434 textfield.getText() + " from " +
435 database.getSelectedItem(), ex);
438 SequenceI[] results = new SequenceI[result.size()];
439 for (int i = 0, j = result.size(); i < j; i++)
441 results[i] = (SequenceI) result.elementAt(i);
442 result.setElementAt(null, i);
447 Alignment parseResult(String result, String title)
449 String format = new IdentifyFile().Identify(result, "Paste");
450 Alignment sequences = null;
451 if (FormatAdapter.isValidFormat(format))
456 sequences = new FormatAdapter().readFile(result.toString(), "Paste",
462 if (sequences != null)
464 return parseResult(sequences, title, format);
469 showErrorMessage("Error retrieving " + textfield.getText()
470 + " from " + database.getSelectedItem());
476 Alignment parseResult(Alignment al, String title, String currentFileFormat)
479 if (al != null && al.getHeight() > 0)
481 if (alignFrame == null)
483 AlignFrame af = new AlignFrame(al,
484 AlignFrame.DEFAULT_WIDTH,
485 AlignFrame.DEFAULT_HEIGHT);
486 if (currentFileFormat != null)
488 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT FORMAT FOR NON-FormatAdapter Sourced Alignments?
493 title = "Retrieved from " + database.getSelectedItem();
496 Desktop.addInternalFrame(af,
498 AlignFrame.DEFAULT_WIDTH,
499 AlignFrame.DEFAULT_HEIGHT);
501 af.statusBar.setText("Successfully pasted alignment file");
505 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
512 for (int i = 0; i < al.getHeight(); i++)
514 alignFrame.viewport.alignment.addSequence(al.getSequenceAt(i)); // this also creates dataset sequence entries
516 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.
518 alignFrame.viewport.alignment.getWidth();
519 alignFrame.viewport.firePropertyChange("alignment", null,
521 getAlignment().getSequences());
527 void showErrorMessage(final String error)
530 javax.swing.SwingUtilities.invokeLater(new Runnable()
534 JOptionPane.showInternalMessageDialog(Desktop.desktop,
535 error, "Error Retrieving Data",
536 JOptionPane.WARNING_MESSAGE);