ensure lastSeq is refreshed if new feature added
[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       while (qset.hasMoreTokens() && ((query = qset.nextToken())!=null))
215       {
216         StringBuffer respart = getPDBFile(query.toUpperCase());
217         if(respart!=null)
218           result.append(respart);
219       }
220
221
222       if (result.length()>0)
223         parseResult(result.toString(), null);
224     }
225     else if( database.getSelectedItem().equals("PFAM"))
226     {
227       try{
228         result.append(new FastaFile(
229            "http://www.sanger.ac.uk/cgi-bin/Pfam/getalignment.pl?format=fal&acc="
230            +  textfield.getText().toUpperCase(), "URL").print()
231            );
232
233          if(result.length()>0)
234            parseResult( result.toString(), textfield.getText().toUpperCase() );
235
236       }catch(java.io.IOException ex)
237       {   result = null;    }
238     }
239
240     if (result == null || result.length() == 0)
241       showErrorMessage("Error retrieving " + textfield.getText()
242                        + " from " + database.getSelectedItem());
243
244     resetDialog();
245     return;
246   }
247
248   void getUniprotFile(String id)
249   {
250     EBIFetchClient ebi = new EBIFetchClient();
251     File file = ebi.fetchDataAsFile("uniprot:" + id, "xml", null);
252
253     DBRefFetcher dbref = new DBRefFetcher();
254     Vector entries = dbref.getUniprotEntries(file);
255
256     if (entries != null)
257     {
258       //First, make the new sequences
259       Enumeration en = entries.elements();
260       while (en.hasMoreElements())
261       {
262         UniprotEntry entry = (UniprotEntry) en.nextElement();
263
264         StringBuffer name = new StringBuffer(">UniProt/Swiss-Prot");
265         Enumeration en2 = entry.getAccession().elements();
266         while (en2.hasMoreElements())
267         {
268           name.append("|");
269           name.append(en2.nextElement());
270         }
271         en2 = entry.getName().elements();
272         while (en2.hasMoreElements())
273         {
274           name.append("|");
275           name.append(en2.nextElement());
276         }
277
278         if (entry.getProtein() != null)
279         {
280            name.append(" " + entry.getProtein().getName().elementAt(0));
281         }
282
283         result.append(name + "\n" + entry.getUniprotSequence().getContent() +
284                       "\n");
285
286       }
287
288       //Then read in the features and apply them to the dataset
289       SequenceI[] sequence = parseResult(result.toString(), null);
290       for (int i = 0; i < entries.size(); i++)
291       {
292         UniprotEntry entry = (UniprotEntry) entries.elementAt(i);
293         Enumeration e = entry.getDbReference().elements();
294         Vector onlyPdbEntries = new Vector();
295         while (e.hasMoreElements())
296         {
297           PDBEntry pdb = (PDBEntry) e.nextElement();
298           if (!pdb.getType().equals("PDB"))
299             continue;
300
301           onlyPdbEntries.addElement(pdb);
302         }
303
304         Enumeration en2 = entry.getAccession().elements();
305         while (en2.hasMoreElements())
306         {
307           sequence[i].getDatasetSequence().addDBRef(new DBRefEntry(DBRefSource.UNIPROT,
308                     "0",
309                     en2.nextElement().toString()));
310         }
311
312
313
314
315         sequence[i].getDatasetSequence().setPDBId(onlyPdbEntries);
316         if (entry.getFeature() != null)
317         {
318           e = entry.getFeature().elements();
319           while (e.hasMoreElements())
320           {
321             SequenceFeature sf = (SequenceFeature) e.nextElement();
322             sf.setFeatureGroup("Uniprot");
323             sequence[i].getDatasetSequence().addSequenceFeature( sf );
324           }
325         }
326       }
327     }
328   }
329
330   StringBuffer getPDBFile(String id)
331   {
332     StringBuffer result = new StringBuffer();
333     String chain = null;
334     if (id.indexOf(":") > -1)
335     {
336       chain = id.substring(id.indexOf(":") + 1);
337       id = id.substring(0, id.indexOf(":"));
338     }
339
340     EBIFetchClient ebi = new EBIFetchClient();
341     String file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").getAbsolutePath();
342     if (file == null)
343       return null;
344     try
345     {
346       PDBfile pdbfile = new PDBfile(file, jalview.io.AppletFormatAdapter.FILE);
347       for (int i = 0; i < pdbfile.chains.size(); i++)
348       {
349         if (chain == null ||
350             ( (PDBChain) pdbfile.chains.elementAt(i)).id.
351             toUpperCase().equals(chain))
352
353           result.append("\n>PDB|" + id + "|" +
354                         ( (PDBChain) pdbfile.chains.elementAt(i)).sequence.
355                         getName() +
356                         "\n"
357                         +
358                         ( (PDBChain) pdbfile.chains.elementAt(i)).sequence.
359                         getSequence());
360       }
361     }
362     catch (Exception ex) // Problem parsing PDB file
363     {
364       jalview.bin.Cache.log.warn("Exception when retrieving " +
365                                  textfield.getText() + " from " +
366                                  database.getSelectedItem(), ex);
367       return null;
368     }
369
370     return result;
371   }
372
373   SequenceI[] parseResult(String result, String title)
374   {
375     String format = new IdentifyFile().Identify(result, "Paste");
376     SequenceI[] sequences = null;
377
378     if (FormatAdapter.isValidFormat(format))
379     {
380       sequences = null;
381       try{ sequences = new FormatAdapter().readFile(result.toString(), "Paste",
382                                                format);}
383       catch(Exception ex){}
384
385       if (sequences != null && sequences.length > 0)
386       {
387         if (alignFrame == null)
388         {
389           AlignFrame af = new AlignFrame(new Alignment(sequences),
390                                            AlignFrame.DEFAULT_WIDTH,
391                                            AlignFrame.DEFAULT_HEIGHT
392 );
393           af.currentFileFormat = format;
394           if(title==null)
395             title = "Retrieved from " + database.getSelectedItem();
396           Desktop.addInternalFrame(af,
397                                    title,
398                                    AlignFrame.DEFAULT_WIDTH,
399                                    AlignFrame.DEFAULT_HEIGHT);
400           af.statusBar.setText("Successfully pasted alignment file");
401
402           try
403           {
404             af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
405           }
406           catch (Exception ex)
407           {}
408         }
409         else
410         {
411           for (int i = 0; i < sequences.length; i++)
412           {
413             alignFrame.viewport.alignment.addSequence(sequences[i]);
414
415             ////////////////////////////
416             //Dataset needs extension;
417             /////////////////////////////
418             Sequence ds = new Sequence(sequences[i].getName(),
419                                        AlignSeq.extractGaps("-. ",
420                 sequences[i].getSequenceAsString()),
421                                        sequences[i].getStart(),
422                                        sequences[i].getEnd());
423             sequences[i].setDatasetSequence(ds);
424             alignFrame.viewport.alignment.getDataset().addSequence(ds);
425           }
426           alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.
427                                         getHeight());
428           alignFrame.viewport.alignment.getWidth();
429           alignFrame.viewport.firePropertyChange("alignment", null,
430                                                  alignFrame.viewport.
431                                                  getAlignment().getSequences());
432
433         }
434
435         if (database.getSelectedItem().equals("PDB"))
436         {
437           // Parse out the ids from the structured names
438           boolean errors = false;
439           for (int i = 0; i < sequences.length; i++)
440           {
441             PDBEntry entry = new PDBEntry();
442             com.stevesoft.pat.Regex idbits = new com.stevesoft.pat.Regex(
443                 "PDB\\|([0-9A-z]{4})\\|(.)");
444             if (idbits.search(sequences[i].getName()))
445             {
446               String pdbid = idbits.substring(1);
447               String pdbccode = idbits.substring(2);
448               // Construct the PDBEntry
449               entry.setId(pdbid);
450               if (entry.getProperty() == null)
451                 entry.setProperty(new Hashtable());
452               entry.getProperty().put("chains",
453                                       pdbccode
454                                       + "=" + sequences[i].getStart()
455                                       + "-" + sequences[i].getEnd());
456               sequences[i].getDatasetSequence().addPDBId(entry);
457
458               // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source
459               // JBPNote - PDB DBRefEntry should also carry the chain and mapping information
460               DBRefEntry dbentry = new DBRefEntry(jalview.datamodel.DBRefSource.PDB,"0",pdbid);
461               sequences[i].getDatasetSequence().addDBRef(dbentry);
462             }
463             else
464             {
465               // don't add an entry for this chain, but this is probably a bug
466               // that the user should know about.
467               jalview.bin.Cache.log.warn(
468                   "No PDBEntry constructed for sequence " + i + " : " +
469                   sequences[i].getName());
470               errors = true;
471             }
472           }
473           if (errors)
474             jalview.bin.Cache.log.warn(
475                 "Query string that resulted in PDBEntry construction failure was :\n" +
476                 textfield.getText());
477         }
478
479       }
480       else
481         showErrorMessage("Error retrieving " + textfield.getText()
482                          + " from " + database.getSelectedItem());
483     }
484
485     return sequences;
486
487   }
488
489   void showErrorMessage(final String error)
490   {
491     resetDialog();
492     javax.swing.SwingUtilities.invokeLater(new Runnable()
493     {
494       public void run()
495       {
496         JOptionPane.showInternalMessageDialog(Desktop.desktop,
497                                               error, "Error Retrieving Data",
498                                           JOptionPane.WARNING_MESSAGE);
499       }
500     });
501   }
502 }
503