vamsasDemo new branch
[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;\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           if (file != null)\r
143           {\r
144             ReadUniprotFile(file, align, ids);\r
145           }\r
146         }\r
147       }\r
148     }\r
149     catch (Exception ex)\r
150     {\r
151       ex.printStackTrace();\r
152     }\r
153 \r
154     if (sbuffer.length() > 0)\r
155     {\r
156       output.setText(\r
157           "Your sequences have been matched to Uniprot. Some of the ids have been\n" +\r
158           "altered, most likely the start/end residue will have been updated.\n" +\r
159           "Save your alignment to maintain the updated id.\n\n" +\r
160           sbuffer.toString());\r
161       Desktop.addInternalFrame(output, "Sequence names updated ", 600, 300);\r
162     }\r
163 \r
164     if (unknownSequences.size() > 0)\r
165     {\r
166        new WSWUBlastClient(ap, align, unknownSequences);\r
167     }\r
168     else\r
169        ((Alignment)align).featuresAdded = true;\r
170 \r
171 \r
172     ap.repaint();\r
173   }\r
174 \r
175   /**\r
176    * DOCUMENT ME!\r
177    *\r
178    * @param result DOCUMENT ME!\r
179    * @param out DOCUMENT ME!\r
180    * @param align DOCUMENT ME!\r
181    */\r
182   void ReadUniprotFile(File file, AlignmentI align, Vector ids)\r
183   {\r
184     if(!file.exists())\r
185       return;\r
186 \r
187     SequenceI sequence = null;\r
188     //       String pdb = null;\r
189 \r
190     Vector entries = getUniprotEntries(file);\r
191 \r
192     int i, iSize = entries==null?0:entries.size();\r
193     UniprotEntry entry;\r
194     for (i = 0; i < iSize; i++)\r
195     {\r
196       entry = (UniprotEntry) entries.elementAt(i);\r
197       String idmatch = entry.getAccession();\r
198       sequence = align.findName(idmatch);\r
199 \r
200       if (sequence == null)\r
201       {\r
202         //Sequence maybe Name, not Accession\r
203         idmatch = entry.getName();\r
204         sequence = align.findName(idmatch);\r
205       }\r
206 \r
207       if (sequence == null)\r
208       {\r
209         continue;\r
210       }\r
211 \r
212       ids.remove(sequence.getName());\r
213       unknownSequences.remove(sequence.getName());\r
214 \r
215       String nonGapped = AlignSeq.extractGaps("-. ", sequence.getSequence());\r
216 \r
217       int absStart = entry.getUniprotSequence().getContent().indexOf(\r
218           nonGapped.toString());\r
219 \r
220       if (absStart == -1)\r
221       {\r
222         unknownSequences.add(sequence.getName());\r
223         sbuffer.append(sequence.getName() +\r
224                        " SEQUENCE NOT %100 MATCH \n");\r
225 \r
226         continue;\r
227       }\r
228 \r
229       int absEnd = absStart + nonGapped.toString().length();\r
230       absStart += 1;\r
231 \r
232       if ( (absStart != sequence.getStart()) ||\r
233           (absEnd != sequence.getEnd()))\r
234       {\r
235         sbuffer.append("Updated: " + sequence.getName() + " " +\r
236                        sequence.getStart() + "/" + sequence.getEnd() +\r
237                        "  to  " + absStart + "/" + absEnd + "\n");\r
238       }\r
239 \r
240       sequence.setSequenceFeatures(entry.getFeatures());\r
241       sequence.setStart(absStart);\r
242       sequence.setEnd(absEnd);\r
243     }\r
244   }\r
245 }\r
246 \r
247 \r