b4370b7f73f4001318e11eb4d1fb132dc727ad12
[jalview.git] / src / jalview / gui / SequenceFetcher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
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.
9  *
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.
14  *
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
18  */
19 package jalview.gui;
20
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import jalview.io.EBIFetchClient;
25 import MCview.*;
26 import jalview.datamodel.*;
27 import jalview.analysis.AlignSeq;
28 import java.io.File;
29 import jalview.io.*;
30 import java.util.*;
31
32 public class SequenceFetcher
33     extends JPanel implements Runnable
34 {
35   JInternalFrame frame;
36   AlignFrame alignFrame;
37   StringBuffer result;
38   final String noDbSelected = "-- Select Database --";
39   public SequenceFetcher(AlignFrame af)
40   {
41     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");
48
49     try
50     {
51       jbInit();
52     }
53     catch (Exception ex)
54     {
55       ex.printStackTrace();
56     }
57
58     frame = new JInternalFrame();
59     frame.setContentPane(this);
60     if (System.getProperty("os.name").startsWith("Mac"))
61       Desktop.addInternalFrame(frame, getFrameTitle(), 400, 140);
62     else
63       Desktop.addInternalFrame(frame, getFrameTitle(), 400, 125);
64   }
65
66   private String getFrameTitle()
67   {
68     return ( (alignFrame == null) ? "New " : "Additional ") + "Sequence Fetcher";
69   }
70
71   private void jbInit()
72       throws Exception
73   {
74     this.setLayout(gridBagLayout1);
75
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));
80     jLabel1.setText(
81         "Separate multiple accession ids with semi colon \";\"");
82     ok.setText("OK");
83     ok.addActionListener(new ActionListener()
84     {
85       public void actionPerformed(ActionEvent e)
86       {
87         ok_actionPerformed(e);
88       }
89     });
90     close.setText("Close");
91     close.addActionListener(new ActionListener()
92     {
93       public void actionPerformed(ActionEvent e)
94       {
95         close_actionPerformed(e);
96       }
97     });
98     textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
99     textfield.addActionListener(new ActionListener()
100     {
101       public void actionPerformed(ActionEvent e)
102       {
103         ok_actionPerformed(e);
104       }
105     });
106     jPanel1.add(ok);
107     jPanel1.add(close);
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));
124   }
125
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)
134   {
135     try
136     {
137       frame.setClosed(true);
138     }
139     catch (Exception ex)
140     {}
141   }
142
143   public void ok_actionPerformed(ActionEvent e)
144   {
145     database.setEnabled(false);
146     textfield.setEnabled(false);
147     ok.setEnabled(false);
148     close.setEnabled(false);
149
150     Thread worker = new Thread(this);
151     worker.start();
152   }
153
154   private void resetDialog()
155   {
156     database.setEnabled(true);
157     textfield.setEnabled(true);
158     ok.setEnabled(true);
159     close.setEnabled(true);
160   }
161
162   public void run()
163   {
164     String error = "";
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)
172     {
173       showErrorMessage(error);
174       resetDialog();
175       return;
176     }
177
178     result = new StringBuffer();
179     if (database.getSelectedItem().equals("Uniprot"))
180     {
181       getUniprotFile(textfield.getText());
182     }
183     else if (database.getSelectedItem().equals("EMBL")
184              || database.getSelectedItem().equals("EMBLCDS"))
185     {
186       StringTokenizer st = new StringTokenizer(textfield.getText(), ";");
187       while(st.hasMoreTokens())
188       {
189         EBIFetchClient dbFetch = new EBIFetchClient();
190
191         String[] reply = dbFetch.fetchData(
192             database.getSelectedItem().toString().toLowerCase(
193             ) + ":" + st.nextToken(),
194             "fasta", "raw");
195 //
196         if (reply != null)
197         {
198           for (int i = 0; i < reply.length; i++)
199             result.append(reply[i] + "\n");
200         }
201       }
202
203       if(result!=null && result.length()>1) // arbitrary minimum length for a seuqence file
204       {
205         System.out.println(result.toString());
206
207         parseResult(result.toString(), null);
208       }
209     }
210     else if (database.getSelectedItem().equals("PDB"))
211     {
212       StringTokenizer qset = new StringTokenizer(textfield.getText(), ";");
213       String query;
214       SequenceI[] seqs = null;
215       while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null))
216       {
217         SequenceI[] seqparts = getPDBFile(query.toUpperCase());
218           if (seqparts!=null) {
219               if (seqs==null) {
220                   seqs = seqparts;
221               } else {
222                   SequenceI[] newseqs = new SequenceI[seqs.length+seqparts.length];
223                   int i=0;
224                   for (;i<seqs.length; i++) {
225                       newseqs[i] = seqs[i];
226                       seqs[i] = null;
227                   }
228                   for (int j=0;j<seqparts.length; i++, j++)
229                       newseqs[i] = seqparts[j];
230                   seqs=newseqs;
231               }
232               result.append("# Success for "+query.toUpperCase()+"\n");
233           }
234       }
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");
238       }
239     }
240     else if( database.getSelectedItem().equals("PFAM"))
241     {
242       try{
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()
246            );
247
248          if(result.length()>0)
249            parseResult( result.toString(), textfield.getText().toUpperCase() );
250
251       }catch(java.io.IOException ex)
252       {   result = null;    }
253     }
254
255     if (result == null || result.length() == 0)
256       showErrorMessage("Error retrieving " + textfield.getText()
257                        + " from " + database.getSelectedItem());
258
259     resetDialog();
260     return;
261   }
262
263   void getUniprotFile(String id)
264   {
265     EBIFetchClient ebi = new EBIFetchClient();
266     File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
267
268     DBRefFetcher dbref = new DBRefFetcher();
269     Vector entries = dbref.getUniprotEntries(file);
270
271     if (entries != null)
272     {
273       //First, make the new sequences
274       Enumeration en = entries.elements();
275       while (en.hasMoreElements())
276       {
277         UniprotEntry entry = (UniprotEntry) en.nextElement();
278
279         StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
280         Enumeration en2 = entry.getAccession().elements();
281         while (en2.hasMoreElements())
282         {
283           name.append("|");
284           name.append(en2.nextElement());
285         }
286         en2 = entry.getName().elements();
287         while (en2.hasMoreElements())
288         {
289           name.append("|");
290           name.append(en2.nextElement());
291         }
292
293         if (entry.getProtein() != null)
294         {
295            name.append(" " + entry.getProtein().getName().elementAt(0));
296         }
297
298         result.append(name + "\n" + entry.getUniprotSequence().getContent() +
299                       "\n");
300
301       }
302
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++)
306       {
307         UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
308         Enumeration e = entry.getDbReference().elements();
309         Vector onlyPdbEntries = new Vector();
310         while (e.hasMoreElements())
311         {
312           PDBEntry pdb = (PDBEntry) e.nextElement();
313           if (!pdb.getType().equals("PDB"))
314             continue;
315
316           onlyPdbEntries.addElement(pdb);
317         }
318
319         Enumeration en2 = entry.getAccession().elements();
320         while (en2.hasMoreElements())
321         {
322           al.getSequenceAt(i).getDatasetSequence().addDBRef(new DBRefEntry(DBRefSource.UNIPROT,
323                     "0",
324                     en2.nextElement().toString()));
325         }
326
327
328
329
330         al.getSequenceAt(i).getDatasetSequence().setPDBId(onlyPdbEntries);
331         if (entry.getFeature() != null)
332         {
333           e = entry.getFeature().elements();
334           while (e.hasMoreElements())
335           {
336             SequenceFeature sf = (SequenceFeature) e.nextElement();
337             sf.setFeatureGroup("Uniprot");
338             al.getSequenceAt(i).getDatasetSequence().addSequenceFeature( sf );
339           }
340         }
341       }
342     }
343   }
344
345   SequenceI[] getPDBFile(String id)
346   {
347     Vector result = new Vector();
348     String chain = null;
349     if (id.indexOf(":") > -1)
350     {
351       chain = id.substring(id.indexOf(":") + 1);
352       id = id.substring(0, id.indexOf(":"));
353     }
354
355     EBIFetchClient ebi = new EBIFetchClient();
356     String file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").getAbsolutePath();
357     if (file == null)
358       return null;
359     try
360     {
361       PDBfile pdbfile = new PDBfile(file, jalview.io.AppletFormatAdapter.FILE);
362       for (int i = 0; i < pdbfile.chains.size(); i++)
363       {
364         if (chain == null ||
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
373             // like below
374             /*
375              * PDBEntry entry = new PDBEntry();
376             // Construct the PDBEntry
377             entry.setId(id);
378             if (entry.getProperty() == null)
379                 entry.setProperty(new Hashtable());
380             entry.getProperty().put("chains",
381                         pdbchain.id
382                         + "=" + sq.getStart()
383                         + "-" + sq.getEnd()); 
384             sq.getDatasetSequence().addPDBId(entry);
385             */
386             // Add PDB DB Refs
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());
393         }
394       }
395     }
396     catch (Exception ex) // Problem parsing PDB file
397     {
398       jalview.bin.Cache.log.warn("Exception when retrieving " +
399                                  textfield.getText() + " from " +
400                                  database.getSelectedItem(), ex);
401       return null;
402     }
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);
407     }
408     return results;
409   }
410   Alignment parseResult(String result, String title)
411   {
412       String format = new IdentifyFile().Identify(result, "Paste");
413       Alignment sequences = null;
414       if (FormatAdapter.isValidFormat(format))
415       {
416         sequences = null;
417         try{ sequences = new FormatAdapter().readFile(result.toString(), "Paste",
418                                                  format);}
419         catch(Exception ex){}
420
421         if (sequences!=null)
422             return parseResult(sequences, title, format);
423       } else {
424               showErrorMessage("Error retrieving " + textfield.getText()
425                       + " from " + database.getSelectedItem());
426       }
427
428       return null;
429   }
430   Alignment parseResult(Alignment al, String title, String currentFileFormat) {
431     
432       if (al != null && al.getHeight() > 0)
433       {
434         if (alignFrame == null)
435         {
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?
441           
442           if(title==null)
443             title = "Retrieved from " + database.getSelectedItem();
444           
445           Desktop.addInternalFrame(af,
446                                    title,
447                                    AlignFrame.DEFAULT_WIDTH,
448                                    AlignFrame.DEFAULT_HEIGHT);
449           
450           af.statusBar.setText("Successfully pasted alignment file");
451
452           try
453           {
454             af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
455           }
456           catch (Exception ex)
457           {}
458         }
459         else
460         {
461           for (int i = 0; i < al.getHeight(); i++)
462           {
463             alignFrame.viewport.alignment.addSequence(al.getSequenceAt(i)); // this also creates dataset sequence entries
464           }
465           alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.
466                                         getHeight());
467           alignFrame.viewport.alignment.getWidth();
468           alignFrame.viewport.firePropertyChange("alignment", null,
469                                                  alignFrame.viewport.
470                                                  getAlignment().getSequences());
471         }
472       }
473     return al;
474   }
475
476   void showErrorMessage(final String error)
477   {
478     resetDialog();
479     javax.swing.SwingUtilities.invokeLater(new Runnable()
480     {
481       public void run()
482       {
483         JOptionPane.showInternalMessageDialog(Desktop.desktop,
484                                               error, "Error Retrieving Data",
485                                           JOptionPane.WARNING_MESSAGE);
486       }
487     });
488   }
489 }
490