clear all
	
%% Plot the example images

SourceFileCell = {...
	'./ExtractedStacks/Cond_1/Image_80.mat',...
	'./ExtractedStacks/Cond_2/Image_6.mat',...
	'./ExtractedStacks/Cond_4/Image_40.mat' 
	};

SourceFileCell{1} = './ExtractedStacks/Cond_1/Image_58.mat';
SourceFileCell{1} = './ExtractedStacks/Cond_1/Image_53.mat';
SourceFileCell{1} = './ExtractedStacks/Cond_1/Image_52.mat';


SourceFileCell{3} = './ExtractedStacks/Cond_4/Image_3.mat';

TitlesCell = {...
    'Control','Flavopiridol','JQ-1'};

DNA_blurRange = 0.06; % micrometers
K27ac_blurRange = 0.01; % micrometers
Ser2P_blurRange = 0.0001; % micrometers
Ser5P_blurRange = 0.01; % micrometers

imgRanges = {...
	[18,36,16,34],...
	[21,41,14,34],...
	[6,24,24,42]};

imgRanges{1} = [31.5,31.5,24,24]+[-1,+1,-1,+1].*8.5;
imgRanges{2} = [31,31,23.5,23.5]+[-1,+1,-1,+1].*8.5;
imgRanges{3} = [18.5,18.5,11.5,11.5]+[-1,+1,-1,+1].*8.5;

zCoordinate = [14,23,23];
rotate_flag = [false,false,false];

scaleBar = 5.0; % in microns


zoomLocations = {...
    [5.1,5.4],...
    [6.5,8.5],...
    [12.8,7.0]};
zoomRange = 4.0; % in microns



numPlots = numel(imgRanges);

figure(1)
clf

