--- /dev/null
+package jalview.analysis;\r
+\r
+import java.io.*;\r
+import java.util.*;\r
+import jalview.io.*;\r
+import jalview.gui.*;\r
+import jalview.datamodel.*;\r
+\r
+public class SequenceFeatureFetcher implements Runnable\r
+{\r
+ AlignmentI align;\r
+ AlignmentPanel ap;\r
+\r
+ public SequenceFeatureFetcher(AlignmentI align, AlignmentPanel ap)\r
+ {\r
+ this.align = align;\r
+ this.ap = ap;\r
+ Thread thread = new Thread(this);\r
+ thread.start();\r
+ }\r
+\r
+ public void run()\r
+{\r
+\r
+ String cache = jalview.bin.Cache.getProperty("UNIPROT_CACHE");\r
+\r
+ RandomAccessFile out = null;\r
+\r
+ try{\r
+ if (cache == null)\r
+ {\r
+ jalview.bin.Cache.setProperty("UNIPROT_CACHE", System.getProperty("user.home/uniprot.xml"));\r
+ }\r
+\r
+\r
+\r
+ File test = new File(cache);\r
+ if( !test.exists() )\r
+ {\r
+ out = new RandomAccessFile(cache, "rw");\r
+ out.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");\r
+ out.writeBytes("<UNIPROT_CACHE>\n");\r
+ }\r
+ else\r
+ {\r
+ out = new RandomAccessFile(cache, "rw");\r
+ // open exisiting cache and remove </UNIPROT_CACHE> from the end\r
+ long lastLine = 0;\r
+ String data;\r
+ while ( (data = out.readLine()) != null)\r
+ {\r
+ if (data.indexOf("</entry>") > -1)\r
+ lastLine = out.getFilePointer();\r
+\r
+ }\r
+ out.seek(lastLine);\r
+ }\r
+\r
+ int seqIndex = 0;\r
+ Vector sequences = align.getSequences();\r
+\r
+ while (seqIndex < sequences.size())\r
+ {\r
+ ArrayList ids = new ArrayList();\r
+ for (int i = 0; seqIndex < sequences.size() && i < 50; seqIndex++, i++)\r
+ {\r
+ SequenceI sequence = (SequenceI) sequences.get(seqIndex);\r
+ ids.add(sequence.getName());\r
+ }\r
+\r
+ tryLocalCacheFirst(ids, align);\r
+\r
+ if (ids.size() > 0)\r
+ {\r
+ StringBuffer remainingIds = new StringBuffer("uniprot:");\r
+ for (int i = 0; i < ids.size(); i++)\r
+ remainingIds.append(ids.get(i) + ";");\r
+\r
+ EBIFetchClient ebi = new EBIFetchClient();\r
+ String[] result = ebi.fetchData(remainingIds.toString(), "xml", null);\r
+\r
+ if(result!=null)\r
+ ReadUniprotFile(result, out, align);\r
+ }\r
+\r
+ }\r
+\r
+ if (out != null)\r
+ {\r
+ out.writeBytes("</UNIPROT_CACHE>\n");\r
+ out.close();\r
+ }\r
+ }catch(Exception ex){ex.printStackTrace();}\r
+\r
+\r
+}\r
+\r
+void ReadUniprotFile(String [] result, RandomAccessFile out, AlignmentI align)\r
+{\r
+ SequenceI sequence = null;\r
+ Vector features = null;\r
+ String type, description, status, start, end, pdb = null;\r
+\r
+\r
+ for (int r = 0; r < result.length; r++)\r
+ {\r
+ if(sequence==null && result[r].indexOf("<name>")>-1)\r
+ {\r
+ long filePointer = 0;\r
+\r
+ if(out!=null)\r
+ try{\r
+ filePointer=out.getFilePointer();\r
+ out.writeBytes("<entry>\n");\r
+ }catch(Exception ex){}\r
+\r
+ sequence = align.findName( parseElement( result[r], "<name>" , out)) ;\r
+ if(sequence==null)\r
+ {\r
+ System.out.println("Couldnt find sequence id. Suggestion is "+result[r]);\r
+\r
+ // this entry has been suggested by ebi.\r
+ // doesn't match id in alignment file\r
+ try { out.setLength(filePointer); } catch (Exception ex) {}\r
+ // now skip to next entry\r
+ while( result[r].indexOf("</entry>")==-1)\r
+ r++;\r
+ }\r
+\r
+ features = new Vector();\r
+ type=""; start="0"; end="0"; description=""; status=""; pdb="";\r
+\r
+ }\r
+\r
+ if(sequence==null)\r
+ continue;\r
+\r
+ if( result[r].indexOf("<property type=\"pdb accession\"")>-1)\r
+ {\r
+ pdb = parseValue( result[r], "value=" , out);\r
+ sequence.setPDBId(pdb);\r
+ }\r
+\r
+ if(result[r].indexOf("feature type")>-1)\r
+ {\r
+ type = parseValue( result[r], "type=" , out);\r
+ description = parseValue( result[r], "description=" , null );\r
+ status = parseValue ( result[r], "status=", null);\r
+\r
+ while( result[r].indexOf("position")==-1)\r
+ {\r
+ r++; //<location>\r
+ }\r
+ // r++;\r
+ if(result[r].indexOf("begin")>-1)\r
+ {\r
+ start = parseValue( result[r], "position=" , out);\r
+ end = parseValue( result[++r], "position=" , out);\r
+ }\r
+ else\r
+ {\r
+ start = parseValue( result[r], "position=" , out);\r
+ end = parseValue( result[r], "position=" , null);\r
+ }\r
+ int sstart = Integer.parseInt(start);\r
+ int eend = Integer.parseInt(end);\r
+ if(out!=null)\r
+ try{ out.writeBytes("</feature>\n"); }catch(Exception ex){}\r
+\r
+\r
+ if(sstart>=sequence.getStart() && eend<=sequence.getEnd())\r
+ {\r
+ SequenceFeature sf = new SequenceFeature(type,\r
+ sstart,\r
+ eend,\r
+ description,\r
+ status);\r
+ features.add(sf);\r
+ }\r
+ }\r
+\r
+ if(result[r].indexOf("</entry>")>-1)\r
+ {\r
+ if(features!=null)\r
+ sequence.setSequenceFeatures( features );\r
+ features = null;\r
+ sequence = null;\r
+ if(out!=null)\r
+ try{ out.writeBytes("</entry>\n"); }catch(Exception ex){}\r
+\r
+ }\r
+ }\r
+\r
+ ap.RefreshPanels();\r
+\r
+}\r
+\r
+void tryLocalCacheFirst(ArrayList ids, AlignmentI align)\r
+{\r
+ ArrayList cacheData = new ArrayList();\r
+ try{\r
+ BufferedReader in = new BufferedReader(\r
+ new FileReader(jalview.bin.Cache.getProperty("UNIPROT_CACHE")));\r
+\r
+ // read through cache file, if the cache has sequences we're looking for\r
+ // add the lines to a new String array, Readthis new array and\r
+ // make sure we remove the ids from the list to retrieve from EBI\r
+ String data;\r
+ while( ( data=in.readLine())!=null)\r
+ {\r
+ if(data.indexOf("name")>-1)\r
+ {\r
+ String name = parseElement( data, "<name>" , null) ;\r
+ if(ids.contains( name ) )\r
+ {\r
+ cacheData.add("<entry>");\r
+ cacheData.add(data);\r
+ while( data.indexOf("</entry>")==-1)\r
+ {\r
+ data = in.readLine();\r
+ cacheData.add(data);\r
+ }\r
+ cacheData.add(data);\r
+\r
+ ids.remove( name );\r
+ }\r
+ }\r
+ }\r
+ }\r
+ catch(Exception ex){ex.printStackTrace();}\r
+\r
+ String [] localData = new String[cacheData.size()];\r
+ cacheData.toArray( localData );\r
+ if(localData!=null && localData.length>0)\r
+ ReadUniprotFile(localData, null, align);\r
+}\r
+\r
+\r
+String parseValue(String line, String tag, RandomAccessFile out)\r
+{\r
+ if(out!=null)\r
+ try{ out.writeBytes(line+"\n"); }catch(Exception ex){}\r
+\r
+\r
+ int index = line.indexOf(tag)+tag.length()+1;\r
+ if(index==tag.length())\r
+ return "";\r
+\r
+ return line.substring( index, line.indexOf("\"", index+1) );\r
+}\r
+\r
+\r
+String parseElement(String line, String tag, RandomAccessFile out)\r
+{\r
+ if (out != null)\r
+ try\r
+ {\r
+ out.writeBytes(line + "\n");\r
+ }\r
+ catch (Exception ex)\r
+ {}\r
+\r
+ int index = line.indexOf(tag) + tag.length();\r
+ return line.substring(index, line.indexOf("</"));\r
+}\r
+\r
+\r
+}\r