% Retrieve and analyze informative clusters

function [ClustersAlasopa,ClustersBlasopa,JA,JmaxA,JB,JmaxB]=Comparetreesnodes(treeA,treeB)

% Retrieve Informative Clusters
[ClustersAlasopa,ClustersAlasopapos,ClustersBlasopa,ClustersBlasopapos,TreeInfoA,TreeInfoB]=GetClusters(treeA,treeB);


% TREE A 

%Get the tree info
TreeInfoA=get(treeA);
%Get the tree matrix
[MATA, IDA, DISTA] = getmatrix(treeA);
for i=1:TreeInfoA.NumLeaves
    IDSortA{i}=IDA{i};
end
[IDSortA IDXA]=sort(IDSortA);
[jefesA chilpallatesA kA]=find(MATA);
%We extract the route from each node to the root

for i=1:TreeInfoA.NumLeaves
    j=1;
    kA=i;
    while(kA<TreeInfoA.NumNodes)
        ListaA(i,j)=jefesA(kA);
        kA=ListaA(i,j);
        j=j+1;
    end
end
% Get JMAX & J (MAXIMUM JERARQUIA & JERARQUIA BY NODE)
JmaxA=numel(ListaA(1,:))-2;

token=0;
z=1;
r=ClustersAlasopapos(z);
ClustersAlasopanods(numel(ClustersAlasopapos))=zeros;
for z=1:numel(ClustersAlasopapos)
    token=0;
    r=ClustersAlasopapos(z);
    for i=1:numel(ListaA(:,1)) 
        
        for j=1:numel(ListaA(1,:))
            
            tocompare1=(TreeInfoA.Pointers(r,1));
            tocompare2=(TreeInfoA.Pointers(r,2));
            
            if tocompare1==ListaA(i,j) || tocompare2==ListaA(i,j)
                token=j;
              if ClustersAlasopanods(z)>token
                ClustersAlasopanods(z)=ClustersAlasopanods(z);
              elseif ClustersAlasopanods(z)<token
                      ClustersAlasopanods(z)=token;
                  else
                  ClustersAlasopanods(z)=token;
              end
            end
        end
    end
end

JA=ClustersAlasopanods;

% TREE B 

%Get the tree info
TreeInfoB=get(treeB);
%Get the tree matrix
[MATB, IDB, DISTB] = getmatrix(treeB);
for i=1:TreeInfoB.NumLeaves
    IDSortB{i}=IDB{i};
end
[IDSortB IDXB]=sort(IDSortB);
[jefesB chilpallatesB kB]=find(MATB);
%We extract the route from each node to the root

for i=1:TreeInfoB.NumLeaves
    j=1;
    kB=i;
    while(kB<TreeInfoB.NumNodes)
        ListaB(i,j)=jefesB(kB);
        kB=ListaB(i,j);
        j=j+1;
    end
end
% Get JMAX & J (MAXIMUM JERARQUIA & JERARQUIA BY NODE)
JmaxB=numel(ListaB(1,:))-2;

token=0;
z=1;
r=ClustersBlasopapos(z);
ClustersBlasopanods(numel(ClustersBlasopapos))=zeros;
for z=1:numel(ClustersBlasopapos)
    token=0;
    r=ClustersBlasopapos(z);
    for i=1:numel(ListaB(:,1)) 
        
        for j=1:numel(ListaB(1,:))
            
            tocompare1=(TreeInfoB.Pointers(r,1));
            tocompare2=(TreeInfoB.Pointers(r,2));
            
            if tocompare1==ListaB(i,j) || tocompare2==ListaB(i,j)
                token=j;
              if ClustersBlasopanods(z)>token
                ClustersBlasopanods(z)=ClustersBlasopanods(z);
              elseif ClustersBlasopanods(z)<token
                      ClustersBlasopanods(z)=token;
                  else
                  ClustersBlasopanods(z)=token;
              end
            end
        end
    end
end

JB=ClustersBlasopanods;

end

