Prompt before blasting
[jalview.git] / src / jalview / io / SequenceFeatureFetcher.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.io;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import jalview.gui.*;\r
24 \r
25 import java.io.*;\r
26 \r
27 import java.util.*;\r
28 \r
29 import org.exolab.castor.mapping.Mapping;\r
30 \r
31 import org.exolab.castor.xml.*;\r
32 import jalview.analysis.AlignSeq;\r
33 \r
34 \r
35 \r
36 /**\r
37  * DOCUMENT ME!\r
38  *\r
39  * @author $author$\r
40  * @version $Revision$\r
41  */\r
42 public class SequenceFeatureFetcher implements Runnable\r
43 {\r
44 \r
45   AlignmentI align;\r
46   AlignmentPanel ap;\r
47   ArrayList unknownSequences;\r
48   CutAndPasteTransfer output = new CutAndPasteTransfer();\r
49   StringBuffer sbuffer = new StringBuffer();\r
50 \r
51   Vector localCache = new Vector();\r
52 \r
53   Vector getUniprotEntries(File file)\r
54   {\r
55 \r
56     UniprotFile uni = new UniprotFile();\r
57     try\r
58     {\r
59       // 1. Load the mapping information from the file\r
60       Mapping map = new Mapping(uni.getClass().getClassLoader());\r
61       java.net.URL url = uni.getClass().getResource("/uniprot_mapping.xml");\r
62       map.loadMapping(url);\r
63 \r
64       // 2. Unmarshal the data\r
65       Unmarshaller unmar = new Unmarshaller();\r
66       unmar.setIgnoreExtraElements(true);\r
67       unmar.setMapping(map);\r
68       uni = (UniprotFile) unmar.unmarshal(new FileReader(file));\r
69       localCache.addAll( uni.getUniprotEntries() );\r
70 \r
71       // 3. marshal the data with the total price back and print the XML in the console\r
72     //    Marshaller marshaller = new Marshaller(\r
73       //      new FileWriter(jalview.bin.Cache.getProperty("UNIPROT_CACHE"))\r
74      //       );\r
75        //  marshaller.setMapping(map);\r
76        //  marshaller.marshal(uni);\r
77 \r
78     }\r
79     catch (Exception e)\r
80     {\r
81       System.out.println("Error getUniprotEntries() "+e);\r
82      // e.printStackTrace();\r
83      // if(!updateLocalCache)\r
84       //  file.delete();\r
85 \r
86     }\r
87     return uni.getUniprotEntries();\r
88   }\r
89 \r
90   /**\r
91    * Creates a new SequenceFeatureFetcher object.\r
92    *\r
93    * @param align DOCUMENT ME!\r
94    * @param ap DOCUMENT ME!\r
95    */\r
96   public SequenceFeatureFetcher(AlignmentI align, AlignmentPanel ap)\r
97   {\r
98     unknownSequences = new ArrayList();\r
99     this.align = align.getDataset();\r
100     this.ap = ap;\r
101 \r
102     Thread thread = new Thread(this);\r
103     thread.start();\r
104   }\r
105 \r
106   /**\r
107    * DOCUMENT ME!\r
108    */\r
109   public void run()\r
110   {\r
111     try\r
112     {\r
113       int seqIndex = 0;\r
114       Vector sequences = align.getSequences();\r
115 \r
116       while (seqIndex < sequences.size())\r
117       {\r
118         Vector ids = new Vector();\r
119 \r
120         for (int i = 0; (seqIndex < sequences.size()) && (i < 50);\r
121              seqIndex++, i++)\r
122         {\r
123           SequenceI sequence = (SequenceI) sequences.get(seqIndex);\r
124           ids.add(sequence.getName());\r
125           unknownSequences.add(sequence.getName());\r
126         }\r
127 \r
128         ///////////////////////////////////\r
129         ///READ FROM EBI\r
130         if (ids.size() > 0)\r
131         {\r
132           StringBuffer remainingIds = new StringBuffer("uniprot:");\r
133           for (int i = 0; i < ids.size(); i++)\r
134            {\r
135              remainingIds.append(ids.get(i) + ";");\r
136            }\r
137           EBIFetchClient ebi = new EBIFetchClient();\r
138           File file = ebi.fetchDataAsFile(remainingIds.toString(),\r
139                                           "xml", null);\r
140 \r
141 \r
142 \r
143           if (file != null)\r
144           {\r
145             ReadUniprotFile(file, align, ids);\r
146           }\r
147         }\r
148       }\r
149     }\r
150     catch (Exception ex)\r
151     {\r
152       ex.printStackTrace();\r
153     }\r
154 \r
155     if (sbuffer.length() > 0)\r
156     {\r
157       output.setText(\r
158           "Your sequences have been matched to Uniprot. Some of the ids have been\n" +\r
159           "altered, most likely the start/end residue will have been updated.\n" +\r
160           "Save your alignment to maintain the updated id.\n\n" +\r
161           sbuffer.toString());\r
162       Desktop.addInternalFrame(output, "Sequence names updated ", 600, 300);\r
163     }\r
164 \r
165     if (unknownSequences.size() > 0)\r
166     {\r
167       int reply = javax.swing.JOptionPane.showInternalConfirmDialog(\r
168           Desktop.desktop, "Couldn't find a match for "+unknownSequences.size()+" sequences."\r
169               +"\nPerform blast for unknown sequences?",\r
170                   "Blast for Unidentified Sequences",\r
171                    javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE);\r
172 \r
173       if(reply == javax.swing.JOptionPane.YES_OPTION)\r
174        new WSWUBlastClient(ap, align, unknownSequences);\r
175     }\r
176     else\r
177        ((Alignment)align).featuresAdded = true;\r
178 \r
179 \r
180     ap.repaint();\r
181   }\r
182 \r
183   /**\r
184    * DOCUMENT ME!\r
185    *\r
186    * @param result DOCUMENT ME!\r
187    * @param out DOCUMENT ME!\r
188    * @param align DOCUMENT ME!\r
189    */\r
190   void ReadUniprotFile(File file, AlignmentI align, Vector ids)\r
191   {\r
192     if(!file.exists())\r
193       return;\r
194 \r
195     SequenceI sequence = null;\r
196     //       String pdb = null;\r
197 \r
198     Vector entries = getUniprotEntries(file);\r
199 \r
200     int i, iSize = entries==null?0:entries.size();\r
201     UniprotEntry entry;\r
202     for (i = 0; i < iSize; i++)\r
203     {\r
204       entry = (UniprotEntry) entries.elementAt(i);\r
205       String idmatch = entry.getAccession().elementAt(0).toString();\r
206       sequence = align.findName(idmatch);\r
207 \r
208       if (sequence == null)\r
209       {\r
210         //Sequence maybe Name, not Accession\r
211 \r
212         idmatch = entry.getName().elementAt(0).toString();;\r
213         sequence = align.findName(idmatch);\r
214       }\r
215 \r
216       if (sequence == null)\r
217       {\r
218         System.out.println("not found");\r
219         continue;\r
220       }\r
221 \r
222       ids.remove(sequence.getName());\r
223       unknownSequences.remove(sequence.getName());\r
224 \r
225       String nonGapped = AlignSeq.extractGaps("-. ", sequence.getSequence());\r
226 \r
227       int absStart = entry.getUniprotSequence().getContent().indexOf(\r
228           nonGapped.toString());\r
229 \r
230       if (absStart == -1)\r
231       {\r
232         unknownSequences.add(sequence.getName());\r
233         sbuffer.append(sequence.getName() +\r
234                        " SEQUENCE NOT %100 MATCH \n");\r
235 \r
236         continue;\r
237       }\r
238 \r
239       int absEnd = absStart + nonGapped.toString().length();\r
240       absStart += 1;\r
241 \r
242       if ( (absStart != sequence.getStart()) ||\r
243           (absEnd != sequence.getEnd()))\r
244       {\r
245         sbuffer.append("Updated: " + sequence.getName() + " " +\r
246                        sequence.getStart() + "/" + sequence.getEnd() +\r
247                        "  to  " + absStart + "/" + absEnd + "\n");\r
248       }\r
249 \r
250       sequence.setSequenceFeatures(entry.getFeatures());\r
251       sequence.setStart(absStart);\r
252       sequence.setEnd(absEnd);\r
253     }\r
254   }\r
255 }\r
256 \r
257 \r