5 import javax.swing.*;
\r
7 import jalview.gui.*;
\r
8 import jalview.datamodel.*;
\r
10 public class SequenceFeatureFetcher implements Runnable
\r
14 ArrayList unknownSequences;
\r
15 CutAndPasteTransfer output = new CutAndPasteTransfer();
\r
16 StringBuffer sbuffer = new StringBuffer();
\r
18 public SequenceFeatureFetcher(AlignmentI align, AlignmentPanel ap)
\r
20 unknownSequences = new ArrayList();
\r
23 Thread thread = new Thread(this);
\r
30 String cache = jalview.bin.Cache.getProperty("UNIPROT_CACHE");
\r
32 RandomAccessFile out = null;
\r
37 jalview.bin.Cache.setProperty("UNIPROT_CACHE", System.getProperty("user.home")+"/uniprot.xml");
\r
38 cache = jalview.bin.Cache.getProperty("UNIPROT_CACHE");
\r
43 File test = new File(cache);
\r
44 if( !test.exists() )
\r
46 out = new RandomAccessFile(cache, "rw");
\r
47 out.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
\r
48 out.writeBytes("<UNIPROT_CACHE>\n");
\r
52 out = new RandomAccessFile(cache, "rw");
\r
53 // open exisiting cache and remove </UNIPROT_CACHE> from the end
\r
56 while ( (data = out.readLine()) != null)
\r
58 if (data.indexOf("</entry>") > -1)
\r
59 lastLine = out.getFilePointer();
\r
66 Vector sequences = align.getSequences();
\r
68 while (seqIndex < sequences.size())
\r
70 ArrayList ids = new ArrayList();
\r
71 for (int i = 0; seqIndex < sequences.size() && i < 50; seqIndex++, i++)
\r
73 SequenceI sequence = (SequenceI) sequences.get(seqIndex);
\r
74 ids.add(sequence.getName());
\r
77 tryLocalCacheFirst(ids, align);
\r
81 StringBuffer remainingIds = new StringBuffer("uniprot:");
\r
82 for (int i = 0; i < ids.size(); i++)
\r
83 remainingIds.append(ids.get(i) + ";");
\r
85 EBIFetchClient ebi = new EBIFetchClient();
\r
86 String[] result = ebi.fetchData(remainingIds.toString(), "xml", null);
\r
89 ReadUniprotFile(result, out, align);
\r
96 out.writeBytes("</UNIPROT_CACHE>\n");
\r
99 }catch(Exception ex){ex.printStackTrace();}
\r
102 findMissingIds(align);
\r
103 if(sbuffer.length()>0)
\r
105 output.setText("Your sequences have been matched to Uniprot. Some of the ids have been\n"
\r
106 +"altered, most likely the start/end residue will have been updated.\n"
\r
107 +"Save your alignment to maintain the updated id.\n\n"+sbuffer.toString());
\r
108 Desktop.addInternalFrame(output, "Sequence names updated ", 600,300);
\r
112 if(unknownSequences.size()>0)
\r
114 //ignore for now!!!!!!!!!!
\r
115 // WSWUBlastClient blastClient = new WSWUBlastClient(align, unknownSequences);
\r
120 void ReadUniprotFile(String [] result, RandomAccessFile out, AlignmentI align)
\r
122 SequenceI sequence = null;
\r
123 Vector features = null;
\r
124 String type, description, status, start, end, pdb = null;
\r
127 for (int r = 0; r < result.length; r++)
\r
129 if(sequence==null && result[r].indexOf("<name>")>-1)
\r
131 long filePointer = 0;
\r
135 filePointer=out.getFilePointer();
\r
136 out.writeBytes("<entry>\n");
\r
137 }catch(Exception ex){}
\r
139 String seqName = parseElement( result[r], "<name>" , out);
\r
140 sequence = align.findName( seqName ) ;
\r
143 sequence = align.findName( seqName.substring(0, seqName.indexOf('_')));
\r
146 sbuffer.append("changing "+sequence.getName()+" to "+seqName+"\n");
\r
147 sequence.setName(seqName);
\r
152 sbuffer.append("UNIPROT updated suggestion is "+result[r]+"\n");
\r
153 sequence = align.findName( result[r] ) ;
\r
155 // this entry has been suggested by ebi.
\r
156 // doesn't match id in alignment file
\r
157 try { out.setLength(filePointer); } catch (Exception ex) {}
\r
158 // now skip to next entry
\r
159 while( result[r].indexOf("</entry>")==-1)
\r
163 features = new Vector();
\r
164 type=""; start="0"; end="0"; description=""; status=""; pdb="";
\r
171 if( result[r].indexOf("<property type=\"pdb accession\"")>-1)
\r
173 pdb = parseValue( result[r], "value=" , out);
\r
174 sequence.setPDBId(pdb);
\r
177 if(result[r].indexOf("feature type")>-1)
\r
179 type = parseValue( result[r], "type=" , out);
\r
180 description = parseValue( result[r], "description=" , null );
\r
181 status = parseValue ( result[r], "status=", null);
\r
183 while( result[r].indexOf("position")==-1)
\r
188 if(result[r].indexOf("begin")>-1)
\r
190 start = parseValue( result[r], "position=" , out);
\r
191 end = parseValue( result[++r], "position=" , out);
\r
195 start = parseValue( result[r], "position=" , out);
\r
196 end = parseValue( result[r], "position=" , null);
\r
198 int sstart = Integer.parseInt(start);
\r
199 int eend = Integer.parseInt(end);
\r
201 try{ out.writeBytes("</feature>\n"); }catch(Exception ex){}
\r
203 SequenceFeature sf = new SequenceFeature(type,
\r
211 if(result[r].indexOf("<sequence")>-1)
\r
213 StringBuffer seqString = new StringBuffer();
\r
216 try { out.writeBytes(result[r]+"\n"); } catch (Exception ex){}
\r
218 while(result[++r].indexOf("</sequence>")==-1)
\r
220 seqString.append(result[r]);
\r
222 try { out.writeBytes(result[r]+"\n"); } catch (Exception ex){}
\r
226 try { out.writeBytes(result[r]+"\n"); } catch (Exception ex){}
\r
228 StringBuffer nonGapped = new StringBuffer();
\r
229 for (int i = 0; i < sequence.getSequence().length(); i++)
\r
231 if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))
\r
232 nonGapped.append(sequence.getCharAt(i));
\r
235 int absStart = seqString.toString().indexOf(nonGapped.toString());
\r
238 unknownSequences.add(sequence.getName());
\r
240 sbuffer.append(sequence.getName()+ " SEQUENCE NOT %100 MATCH \n");
\r
244 int absEnd = absStart + nonGapped.toString().length();
\r
247 if(absStart!=sequence.getStart() || absEnd!=sequence.getEnd())
\r
248 sbuffer.append("Updated: "+sequence.getName()+" "+
\r
249 sequence.getStart()+"/"+sequence.getEnd()+" to "+ absStart+"/"+absEnd+"\n");
\r
252 sequence.setStart(absStart);
\r
253 sequence.setEnd(absEnd);
\r
257 if(result[r].indexOf("</entry>")>-1)
\r
260 sequence.setSequenceFeatures( features );
\r
264 try{ out.writeBytes("</entry>\n"); }catch(Exception ex){}
\r
270 void findMissingIds(AlignmentI align)
\r
273 ArrayList cachedIds = new ArrayList();
\r
277 BufferedReader in = new BufferedReader(
\r
278 new FileReader(jalview.bin.Cache.getProperty("UNIPROT_CACHE")));
\r
280 while ( (data = in.readLine()) != null)
\r
282 if (data.indexOf("name") > -1)
\r
284 String name = parseElement(data, "<name>", null);
\r
285 cachedIds.add(name);
\r
289 catch (Exception ex)
\r
290 { ex.printStackTrace(); }
\r
292 for(int i=0; i<align.getHeight(); i++)
\r
293 if( !cachedIds.contains( align.getSequenceAt(i).getName() ) )
\r
294 unknownSequences.add( align.getSequenceAt(i).getName() );
\r
299 void tryLocalCacheFirst(ArrayList ids, AlignmentI align)
\r
301 ArrayList cacheData = new ArrayList();
\r
303 BufferedReader in = new BufferedReader(
\r
304 new FileReader(jalview.bin.Cache.getProperty("UNIPROT_CACHE")));
\r
306 // read through cache file, if the cache has sequences we're looking for
\r
307 // add the lines to a new String array, Readthis new array and
\r
308 // make sure we remove the ids from the list to retrieve from EBI
\r
310 while( ( data=in.readLine())!=null)
\r
312 if(data.indexOf("name")>-1)
\r
314 String name = parseElement( data, "<name>" , null) ;
\r
315 if(ids.contains( name ) )
\r
317 cacheData.add("<entry>");
\r
318 cacheData.add(data);
\r
319 while( data.indexOf("</entry>")==-1)
\r
321 data = in.readLine();
\r
322 cacheData.add(data);
\r
324 cacheData.add(data);
\r
326 ids.remove( name );
\r
331 catch(Exception ex){ex.printStackTrace();}
\r
333 String [] localData = new String[cacheData.size()];
\r
334 cacheData.toArray( localData );
\r
335 if(localData!=null && localData.length>0)
\r
336 ReadUniprotFile(localData, null, align);
\r
340 String parseValue(String line, String tag, RandomAccessFile out)
\r
343 try{ out.writeBytes(line+"\n"); }catch(Exception ex){}
\r
346 int index = line.indexOf(tag)+tag.length()+1;
\r
347 if(index==tag.length())
\r
350 return line.substring( index, line.indexOf("\"", index+1) );
\r
354 String parseElement(String line, String tag, RandomAccessFile out)
\r
359 out.writeBytes(line + "\n");
\r
361 catch (Exception ex)
\r
364 int index = line.indexOf(tag) + tag.length();
\r
365 return line.substring(index, line.indexOf("</"));
\r