% This script plots all the clusters 

% Partition Matrix processing with a cut of threshold to find in which
% cluster belongs each data row. Each gene, belongs to a cluster if
% ikanopoiei the threshold value. Also each gene could belongs to polu se 3
% clusters

% INPUTS
u = U';
c = V;
% Input percent threshold for clusters. Default value = 0.3
thresh = 0.3;
% Input again time-points because modified during FuDESTiS implementation
ts = 0:size(data,2)-1;

% Construct mcidx matrix with the cluster indexes of each gene. 
[Ngenes, Nc] = size(u);
u_sort = zeros(size(u));
index_matrix = zeros(size(u));
uvals_matrix = zeros(size(u));
mcidx = zeros(size(u));

% Descend sort in U matrix to obtain cluster indexes in firts columns
for i=1:Ngenes
    u_sort(i,:) = u(i,:);
    [uvals, uindex] = sort(u_sort(i,:),'descend');
    index_matrix(i,:) = uindex;
    uvals_matrix(i,:) = uvals;
    mcidx(i,uindex(1)) = uindex(1);
end

% Each gene could belong to a maximun 3 different clusters. 
for i = 1:Ngenes
    if uvals_matrix(i,2) > thresh
        mcidx(i,index_matrix(i,2)) = index_matrix(i,2);
    end
    if uvals_matrix(i,3) > thresh
        mcidx(i,index_matrix(i,3)) = index_matrix(i,3);
    end
end

% Check Test for membership function
count = zeros(Ngenes,1);
for i = 1:Ngenes
    for j=1:Nc
        if mcidx(i,j)~= 0
            count(i) = count(i)+1;
        end
    end
end  
       
% Plotting
for k = 1:best_Ncluster
    figure;
    plot(ts, data((mcidx(:,k)==k),:),'Color',[0.7, 0.7, 0.7] )
end




