2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
21 import jalview.datamodel.*;
\r
23 import jalview.gui.*;
\r
25 import jalview.io.*;
\r
36 * @version $Revision$
\r
38 public class SequenceFeatureFetcher implements Runnable
\r
42 ArrayList unknownSequences;
\r
43 CutAndPasteTransfer output = new CutAndPasteTransfer();
\r
44 StringBuffer sbuffer = new StringBuffer();
\r
47 * Creates a new SequenceFeatureFetcher object.
\r
49 * @param align DOCUMENT ME!
\r
50 * @param ap DOCUMENT ME!
\r
52 public SequenceFeatureFetcher(AlignmentI align, AlignmentPanel ap)
\r
54 unknownSequences = new ArrayList();
\r
58 Thread thread = new Thread(this);
\r
67 RandomAccessFile out = null;
\r
71 String cache = System.getProperty("user.home") +
\r
72 "/.jalview.uniprot.xml";
\r
74 File test = new File(cache);
\r
78 out = new RandomAccessFile(cache, "rw");
\r
79 out.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
\r
80 out.writeBytes("<UNIPROT_CACHE>\n");
\r
84 out = new RandomAccessFile(cache, "rw");
\r
86 // open exisiting cache and remove </UNIPROT_CACHE> from the end
\r
90 while ((data = out.readLine()) != null)
\r
92 if (data.indexOf("</entry>") > -1)
\r
94 lastLine = out.getFilePointer();
\r
102 Vector sequences = align.getSequences();
\r
104 while (seqIndex < sequences.size())
\r
106 ArrayList ids = new ArrayList();
\r
108 for (int i = 0; (seqIndex < sequences.size()) && (i < 50);
\r
111 SequenceI sequence = (SequenceI) sequences.get(seqIndex);
\r
112 ids.add(sequence.getName());
\r
115 tryLocalCacheFirst(ids, align);
\r
117 if (ids.size() > 0)
\r
119 StringBuffer remainingIds = new StringBuffer("uniprot:");
\r
121 for (int i = 0; i < ids.size(); i++)
\r
122 remainingIds.append(ids.get(i) + ";");
\r
124 EBIFetchClient ebi = new EBIFetchClient();
\r
125 String[] result = ebi.fetchData(remainingIds.toString(),
\r
128 if (result != null)
\r
130 ReadUniprotFile(result, out, align);
\r
137 out.writeBytes("</UNIPROT_CACHE>\n");
\r
141 catch (Exception ex)
\r
143 ex.printStackTrace();
\r
146 findMissingIds(align);
\r
148 if (sbuffer.length() > 0)
\r
151 "Your sequences have been matched to Uniprot. Some of the ids have been\n" +
\r
152 "altered, most likely the start/end residue will have been updated.\n" +
\r
153 "Save your alignment to maintain the updated id.\n\n" +
\r
154 sbuffer.toString());
\r
155 Desktop.addInternalFrame(output, "Sequence names updated ", 600, 300);
\r
158 if (unknownSequences.size() > 0)
\r
160 //ignore for now!!!!!!!!!!
\r
161 // WSWUBlastClient blastClient = new WSWUBlastClient(align, unknownSequences);
\r
164 jalview.gui.PaintRefresher.Refresh(null, align);
\r
170 * @param result DOCUMENT ME!
\r
171 * @param out DOCUMENT ME!
\r
172 * @param align DOCUMENT ME!
\r
174 void ReadUniprotFile(String[] result, RandomAccessFile out, AlignmentI align)
\r
176 SequenceI sequence = null;
\r
177 Vector features = null;
\r
179 String description;
\r
185 for (int r = 0; r < result.length; r++)
\r
187 if ((sequence == null) && (result[r].indexOf("<name>") > -1))
\r
189 long filePointer = 0;
\r
195 filePointer = out.getFilePointer();
\r
196 out.writeBytes("<entry>\n");
\r
198 catch (Exception ex)
\r
203 String seqName = parseElement(result[r], "<name>", out);
\r
204 sequence = align.findName(seqName);
\r
206 if (sequence == null)
\r
208 sequence = align.findName(seqName.substring(0,
\r
209 seqName.indexOf('_')));
\r
211 if (sequence != null)
\r
213 sbuffer.append("changing " + sequence.getName() +
\r
214 " to " + seqName + "\n");
\r
215 sequence.setName(seqName);
\r
219 if (sequence == null)
\r
221 sbuffer.append("UNIPROT updated suggestion is " +
\r
223 sequence = align.findName(result[r]);
\r
225 // this entry has been suggested by ebi.
\r
226 // doesn't match id in alignment file
\r
229 out.setLength(filePointer);
\r
231 catch (Exception ex)
\r
235 // now skip to next entry
\r
236 while (result[r].indexOf("</entry>") == -1)
\r
240 features = new Vector();
\r
249 if (sequence == null)
\r
254 if (result[r].indexOf("<property type=\"pdb accession\"") > -1)
\r
256 pdb = parseValue(result[r], "value=", out);
\r
257 sequence.setPDBId(pdb);
\r
260 if (result[r].indexOf("feature type") > -1)
\r
262 type = parseValue(result[r], "type=", out);
\r
263 description = parseValue(result[r], "description=", null);
\r
264 status = parseValue(result[r], "status=", null);
\r
266 while (result[r].indexOf("position") == -1)
\r
272 if (result[r].indexOf("begin") > -1)
\r
274 start = parseValue(result[r], "position=", out);
\r
275 end = parseValue(result[++r], "position=", out);
\r
279 start = parseValue(result[r], "position=", out);
\r
280 end = parseValue(result[r], "position=", null);
\r
283 int sstart = Integer.parseInt(start);
\r
284 int eend = Integer.parseInt(end);
\r
290 out.writeBytes("</feature>\n");
\r
292 catch (Exception ex)
\r
297 SequenceFeature sf = new SequenceFeature(type, sstart, eend,
\r
298 description, status);
\r
302 if (result[r].indexOf("<sequence") > -1)
\r
304 StringBuffer seqString = new StringBuffer();
\r
310 out.writeBytes(result[r] + "\n");
\r
312 catch (Exception ex)
\r
317 while (result[++r].indexOf("</sequence>") == -1)
\r
319 seqString.append(result[r]);
\r
325 out.writeBytes(result[r] + "\n");
\r
327 catch (Exception ex)
\r
337 out.writeBytes(result[r] + "\n");
\r
339 catch (Exception ex)
\r
344 StringBuffer nonGapped = new StringBuffer();
\r
346 for (int i = 0; i < sequence.getSequence().length(); i++)
\r
348 if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))
\r
350 nonGapped.append(sequence.getCharAt(i));
\r
354 int absStart = seqString.toString().indexOf(nonGapped.toString());
\r
356 if (absStart == -1)
\r
358 unknownSequences.add(sequence.getName());
\r
360 sbuffer.append(sequence.getName() +
\r
361 " SEQUENCE NOT %100 MATCH \n");
\r
366 int absEnd = absStart + nonGapped.toString().length();
\r
369 if ((absStart != sequence.getStart()) ||
\r
370 (absEnd != sequence.getEnd()))
\r
372 sbuffer.append("Updated: " + sequence.getName() + " " +
\r
373 sequence.getStart() + "/" + sequence.getEnd() +
\r
374 " to " + absStart + "/" + absEnd + "\n");
\r
377 sequence.setStart(absStart);
\r
378 sequence.setEnd(absEnd);
\r
381 if (result[r].indexOf("</entry>") > -1)
\r
383 if (features != null)
\r
385 sequence.setSequenceFeatures(features);
\r
395 out.writeBytes("</entry>\n");
\r
397 catch (Exception ex)
\r
408 * @param align DOCUMENT ME!
\r
410 void findMissingIds(AlignmentI align)
\r
413 ArrayList cachedIds = new ArrayList();
\r
417 BufferedReader in = new BufferedReader(new FileReader(
\r
418 jalview.bin.Cache.getProperty("UNIPROT_CACHE")));
\r
420 while ((data = in.readLine()) != null)
\r
422 if (data.indexOf("name") > -1)
\r
424 String name = parseElement(data, "<name>", null);
\r
425 cachedIds.add(name);
\r
429 catch (Exception ex)
\r
431 ex.printStackTrace();
\r
434 for (int i = 0; i < align.getHeight(); i++)
\r
435 if (!cachedIds.contains(align.getSequenceAt(i).getName()))
\r
437 unknownSequences.add(align.getSequenceAt(i).getName());
\r
444 * @param ids DOCUMENT ME!
\r
445 * @param align DOCUMENT ME!
\r
447 void tryLocalCacheFirst(ArrayList ids, AlignmentI align)
\r
449 ArrayList cacheData = new ArrayList();
\r
453 BufferedReader in = new BufferedReader(new FileReader(
\r
454 jalview.bin.Cache.getProperty("UNIPROT_CACHE")));
\r
456 // read through cache file, if the cache has sequences we're looking for
\r
457 // add the lines to a new String array, Readthis new array and
\r
458 // make sure we remove the ids from the list to retrieve from EBI
\r
461 while ((data = in.readLine()) != null)
\r
463 if (data.indexOf("name") > -1)
\r
465 String name = parseElement(data, "<name>", null);
\r
467 if (ids.contains(name))
\r
469 cacheData.add("<entry>");
\r
470 cacheData.add(data);
\r
472 while (data.indexOf("</entry>") == -1)
\r
474 data = in.readLine();
\r
475 cacheData.add(data);
\r
478 cacheData.add(data);
\r
485 catch (Exception ex)
\r
487 ex.printStackTrace();
\r
490 String[] localData = new String[cacheData.size()];
\r
491 cacheData.toArray(localData);
\r
493 if ((localData != null) && (localData.length > 0))
\r
495 ReadUniprotFile(localData, null, align);
\r
502 * @param line DOCUMENT ME!
\r
503 * @param tag DOCUMENT ME!
\r
504 * @param out DOCUMENT ME!
\r
506 * @return DOCUMENT ME!
\r
508 String parseValue(String line, String tag, RandomAccessFile out)
\r
514 out.writeBytes(line + "\n");
\r
516 catch (Exception ex)
\r
521 int index = line.indexOf(tag) + tag.length() + 1;
\r
523 if (index == tag.length())
\r
528 return line.substring(index, line.indexOf("\"", index + 1));
\r
534 * @param line DOCUMENT ME!
\r
535 * @param tag DOCUMENT ME!
\r
536 * @param out DOCUMENT ME!
\r
538 * @return DOCUMENT ME!
\r
540 String parseElement(String line, String tag, RandomAccessFile out)
\r
546 out.writeBytes(line + "\n");
\r
548 catch (Exception ex)
\r
553 int index = line.indexOf(tag) + tag.length();
\r
555 return line.substring(index, line.indexOf("</"));
\r