1 package jalview.analysis;
\r
6 import jalview.gui.*;
\r
7 import jalview.datamodel.*;
\r
9 public class SequenceFeatureFetcher implements Runnable
\r
14 public SequenceFeatureFetcher(AlignmentI align, AlignmentPanel ap)
\r
18 Thread thread = new Thread(this);
\r
25 String cache = jalview.bin.Cache.getProperty("UNIPROT_CACHE");
\r
27 RandomAccessFile out = null;
\r
32 jalview.bin.Cache.setProperty("UNIPROT_CACHE", System.getProperty("user.home/uniprot.xml"));
\r
37 File test = new File(cache);
\r
38 if( !test.exists() )
\r
40 out = new RandomAccessFile(cache, "rw");
\r
41 out.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
\r
42 out.writeBytes("<UNIPROT_CACHE>\n");
\r
46 out = new RandomAccessFile(cache, "rw");
\r
47 // open exisiting cache and remove </UNIPROT_CACHE> from the end
\r
50 while ( (data = out.readLine()) != null)
\r
52 if (data.indexOf("</entry>") > -1)
\r
53 lastLine = out.getFilePointer();
\r
60 Vector sequences = align.getSequences();
\r
62 while (seqIndex < sequences.size())
\r
64 ArrayList ids = new ArrayList();
\r
65 for (int i = 0; seqIndex < sequences.size() && i < 50; seqIndex++, i++)
\r
67 SequenceI sequence = (SequenceI) sequences.get(seqIndex);
\r
68 ids.add(sequence.getName());
\r
71 tryLocalCacheFirst(ids, align);
\r
75 StringBuffer remainingIds = new StringBuffer("uniprot:");
\r
76 for (int i = 0; i < ids.size(); i++)
\r
77 remainingIds.append(ids.get(i) + ";");
\r
79 EBIFetchClient ebi = new EBIFetchClient();
\r
80 String[] result = ebi.fetchData(remainingIds.toString(), "xml", null);
\r
83 ReadUniprotFile(result, out, align);
\r
90 out.writeBytes("</UNIPROT_CACHE>\n");
\r
93 }catch(Exception ex){ex.printStackTrace();}
\r
98 void ReadUniprotFile(String [] result, RandomAccessFile out, AlignmentI align)
\r
100 SequenceI sequence = null;
\r
101 Vector features = null;
\r
102 String type, description, status, start, end, pdb = null;
\r
105 for (int r = 0; r < result.length; r++)
\r
107 if(sequence==null && result[r].indexOf("<name>")>-1)
\r
109 long filePointer = 0;
\r
113 filePointer=out.getFilePointer();
\r
114 out.writeBytes("<entry>\n");
\r
115 }catch(Exception ex){}
\r
117 sequence = align.findName( parseElement( result[r], "<name>" , out)) ;
\r
120 System.out.println("Couldnt find sequence id. Suggestion is "+result[r]);
\r
122 // this entry has been suggested by ebi.
\r
123 // doesn't match id in alignment file
\r
124 try { out.setLength(filePointer); } catch (Exception ex) {}
\r
125 // now skip to next entry
\r
126 while( result[r].indexOf("</entry>")==-1)
\r
130 features = new Vector();
\r
131 type=""; start="0"; end="0"; description=""; status=""; pdb="";
\r
138 if( result[r].indexOf("<property type=\"pdb accession\"")>-1)
\r
140 pdb = parseValue( result[r], "value=" , out);
\r
141 sequence.setPDBId(pdb);
\r
144 if(result[r].indexOf("feature type")>-1)
\r
146 type = parseValue( result[r], "type=" , out);
\r
147 description = parseValue( result[r], "description=" , null );
\r
148 status = parseValue ( result[r], "status=", null);
\r
150 while( result[r].indexOf("position")==-1)
\r
155 if(result[r].indexOf("begin")>-1)
\r
157 start = parseValue( result[r], "position=" , out);
\r
158 end = parseValue( result[++r], "position=" , out);
\r
162 start = parseValue( result[r], "position=" , out);
\r
163 end = parseValue( result[r], "position=" , null);
\r
165 int sstart = Integer.parseInt(start);
\r
166 int eend = Integer.parseInt(end);
\r
168 try{ out.writeBytes("</feature>\n"); }catch(Exception ex){}
\r
171 if(sstart>=sequence.getStart() && eend<=sequence.getEnd())
\r
173 SequenceFeature sf = new SequenceFeature(type,
\r
182 if(result[r].indexOf("</entry>")>-1)
\r
185 sequence.setSequenceFeatures( features );
\r
189 try{ out.writeBytes("</entry>\n"); }catch(Exception ex){}
\r
194 ap.RefreshPanels();
\r
198 void tryLocalCacheFirst(ArrayList ids, AlignmentI align)
\r
200 ArrayList cacheData = new ArrayList();
\r
202 BufferedReader in = new BufferedReader(
\r
203 new FileReader(jalview.bin.Cache.getProperty("UNIPROT_CACHE")));
\r
205 // read through cache file, if the cache has sequences we're looking for
\r
206 // add the lines to a new String array, Readthis new array and
\r
207 // make sure we remove the ids from the list to retrieve from EBI
\r
209 while( ( data=in.readLine())!=null)
\r
211 if(data.indexOf("name")>-1)
\r
213 String name = parseElement( data, "<name>" , null) ;
\r
214 if(ids.contains( name ) )
\r
216 cacheData.add("<entry>");
\r
217 cacheData.add(data);
\r
218 while( data.indexOf("</entry>")==-1)
\r
220 data = in.readLine();
\r
221 cacheData.add(data);
\r
223 cacheData.add(data);
\r
225 ids.remove( name );
\r
230 catch(Exception ex){ex.printStackTrace();}
\r
232 String [] localData = new String[cacheData.size()];
\r
233 cacheData.toArray( localData );
\r
234 if(localData!=null && localData.length>0)
\r
235 ReadUniprotFile(localData, null, align);
\r
239 String parseValue(String line, String tag, RandomAccessFile out)
\r
242 try{ out.writeBytes(line+"\n"); }catch(Exception ex){}
\r
245 int index = line.indexOf(tag)+tag.length()+1;
\r
246 if(index==tag.length())
\r
249 return line.substring( index, line.indexOf("\"", index+1) );
\r
253 String parseElement(String line, String tag, RandomAccessFile out)
\r
258 out.writeBytes(line + "\n");
\r
260 catch (Exception ex)
\r
263 int index = line.indexOf(tag) + tag.length();
\r
264 return line.substring(index, line.indexOf("</"));
\r