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