Check hiddenSequence before placing in tmp
[jalview.git] / src / jalview / gui / SequenceFetcher.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 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         Enumeration en2 = entry.getAccession().elements();\r
305         while (en2.hasMoreElements())\r
306         {\r
307           sequence[i].getDatasetSequence().addDBRef(new DBRefEntry(DBRefSource.UNIPROT,\r
308                     "0",\r
309                     en2.nextElement().toString()));\r
310         }\r
311 \r
312 \r
313 \r
314 \r
315         sequence[i].getDatasetSequence().setPDBId(onlyPdbEntries);\r
316         if (entry.getFeature() != null)\r
317         {\r
318           e = entry.getFeature().elements();\r
319           while (e.hasMoreElements())\r
320           {\r
321             SequenceFeature sf = (SequenceFeature) e.nextElement();\r
322             sf.setFeatureGroup("Uniprot");\r
323             sequence[i].getDatasetSequence().addSequenceFeature( sf );\r
324           }\r
325         }\r
326       }\r
327     }\r
328   }\r
329 \r
330   StringBuffer getPDBFile(String id)\r
331   {\r
332     StringBuffer result = new StringBuffer();\r
333     String chain = null;\r
334     if (id.indexOf(":") > -1)\r
335     {\r
336       chain = id.substring(id.indexOf(":") + 1);\r
337       id = id.substring(0, id.indexOf(":"));\r
338     }\r
339 \r
340     EBIFetchClient ebi = new EBIFetchClient();\r
341     String file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").getAbsolutePath();\r
342     if (file == null)\r
343       return null;\r
344     try\r
345     {\r
346       PDBfile pdbfile = new PDBfile(file, jalview.io.AppletFormatAdapter.FILE);\r
347       for (int i = 0; i < pdbfile.chains.size(); i++)\r
348       {\r
349         if (chain == null ||\r
350             ( (PDBChain) pdbfile.chains.elementAt(i)).id.\r
351             toUpperCase().equals(chain))\r
352 \r
353           result.append("\n>PDB|" + id + "|" +\r
354                         ( (PDBChain) pdbfile.chains.elementAt(i)).sequence.\r
355                         getName() +\r
356                         "\n"\r
357                         +\r
358                         ( (PDBChain) pdbfile.chains.elementAt(i)).sequence.\r
359                         getSequence());\r
360       }\r
361     }\r
362     catch (Exception ex) // Problem parsing PDB file\r
363     {\r
364       jalview.bin.Cache.log.warn("Exception when retrieving " +\r
365                                  textfield.getText() + " from " +\r
366                                  database.getSelectedItem(), ex);\r
367       return null;\r
368     }\r
369 \r
370     return result;\r
371   }\r
372 \r
373   SequenceI[] parseResult(String result, String title)\r
374   {\r
375     String format = new IdentifyFile().Identify(result, "Paste");\r
376     SequenceI[] sequences = null;\r
377 \r
378     if (FormatAdapter.isValidFormat(format))\r
379     {\r
380       sequences = null;\r
381       try{ sequences = new FormatAdapter().readFile(result.toString(), "Paste",\r
382                                                format);}\r
383       catch(Exception ex){}\r
384 \r
385       if (sequences != null && sequences.length > 0)\r
386       {\r
387         if (alignFrame == null)\r
388         {\r
389           AlignFrame af = new AlignFrame(new Alignment(sequences),\r
390                                            AlignFrame.DEFAULT_WIDTH,\r
391                                            AlignFrame.DEFAULT_HEIGHT\r
392 );\r
393           af.currentFileFormat = format;\r
394           if(title==null)\r
395             title = "Retrieved from " + database.getSelectedItem();\r
396           Desktop.addInternalFrame(af,\r
397                                    title,\r
398                                    AlignFrame.DEFAULT_WIDTH,\r
399                                    AlignFrame.DEFAULT_HEIGHT);\r
400           af.statusBar.setText("Successfully pasted alignment file");\r
401 \r
402           try\r
403           {\r
404             af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));\r
405           }\r
406           catch (Exception ex)\r
407           {}\r
408         }\r
409         else\r
410         {\r
411           for (int i = 0; i < sequences.length; i++)\r
412           {\r
413             alignFrame.viewport.alignment.addSequence(sequences[i]);\r
414 \r
415             ////////////////////////////\r
416             //Dataset needs extension;\r
417             /////////////////////////////\r
418             Sequence ds = new Sequence(sequences[i].getName(),\r
419                                        AlignSeq.extractGaps("-. ",\r
420                 sequences[i].getSequence()),\r
421                                        sequences[i].getStart(),\r
422                                        sequences[i].getEnd());\r
423             sequences[i].setDatasetSequence(ds);\r
424             alignFrame.viewport.alignment.getDataset().addSequence(ds);\r
425           }\r
426           alignFrame.viewport.setEndSeq(alignFrame.viewport.alignment.\r
427                                         getHeight());\r
428           alignFrame.viewport.alignment.getWidth();\r
429           alignFrame.viewport.firePropertyChange("alignment", null,\r
430                                                  alignFrame.viewport.\r
431                                                  getAlignment().getSequences());\r
432 \r
433         }\r
434 \r
435         if (database.getSelectedItem().equals("PDB"))\r
436         {\r
437           // Parse out the ids from the structured names\r
438           boolean errors = false;\r
439           for (int i = 0; i < sequences.length; i++)\r
440           {\r
441             PDBEntry entry = new PDBEntry();\r
442             com.stevesoft.pat.Regex idbits = new com.stevesoft.pat.Regex(\r
443                 "PDB\\|([0-9A-z]{4})\\|(.)");\r
444             if (idbits.search(sequences[i].getName()))\r
445             {\r
446               String pdbid = idbits.substring(1);\r
447               String pdbccode = idbits.substring(2);\r
448               // Construct the PDBEntry\r
449               entry.setId(pdbid);\r
450               if (entry.getProperty() == null)\r
451                 entry.setProperty(new Hashtable());\r
452               entry.getProperty().put("chains",\r
453                                       pdbccode\r
454                                       + "=" + sequences[i].getStart()\r
455                                       + "-" + sequences[i].getEnd());\r
456               sequences[i].getDatasetSequence().addPDBId(entry);\r
457 \r
458               // We make a DBRefEtntry because we have obtained the PDB file from a verifiable source\r
459               // JBPNote - PDB DBRefEntry should also carry the chain and mapping information\r
460               DBRefEntry dbentry = new DBRefEntry(jalview.datamodel.DBRefSource.PDB,"0",pdbid);\r
461               sequences[i].getDatasetSequence().addDBRef(dbentry);\r
462             }\r
463             else\r
464             {\r
465               // don't add an entry for this chain, but this is probably a bug\r
466               // that the user should know about.\r
467               jalview.bin.Cache.log.warn(\r
468                   "No PDBEntry constructed for sequence " + i + " : " +\r
469                   sequences[i].getName());\r
470               errors = true;\r
471             }\r
472           }\r
473           if (errors)\r
474             jalview.bin.Cache.log.warn(\r
475                 "Query string that resulted in PDBEntry construction failure was :\n" +\r
476                 textfield.getText());\r
477         }\r
478 \r
479       }\r
480       else\r
481         showErrorMessage("Error retrieving " + textfield.getText()\r
482                          + " from " + database.getSelectedItem());\r
483     }\r
484 \r
485     return sequences;\r
486 \r
487   }\r
488 \r
489   void showErrorMessage(final String error)\r
490   {\r
491     resetDialog();\r
492     javax.swing.SwingUtilities.invokeLater(new Runnable()\r
493     {\r
494       public void run()\r
495       {\r
496         JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
497                                               error, "Error Retrieving Data",\r
498                                           JOptionPane.WARNING_MESSAGE);\r
499       }\r
500     });\r
501   }\r
502 }\r
503 \r