
%This file is written to map raw FRET images to cAMP concnetration images,
%Utilizes Hill equation - so, we should know disscociation constant and hill coefficent before running 
%Make sure, all raw FRET images are sequentially numbered and saved in one folder
%After finished runing, both raw cAMP concentration and colorbar cAMP images are saved. 


if exist('PathName')==1
    OldPathName=PathName;
else
    OldPathName='C:\';
end

warning('off','all')


[FileName,PathName,FilterIndex] = uigetfile('\*.tif','Select first RawFRET Tiff image',OldPathName);
PathName=PathName(1:(length(PathName)-1));
num = length(dir([PathName,'\*.tif']));
props=imfinfo(strcat(PathName,'\',FileName));
M=props.Width;
N=props.Height;

for i = 1:num
   single=imread(strcat(PathName,'\RawFRET_',num2str(i),'.tif'));
   imwrite(single, strcat(PathName,'\Raw_stack.tif'), 'WriteMode', 'append',  'Compression','none');
end

FileNameTif = 'Raw_stack.tif';
PathNameTif = PathName;
% [FileNameTif,PathNameTif,FilterIndexTif]=getFile(strcat(PathName,'Raw_stack.tif'));
InfoImage=imfinfo(strcat(PathNameTif,'\',FileNameTif));
mImage=InfoImage(1).Width;
nImage=InfoImage(1).Height;
NumberImages=length(InfoImage);
FinalImage=zeros(nImage,mImage,NumberImages,'uint16');

[PathStr,FileName,Ext]=fileparts(InfoImage(1).Filename);
output_directory=strcat(PathStr,'\',FileName,'_Output');
mkdir(output_directory);
FalsecoloredImages=strcat(output_directory,'\FalsecoloredImages');
mkdir(FalsecoloredImages);
cAMPRawImages=strcat(output_directory,'\cAMPRawImages');
mkdir(cAMPRawImages);
TifLink = Tiff(strcat(PathNameTif,'\',FileNameTif), 'r');

% Open all pages of multi-page tiff file
for i=1:NumberImages
   TifLink.setDirectory(i);
   FinalImage(:,:,i)=TifLink.read();
end
TifLink.close();

% Convert FRET efficiencies to cAMP concentrations
nn=1; % Hill coefficient (unitless) 
Kd=1; % Dissociation constant (uM)
Kd_GrayScaleEncodingValue=1000; % Number of grayscale levels to encode Kd as (e.g. value of 1 Kd = x number of grayscale levels on unisigned integer images)
cAMP_FinalImage=((Kd./((1./((1-double(FinalImage)./65535)))-1)).^(1./nn))*1000; % Converts FRET image (ranges from 0=0 FRET to 65535=1 FRET (100%)) to cAMP image (ranges from 0=0 nM to 65535=65.535 uM)


% Maximum concentration to consider
cAMP_max_conc=30000; % (nM)
cAMP_FinalImage(cAMP_FinalImage>cAMP_max_conc)=0;
Normalized_cAMP_FinalImage=cAMP_FinalImage/(Kd*1000);
Normalized_cAMP_FinalImage=Normalized_cAMP_FinalImage*Kd_GrayScaleEncodingValue; % Encodes fractions of Kd as Kd*GrayScaleEncodingValue to prevent truncation error when converting to uint16

% Set the minimum and maximum grayscale level to be considered
mingrayscale=0;
maxgrayscale=10000; %Adjust maxgrayscale value accordingly to visulaize gradinets in cAMP concentration images
FalseColoredImage=zeros(nImage,mImage,NumberImages,'uint16');

MaxColorbarLabel=strcat(num2str(maxgrayscale/Kd_GrayScaleEncodingValue),'*Kd');
MidColorbarLabel=strcat(num2str(((maxgrayscale+mingrayscale)/2)/Kd_GrayScaleEncodingValue),'*Kd');
MinColorbarLabel=strcat(num2str(mingrayscale/Kd_GrayScaleEncodingValue),'*Kd');


figure1=figure('Color',[1 1 1]);
newMap = [0 0 0;0 0 0.53125;0 0 0.546875;0 0 0.5625;0 0 0.578125;0 0 0.59375;0 0 0.609375;0 0 0.625;0 0 0.640625;0 0 0.65625;0 0 0.671875;0 0 0.6875;0 0 0.703125;0 0 0.71875;0 0 0.734375;0 0 0.75;0 0 0.765625;0 0 0.78125;0 0 0.796875;0 0 0.8125;0 0 0.828125;0 0 0.84375;0 0 0.859375;0 0 0.875;0 0 0.890625;0 0 0.90625;0 0 0.921875;0 0 0.9375;0 0 0.953125;0 0 0.96875;0 0 0.984375;0 0 1;0 0.015625 1;0 0.03125 1;0 0.046875 1;0 0.0625 1;0 0.078125 1;0 0.09375 1;0 0.109375 1;0 0.125 1;0 0.140625 1;0 0.15625 1;0 0.171875 1;0 0.1875 1;0 0.203125 1;0 0.21875 1;0 0.234375 1;0 0.25 1;0 0.265625 1;0 0.28125 1;0 0.296875 1;0 0.3125 1;0 0.328125 1;0 0.34375 1;0 0.359375 1;0 0.375 1;0 0.390625 1;0 0.40625 1;0 0.421875 1;0 0.4375 1;0 0.453125 1;0 0.46875 1;0 0.484375 1;0 0.5 1;0 0.515625 1;0 0.53125 1;0 0.546875 1;0 0.5625 1;0 0.578125 1;0 0.59375 1;0 0.609375 1;0 0.625 1;0 0.640625 1;0 0.65625 1;0 0.671875 1;0 0.6875 1;0 0.703125 1;0 0.71875 1;0 0.734375 1;0 0.75 1;0 0.765625 1;0 0.78125 1;0 0.796875 1;0 0.8125 1;0 0.828125 1;0 0.84375 1;0 0.859375 1;0 0.875 1;0 0.890625 1;0 0.90625 1;0 0.921875 1;0 0.9375 1;0 0.953125 1;0 0.96875 1;0 0.984375 1;0 1 1;0.015625 1 0.984375;0.03125 1 0.96875;0.046875 1 0.953125;0.0625 1 0.9375;0.078125 1 0.921875;0.09375 1 0.90625;0.109375 1 0.890625;0.125 1 0.875;0.140625 1 0.859375;0.15625 1 0.84375;0.171875 1 0.828125;0.1875 1 0.8125;0.203125 1 0.796875;0.21875 1 0.78125;0.234375 1 0.765625;0.25 1 0.75;0.265625 1 0.734375;0.28125 1 0.71875;0.296875 1 0.703125;0.3125 1 0.6875;0.328125 1 0.671875;0.34375 1 0.65625;0.359375 1 0.640625;0.375 1 0.625;0.390625 1 0.609375;0.40625 1 0.59375;0.421875 1 0.578125;0.4375 1 0.5625;0.453125 1 0.546875;0.46875 1 0.53125;0.484375 1 0.515625;0.5 1 0.5;0.515625 1 0.484375;0.53125 1 0.46875;0.546875 1 0.453125;0.5625 1 0.4375;0.578125 1 0.421875;0.59375 1 0.40625;0.609375 1 0.390625;0.625 1 0.375;0.640625 1 0.359375;0.65625 1 0.34375;0.671875 1 0.328125;0.6875 1 0.3125;0.703125 1 0.296875;0.71875 1 0.28125;0.734375 1 0.265625;0.75 1 0.25;0.765625 1 0.234375;0.78125 1 0.21875;0.796875 1 0.203125;0.8125 1 0.1875;0.828125 1 0.171875;0.84375 1 0.15625;0.859375 1 0.140625;0.875 1 0.125;0.890625 1 0.109375;0.90625 1 0.09375;0.921875 1 0.078125;0.9375 1 0.0625;0.953125 1 0.046875;0.96875 1 0.03125;0.984375 1 0.015625;1 1 0;1 0.984375 0;1 0.96875 0;1 0.953125 0;1 0.9375 0;1 0.921875 0;1 0.90625 0;1 0.890625 0;1 0.875 0;1 0.859375 0;1 0.84375 0;1 0.828125 0;1 0.8125 0;1 0.796875 0;1 0.78125 0;1 0.765625 0;1 0.75 0;1 0.734375 0;1 0.71875 0;1 0.703125 0;1 0.6875 0;1 0.671875 0;1 0.65625 0;1 0.640625 0;1 0.625 0;1 0.609375 0;1 0.59375 0;1 0.578125 0;1 0.5625 0;1 0.546875 0;1 0.53125 0;1 0.515625 0;1 0.5 0;1 0.484375 0;1 0.46875 0;1 0.453125 0;1 0.4375 0;1 0.421875 0;1 0.40625 0;1 0.390625 0;1 0.375 0;1 0.359375 0;1 0.34375 0;1 0.328125 0;1 0.3125 0;1 0.296875 0;1 0.28125 0;1 0.265625 0;1 0.25 0;1 0.234375 0;1 0.21875 0;1 0.203125 0;1 0.1875 0;1 0.171875 0;1 0.15625 0;1 0.140625 0;1 0.125 0;1 0.109375 0;1 0.09375 0;1 0.078125 0;1 0.0625 0;1 0.046875 0;1 0.03125 0;1 0.015625 0;1 0 0;0.984375 0 0;0.96875 0 0;0.953125 0 0;0.9375 0 0;0.921875 0 0;0.90625 0 0;0.890625 0 0;0.875 0 0;0.859375 0 0;0.84375 0 0;0.828125 0 0;0.8125 0 0;0.796875 0 0;0.78125 0 0;0.765625 0 0;0.75 0 0;0.734375 0 0;0.71875 0 0;0.703125 0 0;0.6875 0 0;0.671875 0 0;0.65625 0 0;0.640625 0 0;0.625 0 0;0.609375 0 0;0.59375 0 0;0.578125 0 0;0.5625 0 0;0.546875 0 0;0.53125 0 0;0.515625 0 0;0.5 0 0];
cAMP_Digits = numel(num2str(41)); %jet color bar info is edited to make the first channel completely black
cAMP_Zformatdigits = strcat('%0',num2str(cAMP_Digits),'.0f');

for i=1:NumberImages
  
    FalseColoredFrame=grs2rgb(Normalized_cAMP_FinalImage(:,:,i),newMap,mingrayscale,maxgrayscale); %this calls the function grs2grb, where grayscale image are converted to rgb images based on colormap info provided
    OutputFileName=strcat(FalsecoloredImages,'\',FileName,'_FalseColored_',num2str(i),'.tif');
    imwrite(FalseColoredFrame,OutputFileName);
OutputFileName=strcat(cAMPRawImages,'\',FileName,'_RawcAMP_',num2str(i,cAMP_Zformatdigits),'.tif');
imwrite(uint16(Normalized_cAMP_FinalImage(:,:,i)),OutputFileName);
    
end


FileName = 'Raw_stack_FalseColored_1.tif';

props=imfinfo(strcat(FalsecoloredImages,'\',FileName));

for i = 1:NumberImages
   single=imread(strcat(FalsecoloredImages,'\Raw_stack_FalseColored_',num2str(i),'.tif'));
   imwrite(single, strcat(output_directory,'\Raw_stack_FalseColored.tif'), 'WriteMode', 'append', 'Compression','none');
end


close all