2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
23 import java.awt.event.*;
24 import jalview.io.EBIFetchClient;
26 import jalview.datamodel.*;
27 import jalview.analysis.AlignSeq;
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"))
61 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 140);
63 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 125);
66 private String getFrameTitle()
68 return ( (alignFrame == null) ? "New " : "Additional ") + "Sequence Fetcher";
74 this.setLayout(gridBagLayout1);
76 database.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
77 database.setMinimumSize(new Dimension(160, 21));
78 database.setPreferredSize(new Dimension(160, 21));
79 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
81 "Separate multiple accession ids with semi colon \";\"");
83 ok.addActionListener(new ActionListener()
85 public void actionPerformed(ActionEvent e)
87 ok_actionPerformed(e);
90 close.setText("Close");
91 close.addActionListener(new ActionListener()
93 public void actionPerformed(ActionEvent e)
95 close_actionPerformed(e);
98 textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
99 textfield.addActionListener(new ActionListener()
101 public void actionPerformed(ActionEvent e)
103 ok_actionPerformed(e);
108 this.add(jLabel1, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0
109 , GridBagConstraints.WEST,
110 GridBagConstraints.NONE,
111 new Insets(7, 4, 0, 6), 77, 6));
112 this.add(jPanel1, new GridBagConstraints(0, 2, 2, 1, 1.0, 1.0
113 , GridBagConstraints.WEST,
114 GridBagConstraints.BOTH,
115 new Insets(7, -2, 7, 12), 241, -2));
116 this.add(database, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
117 , GridBagConstraints.WEST,
118 GridBagConstraints.NONE,
119 new Insets(0, 4, 0, 0), 1, 0));
120 this.add(textfield, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
121 , GridBagConstraints.CENTER,
122 GridBagConstraints.NONE,
123 new Insets(0, 0, 0, 6), 211, 1));
126 JComboBox database = new JComboBox();
127 JLabel jLabel1 = new JLabel();
128 JButton ok = new JButton();
129 JButton close = new JButton();
130 JPanel jPanel1 = new JPanel();
131 JTextField textfield = new JTextField();
132 GridBagLayout gridBagLayout1 = new GridBagLayout();
133 public void close_actionPerformed(ActionEvent e)
137 frame.setClosed(true);
143 public void ok_actionPerformed(ActionEvent e)
145 database.setEnabled(false);
146 textfield.setEnabled(false);
147 ok.setEnabled(false);
148 close.setEnabled(false);
150 Thread worker = new Thread(this);
154 private void resetDialog()
156 database.setEnabled(true);
157 textfield.setEnabled(true);
159 close.setEnabled(true);
165 if (database.getSelectedItem().equals(noDbSelected))
166 error += "Please select the source database\n";
167 com.stevesoft.pat.Regex empty = new com.stevesoft.pat.Regex("\\s+", "");
168 textfield.setText(empty.replaceAll(textfield.getText()));
169 if (textfield.getText().length() == 0)
170 error += "Please enter a (semi-colon separated list of) database id(s)";
171 if (error.length() > 0)
173 showErrorMessage(error);
178 result = new StringBuffer();
179 if (database.getSelectedItem().equals("Uniprot"))
181 getUniprotFile(textfield.getText());
183 else if (database.getSelectedItem().equals("EMBL")
184 || database.getSelectedItem().equals("EMBLCDS"))
186 StringTokenizer st = new StringTokenizer(textfield.getText(), ";");
187 while(st.hasMoreTokens())
189 EBIFetchClient dbFetch = new EBIFetchClient();
191 String[] reply = dbFetch.fetchData(
192 database.getSelectedItem().toString().toLowerCase(
193 ) + ":" + st.nextToken(),
198 for (int i = 0; i < reply.length; i++)
199 result.append(reply[i] + "\n");
203 if(result!=null && result.length()>1) // arbitrary minimum length for a seuqence file
205 System.out.println(result.toString());
207 parseResult(result.toString(), null);
210 else if (database.getSelectedItem().equals("PDB"))
212 StringTokenizer qset = new StringTokenizer(textfield.getText(), ";");
214 while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null))
216 StringBuffer respart = getPDBFile(query.toUpperCase());
218 result.append(respart);
222 if (result.length()>0)
223 parseResult(result.toString(), null);
225 else if( database.getSelectedItem().equals("PFAM"))
228 result.append(new FastaFile(
229 "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc="
230 + textfield.getText().toUpperCase(), "URL").print()
233 if(result.length()>0)
234 parseResult( result.toString(), textfield.getText().toUpperCase() );
236 }catch(java.io.IOException ex)
240 if (result == null || result.length() == 0)
241 showErrorMessage("Error retrieving " + textfield.getText()
242 + " from " + database.getSelectedItem());
248 void getUniprotFile(String id)
250 EBIFetchClient ebi = new EBIFetchClient();
251 File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
253 DBRefFetcher dbref = new DBRefFetcher();
254 Vector entries = dbref.getUniprotEntries(file);
258 //First, make the new sequences
259 Enumeration en = entries.elements();
260 while (en.hasMoreElements())
262 UniprotEntry entry = (UniprotEntry) en.nextElement();
264 StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
265 Enumeration en2 = entry.getAccession().elements();
266 while (en2.hasMoreElements())
269 name.append(en2.nextElement());
271 en2 = entry.getName().elements();
272 while (en2.hasMoreElements())
275 name.append(en2.nextElement());
278 if (entry.getProtein() != null)
280 name.append(" " + entry.getProtein().getName().elementAt(0));
283 result.append(name + "\n" + entry.getUniprotSequence().getContent() +
288 //Then read in the features and apply them to the dataset
289 SequenceI[] sequence = parseResult(result.toString(), null);
290 for (int i = 0; i < entries.size(); i++)
292 UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
293 Enumeration e = entry.getDbReference().elements();
294 Vector onlyPdbEntries = new Vector();
295 while (e.hasMoreElements())
297 PDBEntry pdb = (PDBEntry) e.nextElement();
298 if (!pdb.getType().equals("PDB"))
301 onlyPdbEntries.addElement(pdb);
304 Enumeration en2 = entry.getAccession().elements();
305 while (en2.hasMoreElements())
307 sequence[i].getDatasetSequence().addDBRef(new DBRefEntry(DBRefSource.UNIPROT,
309 en2.nextElement().toString()));
315 sequence[i].getDatasetSequence().setPDBId(onlyPdbEntries);
316 if (entry.getFeature() != null)
318 e = entry.getFeature().elements();
319 while (e.hasMoreElements())
321 SequenceFeature sf = (SequenceFeature) e.nextElement();
322 sf.setFeatureGroup("Uniprot");
323 sequence[i].getDatasetSequence().addSequenceFeature( sf );
330 StringBuffer getPDBFile(String id)
332 StringBuffer result = new StringBuffer();
334 if (id.indexOf(":") > -1)
336 chain = id.substring(id.indexOf(":") + 1);
337 id = id.substring(0, id.indexOf(":"));
340 EBIFetchClient ebi = new EBIFetchClient();
341 String file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").getAbsolutePath();
346 PDBfile pdbfile = new PDBfile(file, jalview.io.AppletFormatAdapter.FILE);
347 for (int i = 0; i < pdbfile.chains.size(); i++)
350 ( (PDBChain) pdbfile.chains.elementAt(i)).id.
351 toUpperCase().equals(chain))
353 result.append("\n>PDB|" + id + "|" +
354 ( (PDBChain) pdbfile.chains.elementAt(i)).sequence.
358 ( (PDBChain) pdbfile.chains.elementAt(i)).sequence.
362 catch (Exception ex) // Problem parsing PDB file
364 jalview.bin.Cache.log.warn("Exception when retrieving " +
365 textfield.getText() + " from " +
366 database.getSelectedItem(), ex);
373 SequenceI[] parseResult(String result, String title)
375 String format = new IdentifyFile().Identify(result, "Paste");
376 SequenceI[] sequences = null;
378 if (FormatAdapter.isValidFormat(format))
381 try{ sequences = new FormatAdapter().readFile(result.toString(), "Paste",
383 catch(Exception ex){}
385 if (sequences != null && sequences.length > 0)
387 if (alignFrame == null)
389 AlignFrame af = new AlignFrame(new Alignment(sequences),
390 AlignFrame.DEFAULT_WIDTH,
391 AlignFrame.DEFAULT_HEIGHT
393 af.currentFileFormat = format;
395 title = "Retrieved from " + database.getSelectedItem();
396 Desktop.addInternalFrame(af,
398 AlignFrame.DEFAULT_WIDTH,
399 AlignFrame.DEFAULT_HEIGHT);
400 af.statusBar.setText("Successfully pasted alignment file");
404 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
411 for (int i = 0; i < sequences.length; i++)
413 alignFrame.viewport.alignment.addSequence(sequences[i]);
415 ////////////////////////////
416 //Dataset needs extension;
417 /////////////////////////////
418 Sequence ds = new Sequence(sequences[i].getName(),
419 AlignSeq.extractGaps("-. ",
420 sequences[i].getSequenceAsString()),
421 sequences[i].getStart(),
422 sequences[i].getEnd());
423 sequences[i].setDatasetSequence(ds);
424 alignFrame.viewport.alignment.getDataset().addSequence(ds);
426 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.
428 alignFrame.viewport.alignment.getWidth();
429 alignFrame.viewport.firePropertyChange("alignment", null,
431 getAlignment().getSequences());
435 if (database.getSelectedItem().equals("PDB"))
437 // Parse out the ids from the structured names
438 boolean errors = false;
439 for (int i = 0; i < sequences.length; i++)
441 PDBEntry entry = new PDBEntry();
442 com.stevesoft.pat.Regex idbits = new com.stevesoft.pat.Regex(
443 "PDB\\|([0-9A-z]{4})\\|(.)");
444 if (idbits.search(sequences[i].getName()))
446 String pdbid = idbits.substring(1);
447 String pdbccode = idbits.substring(2);
448 // Construct the PDBEntry
450 if (entry.getProperty() == null)
451 entry.setProperty(new Hashtable());
452 entry.getProperty().put("chains",
454 + "=" + sequences[i].getStart()
455 + "-" + sequences[i].getEnd());
456 sequences[i].getDatasetSequence().addPDBId(entry);
458 // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source
459 // JBPNote - PDB DBRefEntry should also carry the chain and mapping information
460 DBRefEntry dbentry = new DBRefEntry(jalview.datamodel.DBRefSource.PDB,"0",pdbid);
461 sequences[i].getDatasetSequence().addDBRef(dbentry);
465 // don't add an entry for this chain, but this is probably a bug
466 // that the user should know about.
467 jalview.bin.Cache.log.warn(
468 "No PDBEntry constructed for sequence " + i + " : " +
469 sequences[i].getName());
474 jalview.bin.Cache.log.warn(
475 "Query string that resulted in PDBEntry construction failure was :\n" +
476 textfield.getText());
481 showErrorMessage("Error retrieving " + textfield.getText()
482 + " from " + database.getSelectedItem());
489 void showErrorMessage(final String error)
492 javax.swing.SwingUtilities.invokeLater(new Runnable()
496 JOptionPane.showInternalMessageDialog(Desktop.desktop,
497 error, "Error Retrieving Data",
498 JOptionPane.WARNING_MESSAGE);