for pp = 1:numPlots
	
	pp
	
	thisFilePath = SourceFileCell{pp};
	loadStruct = load(thisFilePath,...
		'imgStack','imgSize','pixelSize','zStepSize');
	imgStack = loadStruct.imgStack;
	imgSize = loadStruct.imgSize;
	pixelSize = loadStruct.pixelSize;
	zStepSize = loadStruct.zStepSize;

	thisImg = imgStack;
	thisPixelSize = pixelSize;
	
	% --- Removal of DNA background
    DNA_img = double(thisImg{1}(:,:,zCoordinate(pp)));
	K27ac_img = double(thisImg{2}(:,:,zCoordinate(pp)));
	Ser5P_img = double(thisImg{3}(:,:,zCoordinate(pp)));
	Ser2P_img = double(thisImg{4}(:,:,zCoordinate(pp)));
	
	if rotate_flag(pp)
        DNA_img = DNA_img';
		K27ac_img = K27ac_img';
		Ser2P_img = Ser2P_img';
		Ser5P_img = Ser5P_img';
    end
    
    DNA_img = imgaussfilt(DNA_img,...
		DNA_blurRange./thisPixelSize);
	K27ac_img = imgaussfilt(K27ac_img,...
		K27ac_blurRange./thisPixelSize);
	Ser5P_img = imgaussfilt(Ser5P_img,...
		Ser5P_blurRange./thisPixelSize);
	Ser2P_img = imgaussfilt(Ser2P_img,...
		Ser2P_blurRange./thisPixelSize);
	
	thisImgRange = round(imgRanges{pp}./thisPixelSize)+1;
	DNA_img = DNA_img(...
		thisImgRange(1):thisImgRange(2),...
		thisImgRange(3):thisImgRange(4));
    K27ac_img = K27ac_img(...
		thisImgRange(1):thisImgRange(2),...
		thisImgRange(3):thisImgRange(4));
	Ser2P_img = Ser2P_img(...
		thisImgRange(1):thisImgRange(2),...
		thisImgRange(3):thisImgRange(4));
	Ser5P_img = Ser5P_img(...
		thisImgRange(1):thisImgRange(2),...
		thisImgRange(3):thisImgRange(4));
	thisSize = size(Ser5P_img);
	
    thisSize
    
    DNA_img = DNA_img./median(DNA_img(:));
    DNA_lims = prctile(DNA_img(:),[30,100]);
    K27ac_img = K27ac_img-prctile(K27ac_img(:),0.01);
	K27ac_lims = [500,12000];
	Ser5P_img = Ser5P_img-0.*prctile(Ser5P_img(:),0.01);
	Ser5P_lims = [600,4500];
    Ser2P_img = Ser2P_img-prctile(Ser2P_img(:),0.01);
    Ser2P_lims = [0,2000];
		
	subplot(4,numPlots.*2,numPlots.*0+pp)
	imagesc([0,thisSize(2)].*thisPixelSize,...
		[0,thisSize(1)].*thisPixelSize,...
		DNA_img,DNA_lims)
	axis equal tight
	set(gca,'XTick',[],'YTick',[])
	set(gca,'YDir','normal')
	hold on
	plot(0.5+[0,scaleBar],thisSize(1).*thisPixelSize-[0.5,0.5],...
		'w-','LineWidth',3) % Plot scale bar
	textObject = ...
		text(0.5+0.5.*scaleBar,thisSize(2).*thisPixelSize-2.0,...
		sprintf('%d \\mum',scaleBar));
	set(textObject,'Color',[1,1,1],'HorizontalAlignment','center',...
		'FontSize',12,'FontName','Helvetica Neue')
	if pp==1
        ylabel('DNA')
    end
    title(TitlesCell{pp},'FontWeight','normal')
    
    subplot(4,numPlots.*2,numPlots.*2.*1+pp)
	imagesc([0,thisSize(2)].*thisPixelSize,...
		[0,thisSize(1)].*thisPixelSize,...
		K27ac_img,K27ac_lims)
	axis equal tight
	set(gca,'XTick',[],'YTick',[])
	set(gca,'YDir','normal')
	hold on
	if pp==1
        ylabel('H3K27ac')
    end
    
    subplot(4,numPlots.*2,numPlots.*2.*2+pp)
	imagesc([0,thisSize(2)].*thisPixelSize,...
		[0,thisSize(1)].*thisPixelSize,...
		Ser5P_img,Ser5P_lims)
	axis equal tight
	set(gca,'XTick',[],'YTick',[])
	set(gca,'YDir','normal')
	hold on
	if pp==1
        ylabel('Pol II Ser5P')
    end
    
    
    subplot(4,numPlots.*2,numPlots.*2.*3+pp)
	imagesc([0,thisSize(2)].*thisPixelSize,...
		[0,thisSize(1)].*thisPixelSize,...
		Ser2P_img,Ser2P_lims)
	axis equal tight
	set(gca,'XTick',[],'YTick',[])
	set(gca,'YDir','normal')
	hold on
	if pp==1
        ylabel('Pol II Ser2P')
    end
    
    
    % Prepare the color channels for RGB plotting
    
    thisZoomLoc = zoomLocations{pp};
    thisImgRange = ...
        [thisZoomLoc(2)+zoomRange.*[-0.5,+0.5],...
        thisZoomLoc(1)+zoomRange.*[-0.5,+0.5]];
    
    for kk = 1:4
        subplot(4,numPlots.*2,numPlots.*2.*(kk-1)+pp)
        plot(thisImgRange([3,4,4,3,3]),...
            thisImgRange([1,1,2,2,1]),...
            'w-','LineWidth',1.5)
    end
    thisImgRange = round(thisImgRange./thisPixelSize)+1;

    DNA_img = DNA_img(...
        thisImgRange(1):thisImgRange(2),...
        thisImgRange(3):thisImgRange(4));
    K27ac_img = K27ac_img(...
        thisImgRange(1):thisImgRange(2),...
        thisImgRange(3):thisImgRange(4));
	Ser2P_img = Ser2P_img(...
		thisImgRange(1):thisImgRange(2),...
		thisImgRange(3):thisImgRange(4));
	Ser5P_img = Ser5P_img(...
		thisImgRange(1):thisImgRange(2),...
		thisImgRange(3):thisImgRange(4));
	thisSize = size(Ser5P_img);

    subplot(4,numPlots.*2,numPlots.*2.*0+numPlots+pp)
	imagesc([0,thisSize(2)].*thisPixelSize,...
		[0,thisSize(1)].*thisPixelSize,...
		DNA_img)
	axis equal tight
	set(gca,'XTick',[],'YTick',[])
	set(gca,'YDir','normal')
    title(TitlesCell{pp},'FontWeight','normal')
    
    
    subplot(4,numPlots.*2,numPlots.*2.*1+numPlots+pp)
	imagesc([0,thisSize(2)].*thisPixelSize,...
		[0,thisSize(1)].*thisPixelSize,...
		K27ac_img)
	axis equal tight
	set(gca,'XTick',[],'YTick',[])
	set(gca,'YDir','normal')

    subplot(4,numPlots.*2,numPlots.*2.*2+numPlots+pp)
	imagesc([0,thisSize(2)].*thisPixelSize,...
		[0,thisSize(1)].*thisPixelSize,...
		Ser5P_img)
	axis equal tight
	set(gca,'XTick',[],'YTick',[])
	set(gca,'YDir','normal')

    subplot(4,numPlots.*2,numPlots.*2.*3+numPlots+pp)
	imagesc([0,thisSize(2)].*thisPixelSize,...
		[0,thisSize(1)].*thisPixelSize,...
		Ser2P_img)
	axis equal tight
	set(gca,'XTick',[],'YTick',[])
	set(gca,'YDir','normal')
    
end

colormap(inferno)