1 package jalview.analysis;
\r
6 import jalview.gui.*;
\r
7 import jalview.datamodel.*;
\r
9 public class SequenceFeatureFetcher implements Runnable
\r
13 ArrayList unknownSequences;
\r
15 public SequenceFeatureFetcher(AlignmentI align, AlignmentPanel ap)
\r
17 unknownSequences = new ArrayList();
\r
20 Thread thread = new Thread(this);
\r
27 String cache = jalview.bin.Cache.getProperty("UNIPROT_CACHE");
\r
29 RandomAccessFile out = null;
\r
34 jalview.bin.Cache.setProperty("UNIPROT_CACHE", System.getProperty("user.home")+"/uniprot.xml");
\r
35 cache = jalview.bin.Cache.getProperty("UNIPROT_CACHE");
\r
40 File test = new File(cache);
\r
41 if( !test.exists() )
\r
43 out = new RandomAccessFile(cache, "rw");
\r
44 out.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
\r
45 out.writeBytes("<UNIPROT_CACHE>\n");
\r
49 out = new RandomAccessFile(cache, "rw");
\r
50 // open exisiting cache and remove </UNIPROT_CACHE> from the end
\r
53 while ( (data = out.readLine()) != null)
\r
55 if (data.indexOf("</entry>") > -1)
\r
56 lastLine = out.getFilePointer();
\r
63 Vector sequences = align.getSequences();
\r
65 while (seqIndex < sequences.size())
\r
67 ArrayList ids = new ArrayList();
\r
68 for (int i = 0; seqIndex < sequences.size() && i < 50; seqIndex++, i++)
\r
70 SequenceI sequence = (SequenceI) sequences.get(seqIndex);
\r
71 ids.add(sequence.getName());
\r
74 tryLocalCacheFirst(ids, align);
\r
78 StringBuffer remainingIds = new StringBuffer("uniprot:");
\r
79 for (int i = 0; i < ids.size(); i++)
\r
80 remainingIds.append(ids.get(i) + ";");
\r
82 EBIFetchClient ebi = new EBIFetchClient();
\r
83 System.out.println(remainingIds.toString());
\r
84 String[] result = ebi.fetchData(remainingIds.toString(), "xml", null);
\r
87 ReadUniprotFile(result, out, align);
\r
94 out.writeBytes("</UNIPROT_CACHE>\n");
\r
97 }catch(Exception ex){ex.printStackTrace();}
\r
100 findMissingIds(align);
\r
102 if(unknownSequences.size()>0)
\r
104 WSWUBlastClient blastClient = new WSWUBlastClient(align, unknownSequences);
\r
109 void ReadUniprotFile(String [] result, RandomAccessFile out, AlignmentI align)
\r
111 SequenceI sequence = null;
\r
112 Vector features = null;
\r
113 String type, description, status, start, end, pdb = null;
\r
116 for (int r = 0; r < result.length; r++)
\r
118 if(sequence==null && result[r].indexOf("<name>")>-1)
\r
120 long filePointer = 0;
\r
124 filePointer=out.getFilePointer();
\r
125 out.writeBytes("<entry>\n");
\r
126 }catch(Exception ex){}
\r
128 String seqName = parseElement( result[r], "<name>" , out);
\r
129 sequence = align.findName( seqName ) ;
\r
132 sequence = align.findName( seqName.substring(0, seqName.indexOf('_')));
\r
135 System.out.println("changing "+sequence.getName()+" to "+seqName);
\r
136 sequence.setName(seqName);
\r
141 System.out.println("UNIPROT updated suggestion is "+result[r]);
\r
142 sequence = align.findName( result[r] ) ;
\r
144 // this entry has been suggested by ebi.
\r
145 // doesn't match id in alignment file
\r
146 try { out.setLength(filePointer); } catch (Exception ex) {}
\r
147 // now skip to next entry
\r
148 while( result[r].indexOf("</entry>")==-1)
\r
152 features = new Vector();
\r
153 type=""; start="0"; end="0"; description=""; status=""; pdb="";
\r
160 if( result[r].indexOf("<property type=\"pdb accession\"")>-1)
\r
162 pdb = parseValue( result[r], "value=" , out);
\r
163 sequence.setPDBId(pdb);
\r
166 if(result[r].indexOf("feature type")>-1)
\r
168 type = parseValue( result[r], "type=" , out);
\r
169 description = parseValue( result[r], "description=" , null );
\r
170 status = parseValue ( result[r], "status=", null);
\r
172 while( result[r].indexOf("position")==-1)
\r
177 if(result[r].indexOf("begin")>-1)
\r
179 start = parseValue( result[r], "position=" , out);
\r
180 end = parseValue( result[++r], "position=" , out);
\r
184 start = parseValue( result[r], "position=" , out);
\r
185 end = parseValue( result[r], "position=" , null);
\r
187 int sstart = Integer.parseInt(start);
\r
188 int eend = Integer.parseInt(end);
\r
190 try{ out.writeBytes("</feature>\n"); }catch(Exception ex){}
\r
193 if(sstart>=sequence.getStart() && eend<=sequence.getEnd())
\r
195 SequenceFeature sf = new SequenceFeature(type,
\r
204 if(result[r].indexOf("<sequence")>-1)
\r
206 StringBuffer seqString = new StringBuffer();
\r
209 try { out.writeBytes(result[r]+"\n"); } catch (Exception ex){}
\r
211 while(result[++r].indexOf("</sequence>")==-1)
\r
213 seqString.append(result[r]);
\r
215 try { out.writeBytes(result[r]+"\n"); } catch (Exception ex){}
\r
219 try { out.writeBytes(result[r]+"\n"); } catch (Exception ex){}
\r
221 StringBuffer nonGapped = new StringBuffer();
\r
222 for (int i = 0; i < sequence.getSequence().length(); i++)
\r
224 if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))
\r
225 nonGapped.append(sequence.getCharAt(i));
\r
228 int absStart = seqString.toString().indexOf(nonGapped.toString());
\r
231 unknownSequences.add(sequence.getName());
\r
233 System.out.println(sequence.getName()+ "does not match ");
\r
237 int absEnd = absStart + nonGapped.toString().length();
\r
240 if(absStart!=sequence.getStart() || absEnd!=sequence.getEnd())
\r
241 System.out.println("Updated: "+sequence.getName()+" "+
\r
242 sequence.getStart()+"/"+sequence.getEnd()+" to "+ absStart+"/"+absEnd);
\r
245 sequence.setStart(absStart);
\r
246 sequence.setEnd(absEnd);
\r
250 if(result[r].indexOf("</entry>")>-1)
\r
253 sequence.setSequenceFeatures( features );
\r
257 try{ out.writeBytes("</entry>\n"); }catch(Exception ex){}
\r
263 void findMissingIds(AlignmentI align)
\r
266 ArrayList cachedIds = new ArrayList();
\r
270 BufferedReader in = new BufferedReader(
\r
271 new FileReader(jalview.bin.Cache.getProperty("UNIPROT_CACHE")));
\r
273 while ( (data = in.readLine()) != null)
\r
275 if (data.indexOf("name") > -1)
\r
277 String name = parseElement(data, "<name>", null);
\r
278 cachedIds.add(name);
\r
282 catch (Exception ex)
\r
283 { ex.printStackTrace(); }
\r
285 for(int i=0; i<align.getHeight(); i++)
\r
286 if( !cachedIds.contains( align.getSequenceAt(i).getName() ) )
\r
287 unknownSequences.add( align.getSequenceAt(i).getName() );
\r
292 void tryLocalCacheFirst(ArrayList ids, AlignmentI align)
\r
294 ArrayList cacheData = new ArrayList();
\r
296 BufferedReader in = new BufferedReader(
\r
297 new FileReader(jalview.bin.Cache.getProperty("UNIPROT_CACHE")));
\r
299 // read through cache file, if the cache has sequences we're looking for
\r
300 // add the lines to a new String array, Readthis new array and
\r
301 // make sure we remove the ids from the list to retrieve from EBI
\r
303 while( ( data=in.readLine())!=null)
\r
305 if(data.indexOf("name")>-1)
\r
307 String name = parseElement( data, "<name>" , null) ;
\r
308 if(ids.contains( name ) )
\r
310 cacheData.add("<entry>");
\r
311 cacheData.add(data);
\r
312 while( data.indexOf("</entry>")==-1)
\r
314 data = in.readLine();
\r
315 cacheData.add(data);
\r
317 cacheData.add(data);
\r
319 ids.remove( name );
\r
324 catch(Exception ex){ex.printStackTrace();}
\r
326 String [] localData = new String[cacheData.size()];
\r
327 cacheData.toArray( localData );
\r
328 if(localData!=null && localData.length>0)
\r
329 ReadUniprotFile(localData, null, align);
\r
333 String parseValue(String line, String tag, RandomAccessFile out)
\r
336 try{ out.writeBytes(line+"\n"); }catch(Exception ex){}
\r
339 int index = line.indexOf(tag)+tag.length()+1;
\r
340 if(index==tag.length())
\r
343 return line.substring( index, line.indexOf("\"", index+1) );
\r
347 String parseElement(String line, String tag, RandomAccessFile out)
\r
352 out.writeBytes(line + "\n");
\r
354 catch (Exception ex)
\r
357 int index = line.indexOf(tag) + tag.length();
\r
358 return line.substring(index, line.indexOf("</"));
\r