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 SequenceI[] seqs = null;
215 while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null))
217 SequenceI[] seqparts = getPDBFile(query.toUpperCase());
218 if (seqparts!=null) {
222 SequenceI[] newseqs = new SequenceI[seqs.length+seqparts.length];
224 for (;i<seqs.length; i++) {
225 newseqs[i] = seqs[i];
228 for (int j=0;j<seqparts.length; i++, j++)
229 newseqs[i] = seqparts[j];
232 result.append("# Success for "+query.toUpperCase()+"\n");
235 if (seqs!=null && seqs.length>0) {
236 if (parseResult(new Alignment(seqs), null, null)!=null)
237 result.append("# Successfully parsed the PDB File Queries into an Alignment");
240 else if( database.getSelectedItem().equals("PFAM"))
243 result.append(new FastaFile(
244 "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc="
245 + textfield.getText().toUpperCase(), "URL").print()
248 if(result.length()>0)
249 parseResult( result.toString(), textfield.getText().toUpperCase() );
251 }catch(java.io.IOException ex)
255 if (result == null || result.length() == 0)
256 showErrorMessage("Error retrieving " + textfield.getText()
257 + " from " + database.getSelectedItem());
263 void getUniprotFile(String id)
265 EBIFetchClient ebi = new EBIFetchClient();
266 File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
268 DBRefFetcher dbref = new DBRefFetcher();
269 Vector entries = dbref.getUniprotEntries(file);
273 //First, make the new sequences
274 Enumeration en = entries.elements();
275 while (en.hasMoreElements())
277 UniprotEntry entry = (UniprotEntry) en.nextElement();
279 StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
280 Enumeration en2 = entry.getAccession().elements();
281 while (en2.hasMoreElements())
284 name.append(en2.nextElement());
286 en2 = entry.getName().elements();
287 while (en2.hasMoreElements())
290 name.append(en2.nextElement());
293 if (entry.getProtein() != null)
295 name.append(" " + entry.getProtein().getName().elementAt(0));
298 result.append(name + "\n" + entry.getUniprotSequence().getContent() +
303 //Then read in the features and apply them to the dataset
304 Alignment al = parseResult(result.toString(), null);
305 for (int i = 0; i < entries.size(); i++)
307 UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
308 Enumeration e = entry.getDbReference().elements();
309 Vector onlyPdbEntries = new Vector();
310 while (e.hasMoreElements())
312 PDBEntry pdb = (PDBEntry) e.nextElement();
313 if (!pdb.getType().equals("PDB"))
316 onlyPdbEntries.addElement(pdb);
319 Enumeration en2 = entry.getAccession().elements();
320 while (en2.hasMoreElements())
322 al.getSequenceAt(i).getDatasetSequence().addDBRef(new DBRefEntry(DBRefSource.UNIPROT,
324 en2.nextElement().toString()));
330 al.getSequenceAt(i).getDatasetSequence().setPDBId(onlyPdbEntries);
331 if (entry.getFeature() != null)
333 e = entry.getFeature().elements();
334 while (e.hasMoreElements())
336 SequenceFeature sf = (SequenceFeature) e.nextElement();
337 sf.setFeatureGroup("Uniprot");
338 al.getSequenceAt(i).getDatasetSequence().addSequenceFeature( sf );
345 SequenceI[] getPDBFile(String id)
347 Vector result = new Vector();
349 if (id.indexOf(":") > -1)
351 chain = id.substring(id.indexOf(":") + 1);
352 id = id.substring(0, id.indexOf(":"));
355 EBIFetchClient ebi = new EBIFetchClient();
356 String file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").getAbsolutePath();
361 PDBfile pdbfile = new PDBfile(file, jalview.io.AppletFormatAdapter.FILE);
362 for (int i = 0; i < pdbfile.chains.size(); i++)
365 ( (PDBChain) pdbfile.chains.elementAt(i)).id.
366 toUpperCase().equals(chain)) {
367 PDBChain pdbchain = (PDBChain) pdbfile.chains.elementAt(i);
368 // Get the Chain's Sequence - who's dataset includes any special features added from the PDB file
369 SequenceI sq = pdbchain.sequence;
370 // Specially formatted name for the PDB chain sequences retrieved from the PDB
371 sq.setName("PDB|"+id+"|"+sq.getName());
372 // Might need to add more metadata to the PDBEntry object
375 * PDBEntry entry = new PDBEntry();
376 // Construct the PDBEntry
378 if (entry.getProperty() == null)
379 entry.setProperty(new Hashtable());
380 entry.getProperty().put("chains",
382 + "=" + sq.getStart()
383 + "-" + sq.getEnd());
384 sq.getDatasetSequence().addPDBId(entry);
387 // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source
388 // JBPNote - PDB DBRefEntry should also carry the chain and mapping information
389 DBRefEntry dbentry = new DBRefEntry(jalview.datamodel.DBRefSource.PDB,"0",id+pdbchain.id);
390 sq.addDBRef(dbentry);
391 // and add seuqence to the retrieved set
392 result.addElement(sq.deriveSequence());
396 catch (Exception ex) // Problem parsing PDB file
398 jalview.bin.Cache.log.warn("Exception when retrieving " +
399 textfield.getText() + " from " +
400 database.getSelectedItem(), ex);
403 SequenceI[] results = new SequenceI[result.size()];
404 for (int i=0,j=result.size();i<j; i++) {
405 results[i] = (SequenceI) result.elementAt(i);
406 result.setElementAt(null,i);
410 Alignment parseResult(String result, String title)
412 String format = new IdentifyFile().Identify(result, "Paste");
413 Alignment sequences = null;
414 if (FormatAdapter.isValidFormat(format))
417 try{ sequences = new FormatAdapter().readFile(result.toString(), "Paste",
419 catch(Exception ex){}
422 return parseResult(sequences, title, format);
424 showErrorMessage("Error retrieving " + textfield.getText()
425 + " from " + database.getSelectedItem());
430 Alignment parseResult(Alignment al, String title, String currentFileFormat) {
432 if (al != null && al.getHeight() > 0)
434 if (alignFrame == null)
436 AlignFrame af = new AlignFrame(al,
437 AlignFrame.DEFAULT_WIDTH,
438 AlignFrame.DEFAULT_HEIGHT);
439 if (currentFileFormat!=null)
440 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT FORMAT FOR NON-FormatAdapter Sourced Alignments?
443 title = "Retrieved from " + database.getSelectedItem();
445 Desktop.addInternalFrame(af,
447 AlignFrame.DEFAULT_WIDTH,
448 AlignFrame.DEFAULT_HEIGHT);
450 af.statusBar.setText("Successfully pasted alignment file");
454 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
461 for (int i = 0; i < al.getHeight(); i++)
463 alignFrame.viewport.alignment.addSequence(al.getSequenceAt(i)); // this also creates dataset sequence entries
465 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.
467 alignFrame.viewport.alignment.getWidth();
468 alignFrame.viewport.firePropertyChange("alignment", null,
470 getAlignment().getSequences());
476 void showErrorMessage(final String error)
479 javax.swing.SwingUtilities.invokeLater(new Runnable()
483 JOptionPane.showInternalMessageDialog(Desktop.desktop,
484 error, "Error Retrieving Data",
485 JOptionPane.WARNING_MESSAGE);