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