function [image_out,anchorlines] = horizalign(imagex,anchorlines,pointertoaxes);
% 
% Image manipulation of 2D gels, horizontal alignment.
% R. Das, 28 November, 2004.
%

%change for the contrast
ContrastScale = 2;

if exist('pointertoaxes') axes(pointertoaxes);end;
if ~exist('anchorlines') anchorlines=[];end;


[sizey,sizex] = size(imagex);
%Keep center fixed through alignment.
fixpos= round( sizey/2 );

currentaxis = [1 sizex 1 sizey]; 
axis(currentaxis);zoomedin = 0;
stopalign = 0;
plottitle = [...
        'left button: start anchorline       right button (apple-click): zoom in/out \newline',...
        '1,2,C: adjust colorscale        Q: do alignment            Z:finished'];

numanchorlines = size(anchorlines,1);
numlanes = size(imagex,2);
if (numanchorlines>0)
    imagex_align = calculate_alignedprofiles(imagex,anchorlines,fixpos);
else
    imagex_align = imagex;
end

%plot the image!
hold off; image(abs(imagex_align))
maxprof = squeeze(max(max(imagex_align)))/80; 
grayscaleon = 1;
setcolormap(grayscaleon,maxprof);
%set(gca,'xtick',(numfinebins+1)/2:numfinebins:numlanes,'xticklabel',1:numlanes/numfinebins,'xminortick','on');

%If there were predefined anchorlines, draw them too -- as horizontal
%lines.
for k=1: numanchorlines
    hold on ; h_old(k) = plot([1 sizex], [anchorlines(k,fixpos) anchorlines(k,fixpos)],'r'); hold off
end

while (stopalign == 0)
    pickedpeak = 0;
    stopinput = 0; 
    count = 0;
    while (stopinput == 0)
        title(plottitle);
        [xselpick,yselpick,button] = ginput(1);
        switch(button)
            case 1
                [xsel,ysel,pickedpeak,grayscaleon,maxprof] = quickpolyxline(imagex,0,yselpick,0,grayscaleon,maxprof,ContrastScale);
                if (pickedpeak>0) 
                    count = count + 1;
                    alignmentpoints(count,:) = interp1(xsel,ysel,1:sizex);   hold on                                     
                    laneline(count)    = plot(1:sizex,alignmentpoints(count,:),'r');    
                    firstsymbol(count) = plot(1,alignmentpoints(count,1),'ro'); 
                    lastsymbol(count)  = plot(sizex,alignmentpoints(count,sizex),'ro'); hold off
                end    
            case 3        
                if (zoomedin == 1)
                    currentaxis = [1 sizex 1 sizey]; 
                    axis(currentaxis);
                    zoomedin = 0;
                else
                    yselzoom = yselpick;
                    ymin = max(round(yselzoom - sizey/10),0);
                    ymax = min(round(yselzoom + sizey/10),sizey);

                    currentaxis=[1 sizex ymin ymax ];
                    axis(currentaxis);
                    zoomedin = 1;
                end
        end
        switch char(button)
            case '1'
                maxprof = maxprof/ContrastScale;
                setcolormap(grayscaleon, maxprof);    
            case '2'
                maxprof = maxprof*ContrastScale;
                setcolormap(grayscaleon, maxprof);    
            case {'c','C'}
                grayscaleon = 1 - grayscaleon;
                setcolormap(grayscaleon, maxprof);    
            case {'e','E'}
                % There are two kinds of lines that may need to be erased:
                %   already straightened anchorlines and
                %    newly drawn anchorlines ("alignmentpoints"). 
                % They need to be treated separately.
                    
                [closeoldanchor, anchornum] = min( (anchorlines(:,round(xselpick)) - yselpick).^2);
                closenewanchor = sizey^2;
                if (count>0) [closenewanchor, anchornum_new] = min( (alignmentpoints(:,round(xselpick)) - yselpick).^2); end
                if (closeoldanchor < closenewanchor) %old anchorline closer
%                        set(h_old(anchornum),'visible','off');
                        anchorlines = anchorlines([1:anchornum-1  anchornum+1:numanchorlines],:);                        
                        stopinput = 1; %Better show user what happens when you getrid of the old one.    
                    else %new anchorline closer.
                     set(laneline(anchornum_new)   ,'visible','off');
                     set(firstsymbol(anchornum_new),'visible','off');
                     set(lastsymbol(anchornum_new) ,'visible','off');
                     newnumbers = [1: anchornum_new-1   anchornum_new+1:count];    
                     alignmentpoints = alignmentpoints(newnumbers,:);
                     laneline    = laneline(newnumbers);
                     firstsymbol = firstsymbol(newnumbers);
                     lastsymbol  = lastsymbol(newnumbers);
                     count= count-1;
                 end
                
            case {'q','Q'}
                stopinput = 1;
                if (pickedpeak == 0)
                    stopalign = 1;
                end;
            case {'z','Z'}
                stopinput = 1;
                stopalign = 1;
        end
    end
    
    if (count>0)
        anchorlines = newalignmentpoints_to_anchorlines(alignmentpoints, anchorlines, fixpos,sizey);
        alignmentpoints = [];numanchorlines = size(anchorlines,1);
        
        imagex_align = calculate_alignedprofiles(imagex,anchorlines,fixpos);        
        hold off; image(abs(imagex_align)); setcolormap(grayscaleon,maxprof); hold on
        %    set(gca,'xtick',(numfinebins+1)/2:numfinebins:numlanes,'xticklabel',1:numlanes/numfinebins,'xminortick','on');
        axis(currentaxis);
        for k=1: numanchorlines
            hold on ; h_old(k) = plot([1 sizex], [anchorlines(k,fixpos) anchorlines(k,fixpos)],'r'); hold off
        end
    end
    
end

title('Done aligning gel.');
image_out=imagex_align;
