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