% Analysis of co-localization of two factors. Calculate co-localization
% percentage of second factor (gfp) on the first factor's position (cy5). In this function, it is supposed to have the two
% samples (Lane1 and Lane2) which are composed by target sample and control sample (e.g. Cbp80-PrA and Cbp80 no tag). In
% this code, it is assumed that each lane sample has three images and Lane 1 and 2 are the sample of Cbp80-PrA tagged and no tag control for on beads pulldown, respectively. 
% Analyze co-localization in each images and then combine these 3 data and shows result as total Lane1 and
% Lane2. Finally, the co-localization percentage of Lane1 subtracted by
% Lane2 value, which removes background spots number, is calculated.
%% Make the histogram of second factor image intensity on spot position in first factor image (Lane1_1).
% This is to decide the cut off to judge the co-localization based on the
% intensity of AOI at the first factor (Cy5) spots position in the second factor
% (gfp) image.
[fn fp]=uigetfile % Choose the aoifits file of second factor image at first factor position generated by imscroll.
eval(['load ' [fp fn] ' -mat'])
Intensity_Lane1_1_gfp=aoifits.data(:,8);
Size_Lane1_1=size(Intensity_Lane1_1_gfp);
Total_AOI_num_Lane1_1=Size_Lane1_1(1,1);
Hist_colocalization=histogram(Intensity_Lane1_1_gfp,200);
xlabel('Spot intensity','FontSize', 15)
ylabel('Spot number','FontSize',15)
set(gca,'FontSize',12)
title({'Histogram of -mNG image on' 'SNAPf positive position (Lane1-1)'},'FontSize',15)
%% Make the histogram of second factor image intensity on spot position in first factor image (Lane1_2).
% This is to decide the cut off to judge the co-localization based on the
% intensity of AOI at the first factor (Cy5) spots position in the second factor
% (gfp) image.
[fn fp]=uigetfile % Choose the aoifits file of second factor image at first factor position generated by imscroll.
eval(['load ' [fp fn] ' -mat'])
Intensity_Lane1_2_gfp=aoifits.data(:,8);
Size_Lane1_2=size(Intensity_Lane1_2_gfp);
Total_AOI_num_Lane1_2=Size_Lane1_2(1,1);
Hist_colocalization=histogram(Intensity_Lane1_2_gfp,200);
xlabel('Spot intensity','FontSize', 15)
ylabel('Spot number','FontSize',15)
set(gca,'FontSize',12)
title({'Histogram of -mNG image on' 'SNAPf positive position (Lane1-2)'},'FontSize',15)
%% Make the histogram of second factor image intensity on spot position in first factor image (Lane1_3).
% This is to decide the cut off to judge the co-localization based on the
% intensity of AOI at the first factor (Cy5) spots position in the second factor
% (gfp) image.
[fn fp]=uigetfile % Choose the aoifits file of second factor image at first factor position generated by imscroll.
eval(['load ' [fp fn] ' -mat'])
Intensity_Lane1_3_gfp=aoifits.data(:,8);
Size_Lane1_3=size(Intensity_Lane1_3_gfp);
Total_AOI_num_Lane1_3=Size_Lane1_3(1,1);
Hist_colocalization=histogram(Intensity_Lane1_3_gfp,200);
xlabel('Spot intensity','FontSize', 15)
ylabel('Spot number','FontSize',15)
set(gca,'FontSize',12)
title({'Histogram of -mNG image on' 'SNAPf positive position (Lane1-3)'},'FontSize',15)
%% Make the histogram of second factor image intensity on spot position in first factor image (Lane2_1).
% This is to decide the cut off to judge the co-localization based on the
% intensity of AOI at the first factor (Cy5) spots position in the second factor
% (gfp) image.
[fn fp]=uigetfile % Choose the aoifits file of second factor image at first factor position generated by imscroll.
eval(['load ' [fp fn] ' -mat'])
Intensity_Lane2_1_gfp=aoifits.data(:,8);
Size_Lane2_1=size(Intensity_Lane2_1_gfp);
Total_AOI_num_Lane2_1=Size_Lane2_1(1,1);
Hist_colocalization=histogram(Intensity_Lane2_1_gfp,200);
xlabel('Spot intensity','FontSize', 15)
ylabel('Spot number','FontSize',15)
set(gca,'FontSize',12)
title({'Histogram of -mNG image on' 'SNAPf positive position (Lane2-1)'},'FontSize',15)
%% Make the histogram of second factor image intensity on spot position in first factor image (Lane2_2).
% This is to decide the cut off to judge the co-localization based on the
% intensity of AOI at the first factor (Cy5) spots position in the second factor
% (gfp) image.
[fn fp]=uigetfile % Choose the aoifits file of second factor image at first factor position generated by imscroll.
eval(['load ' [fp fn] ' -mat'])
Intensity_Lane2_2_gfp=aoifits.data(:,8);
Size_Lane2_2=size(Intensity_Lane2_2_gfp);
Total_AOI_num_Lane2_2=Size_Lane2_2(1,1);
Hist_colocalization=histogram(Intensity_Lane2_2_gfp,200);
xlabel('Spot intensity','FontSize', 15)
ylabel('Spot number','FontSize',15)
set(gca,'FontSize',12)
title({'Histogram of -mNG image on' 'SNAPf positive position (Lane2-2)'},'FontSize',15)
%% Make the histogram of second factor image intensity on spot position in first factor image (Lane2_3).
% This is to decide the cut off to judge the co-localization based on the
% intensity of AOI at the first factor (Cy5) spots position in the second factor
% (gfp) image.
[fn fp]=uigetfile % Choose the aoifits file of second factor image at first factor position generated by imscroll.
eval(['load ' [fp fn] ' -mat'])
Intensity_Lane2_3_gfp=aoifits.data(:,8);
Size_Lane2_3=size(Intensity_Lane2_3_gfp);
Total_AOI_num_Lane2_3=Size_Lane2_3(1,1);
Hist_colocalization=histogram(Intensity_Lane2_3_gfp,200);
xlabel('Spot intensity','FontSize', 15)
ylabel('Spot number','FontSize',15)
set(gca,'FontSize',12)
title({'Histogram of -mNG image on' 'SNAPf positive position (Lane2-3)'},'FontSize',15)
%% Decide the threshold to judge the co-localization.
% Based on the histgram generated above, manually decide the threshold of
% the intensity for co-localization judgment. Here, the threshold is 9000
% as an example.
Tcol=9000;
%% Judge the co-localization with defined threshold (Lane1_1).
Colocalization_Lane1_1=Intensity_Lane1_1_gfp>Tcol;
Sample1_1=zeros(Total_AOI_num_Lane1_1,1)+1;
Colocalization_list_Lane1_1=[Intensity_Lane1_1_gfp Colocalization_Lane1_1 Sample1_1];
%% Judge the co-localization with defined threshold (Lane1_2). 
Colocalization_Lane1_2=Intensity_Lane1_2_gfp>Tcol;
Sample1_2=zeros(Total_AOI_num_Lane1_2,1)+2;
Colocalization_list_Lane1_2=[Intensity_Lane1_2_gfp Colocalization_Lane1_2 Sample1_2];
%% Judge the co-localization with defined threshold (Lane1_3).
Colocalization_Lane1_3=Intensity_Lane1_3_gfp>Tcol;
Sample1_3=zeros(Total_AOI_num_Lane1_3,1)+3;
Colocalization_list_Lane1_3=[Intensity_Lane1_3_gfp Colocalization_Lane1_3 Sample1_3];
%% Judge the co-localization with defined threshold (Lane2_1).
Colocalization_Lane2_1=Intensity_Lane2_1_gfp>Tcol;
Sample2_1=zeros(Total_AOI_num_Lane2_1,1)+1;
Colocalization_list_Lane2_1=[Intensity_Lane2_1_gfp Colocalization_Lane2_1 Sample2_1];
%% Judge the co-localization with defined threshold (Lane2_2).
Colocalization_Lane2_2=Intensity_Lane2_2_gfp>Tcol;
Sample2_2=zeros(Total_AOI_num_Lane2_2,1)+2;
Colocalization_list_Lane2_2=[Intensity_Lane2_2_gfp Colocalization_Lane2_2 Sample2_2];
%% Judge the co-localization with defined threshold (Lane2_3).
Colocalization_Lane2_3=Intensity_Lane2_3_gfp>Tcol;
Sample2_3=zeros(Total_AOI_num_Lane2_3,1)+3;
Colocalization_list_Lane2_3=[Intensity_Lane2_3_gfp Colocalization_Lane2_3 Sample2_3];
%% Combine the tables (gfp) in each Lane.
Colocalization_list_Lane1_images=[Colocalization_list_Lane1_1;Colocalization_list_Lane1_2;Colocalization_list_Lane1_3];
Colocalization_list_Lane2_images=[Colocalization_list_Lane2_1;Colocalization_list_Lane2_2;Colocalization_list_Lane2_3];
%% Calculate the total spot number and co-localized spot number.
Size_Lane1_total=size(Colocalization_list_Lane1_images);
Total_spotnum_Lane1=Size_Lane1_total(1,1);
Size_Lane2_total=size(Colocalization_list_Lane2_images);
Total_spotnum_Lane2=Size_Lane2_total(1,1);
Colocalize_only_Lane1=Colocalization_list_Lane1_images((Colocalization_list_Lane1_images(:,2)==1),:);
Size_Lane1_colonly=size(Colocalize_only_Lane1);
Colocalized_spotnum_Lane1=Size_Lane1_colonly(1,1);
Colocalize_only_Lane2=Colocalization_list_Lane2_images((Colocalization_list_Lane2_images(:,2)==1),:);
Size_Lane2_colonly=size(Colocalize_only_Lane2);
Colocalized_spotnum_Lane2=Size_Lane2_colonly(1,1);
%% Calculation of percent co-localization from background subtracted data set.
Subtracted_total_spotnum=Total_spotnum_Lane1-Total_spotnum_Lane2;
Subtracted_colocalized_spotnum=Colocalized_spotnum_Lane1-Colocalized_spotnum_Lane2;
Percent_colocalization_subtraction=(Subtracted_colocalized_spotnum./Subtracted_total_spotnum).*100;


