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.*;
30 import jalview.datamodel.xdb.embl.*;
31 import jalview.analysis.*;
35 public class SequenceFetcher
36 extends JPanel implements Runnable
39 AlignFrame alignFrame;
41 final String noDbSelected = "-- Select Database --";
42 public SequenceFetcher(AlignFrame af)
45 database.addItem(noDbSelected);
46 database.addItem("Uniprot");
47 database.addItem("EMBL");
48 database.addItem("EMBLCDS");
49 database.addItem("PDB");
50 database.addItem("PFAM");
61 frame = new JInternalFrame();
62 frame.setContentPane(this);
63 if (System.getProperty("os.name").startsWith("Mac"))
65 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 140);
69 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 125);
73 private String getFrameTitle()
75 return ( (alignFrame == null) ? "New " : "Additional ") +
82 this.setLayout(gridBagLayout1);
84 database.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
85 database.setMinimumSize(new Dimension(160, 21));
86 database.setPreferredSize(new Dimension(160, 21));
87 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
89 "Separate multiple accession ids with semi colon \";\"");
91 ok.addActionListener(new ActionListener()
93 public void actionPerformed(ActionEvent e)
95 ok_actionPerformed(e);
98 close.setText("Close");
99 close.addActionListener(new ActionListener()
101 public void actionPerformed(ActionEvent e)
103 close_actionPerformed(e);
106 textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
107 textfield.addActionListener(new ActionListener()
109 public void actionPerformed(ActionEvent e)
111 ok_actionPerformed(e);
116 this.add(jLabel1, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0
117 , GridBagConstraints.WEST,
118 GridBagConstraints.NONE,
119 new Insets(7, 4, 0, 6), 77, 6));
120 this.add(jPanel1, new GridBagConstraints(0, 2, 2, 1, 1.0, 1.0
121 , GridBagConstraints.WEST,
122 GridBagConstraints.BOTH,
123 new Insets(7, -2, 7, 12), 241, -2));
124 this.add(database, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
125 , GridBagConstraints.WEST,
126 GridBagConstraints.NONE,
127 new Insets(0, 4, 0, 0), 1, 0));
128 this.add(textfield, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
129 , GridBagConstraints.CENTER,
130 GridBagConstraints.NONE,
131 new Insets(0, 0, 0, 6), 211, 1));
134 JComboBox database = new JComboBox();
135 JLabel jLabel1 = new JLabel();
136 JButton ok = new JButton();
137 JButton close = new JButton();
138 JPanel jPanel1 = new JPanel();
139 JTextField textfield = new JTextField();
140 GridBagLayout gridBagLayout1 = new GridBagLayout();
141 public void close_actionPerformed(ActionEvent e)
145 frame.setClosed(true);
151 public void ok_actionPerformed(ActionEvent e)
153 database.setEnabled(false);
154 textfield.setEnabled(false);
155 ok.setEnabled(false);
156 close.setEnabled(false);
158 Thread worker = new Thread(this);
162 private void resetDialog()
164 database.setEnabled(true);
165 textfield.setEnabled(true);
167 close.setEnabled(true);
173 if (database.getSelectedItem().equals(noDbSelected))
175 error += "Please select the source database\n";
177 com.stevesoft.pat.Regex empty = new com.stevesoft.pat.Regex("\\s+", "");
178 textfield.setText(empty.replaceAll(textfield.getText()));
179 if (textfield.getText().length() == 0)
181 error += "Please enter a (semi-colon separated list of) database id(s)";
183 if (error.length() > 0)
185 showErrorMessage(error);
190 result = new StringBuffer();
191 if (database.getSelectedItem().equals("Uniprot"))
193 getUniprotFile(textfield.getText());
195 else if (database.getSelectedItem().equals("EMBL")
196 || database.getSelectedItem().equals("EMBLCDS"))
198 String DBRefSource = database.getSelectedItem().equals("EMBLCDS")
199 ? jalview.datamodel.DBRefSource.EMBLCDS
200 : jalview.datamodel.DBRefSource.EMBL;
202 StringTokenizer st = new StringTokenizer(textfield.getText(), ";");
203 SequenceI[] seqs = null;
204 while(st.hasMoreTokens())
206 EBIFetchClient dbFetch = new EBIFetchClient();
208 File reply = dbFetch.fetchDataAsFile(
209 database.getSelectedItem().toString().toLowerCase(
210 ) + ":" + st.nextToken(),
213 jalview.datamodel.xdb.embl.EmblFile efile=null;
214 if (reply != null && reply.exists())
216 efile = jalview.datamodel.xdb.embl.EmblFile.getEmblFile(reply);
219 for (Iterator i=efile.getEntries().iterator(); i.hasNext(); ) {
220 EmblEntry entry = (EmblEntry) i.next();
221 SequenceI[] seqparts = entry.getSequences(false,true, DBRefSource);
222 if (seqparts!=null) {
223 SequenceI[] newseqs = null;
226 newseqs = new SequenceI[seqparts.length];
228 newseqs = new SequenceI[seqs.length+seqparts.length];
230 for (;si<seqs.length; si++) {
231 newseqs[si] = seqs[si];
235 for (int j=0;j<seqparts.length; si++, j++) {
236 newseqs[si] = seqparts[j].deriveSequence(); // place DBReferences on dataset and refer
246 if (seqs!=null && seqs.length>0) {
247 if (parseResult(new Alignment(seqs), null, null)!=null)
248 result.append("# Successfully parsed the "+database.getSelectedItem()+" Queries into an Alignment");
251 else if (database.getSelectedItem().equals("PDB"))
253 StringTokenizer qset = new StringTokenizer(textfield.getText(), ";");
255 SequenceI[] seqs = null;
256 while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null))
258 SequenceI[] seqparts = getPDBFile(query.toUpperCase());
259 if (seqparts != null)
267 SequenceI[] newseqs = new SequenceI[seqs.length+seqparts.length];
269 for (; i < seqs.length; i++)
271 newseqs[i] = seqs[i];
274 for (int j=0;j<seqparts.length; i++, j++)
276 newseqs[i] = seqparts[j];
280 result.append("# Success for "+query.toUpperCase()+"\n");
283 if (seqs != null && seqs.length > 0)
285 if (parseResult(new Alignment(seqs), null, null)!=null)
288 "# Successfully parsed the PDB File Queries into an Alignment");
292 else if( database.getSelectedItem().equals("PFAM"))
296 result.append(new FastaFile(
297 "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc="
298 + textfield.getText().toUpperCase(), "URL").print()
301 if(result.length()>0)
303 parseResult( result.toString(), textfield.getText().toUpperCase() );
307 catch (java.io.IOException ex)
313 if (result == null || result.length() == 0)
315 showErrorMessage("Error retrieving " + textfield.getText()
316 + " from " + database.getSelectedItem());
323 void getUniprotFile(String id)
325 EBIFetchClient ebi = new EBIFetchClient();
326 File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
328 DBRefFetcher dbref = new DBRefFetcher();
329 Vector entries = dbref.getUniprotEntries(file);
333 //First, make the new sequences
334 Enumeration en = entries.elements();
335 while (en.hasMoreElements())
337 UniprotEntry entry = (UniprotEntry) en.nextElement();
339 StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
340 Enumeration en2 = entry.getAccession().elements();
341 while (en2.hasMoreElements())
344 name.append(en2.nextElement());
346 en2 = entry.getName().elements();
347 while (en2.hasMoreElements())
350 name.append(en2.nextElement());
353 if (entry.getProtein() != null)
355 name.append(" " + entry.getProtein().getName().elementAt(0));
358 result.append(name + "\n" + entry.getUniprotSequence().getContent() +
363 //Then read in the features and apply them to the dataset
364 Alignment al = parseResult(result.toString(), null);
365 for (int i = 0; i < entries.size(); i++)
367 UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
368 Enumeration e = entry.getDbReference().elements();
369 Vector onlyPdbEntries = new Vector();
370 while (e.hasMoreElements())
372 PDBEntry pdb = (PDBEntry) e.nextElement();
373 if (!pdb.getType().equals("PDB"))
378 onlyPdbEntries.addElement(pdb);
381 Enumeration en2 = entry.getAccession().elements();
382 while (en2.hasMoreElements())
384 al.getSequenceAt(i).getDatasetSequence().addDBRef(new DBRefEntry(
387 en2.nextElement().toString()));
393 al.getSequenceAt(i).getDatasetSequence().setPDBId(onlyPdbEntries);
394 if (entry.getFeature() != null)
396 e = entry.getFeature().elements();
397 while (e.hasMoreElements())
399 SequenceFeature sf = (SequenceFeature) e.nextElement();
400 sf.setFeatureGroup("Uniprot");
401 al.getSequenceAt(i).getDatasetSequence().addSequenceFeature( sf );
408 SequenceI[] getPDBFile(String id)
410 Vector result = new Vector();
412 if (id.indexOf(":") > -1)
414 chain = id.substring(id.indexOf(":") + 1);
415 id = id.substring(0, id.indexOf(":"));
418 EBIFetchClient ebi = new EBIFetchClient();
419 String file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").
427 PDBfile pdbfile = new PDBfile(file, jalview.io.AppletFormatAdapter.FILE);
428 for (int i = 0; i < pdbfile.chains.size(); i++)
431 ( (PDBChain) pdbfile.chains.elementAt(i)).id.
432 toUpperCase().equals(chain))
434 PDBChain pdbchain = (PDBChain) pdbfile.chains.elementAt(i);
435 // Get the Chain's Sequence - who's dataset includes any special features added from the PDB file
436 SequenceI sq = pdbchain.sequence;
437 // Specially formatted name for the PDB chain sequences retrieved from the PDB
438 sq.setName("PDB|"+id+"|"+sq.getName());
439 // Might need to add more metadata to the PDBEntry object
442 * PDBEntry entry = new PDBEntry();
443 // Construct the PDBEntry
445 if (entry.getProperty() == null)
446 entry.setProperty(new Hashtable());
447 entry.getProperty().put("chains",
449 + "=" + sq.getStart()
450 + "-" + sq.getEnd());
451 sq.getDatasetSequence().addPDBId(entry);
454 // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source
455 // JBPNote - PDB DBRefEntry should also carry the chain and mapping information
456 DBRefEntry dbentry = new DBRefEntry(jalview.datamodel.DBRefSource.PDB,
457 "0", id + pdbchain.id);
458 sq.addDBRef(dbentry);
459 // and add seuqence to the retrieved set
460 result.addElement(sq.deriveSequence());
464 catch (Exception ex) // Problem parsing PDB file
466 jalview.bin.Cache.log.warn("Exception when retrieving " +
467 textfield.getText() + " from " +
468 database.getSelectedItem(), ex);
471 SequenceI[] results = new SequenceI[result.size()];
472 for (int i = 0, j = result.size(); i < j; i++)
474 results[i] = (SequenceI) result.elementAt(i);
475 result.setElementAt(null,i);
479 Alignment parseResult(String result, String title)
481 String format = new IdentifyFile().Identify(result, "Paste");
482 Alignment sequences = null;
483 if (FormatAdapter.isValidFormat(format))
488 sequences = new FormatAdapter().readFile(result.toString(), "Paste",
496 return parseResult(sequences, title, format);
501 showErrorMessage("Error retrieving " + textfield.getText()
502 + " from " + database.getSelectedItem());
508 Alignment parseResult(Alignment al, String title, String currentFileFormat)
511 if (al != null && al.getHeight() > 0)
513 if (alignFrame == null)
515 AlignFrame af = new AlignFrame(al,
516 AlignFrame.DEFAULT_WIDTH,
517 AlignFrame.DEFAULT_HEIGHT);
518 if (currentFileFormat!=null)
520 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT FORMAT FOR NON-FormatAdapter Sourced Alignments?
525 title = "Retrieved from " + database.getSelectedItem();
528 Desktop.addInternalFrame(af,
530 AlignFrame.DEFAULT_WIDTH,
531 AlignFrame.DEFAULT_HEIGHT);
533 af.statusBar.setText("Successfully pasted alignment file");
537 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
544 for (int i = 0; i < al.getHeight(); i++)
546 alignFrame.viewport.alignment.addSequence(al.getSequenceAt(i)); // this also creates dataset sequence entries
548 alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.
550 alignFrame.viewport.alignment.getWidth();
551 alignFrame.viewport.firePropertyChange("alignment", null,
553 getAlignment().getSequences());
559 void showErrorMessage(final String error)
562 javax.swing.SwingUtilities.invokeLater(new Runnable()
566 JOptionPane.showInternalMessageDialog(Desktop.desktop,
567 error, "Error Retrieving Data",
568 JOptionPane.WARNING_MESSAGE);