%%% main_small_world: This script produce the graph theoretical analysis
%%% for a specific animal (dataset)

% Clear workspace
clear all; close all;

% Load the propriate biological data for a specific animal 
load('Biological_data/saad16_003');

% Load the representation of the graph, an array that each row represents a 
% connection between the neuron with the id of the first column and the neuron 
% with id of the second column
load('Network_representation/connected_neurons');


% Define the neurons (assign an id)
neurons_id = 1:size(psm_avalanche,2);
% Number of neurons 
dim = size(psm_avalanche,2);

% Create the connection matrix, a cell (m,n) in this matrix takes value 1 when
% there is a connection between neurons with ids m and n.
% Connection matrix represents the network graph based on the signifficant 
% connections of the STTC
for i = 1:size(connected_neurons,1)
    conn_matrix(connected_neurons(i,1),connected_neurons(i,2)) = 1;    
end

% Find the connected components
% Connected component: network that has at least 3 nodes connected, 
% we exclude from the analysis smaller components.
% We apply the graph theoretical and small world analysis on the connected component
conn_comp                = connected_components_v1(conn_matrix,dim);
delete_comp              = find(cell2mat(conn_comp(:,2))<=3);
conn_comp(delete_comp,:) = [];
%
edges_comp         = [];

for i = 1:size(conn_comp,1)
    % get the dimensions and neurons of the connected component
    dim_comp = conn_comp{i,2};
    nodes    = conn_comp{i,3};
    % find the edges of the connected component
    for j = 1:length(nodes)
        edges = find(connected_neurons(:,1)==nodes(j));
        edges_comp = [edges_comp;connected_neurons(edges,:)];
    end
    % make the small-world analysis following Sporns real-based method
    [SWN,SWP,global_CC,global_rand_CC,global_latt_CC,CC,L_real,L_rand,L_latt,L,degree,deltaC,deltaL,degree_rand,degree_latt] = small_world_sporns(edges_comp,dim_comp,nodes);
    
    % make the small-world analysis following Sporns Erdös like and Erdös-Rényi methods
    [SWN_spr,SWN_erd,SWP_spr,SWP_erd,CC_WN_erd,CC_WN_spr1,L_WN_erd,L_WN_spr1,deltaC_erd,deltaC_spr,deltaL_erd,deltaL_spr,global_CC,L_real,CC_latt_erd,L_latt_erd,L_latt_spr,CC_latt_spr,L_rand1_spr,CC_rand1_spr,CC_rand_erd,L_rand_erd,degree_spr_latt,degree_spr_rand,degree_erd_latt,degree_erd_rand,hubs] = small_world_sporns_erdos(edges_comp,dim_comp,nodes,com_x,com_y)
end

%%
% CDFs for the degree of connectivity for the different graphs 
[cdf_degree,x] = ecdf(degree(degree~=0));
[cdf_degree_rand,x_rand] = ecdf(degree_rand(degree_rand~=0));
[cdf_degree_latt,x_latt] = ecdf(degree_latt(degree_latt~=0));
[cdf_degree_rand_spr,x_rand_spr] = ecdf(degree_spr_rand(degree_spr_rand~=0));
[cdf_degree_latt_spr,x_latt_spr] = ecdf(degree_spr_latt(degree_spr_latt~=0));
[cdf_degree_rand_erd,x_rand_erd] = ecdf(degree_erd_rand(degree_erd_rand~=0));
[cdf_degree_latt_erd,x_latt_erd] = ecdf(degree_erd_latt(degree_erd_latt~=0));

% Plot the CDFs
blue = [90 23 139] ./ 255;
orange = [255 153 51]./255;
green = [0 153 0] ./ 255;

figure;
plot(x,cdf_degree,'Color', blue, 'Linewidth', 2);hold on; 
plot(x_rand_erd,cdf_degree_rand_erd,':','Color',orange, 'Linewidth',2);hold on;
plot(x_rand,cdf_degree_rand,'--','Color', orange, 'Linewidth', 2);hold on;
plot(x_rand_spr,cdf_degree_rand_spr,'-.','Color',orange, 'Linewidth',2);hold on;
plot(x_latt_erd,cdf_degree_latt_erd,':','Color', green, 'Linewidth', 2);hold on;
plot(x_latt,cdf_degree_latt,'--', 'Color', green,'Linewidth', 2);hold on;
plot(x_latt_spr,cdf_degree_latt_spr,'-.','Color', green, 'Linewidth', 2);
legend('Real','Random(Erdös-Rényi)','Random(Sporns,real-based)','Random(Sporns,Erdös-like)','Lattice(Erdös Rényi)','Lattice(Sporns,real-based)','Lattice(Sporns,Erdös-like)','location','southeast','FontSize',14);
title('P52-I12');xlabel('DoC');ylabel('CDF');
set(gca,'FontSize',16,'xscale','log','yscale','log');

% Plot CCDFs of the degree of connectivity
figure;
plot(x,1-cdf_degree,'Color', blue, 'Linewidth', 2);hold on; 
plot(x_rand_erd,1-cdf_degree_rand_erd,':','Color',orange, 'Linewidth', 2);hold on;
plot(x_rand,1-cdf_degree_rand,'--','Color', orange, 'Linewidth', 2);hold on;
plot(x_rand_spr,1-cdf_degree_rand_spr,'-.','Color',orange, 'Linewidth',2);hold on;
plot(x_latt_erd,1-cdf_degree_latt_erd,':','Color', green, 'Linewidth', 2);hold on;
plot(x_latt,1-cdf_degree_latt,'--', 'Color', green,'Linewidth', 2);hold on;
plot(x_latt_spr,1-cdf_degree_latt_spr,'.-','Color', green, 'Linewidth', 2);
title('P42-K13');xlabel('DoC');ylabel('CCDF');
set(gca,'FontSize',16);
legend('Real','Random(Erdös-Rényi)','Random(Sporns,real-based)','Random(Sporns,Erdös-like)','Lattice(Erdös Rényi)','Lattice(Sporns,real-based)','Lattice(Sporns,Erdös-like)','location','northeast','FontSize',14);