2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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 sequence[i].getDatasetSequence().setPDBId(onlyPdbEntries);
\r
305 if (entry.getFeature() != null)
\r
307 e = entry.getFeature().elements();
\r
308 while (e.hasMoreElements())
\r
310 SequenceFeature sf = (SequenceFeature) e.nextElement();
\r
311 sf.setFeatureGroup("Uniprot");
\r
312 sequence[i].getDatasetSequence().addSequenceFeature( sf );
\r
319 StringBuffer getPDBFile(String id)
\r
321 StringBuffer result = new StringBuffer();
\r
322 String chain = null;
\r
323 if (id.indexOf(":") > -1)
\r
325 chain = id.substring(id.indexOf(":") + 1);
\r
326 id = id.substring(0, id.indexOf(":"));
\r
329 EBIFetchClient ebi = new EBIFetchClient();
\r
330 String[] reply = ebi.fetchData("pdb:" + id, "pdb", "raw");
\r
335 PDBfile pdbfile = new PDBfile(reply);
\r
336 for (int i = 0; i < pdbfile.chains.size(); i++)
\r
338 if (chain == null ||
\r
339 ( (PDBChain) pdbfile.chains.elementAt(i)).id.
\r
340 toUpperCase().equals(chain))
\r
341 result.append("\n>PDB|" + id + "|" +
\r
342 ( (PDBChain) pdbfile.chains.elementAt(i)).sequence.
\r
346 ( (PDBChain) pdbfile.chains.elementAt(i)).sequence.
\r
350 catch (Exception ex) // Problem parsing PDB file
\r
352 jalview.bin.Cache.log.warn("Exception when retrieving " +
\r
353 textfield.getText() + " from " +
\r
354 database.getSelectedItem(), ex);
\r
361 SequenceI[] parseResult(String result, String title)
\r
363 String format = new IdentifyFile().Identify(result, "Paste");
\r
364 SequenceI[] sequences = null;
\r
366 if (FormatAdapter.formats.contains(format))
\r
369 try{ sequences = new FormatAdapter().readFile(result.toString(), "Paste",
\r
371 catch(Exception ex){}
\r
373 if (sequences != null && sequences.length > 0)
\r
375 if (alignFrame == null)
\r
377 AlignFrame af = new AlignFrame(new Alignment(sequences));
\r
378 af.currentFileFormat = format;
\r
380 title = "Retrieved from " + database.getSelectedItem();
\r
381 Desktop.addInternalFrame(af,
\r
383 AlignFrame.NEW_WINDOW_WIDTH,
\r
384 AlignFrame.NEW_WINDOW_HEIGHT);
\r
385 af.statusBar.setText("Successfully pasted alignment file");
\r
389 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
\r
391 catch (Exception ex)
\r
396 for (int i = 0; i < sequences.length; i++)
\r
398 alignFrame.viewport.alignment.addSequence(sequences[i]);
\r
400 ////////////////////////////
\r
401 //Dataset needs extension;
\r
402 /////////////////////////////
\r
403 Sequence ds = new Sequence(sequences[i].getName(),
\r
404 AlignSeq.extractGaps("-. ",
\r
405 sequences[i].getSequence()),
\r
406 sequences[i].getStart(),
\r
407 sequences[i].getEnd());
\r
408 sequences[i].setDatasetSequence(ds);
\r
409 alignFrame.viewport.alignment.getDataset().addSequence(ds);
\r
411 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.
\r
413 alignFrame.viewport.alignment.getWidth();
\r
414 alignFrame.viewport.firePropertyChange("alignment", null,
\r
415 alignFrame.viewport.
\r
416 getAlignment().getSequences());
\r
420 if (database.getSelectedItem().equals("PDB"))
\r
422 // Parse out the ids from the structured names
\r
423 boolean errors = false;
\r
424 for (int i = 0; i < sequences.length; i++)
\r
426 PDBEntry entry = new PDBEntry();
\r
427 com.stevesoft.pat.Regex idbits = new com.stevesoft.pat.Regex(
\r
428 "PDB\\|([0-9A-z]{4})\\|(.)");
\r
429 if (idbits.search(sequences[i].getName()))
\r
431 String pdbid = idbits.substring(1);
\r
432 String pdbccode = idbits.substring(2);
\r
433 // Construct the PDBEntry
\r
434 entry.setId(pdbid);
\r
435 if (entry.getProperty() == null)
\r
436 entry.setProperty(new Hashtable());
\r
437 entry.getProperty().put("chains",
\r
439 + "=" + sequences[i].getStart()
\r
440 + "-" + sequences[i].getEnd());
\r
441 sequences[i].getDatasetSequence().addPDBId(entry);
\r
443 // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source
\r
444 // JBPNote - PDB DBRefEntry should also carry the chain and mapping information
\r
445 DBRefEntry dbentry = new DBRefEntry(jalview.datamodel.DBRefSource.PDB,"0",pdbid);
\r
446 sequences[i].getDatasetSequence().addDBRef(dbentry);
\r
450 // don't add an entry for this chain, but this is probably a bug
\r
451 // that the user should know about.
\r
452 jalview.bin.Cache.log.warn(
\r
453 "No PDBEntry constructed for sequence " + i + " : " +
\r
454 sequences[i].getName());
\r
459 jalview.bin.Cache.log.warn(
\r
460 "Query string that resulted in PDBEntry construction failure was :\n" +
\r
461 textfield.getText());
\r
466 showErrorMessage("Error retrieving " + textfield.getText()
\r
467 + " from " + database.getSelectedItem());
\r
474 void showErrorMessage(final String error)
\r
477 javax.swing.SwingUtilities.invokeLater(new Runnable()
\r
481 JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
482 error, "Error Retrieving Data",
\r
483 JOptionPane.WARNING_MESSAGE);
\r