0001 function phylDistStruct=getPhylDist(keggPath,onlyInKingdom)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 if nargin<2
0022 onlyInKingdom=false;
0023 end
0024
0025
0026
0027 [ST I]=dbstack('-completenames');
0028 ravenPath=fileparts(ST(I).file);
0029 distFile=fullfile(ravenPath,'kegg','keggPhylDist.mat');
0030 if exist(distFile, 'file')
0031 fprintf(['NOTE: Importing KEGG phylogenetic distance matrix from ' strrep(distFile,'\','/') '.\n']);
0032 load(distFile);
0033 else
0034
0035 downloadKEGG(keggPath);
0036
0037
0038 fid = fopen(fullfile(keggPath,'taxonomy'), 'r');
0039
0040 phylDistStruct.ids={};
0041
0042
0043 orgCat={};
0044
0045 currentCat={};
0046 depth=0;
0047
0048
0049 orgCounter=0;
0050 while 1
0051
0052 tline = fgetl(fid);
0053
0054
0055 if ~ischar(tline)
0056 break;
0057 end
0058
0059 if any(tline)
0060
0061 if tline(1)=='#'
0062
0063 sPos=strfind(tline,' ')-1;
0064 sPos=sPos(1);
0065
0066
0067 if sPos<depth
0068 currentCat=currentCat(1:sPos);
0069 end
0070 depth=sPos;
0071
0072 currentCat{depth}=tline(sPos+2:end);
0073 else
0074 orgCounter=orgCounter+1;
0075
0076
0077 sPos=find(isstrprop(tline, 'wspace'));
0078 phylDistStruct.ids{orgCounter}=tline(sPos(1)+1:sPos(2)-1);
0079 orgCat{orgCounter}=currentCat;
0080 end
0081 end
0082 end
0083
0084
0085 phylDistStruct.distMat=zeros(numel(phylDistStruct.ids));
0086 for i=1:numel(phylDistStruct.ids)
0087 for j=1:numel(phylDistStruct.ids)
0088 if onlyInKingdom==true
0089 if ~strcmp(orgCat{i}(1),orgCat{j}(1))
0090 phylDistStruct.distMat(i,j)=Inf;
0091 continue;
0092 end
0093 end
0094
0095 dist=numel(orgCat{i})-numel(orgCat{j});
0096 if dist>0
0097 aCat=orgCat{i}(1:end-dist);
0098 else
0099 aCat=orgCat{i};
0100 end
0101 if dist<0
0102 bCat=orgCat{j}(1:end+dist);
0103 else
0104 bCat=orgCat{j};
0105 end
0106
0107
0108 for k=numel(aCat):-1:1
0109 if strcmp(aCat{k},bCat{k})
0110 break;
0111 end
0112 end
0113 phylDistStruct.distMat(i,j)=dist+numel(aCat)-k;
0114 end
0115 end
0116
0117 save(distFile,'phylDistStruct');
0118 end
0119 end