% pdata = serp_analyzer(data);
%
% Plot, analyze and replicate combine function for data provided by
% function serp_read
%
% data  -      output of serp_read
% pdata -      processed data structure for + and - strand
%
% MB 29/04/21

function pdata = serp_analyzer(data);

replicates = size (data(:),1);
colorstr = 'bgrcmykwbgrcmykw';

close all
figure(1); title ('Total + '); xlabel ('Position'); ylabel ('reads'); hold on;
for i = 1:replicates,   
    plot (data(i).total{1,2}(:),data(i).total{1,6}(:),['.' colorstr(i)]);
    idx_zero = find (data(i).total{1,6}(:) == min (data(i).total{1,6}(:)));
    perc_zero = size (idx_zero) / size (data(i).total{1,6}(:) ) * 100;
    % disp (['Replicate ' num2str(i) ' has ' num2str (size  (  data(i).total{1,6}(:) ,1 )) ' positions of which ' num2str(perc_zero) ' % are zero' ]);
    disp (['Total replicate + ' num2str(i) ' has ' num2str(perc_zero) ' % zero' ]);
    if i == 1,
        pdata.plustotal = data(i).total{1,6}(:);
    else pdata.plustotal = pdata.plustotal + data(i).total{1,6}(:);
    end
end
hold off

pdata.plustotal(:) = pdata.plustotal(:) / replicates;
idx_zero = find (pdata.plustotal(:) == min (pdata.plustotal(:)));
perc_zero = size (idx_zero) / size (pdata.plustotal(:) ) * 100;
disp (['Total combined + ' ' has ' num2str(perc_zero) ' % zero' ]);


figure(2); title ('IP + '); xlabel ('Position'); ylabel ('reads'); hold on;
for i = 1:replicates,   
    plot (data(i).IP{1,2}(:),data(i).IP{1,6}(:),['.' colorstr(i)]);
    idx_zero = find (data(i).IP{1,6}(:) == min (data(i).IP{1,6}(:)));
    perc_zero = size (idx_zero) / size (data(i).IP{1,6}(:) ) * 100;
    disp (['IP replicate + ' num2str(i) ' has ' num2str(perc_zero) ' % zero' ]);
    if i == 1,
        pdata.plusIP = data(i).IP{1,6}(:);
    else pdata.plusIP = pdata.plusIP + data(i).IP{1,6}(:);
    end
end
hold off

pdata.plusIP(:) = pdata.plusIP(:) / replicates;
idx_zero = find (pdata.plusIP(:) == min (pdata.plusIP(:)));
perc_zero = size (idx_zero) / size (pdata.plusIP(:) ) * 100;
disp (['IP combined ' ' has ' num2str(perc_zero) ' % zero' ]);

figure (3); title ('Combined + - IP in red - Total in blue'); xlabel ('Position'); ylabel ('reads'); hold on;
plot (data(i).IP{1,2}(:), pdata.plusIP(:),'.b' );
plot (data(i).total{1,2}(:), pdata.plustotal(:),'.r');
hold off

% repeat all of this for minus strand
figure(4); title ('Total - '); xlabel ('Position'); ylabel ('reads'); hold on;
for i = 1:replicates,   
    plot (data(i).total{1,2}(:),data(i).total{1,7}(:),['.' colorstr(i)]);
    idx_zero = find (data(i).total{1,7}(:) == min (data(i).total{1,7}(:)));
    perc_zero = size (idx_zero) / size (data(i).total{1,7}(:) ) * 100;
    % disp (['Replicate ' num2str(i) ' has ' num2str (size  (  data(i).total{1,6}(:) ,1 )) ' positions of which ' num2str(perc_zero) ' % are zero' ]);
    disp (['Total replicate - ' num2str(i) ' has ' num2str(perc_zero) ' % zero' ]);
    if i == 1,
        pdata.minustotal = data(i).total{1,7}(:);
    else pdata.minustotal = pdata.minustotal + data(i).total{1,7}(:);
    end
end
hold off

pdata.minustotal(:) = pdata.minustotal(:) / replicates;
idx_zero = find (pdata.minustotal(:) == min (pdata.minustotal(:)));
perc_zero = size (idx_zero) / size (pdata.minustotal(:) ) * 100;
disp (['Total combined - ' ' has ' num2str(perc_zero) ' % zero' ]);


figure(5); title ('IP - '); xlabel ('Position'); ylabel ('reads'); hold on;
for i = 1:replicates,   
    plot (data(i).IP{1,2}(:),data(i).IP{1,7}(:),['.' colorstr(i)]);
    idx_zero = find (data(i).IP{1,7}(:) == min (data(i).IP{1,7}(:)));
    perc_zero = size (idx_zero) / size (data(i).IP{1,7}(:) ) * 100;
    disp (['IP replicate - ' num2str(i) ' has ' num2str(perc_zero) ' % zero' ]);
    if i == 1,
        pdata.minusIP = data(i).IP{1,7}(:);
    else pdata.minusIP = pdata.minusIP + data(i).IP{1,7}(:);
    end
end
hold off

pdata.minusIP(:) = pdata.minusIP(:) / replicates;
idx_zero = find (pdata.minusIP(:) == min (pdata.minusIP(:)));
perc_zero = size (idx_zero) / size (pdata.minusIP(:) ) * 100;
disp (['IP combined - ' ' has ' num2str(perc_zero) ' % zero' ]);

figure (6); title ('Combined minus - IP in red - Total in blue'); xlabel ('Position'); ylabel ('reads'); hold on;
plot (data(i).IP{1,2}(:), pdata.minusIP(:),'.b' );
plot (data(i).total{1,2}(:), pdata.minustotal(:),'.r');
hold off
