
clear;clc

addpath('/home/k1514724/BCT/2017_01_15_BCT/')

cd /home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/
files=dir('fixnormed*.txt');

strngth(1:length(files),1:90)=zeros;
density(1:length(files),1)=zeros;
clustering_coefficient(1:length(files),1:90)=zeros;
average_clust_coef(1:length(files),1)=zeros;
characteristic_path_length(1:length(files),1)=zeros;
Eglob(1:length(files),1)=zeros;
Eloc(1:length(files),1:90)=zeros;
BC(1:length(files),1:90)=zeros;
Modularity(1:length(files),1)=zeros;
Z(1:length(files),1:90)=zeros;
P(1:length(files),1:90)=zeros;
% r(1:length(files),1)=zeros;

for l=1:length(files)
    
    connection_matrix(1:90,1:90)=zeros;
    files(l).name
    temp1=load(files(l).name);
    connection_matrix(1:90,1:90)=temp1;
    W_fix=weight_conversion(connection_matrix,'autofix');
%   connection_matrix=weight_conversion(W_fix, 'normalize');

    connection_matrix=W_fix;
    connection_matrix(connection_matrix<0)=0;
    clear temp1; clear W_fix;
    
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %strength
    
     strngth(l,:) = (strengths_und(connection_matrix));
     
     density(l,:)=density_und(connection_matrix);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   % clustering coefficient
   
   clustering_coefficient(l,:)=(clustering_coef_wu(connection_matrix))';
   average_clust_coef(l,:)=mean(clustering_coefficient(l,:));
%  clustering_coefficient_nodal(chk,l)=clustering_coef_wu(thresholded_connection_matrix);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   % characteristic path length 

   L=1./connection_matrix;
   L(L==Inf)=0;
   D=mean(distance_wei(L));
   characteristic_path_length(l,:)=charpath(D);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   % global efficiency and local efficiency
   
   Eglob(l,:) = efficiency_wei(connection_matrix);
   Eloc(l,:) = (efficiency_wei(connection_matrix,1))';
%  Eloc_nodal(chk,l) = efficiency_wei(thresholded_connection_matrix,1);
   

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    % betweenness centrality
 BC(l,:)=(betweenness_wei(L))';
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Assortativity
% r(l,1)=assortativity_wei(connection_matrix,0);

% Fine-tuned louvain community detection
  % Iterative community finetuning.
        % W is the input connection matrix.
        W=connection_matrix;
        n  = size(W,1);             % number of nodes
        M  = 1:n;                   % initial community affiliations 
        Q0 = -1; Q1 = 0;            % initialize modularity values
        while Q1-Q0>1e-5;           % while modularity increases
            Q0 = Q1;                % perform community detection
            [M, Q1] = community_louvain(W, [], M);
        end
Modularity(l,1)=Q1;
Z(l,:)=(module_degree_zscore(W,M,0))';% within module degree z-score
P(l,:)= (participation_coef(W,M))';% Participation coefficient
clear M; clear Q1;
end

save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_nodal_strength.txt','strngth','-ascii')
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_global_density.txt','density','-ascii')
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_nodal_cluscoeff.txt','clustering_coefficient','-ascii')
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_average_cluscoeff.txt','average_clust_coef','-ascii');
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_global_charpathlength.txt','characteristic_path_length','-ascii')
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_Eglob.txt','Eglob','-ascii')
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_nodal_efficiency.txt','Eloc','-ascii')
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_nodal_betweenness_centrality.txt','BC','-ascii')
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_nodal_zscore.txt','Z','-ascii')
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_nodal_participation_score.txt','P','-ascii')
save('/home/k1514724/subhadip/BBC/analysis/Parcorr_rsfMRI_Netmats/rsfMRI_modularity.txt','Modularity','-ascii')
