% get order MAIN FUNCTION in determinig leaves order for progressive
% analysis
% Input: Pointers generated from NJ tree analysis.
% Output: Order in which sequences should be analyzed in progresive.
% 11may11
% B

%% Previous: progresive

function leavesorder=orderpointers(pointers)

%% First variables
n=numel(pointers(:,1))+1;
m=n-1;
matrix=pointers;
c=1;
d=1;
leavesorder(c)=pointers(1,1);
c=c+1;
leavesorder(c)=pointers(1,2);
c=c+1;
nodesorder(d)=pointers(1,3);
d=d+1;
z=pointers(1,3);

stopit=pointers(n-1,3);

[y,e,r,addtolistl1,addtolistl2,addtolistn1,addtolistn2]=orderloop(z,leavesorder,nodesorder,matrix,n);

%% Determine ending correction need
[foundpointer1,foundpointer2,a]=findpointer(stopit,matrix);
if foundpointer1<=n || foundpointer2<=n 
flag=1;
else
flag=0;
end

%% Process 
if flag==1;
while numel(leavesorder)~=n-1
%% loop & addtolist   
[y,e,r,addtolistl1,addtolistl2,addtolistn1,addtolistn2]=orderloop(z,leavesorder,nodesorder,matrix,n);
[leavesorder,nodesorder,c,d,z]=additnow(leavesorder,nodesorder,addtolistl1,addtolistn1,addtolistl2,addtolistn2,c,d,y,e,r,n);
end
%% correct ending if needed
[foundpointer1,foundpointer2,a]=findpointer(stopit,matrix);
if foundpointer1<=n 
leavesorder(c)=foundpointer1;
c=c+1;
end
if foundpointer2<=n
leavesorder(c)=foundpointer2;
end
 else
while numel(leavesorder)~=n
%% loop & addtolist   
[y,e,r,addtolistl1,addtolistl2,addtolistn1,addtolistn2]=orderloop(z,leavesorder,nodesorder,matrix,n);
[leavesorder,nodesorder,c,d,z]=additnow(leavesorder,nodesorder,addtolistl1,addtolistn1,addtolistl2,addtolistn2,c,d,y,e,r,n);
end
end
    



end
    
    
   


    
        

