1 package jalview.analysis;
\r
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 JInternalFrame outputFrame = new JInternalFrame();
\r
16 CutAndPasteTransfer output = new CutAndPasteTransfer(false);
\r
17 StringBuffer sbuffer = new StringBuffer();
\r
19 public SequenceFeatureFetcher(AlignmentI align, AlignmentPanel ap)
\r
21 unknownSequences = new ArrayList();
\r
24 Thread thread = new Thread(this);
\r
31 String cache = jalview.bin.Cache.getProperty("UNIPROT_CACHE");
\r
33 RandomAccessFile out = null;
\r
38 jalview.bin.Cache.setProperty("UNIPROT_CACHE", System.getProperty("user.home")+"/uniprot.xml");
\r
39 cache = jalview.bin.Cache.getProperty("UNIPROT_CACHE");
\r
44 File test = new File(cache);
\r
45 if( !test.exists() )
\r
47 out = new RandomAccessFile(cache, "rw");
\r
48 out.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
\r
49 out.writeBytes("<UNIPROT_CACHE>\n");
\r
53 out = new RandomAccessFile(cache, "rw");
\r
54 // open exisiting cache and remove </UNIPROT_CACHE> from the end
\r
57 while ( (data = out.readLine()) != null)
\r
59 if (data.indexOf("</entry>") > -1)
\r
60 lastLine = out.getFilePointer();
\r
67 Vector sequences = align.getSequences();
\r
69 while (seqIndex < sequences.size())
\r
71 ArrayList ids = new ArrayList();
\r
72 for (int i = 0; seqIndex < sequences.size() && i < 50; seqIndex++, i++)
\r
74 SequenceI sequence = (SequenceI) sequences.get(seqIndex);
\r
75 ids.add(sequence.getName());
\r
78 tryLocalCacheFirst(ids, align);
\r
82 StringBuffer remainingIds = new StringBuffer("uniprot:");
\r
83 for (int i = 0; i < ids.size(); i++)
\r
84 remainingIds.append(ids.get(i) + ";");
\r
86 EBIFetchClient ebi = new EBIFetchClient();
\r
87 String[] result = ebi.fetchData(remainingIds.toString(), "xml", null);
\r
90 ReadUniprotFile(result, out, align);
\r
97 out.writeBytes("</UNIPROT_CACHE>\n");
\r
100 }catch(Exception ex){ex.printStackTrace();}
\r
102 ap.RefreshPanels();
\r
103 findMissingIds(align);
\r
104 if(sbuffer.length()>0)
\r
106 output.formatForOutput();
\r
107 outputFrame.setContentPane(output);
\r
108 output.setText("Your sequences have been matched to Uniprot. Some of the ids have been\n"
\r
109 +"altered, most likely the start/end residue will have been updated.\n"
\r
110 +"Save your alignment to maintain the updated id.\n\n"+sbuffer.toString());
\r
111 Desktop.addInternalFrame(outputFrame, "Sequence names updated ", 600,300);
\r
115 if(unknownSequences.size()>0)
\r
117 //ignore for now!!!!!!!!!!
\r
118 // WSWUBlastClient blastClient = new WSWUBlastClient(align, unknownSequences);
\r
123 void ReadUniprotFile(String [] result, RandomAccessFile out, AlignmentI align)
\r
125 SequenceI sequence = null;
\r
126 Vector features = null;
\r
127 String type, description, status, start, end, pdb = null;
\r
130 for (int r = 0; r < result.length; r++)
\r
132 if(sequence==null && result[r].indexOf("<name>")>-1)
\r
134 long filePointer = 0;
\r
138 filePointer=out.getFilePointer();
\r
139 out.writeBytes("<entry>\n");
\r
140 }catch(Exception ex){}
\r
142 String seqName = parseElement( result[r], "<name>" , out);
\r
143 sequence = align.findName( seqName ) ;
\r
146 sequence = align.findName( seqName.substring(0, seqName.indexOf('_')));
\r
149 sbuffer.append("changing "+sequence.getName()+" to "+seqName+"\n");
\r
150 sequence.setName(seqName);
\r
155 sbuffer.append("UNIPROT updated suggestion is "+result[r]+"\n");
\r
156 sequence = align.findName( result[r] ) ;
\r
158 // this entry has been suggested by ebi.
\r
159 // doesn't match id in alignment file
\r
160 try { out.setLength(filePointer); } catch (Exception ex) {}
\r
161 // now skip to next entry
\r
162 while( result[r].indexOf("</entry>")==-1)
\r
166 features = new Vector();
\r
167 type=""; start="0"; end="0"; description=""; status=""; pdb="";
\r
174 if( result[r].indexOf("<property type=\"pdb accession\"")>-1)
\r
176 pdb = parseValue( result[r], "value=" , out);
\r
177 sequence.setPDBId(pdb);
\r
180 if(result[r].indexOf("feature type")>-1)
\r
182 type = parseValue( result[r], "type=" , out);
\r
183 description = parseValue( result[r], "description=" , null );
\r
184 status = parseValue ( result[r], "status=", null);
\r
186 while( result[r].indexOf("position")==-1)
\r
191 if(result[r].indexOf("begin")>-1)
\r
193 start = parseValue( result[r], "position=" , out);
\r
194 end = parseValue( result[++r], "position=" , out);
\r
198 start = parseValue( result[r], "position=" , out);
\r
199 end = parseValue( result[r], "position=" , null);
\r
201 int sstart = Integer.parseInt(start);
\r
202 int eend = Integer.parseInt(end);
\r
204 try{ out.writeBytes("</feature>\n"); }catch(Exception ex){}
\r
206 SequenceFeature sf = new SequenceFeature(type,
\r
214 if(result[r].indexOf("<sequence")>-1)
\r
216 StringBuffer seqString = new StringBuffer();
\r
219 try { out.writeBytes(result[r]+"\n"); } catch (Exception ex){}
\r
221 while(result[++r].indexOf("</sequence>")==-1)
\r
223 seqString.append(result[r]);
\r
225 try { out.writeBytes(result[r]+"\n"); } catch (Exception ex){}
\r
229 try { out.writeBytes(result[r]+"\n"); } catch (Exception ex){}
\r
231 StringBuffer nonGapped = new StringBuffer();
\r
232 for (int i = 0; i < sequence.getSequence().length(); i++)
\r
234 if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))
\r
235 nonGapped.append(sequence.getCharAt(i));
\r
238 int absStart = seqString.toString().indexOf(nonGapped.toString());
\r
241 unknownSequences.add(sequence.getName());
\r
243 sbuffer.append(sequence.getName()+ " SEQUENCE NOT %100 MATCH \n");
\r
247 int absEnd = absStart + nonGapped.toString().length();
\r
250 if(absStart!=sequence.getStart() || absEnd!=sequence.getEnd())
\r
251 sbuffer.append("Updated: "+sequence.getName()+" "+
\r
252 sequence.getStart()+"/"+sequence.getEnd()+" to "+ absStart+"/"+absEnd+"\n");
\r
255 sequence.setStart(absStart);
\r
256 sequence.setEnd(absEnd);
\r
260 if(result[r].indexOf("</entry>")>-1)
\r
263 sequence.setSequenceFeatures( features );
\r
267 try{ out.writeBytes("</entry>\n"); }catch(Exception ex){}
\r
273 void findMissingIds(AlignmentI align)
\r
276 ArrayList cachedIds = new ArrayList();
\r
280 BufferedReader in = new BufferedReader(
\r
281 new FileReader(jalview.bin.Cache.getProperty("UNIPROT_CACHE")));
\r
283 while ( (data = in.readLine()) != null)
\r
285 if (data.indexOf("name") > -1)
\r
287 String name = parseElement(data, "<name>", null);
\r
288 cachedIds.add(name);
\r
292 catch (Exception ex)
\r
293 { ex.printStackTrace(); }
\r
295 for(int i=0; i<align.getHeight(); i++)
\r
296 if( !cachedIds.contains( align.getSequenceAt(i).getName() ) )
\r
297 unknownSequences.add( align.getSequenceAt(i).getName() );
\r
302 void tryLocalCacheFirst(ArrayList ids, AlignmentI align)
\r
304 ArrayList cacheData = new ArrayList();
\r
306 BufferedReader in = new BufferedReader(
\r
307 new FileReader(jalview.bin.Cache.getProperty("UNIPROT_CACHE")));
\r
309 // read through cache file, if the cache has sequences we're looking for
\r
310 // add the lines to a new String array, Readthis new array and
\r
311 // make sure we remove the ids from the list to retrieve from EBI
\r
313 while( ( data=in.readLine())!=null)
\r
315 if(data.indexOf("name")>-1)
\r
317 String name = parseElement( data, "<name>" , null) ;
\r
318 if(ids.contains( name ) )
\r
320 cacheData.add("<entry>");
\r
321 cacheData.add(data);
\r
322 while( data.indexOf("</entry>")==-1)
\r
324 data = in.readLine();
\r
325 cacheData.add(data);
\r
327 cacheData.add(data);
\r
329 ids.remove( name );
\r
334 catch(Exception ex){ex.printStackTrace();}
\r
336 String [] localData = new String[cacheData.size()];
\r
337 cacheData.toArray( localData );
\r
338 if(localData!=null && localData.length>0)
\r
339 ReadUniprotFile(localData, null, align);
\r
343 String parseValue(String line, String tag, RandomAccessFile out)
\r
346 try{ out.writeBytes(line+"\n"); }catch(Exception ex){}
\r
349 int index = line.indexOf(tag)+tag.length()+1;
\r
350 if(index==tag.length())
\r
353 return line.substring( index, line.indexOf("\"", index+1) );
\r
357 String parseElement(String line, String tag, RandomAccessFile out)
\r
362 out.writeBytes(line + "\n");
\r
364 catch (Exception ex)
\r
367 int index = line.indexOf(tag) + tag.length();
\r
368 return line.substring(index, line.indexOf("</"));
\r