% Tool to compare Phylogenetic Trees

function [NodeCount, IDSort]=GetRouteToRoot(Tree)
%Get the tree info
TreeInfo=get(Tree);
%Get the tree matrix
[MAT, ID, DIST] = getmatrix(Tree);
for i=1:TreeInfo.NumLeaves
    IDSort{i}=ID{i};
end
[IDSort IDX]=sort(IDSort);
[jefes chilpallates k]=find(MAT);
%We extract the route from each live to the root

for i=1:TreeInfo.NumLeaves
    j=1;
    k=i;
    while(k<TreeInfo.NumNodes)
        Lista(i,j)=jefes(k);
        k=Lista(i,j);
        j=j+1;
    end
end
%For each pair of sequences count the number of nodes between them

for i=1:TreeInfo.NumLeaves
    for j=i:TreeInfo.NumLeaves       
         Ref=Lista(i,:);
         Com=Lista(j,:);
        for ii=1:size(Lista,2)
            found=false;
            for jj=1:size(Lista,2)
                if (Ref(ii)==Com(jj))                
                    found=true;
                    break
                end
            end
            if (found)
                NodeCount(i,j)=ii+jj-1;
                 break
            end
        end
    end
end

%Get the hole matrix

NodeCount=NodeCount+NodeCount'-diag(ones(1,size(NodeCount,1)));

%Sort the Node count with respect to the IDs alphabetically
temp1=zeros(size(NodeCount));
temp2=zeros(size(NodeCount));
for i=1:size(NodeCount,1)    
        temp1(i,:)=NodeCount(IDX(i),:);
end
for i=1:size(NodeCount,1)    
        temp2(:,i)=temp1(:,IDX(i));
end
NodeCount=temp2;
         

 
