function []=FLIM_StemCell(name_file,THR_BG,THR_M,THR_mito,median_filter)
%name_file: file name
%THR_BG: threshold of intensity to remove background
%THR_M: Threshold of modulation to separate lipid and NADH
%THR_mito: Threshold of intensity to separate NADH in mitochondria and nucleus+cytoplasm
%-----------------------------load FFT data from SimFCS-------------------------------------
close all;
T1=Open_R64_auto;%load .R64 data
T=T1(~cellfun('isempty', T1));[m k]=size(T);
n=k;
for i=1:k
%%%------------------------------------------------------------------------import raw data-------------------------------------------
I{i}=flip(rot90(T{i}(:,:,1))); 
P{i}=flip(rot90(T{i}(:,:,2)));
M{i}=flip(rot90(T{i}(:,:,3)));
%%------------------------------------------------------------------------some constant values--------------------
Nharm=1 ;% harmonic number
TT= 1000/(Nharm*80);% period expressed in ns that correspont to the laser frequancy repetition
om=2*pi/TT;
g_freeNADH= 1/(1+(om*0.4)^2);s_freeNADH= om*0.4/(1+(om*0.4)^2);
tauref = 0:0.05:1000;
gdata=1./(1+(tauref*om).^2);sdata=(tauref*om)./(1+(tauref*om).^2);
g_3ns= 1/(1+(om*3)^2);s_3ns=om*3/(1+(om*3)^2);
g_4ns= 1/(1+(om*4)^2);s_4ns=om*4/(1+(om*4)^2);
%%-----------------------------------------------------------create median filter and calculate g and s---------------------------------------------------
Mask_I{i}=I{i}>THR_BG;%remove background
I1{i}=medfilt2(I{i},median_filter);I1{i}=medfilt2(I1{i},median_filter);I1{i}=I1{i}.*Mask_I{i};
A{i}=I1{i}(I1{i}>0);[m1{i} n1{i}]=size(A{i});
P1{i}=medfilt2(P{i},median_filter);P1{i}=medfilt2(P1{i},median_filter);P1{i}=P1{i}.*Mask_I{i};
M1{i}=medfilt2(M{i},median_filter);M1{i}=medfilt2(M1{i},median_filter);M1{i}=M1{i}.*Mask_I{i};
g{i}=M1{i}.*cos(P1{i}.*pi/180);s{i}=M1{i}.*sin(P1{i}.*pi/180);%calculate g and s
dlmwrite([char(name_file),'_g_ROI' num2str(i) '.txt'],g{i});%save g in *.text files
dlmwrite([char(name_file),'_s_ROI' num2str(i) '.txt'],s{i});%save s in *.text files
figure; 
binscatter(g{i}(:),s{i}(:),250);colormap(jet);hold on;xlim([0 1]);ylim([0 1]);
plot(gdata,sdata,'-k','LineWidth',0.5);hold on;xlabel('g','fontsize',16);ylabel('s','fontsize',16);
saveas (gcf,['Phasor_plot_all_ROI' num2str(i) '.fig']);
% plot Intensity
figure;
title('Intensity','fontsize',16);set(gcf, 'Position',  [100, 100, 1500, 500]);
subplot(1,2,1);imagesc(I{i});title(strcat('Raw Intensity ',num2str(i)),'fontsize',16);colorbar;colormap(jet);caxis([40 1000]);
set(gca,'FontSize',16);
subplot(1,2,2);imagesc(I1{i});title(strcat('Intensity median filter ',num2str(i)),'fontsize',16);colorbar;colormap(jet);caxis([40 1000]);
set(gca,'FontSize',16);
saveas (gcf,[char(name_file),'_Intensity_' num2str(i) '.fig']);
dlmwrite([char(name_file),'_Intensity_' num2str(i) '.txt'],I1{i});
dlmwrite([char(name_file),'_Raw_Intensity_' num2str(i) '.txt'],I{i});
%plot Phase 
figure;title('Phi','fontsize',16);set(gcf, 'Position',  [100, 100, 1500, 500]);
subplot(1,2,1);imagesc(P{i});title(strcat('Raw phi ',num2str(i)),'fontsize',16);colormap(jet);colorbar;caxis([10 80]);
set(gca,'FontSize',16);
subplot(1,2,2);imagesc(P1{i});title(strcat('Phi median filter ',num2str(i)),'fontsize',16);colormap(jet);colorbar;caxis([10 80]);
set(gca,'FontSize',16);
saveas (gcf,[char(name_file),'_Phi ' num2str(i) '.fig']);
dlmwrite([char(name_file),'_P_' num2str(i) '.txt'],P1{i});
dlmwrite([char(name_file),'_raw_P_' num2str(i) '.txt'],P{i});
%plot Modulation  
figure;title('Modulation','fontsize',16);set(gcf, 'Position',  [100, 100, 1500, 500]);
subplot(1,2,1);imagesc(M{i});title(strcat('Raw Modul ',num2str(i)),'fontsize',16);colormap(jet);colorbar;caxis([0.1 1.4]);
set(gca,'FontSize',16);
subplot(1,2,2);imagesc(M1{i});title(strcat('Modul median filter ',num2str(i)),'fontsize',16);colormap(jet);colorbar;caxis([0.1 1.4]);
set(gca,'FontSize',16);
saveas (gcf,[char(name_file),'_modul ' num2str(i) '.fig']);
dlmwrite([char(name_file),'_M_' num2str(i) '.txt'],M1{i});
dlmwrite([char(name_file),'_raw_M_' num2str(i) '.txt'],M{i});
%%plot all pixels
figure;set(gcf, 'Position',  [100, 100, 1500, 500]);
g_raw{i}=M{i}.*cos(P{i}.*pi/180);s_raw{i}=M{i}.*sin(P{i}.*pi/180);%g and s without median filter and without removing the background
subplot(1,2,1);
plot(gdata,sdata,'-k','LineWidth',0.5);hold on;xlabel('g','fontsize',16);ylabel('s','fontsize',16);
plot(g_freeNADH,s_freeNADH,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);label1 = {'0.4 ns'};
text(g_freeNADH,s_freeNADH,label1,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label4 = {'3 ns'};
plot(g_3ns,s_3ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_3ns,s_3ns,label4,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label5 = {'4 ns'};
plot(g_4ns,s_4ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);%hold on;
text(g_4ns,s_4ns,label5,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');
plot( g_raw{i},s_raw{i},'.','MarkerSize',1,'Color','black');title('raw data','fontsize',16);set(gca,'FontSize',16);
xlim([0 1]);ylim([0 0.5]);set(gca,'FontSize',16); 
subplot(1,2,2);
plot(gdata,sdata,'-k','LineWidth',0.5);hold on;xlabel('g','fontsize',16);ylabel('s','fontsize',16);
plot(g_freeNADH,s_freeNADH,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);label1 = {'0.4 ns'};
text(g_freeNADH,s_freeNADH,label1,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label4 = {'3 ns'};
plot(g_3ns,s_3ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_3ns,s_3ns,label4,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label5 = {'4 ns'};
plot(g_4ns,s_4ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);%hold on;
text(g_4ns,s_4ns,label5,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');
plot(g{i},s{i},'.','MarkerSize',1,'Color','black');title('median filter','fontsize',16);set(gca,'FontSize',16);
xlim([0 1]);ylim([0 0.5]);set(gca,'FontSize',16); 
saveas (gcf,[char(name_file),'_phasor plot ROI' num2str(i) '.fig']);
mean_s_raw{i}=mean(s_raw{i}(s_raw{i}>0));std_s_raw{i}=std(s_raw{i}(s_raw{i}>0));
mean_g_raw{i}=mean(g_raw{i}(g_raw{i}>0));std_g_raw{i}=std(g_raw{i}(g_raw{i}>0));
mean_s{i}=mean(s{i}(s{i}>0));std_s{i}=std(s{i}(s{i}>0));SE_s{i}=std_s{i}/sqrt(m1{i});
mean_g{i}=mean(g{i}(g{i}>0));std_g{i}=std(g{i}(g{i}>0));SE_g{i}=std_s{i}/sqrt(m1{i});
mean_P_raw{i}=mean(P{i}(P{i}>0));std_P_raw{i}=std(P{i}(P{i}>0));
mean_M_raw{i}=mean(M{i}(M{i}>0));std_M_raw{i}=std(M{i}(M{i}>0));
mean_P_median{i}=mean(P1{i}(P1{i}>0));std_P_median{i}=std(P1{i}(P1{i}>0));
mean_M_median{i}=mean(M1{i}(M1{i}>0));std_M_median{i}=std(M1{i}(M1{i}>0));
mean_I_raw{i}=mean(I{i}(I{i}>0));std_I_raw{i}=std(I{i}(I{i}>0));
mean_I_median{i}=mean(I1{i}(I1{i}>0));std_I_median{i}=std(I1{i}(I1{i}>0));
figure;
histogram(I{i},80);hold on; histogram(I1{i}(I1{i}>0),80);legend('raw','median filter');
title(strcat({['I'];['means raw =',num2str(mean_I_raw{i}), 'std raw=',num2str(std_I_raw{i})];['means median=',num2str(mean_I_median{i}), 'std median=',num2str(std_I_median{i})]}),'fontsize',14);
figure (i+5*k);set(gcf, 'Position',  [100, 100, 1500, 500]);
subplot(1,2,1); histogram(s_raw{i},80);hold on; histogram(s{i},80);legend('raw','median filter');set(gca,'FontSize',16); 
title(strcat({['s'];['means raw =',num2str(mean_s_raw{i}), 'std raw=',num2str(std_s_raw{i})];['means median=',num2str(mean_s{i}), 'std median=',num2str(std_s{i})]}),'fontsize',14);
subplot(1,2,2);histogram(g_raw{i},80);hold on; histogram(g{i},80);legend('raw','median filter');set(gca,'FontSize',16); 
title(strcat({['g'];['means raw=',num2str(mean_g_raw{i}), 'std raw=',num2str(std_g_raw{i})];['means median=',num2str(mean_g{i}), 'std median=',num2str(std_g{i})]}),'fontsize',14);
figure (i+6*k);set(gcf, 'Position',  [100, 100, 1500, 500]);
subplot(1,2,1); histogram(P{i},80);hold on; histogram(P1{i}(P1{i}>0),80);legend('raw','median filter');set(gca,'FontSize',16); 
title(strcat({['P'];['means raw=',num2str(mean_P_raw{i}), 'std raw=',num2str(std_P_raw{i})];['means median=',num2str(mean_P_median{i}), 'std median=',num2str(std_P_median{i})]}),'fontsize',14);
subplot(1,2,2);histogram(M{i},80);hold on; histogram(M1{i}(M1{i}>0),80);legend('raw','median filter');set(gca,'FontSize',16); 
title(strcat({['M'];['means raw=',num2str(mean_M_raw{i}), 'std raw=',num2str(std_M_raw{i})];['means median=',num2str(mean_M_median{i}), 'std median=',num2str(std_M_median{i})]}),'fontsize',14);
set(gca,'FontSize',16);   
   tP_all{i}=(1/om).*(tan(P1{i}.*pi/180));%tau phi
   mean_tP{i}=mean(mean(tP_all{i}));std_tP{i}=std(tP_all{i}(tP_all{i}>0));SE_tP{i}=std_tP{i}/sqrt(m1{i});
   figure;imagesc(tP_all{i});title(strcat('tauP ',num2str(i)),'fontsize',16);colormap;colorbar;set(gca,'FontSize',16);
   saveas (gcf,['tP_all_' num2str(i) '.fig'])
   dlmwrite([char(name_file),'_tP_all_' num2str(i) '.txt'],tP_all{i});
   tM_all{i}=(1/om).*sqrt(1./M1{i}.^2-1);%tau modulation
    mean_tM{i}=mean(mean(tM_all{i}));std_tM{i}=std(tM_all{i}(tM_all{i}>0));SE_tM{i}=std_tM{i}/sqrt(m1{i});
   figure;imagesc(tM_all{i});title(strcat('tauM ',num2str(i)),'fontsize',16);colormap;colorbar;set(gca,'FontSize',16);
   saveas (gcf,['tM_all_' num2str(i) '.fig'])
   dlmwrite([char(name_file),'_tM_all_' num2str(i) '.txt'], tM_all{i});
   fB_all{i}=sqrt(((g{i}-g_freeNADH)).^2+((s{i}-s_freeNADH)).^2);%calculate fB 
   dlmwrite([char(name_file),'_fB_all_' num2str(i) '.txt'], fB_all{i});
   mean_fB_all{i}=mean(mean(fB_all{i}(fB_all{i}<0.98)));std_fB{i}=std(fB_all{i}(fB_all{i}<0.98));%mean and std value of fB
   SE_fB{i}=std_fB{i}/sqrt(m1{i});
% %%-------------------------------------------------------------- lipid droplets segmentation--------------------------------------------
Mask_lipid{i}=M1{i}<THR_M;%lipid mask
dlmwrite([char(name_file),'_mask_lipid_' num2str(i) '.txt'], Mask_lipid{i});
I_lipid{i}=I1{i}.*Mask_lipid{i};%Intensity of lipid
B{i}=I1{i}(I1{i}>0);[m2{i} n2{i}]=size(B{i});
P_lipid{i}=P1{i}.*Mask_lipid{i};%Phase of lipid
M_lipid{i}=M1{i}.*Mask_lipid{i};%Modulation of lipid
tP_lipid{i}=(1/om).*(tan(P_lipid{i}.*pi/180));%calculate tP
tM_lipid{i}=(1/om).*sqrt(1./M_lipid{i}.^2-1);tM_lipid{i}(tM_lipid{i}==Inf)=0;% calculate tM and remove the Inf value 
g_lipid{i}=M_lipid{i}.*cos(P_lipid{i}.*pi/180); mean_g_lipid{i}=mean(mean(g_lipid{i}(g_lipid{i}~=0))); std_g_lipid{i}=std(g_lipid{i}(g_lipid{i}~=0));
s_lipid{i}=M_lipid{i}.*sin(P_lipid{i}.*pi/180);mean_s_lipid{i}=mean(mean(s_lipid{i}(s_lipid{i}~=0))); std_s_lipid{i}=std(s_lipid{i}(s_lipid{i}~=0));
mean_tP_lipid{i}=mean(mean(tP_lipid{i}(tP_lipid{i}>0)));std_tP_lipid{i}=std(tP_lipid{i}(tP_lipid{i}>0)); SE_tP_lipid{i}=std_tP_lipid{i}/sqrt(m2{i});%mean and std value of tP
mean_tM_lipid{i}=mean(mean(tM_lipid{i}(tM_lipid{i}>0)));std_tM_lipid{i}=std(tM_lipid{i}(tM_lipid{i}>0)); SE_tM_lipid{i}=std_tP_lipid{i}/sqrt(m2{i}); %mean and std value of tM

dlmwrite([char(name_file),'_I_Lipid_' num2str(i) '.txt'], I_lipid{i});
dlmwrite([char(name_file),'_g_Lipid_' num2str(i) '.txt'], g_lipid{i});
dlmwrite([char(name_file),'_s_Lipid_' num2str(i) '.txt'], s_lipid{i});
dlmwrite([char(name_file),'_P_Lipid_' num2str(i) '.txt'], P_lipid{i});
dlmwrite([char(name_file),'_M_Lipid_' num2str(i) '.txt'], M_lipid{i});
dlmwrite([char(name_file),'_tP_Lipid_' num2str(i) '.txt'], tP_lipid{i});
dlmwrite([char(name_file),'_tM_Lipid_' num2str(i) '.txt'], tM_lipid{i});

 figure; 
 imagesc(I_lipid{i});%plot intensity lipid
 title(strcat('Intensity lipid',num2str(i)),'fontsize',16);colormap(jet);colorbar;caxis([0 1000]);set(gca,'FontSize',16);
 figure;%plot P lipid
 imagesc(P_lipid{i});title(strcat('P lipid ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
 figure;%plot M lipid
 imagesc(M_lipid{i});title(strcat('M lipid ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16); 
 figure;%plot tP lipid
 imagesc(tP_lipid{i});title(strcat('tP lipid ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
 figure;%plot tM lipid
 imagesc(tM_lipid{i});title(strcat('tM lipid ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
% phasor plot of lipid
figure ;title(strcat('Phasor plot Lipid ',num2str(i)),'fontsize',16);hold on;
th = 0:pi/50:pi/2;
xunit1 = THR_M * cos(th);
yunit1 = THR_M * sin(th);
h1 = plot(xunit1, yunit1);hold on;hold on;
plot(gdata,sdata,'-k','LineWidth',0.5);hold on;xlabel('g','fontsize',16);ylabel('s','fontsize',16);
plot(g_freeNADH,s_freeNADH,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);label1 = {'0.4 ns'};
text(g_freeNADH,s_freeNADH,label1,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label4 = {'3 ns'};
plot(g_3ns,s_3ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_3ns,s_3ns,label4,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label5 = {'4 ns'};
plot(g_4ns,s_4ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_4ns,s_4ns,label5,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');
binscatter(g_lipid{i}(:),s_lipid{i}(:),250);colormap(jet);
xlim([0 1]);ylim([0 1]);
saveas (gcf,['g-s_lipid_' num2str(i) '.fig']);
% %%--------------------------------------------------------------segmentation NADH--------------------------------------------
 Mask_NADH{i}=M1{i}>THR_M; %mask for NADH 
 dlmwrite([char(name_file),'_mask_NADH_' num2str(i) '.txt'], Mask_NADH{i});
 I_NADH{i}=I1{i}.*Mask_NADH{i};
 P_NADH{i}=P1{i}.*Mask_NADH{i};%Phase of NADH
M_NADH{i}=M1{i}.*Mask_NADH{i};%Modulation of NADH
tP_NADH{i}=(1/om).*(tan(P_NADH{i}.*pi/180));%calculate tP
tM_NADH{i}=(1/om).*sqrt(1./M_NADH{i}.^2-1);tM_NADH{i}(tM_NADH{i}==Inf)=0;% calculate tM and remove the Inf value 
g_NADH{i}=M_NADH{i}.*cos(P_NADH{i}.*pi/180); mean_g_NADH{i}=mean(mean(g_NADH{i}(g_NADH{i}~=0))); std_g_NADH{i}=std(g_NADH{i}(g_NADH{i}~=0));
s_NADH{i}=M_NADH{i}.*sin(P_NADH{i}.*pi/180); mean_s_NADH{i}=mean(mean(s_NADH{i}(s_NADH{i}~=0))); std_s_NADH{i}=std(s_NADH{i}(s_NADH{i}~=0));
mean_tP_NADH{i}=mean(mean(tP_NADH{i}(tP_NADH{i}>0)));std_tP_NADH{i}=std(tP_NADH{i}(tP_NADH{i}>0));%mean and std value of tP
mean_tM_NADH{i}=mean(mean(tM_NADH{i}(tM_NADH{i}>0)));std_tM_NADH{i}=std(tM_NADH{i}(tM_NADH{i}>0));%mean and std value of tM
fB_NADH{i}=sqrt(((g_NADH{i}-g_freeNADH)).^2+((s_NADH{i}-s_freeNADH)).^2);%calculate fB NADH
mean_fB_NADH{i}=mean(mean(fB_NADH{i}(fB_NADH{i}<0.98)));std_fB_NADH{i}=std(fB_NADH{i}(fB_NADH{i}<0.98));%mean and std value of fB
dlmwrite([char(name_file),'_fB_NADH_' num2str(i) '.txt'], fB_NADH{i});
dlmwrite([char(name_file),'_I_NADH_' num2str(i) '.txt'], I_NADH{i});
dlmwrite([char(name_file),'_g_NADH_' num2str(i) '.txt'], g_NADH{i});
dlmwrite([char(name_file),'_s_NADH_' num2str(i) '.txt'], s_NADH{i});
dlmwrite([char(name_file),'_P_NADH_' num2str(i) '.txt'], P_NADH{i});
dlmwrite([char(name_file),'_M_NADH_' num2str(i) '.txt'], M_NADH{i});
dlmwrite([char(name_file),'_tP_NADH_' num2str(i) '.txt'], tP_NADH{i});
dlmwrite([char(name_file),'_tM_NADH_' num2str(i) '.txt'], tM_NADH{i});
dlmwrite([char(name_file),'_fB_NADH_' num2str(i) '.txt'], fB_NADH{i});

 figure; 
 imagesc(I_NADH{i});%plot intensity NADH
 title(strcat('Intensity NADH',num2str(i)),'fontsize',16);colormap(jet);colorbar;caxis([0 1000]);set(gca,'FontSize',16);
 figure;%plot P NADH
 imagesc(P_NADH{i});title(strcat('P NADH ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
 figure;%plot M NADH
 imagesc(M_NADH{i});title(strcat('M NADH ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16); 
 figure;%plot tP NADH
 imagesc(tP_NADH{i});title(strcat('tP NADH ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
 figure;%plot tM NADH
 imagesc(tM_NADH{i});title(strcat('tM NADH ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
% phasor plot of NADH
figure ;title(strcat('Phasor plot NADH ',num2str(i)),'fontsize',16);hold on;
th = 0:pi/50:pi/2;
xunit1 = THR_M * cos(th);
yunit1 = THR_M * sin(th);
h1 = plot(xunit1, yunit1);hold on;hold on;
plot(gdata,sdata,'-k','LineWidth',0.5);hold on;xlabel('g','fontsize',16);ylabel('s','fontsize',16);
plot(g_freeNADH,s_freeNADH,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);label1 = {'0.4 ns'};
text(g_freeNADH,s_freeNADH,label1,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label4 = {'3 ns'};
plot(g_3ns,s_3ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_3ns,s_3ns,label4,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label5 = {'4 ns'};
plot(g_4ns,s_4ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_4ns,s_4ns,label5,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');
binscatter(g_lipid{i}(:),s_lipid{i}(:),250);colormap(jet);
xlim([0 1]);ylim([0 1]);
saveas (gcf,['g-s_NADH_' num2str(i) '.fig']);
% %---------------------------------------------------------phasor plot all pixels--------------------------------
figure;
th = 0:pi/50:pi/2;
xunit1 = THR_M * cos(th);
yunit1 = THR_M * sin(th);
h1 = plot(xunit1, yunit1);hold on;
xunit2 = THR_M* cos(th);
yunit2 = THR_M * sin(th);
h2 = plot(xunit2, yunit2);hold on;
xlim([0 1]);ylim([0 1]);
plot(gdata,sdata,'-k','LineWidth',0.5);hold on;xlabel('g','fontsize',16);ylabel('s','fontsize',16);
plot(g_freeNADH,s_freeNADH,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);label1 = {'0.4 ns'};
text(g_freeNADH,s_freeNADH,label1,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label4 = {'3 ns'};
plot(g_3ns,s_3ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_3ns,s_3ns,label4,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label5 = {'4 ns'};
plot(g_4ns,s_4ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);%hold on;
text(g_4ns,s_4ns,label5,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');
plot( g_lipid{i},s_lipid{i},'.','MarkerSize',1,'Color','black');hold on;
plot( g_NADH{i},s_NADH{i},'.','MarkerSize',1,'Color', [0.5 0.5 0.5]);hold on;
title(strcat('phasor plot ',num2str(i)),'fontsize',16);hold on;
saveas (gcf,[char(name_file),'_phasor_plot_' num2str(i) '.fig']);
% 
% %---------------------------------------------------------------------number of pixel between lipid and NADH------------------------------------------------------
pixel_lipid{i}=sum(sum(tP_lipid{i}(tP_lipid{i}~=0)));
pixel_NADH{i}=sum(sum(tP_NADH{i}(tP_NADH{i}~=0)));
r{i}=pixel_lipid{i}/(pixel_NADH{i}+ pixel_lipid{i})*100;

% %---------------------------------------------------------------------plot mean g_s of every ROI------------------------------------------------------
 figure;
title('Phasor plot','fontsize',16);hold on;
plot(gdata,sdata,'-k','LineWidth',0.5);hold on;xlabel('g','fontsize',16);ylabel('s','fontsize',16);set(gca,'FontSize',16); 
plot(g_freeNADH,s_freeNADH,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);label1 = {'0.4 ns'};
text(g_freeNADH,s_freeNADH,label1,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label4 = {'3 ns'};
plot(g_3ns,s_3ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);%hold on;
text(g_3ns,s_3ns,label4,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label5 = {'4 ns'};
plot(g_4ns,s_4ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);%hold on;
text(g_4ns,s_4ns,label5,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');
errorbar(mean_g_lipid{i},mean_s_lipid{i},std_s_lipid{i},std_s_lipid{i}, std_g_lipid{i}, std_g_lipid{i},'.','MarkerSize',30,'MarkerEdgeColor','blue'); hold on;
errorbar(mean_g_NADH{i},mean_s_NADH{i},std_s_NADH{i},std_s_NADH{i}, std_g_NADH{i}, std_g_NADH{i},'.','MarkerSize',30,'MarkerEdgeColor','black'); hold on;
plot( mean_g_lipid{i},mean_s_lipid{i},'.','MarkerSize',30,'Color','blue');hold on;
plot( mean_g_NADH{i},mean_s_NADH{i},'.','MarkerSize',30,'Color','black');
xlim([0 1]);ylim([0 1]);hold on;legend('lipid','ROI');hold on;
saveas (gcf,[char(name_file),'_g_s.fig']);%saveas (gcf,[char(name_file),'_g_s.jpg']); 
% 
% %--------------------------------------------------------------Mitochondria segmentation --------------------------------------------
Mask_mito{i}=I_NADH{i}>THR_mito;
dlmwrite([char(name_file),'_mask_mitochondria_' num2str(i) '.txt'],Mask_mito{i});
I_mito{i}=I1{i}.*Mask_mito{i};
P_mito{i}=P1{i}.*Mask_mito{i};%Phase of mito
M_mito{i}=M1{i}.*Mask_mito{i};%Modulation of mito
tP_mito{i}=(1/om).*(tan(P_mito{i}.*pi/180));%calculate tP
tM_mito{i}=(1/om).*sqrt(1./M_mito{i}.^2-1);tM_mito{i}(tM_mito{i}==Inf)=0;% calculate tM and remove the Inf value 
g_mito{i}=M_mito{i}.*cos(P_mito{i}.*pi/180); mean_g_mito{i}=mean(mean(g_mito{i}(g_mito{i}~=0))); std_g_mito{i}=std(g_mito{i}(g_mito{i}~=0));
s_mito{i}=M_mito{i}.*sin(P_mito{i}.*pi/180); mean_s_mito{i}=mean(mean(s_mito{i}(s_mito{i}~=0))); std_s_mito{i}=std(s_mito{i}(s_mito{i}~=0));
mean_tP_mito{i}=mean(mean(tP_mito{i}(tP_mito{i}>0)));std_tP_mito{i}=std(tP_mito{i}(tP_mito{i}>0));%mean and std value of tP
mean_tM_mito{i}=mean(mean(tM_mito{i}(tM_mito{i}>0)));std_tM_mito{i}=std(tM_mito{i}(tM_mito{i}>0));%mean and std value of tM
fB_mito{i}=sqrt(((g_mito{i}-g_freeNADH)).^2+((s_mito{i}-s_freeNADH)).^2);%calculate fB mito
mean_fB_mito{i}=mean(mean(fB_mito{i}(fB_mito{i}<0.98)));std_fB_mito{i}=std(fB_mito{i}(fB_mito{i}<0.98));%mean and std value of fB
dlmwrite([char(name_file),'_fB_mito_' num2str(i) '.txt'], fB_mito{i});
dlmwrite([char(name_file),'_I_mito_' num2str(i) '.txt'], I_mito{i});
dlmwrite([char(name_file),'_g_mito_' num2str(i) '.txt'], g_mito{i});
dlmwrite([char(name_file),'_s_mito_' num2str(i) '.txt'], s_mito{i});
dlmwrite([char(name_file),'_P_mito_' num2str(i) '.txt'], P_mito{i});
dlmwrite([char(name_file),'_M_mito_' num2str(i) '.txt'], M_mito{i});
dlmwrite([char(name_file),'_tP_mito_' num2str(i) '.txt'], tP_mito{i});
dlmwrite([char(name_file),'_tM_mito_' num2str(i) '.txt'], tM_mito{i});
dlmwrite([char(name_file),'_fB_mito_' num2str(i) '.txt'], fB_mito{i});

 figure; 
 imagesc(I_mito{i});%plot intensity mito
 title(strcat('Intensity mito',num2str(i)),'fontsize',16);colormap(jet);colorbar;caxis([0 1000]);set(gca,'FontSize',16);
 figure;%plot P mito
 imagesc(P_mito{i});title(strcat('P mito ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
 figure;%plot M mito
 imagesc(M_mito{i});title(strcat('M mito ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16); 
 figure;%plot tP mito
 imagesc(tP_mito{i});title(strcat('tP mito ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
 figure;%plot tM mito
 imagesc(tM_mito{i});title(strcat('tM mito ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
% phasor plot of mito
figure ;title(strcat('Phasor plot mito ',num2str(i)),'fontsize',16);hold on;
plot(gdata,sdata,'-k','LineWidth',0.5);hold on;xlabel('g','fontsize',16);ylabel('s','fontsize',16);
plot(g_freeNADH,s_freeNADH,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);label1 = {'0.4 ns'};
text(g_freeNADH,s_freeNADH,label1,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label4 = {'3 ns'};
plot(g_3ns,s_3ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_3ns,s_3ns,label4,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label5 = {'4 ns'};
plot(g_4ns,s_4ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_4ns,s_4ns,label5,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');
binscatter(g_mito{i}(:),s_mito{i}(:),250);colormap(jet);
xlim([0 1]);ylim([0 1]);
saveas (gcf,['g-s_mito_' num2str(i) '.fig']);
% %--------------------------------------------------------------Nuclei and Cytoplasm segmentation--------------------------------------------
Mask_nuclei_cyto{i}=Mask_NADH{i}-Mask_mito{i};
dlmwrite([char(name_file),'_mask_nuclei_cyto_' num2str(i) '.txt'],Mask_nuclei_cyto{i});
I_nuclei_cyto{i}=I1{i}.*Mask_nuclei_cyto{i};
P_nuclei_cyto{i}=P1{i}.*Mask_nuclei_cyto{i};%Phase of nuclei_cyto
M_nuclei_cyto{i}=M1{i}.*Mask_nuclei_cyto{i};%Modulation of nuclei_cyto
tP_nuclei_cyto{i}=(1/om).*(tan(P_nuclei_cyto{i}.*pi/180));%calculate tP
tM_nuclei_cyto{i}=(1/om).*sqrt(1./M_nuclei_cyto{i}.^2-1);tM_nuclei_cyto{i}(tM_nuclei_cyto{i}==Inf)=0;% calculate tM and remove the Inf value 
g_nuclei_cyto{i}=M_nuclei_cyto{i}.*cos(P_nuclei_cyto{i}.*pi/180); mean_g_nuclei_cyto{i}=mean(mean(g_nuclei_cyto{i}(g_nuclei_cyto{i}~=0))); std_g_nuclei_cyto{i}=std(g_nuclei_cyto{i}(g_nuclei_cyto{i}~=0));
s_nuclei_cyto{i}=M_nuclei_cyto{i}.*sin(P_nuclei_cyto{i}.*pi/180); mean_s_nuclei_cyto{i}=mean(mean(s_nuclei_cyto{i}(s_nuclei_cyto{i}~=0))); std_s_nuclei_cyto{i}=std(s_nuclei_cyto{i}(s_nuclei_cyto{i}~=0));
mean_tP_nuclei_cyto{i}=mean(mean(tP_nuclei_cyto{i}(tP_nuclei_cyto{i}>0)));std_tP_nuclei_cyto{i}=std(tP_nuclei_cyto{i}(tP_nuclei_cyto{i}>0));%mean and std value of tP
mean_tM_nuclei_cyto{i}=mean(mean(tM_nuclei_cyto{i}(tM_nuclei_cyto{i}>0)));std_tM_nuclei_cyto{i}=std(tM_nuclei_cyto{i}(tM_nuclei_cyto{i}>0));%mean and std value of tM
fB_nuclei_cyto{i}=sqrt(((g_nuclei_cyto{i}-g_freeNADH)).^2+((s_nuclei_cyto{i}-s_freeNADH)).^2);%calculate fB nuclei_cyto
mean_fB_nuclei_cyto{i}=mean(mean(fB_nuclei_cyto{i}(fB_nuclei_cyto{i}<0.98)));std_fB_nuclei_cyto{i}=std(fB_nuclei_cyto{i}(fB_nuclei_cyto{i}<0.98));%mean and std value of fB
dlmwrite([char(name_file),'_fB_nuclei_cyto_' num2str(i) '.txt'], fB_nuclei_cyto{i});
dlmwrite([char(name_file),'_I_nuclei_cyto_' num2str(i) '.txt'], I_nuclei_cyto{i});
dlmwrite([char(name_file),'_g_nuclei_cyto_' num2str(i) '.txt'], g_nuclei_cyto{i});
dlmwrite([char(name_file),'_s_nuclei_cyto_' num2str(i) '.txt'], s_nuclei_cyto{i});
dlmwrite([char(name_file),'_P_nuclei_cyto_' num2str(i) '.txt'], P_nuclei_cyto{i});
dlmwrite([char(name_file),'_M_nuclei_cyto_' num2str(i) '.txt'], M_nuclei_cyto{i});
dlmwrite([char(name_file),'_tP_nuclei_cyto_' num2str(i) '.txt'], tP_nuclei_cyto{i});
dlmwrite([char(name_file),'_tM_nuclei_cyto_' num2str(i) '.txt'], tM_nuclei_cyto{i});
dlmwrite([char(name_file),'_fB_nuclei_cyto_' num2str(i) '.txt'], fB_nuclei_cyto{i});

 figure; 
 imagesc(I_nuclei_cyto{i});%plot intensity nuclei_cyto
 title(strcat('Intensity nuclei_cyto',num2str(i)),'fontsize',16);colormap(jet);colorbar;caxis([0 1000]);set(gca,'FontSize',16);
 figure;%plot P nuclei_cyto
 imagesc(P_nuclei_cyto{i});title(strcat('P nuclei_cyto ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
 figure;%plot M nuclei_cyto
 imagesc(M_nuclei_cyto{i});title(strcat('M nuclei_cyto ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16); 
 figure;%plot tP nuclei_cyto
 imagesc(tP_nuclei_cyto{i});title(strcat('tP nuclei_cyto ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
 figure;%plot tM nuclei_cyto
 imagesc(tM_nuclei_cyto{i});title(strcat('tM nuclei_cyto ',num2str(i)),'fontsize',16);colormap(gray);colorbar;set(gca,'FontSize',16);
% phasor plot of nuclei_cyto
figure ;title(strcat('Phasor plot nuclei_cyto ',num2str(i)),'fontsize',16);hold on;
plot(gdata,sdata,'-k','LineWidth',0.5);hold on;xlabel('g','fontsize',16);ylabel('s','fontsize',16);
plot(g_freeNADH,s_freeNADH,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);label1 = {'0.4 ns'};
text(g_freeNADH,s_freeNADH,label1,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label4 = {'3 ns'};
plot(g_3ns,s_3ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_3ns,s_3ns,label4,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');label5 = {'4 ns'};
plot(g_4ns,s_4ns,'--ks','LineWidth',2,'MarkerFaceColor','k','MarkerSize',4);
text(g_4ns,s_4ns,label5,'FontSize', 12,'VerticalAlignment','bottom','HorizontalAlignment','right');
binscatter(g_nuclei_cyto{i}(:),s_nuclei_cyto{i}(:),250);colormap(jet);
xlim([0 1]);ylim([0 1]);
saveas (gcf,['g-s_nuclei_cyto_' num2str(i) '.fig']);

ROI_number{i}=['ROI' num2str(i)];
 end
ROIs_number=[ROI_number].';
g_all=[g{i}];s_all=[s{i}];
[a,b]=size(g_all);
g_all=reshape(g_all,[a*b,1]);s_all=reshape(s_all,[a*b,1]);
tP_lipid_combine= [tP_lipid{i}];
tP_NADH_combine= [tP_NADH{i}];
tM_lipid_combine= [tM_lipid{i}];
tM_NADH_combine= [tM_NADH{i}]; 
%----save lipid data
Values_lipid_rario= [r].';
Values_g_lipid= [mean_g_lipid].';Values_STDg_lipid= [std_g_lipid].';
Values_s_lipid= [mean_s_lipid].';Values_STDs_lipid= [std_s_lipid].';
Values_tP_lipid= [mean_tP_lipid].';
Values_tM_lipid= [mean_tM_lipid].';
T1 = table(ROIs_number,Values_g_lipid,Values_STDg_lipid,Values_s_lipid,Values_STDs_lipid,Values_tP_lipid,Values_tM_lipid,Values_lipid_rario);
%writetable(T1,[char(name_file) '_lipid.txt']);
writetable(T1,[char(name_file) '_lipid.xls'],'Sheet',1);
%---save NADH data
Values_g_NADH= [mean_g_NADH].';Values_STDg_NADH= [std_g_NADH].';
Values_s_NADH= [mean_s_NADH].';Values_STDs_NADH= [std_s_NADH].';
Values_fB_NADH= [mean_fB_NADH].';
Values_tP_NADH= [mean_tP_NADH].';
Values_tM_NADH= [mean_tM_NADH].';
T2 = table(ROIs_number,Values_g_NADH,Values_STDg_NADH,Values_s_NADH,Values_STDs_NADH,Values_tP_NADH,Values_tM_NADH,Values_fB_NADH);
%writetable(T2,[char(name_file) '_NADH.txt']);
writetable(T2,[char(name_file) '_NADH.xls'],'Sheet',1);
%---save mitochondria data
Values_g_mito= [mean_g_mito].';Values_STDg_mito= [std_g_mito].';
Values_s_mito= [mean_s_mito].';Values_STDs_mito= [std_s_mito].';
Values_fB_mito= [mean_fB_mito].';
Values_tP_mito= [mean_tP_mito].';
Values_tM_mito= [mean_tM_mito].';
T3 = table(ROIs_number,Values_g_mito,Values_STDg_mito,Values_s_mito,Values_STDs_mito,Values_tP_mito,Values_tM_mito,Values_fB_mito);
%writetable(T3,[char(name_file) '_mito.txt']);
writetable(T3,[char(name_file) '_mito.xls'],'Sheet',1);
%---save nuclei and cytoplasm data
Values_g_nuclei_cyto= [mean_g_nuclei_cyto].';Values_STDg_nuclei_cyto= [std_g_nuclei_cyto].';
Values_s_nuclei_cyto= [mean_s_nuclei_cyto].';Values_STDs_nuclei_cyto= [std_s_nuclei_cyto].';
Values_fB_nuclei_cyto= [mean_fB_nuclei_cyto].';
Values_tP_nuclei_cyto= [mean_tP_nuclei_cyto].';
Values_tM_nuclei_cyto= [mean_tM_nuclei_cyto].';
T4 = table(ROIs_number,Values_g_nuclei_cyto,Values_STDg_nuclei_cyto,Values_s_nuclei_cyto,Values_STDs_nuclei_cyto,Values_tP_nuclei_cyto,Values_tM_nuclei_cyto,Values_fB_nuclei_cyto);
%writetable(T4,[char(name_file) '_nuclei_cyto.txt']);
writetable(T4,[char(name_file) '_nuclei_cyto.xls'],'Sheet',1);
%---save threshold
ValuesNames_1={'THR_M_lipid';'THR_BG';'THR_mito'};
Values_1=[THR_M;THR_BG;THR_mito];
T5= table(ValuesNames_1,Values_1);
writetable(T5,[char(name_file) '_Threshold.txt']);
%----

end
%end