function select_ts_mutants_v4(source_plate_format,target_plate_format)

% Fred Cross June 29, 2016. The Rockefeller University, New York, NY.

%Code assumes digital photographs of plates, 96,384,1536, or 6144 array,
%stored in a single directory. Images alternate alphabetically 21 then 33
%for each source. First image should be grid-plate - RED dotted one.

% file management go to data_home to find subdirectory for source and target files. data_home is
% preferred directory to contain these but this is not required

close all;

global data_home

data_home='/usr/local/MATLAB/alignment from Olympus camera';

global data_directory;

global source_plate;
global target_plate;
global target_row;
global target_col;
global keep_picking;

global corners_x;
global corners_y;
global ninespots;
global fid_out;

global ID;
global fid_info;

global target_format;
global source_plate_name;

global source_format;

source_format=source_plate_format;
target_format=target_plate_format;


source_format=source_plate_format;

if source_plate_format==100
    source_format=384;
    source_plate_format=384;
end

target_format=target_plate_format;

load std_corners_x;
load std_corners_y;

fid_out=[];




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% select image files - should be stored in image_files directory. The
%%%% 21,33 pairs MUST BE in numerical (alphabetical) order

%if images already processed can reload mat file (much quicker)

got_directory=0;
while ~got_directory
    data_directory=uigetdir(data_home)
    data_directory_contents = dir(data_directory)
    
    image_sel_mode=menu('Image selection',sprintf('Load new images from %s',data_directory),sprintf('Re-load previously processed image set from %s',data_directory),...
        'Change directory','Quit');
    
    
    
    if image_sel_mode==1
        
        image_filenames={};
        for k=1:length(data_directory_contents)
            if length(data_directory_contents(k).name)> 3
                if strcmp(data_directory_contents(k).name(end-2:end),'JPG') | strcmp(data_directory_contents(k).name(end-2:end),'jpg')
                    image_filenames{length(image_filenames)+1}= data_directory_contents(k).name;
                end
            end
        end
        
        %provide an image of a plate with 9 spots (4 corners and 5 centers)
        try
            [gridindex,v] = listdlg('PromptString','Select grid image:','SelectionMode','single','ListString',image_filenames);
        catch
            disp('could not find files');
            return
        end
        %get plate images, alt 21,33
        sel=0;
        
        while ~sel
            if v
                gridimage_filename=[data_directory,'/',image_filenames{gridindex}];
                
                %    disp(image_filenames{gridindex});
                
            else
                gridimage_filename=[];
            end
            [plate_indices,v] = listdlg('PromptString','Select plate images (alt 21,33):','SelectionMode','multiple','ListString',image_filenames);
            
            if ~v
                continue
            end
            sel=1;
        end
        got_directory=1;
        
    elseif image_sel_mode==2
        %reload previously processed plate images
        mat_filenames={};
        for k=1:length(data_directory_contents)
            if length(data_directory_contents(k).name)> 3
                if strcmp(data_directory_contents(k).name(end-2:end),'mat')
                    mat_filenames{length(mat_filenames)+1}= data_directory_contents(k).name;
                end
            end
        end
        [matindex,v] = listdlg('PromptString','Select mat file:','SelectionMode','single','ListString',mat_filenames);
        sel=0;
        
        while ~sel
            if v
                mat_filename=[data_directory,'/',mat_filenames{matindex}];
            else
                mat_filename=[];
            end
            try
                load(mat_filename);
            catch
                disp('no mat file selected');
                return
            end
            if ~v
                continue
            end
            sel=1;
        end
        got_directory=1;
    elseif image_sel_mode==3
        continue
    else
        disp('no images selected');
        return
    end
    
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% MAKE ID
prompt = {'project ID'};
num_lines = 1;
date_s=date;
date_s(date_s=='-')='_';
def = {[date_s,'_picks']};
answer = inputdlg(prompt,'INPUT',num_lines,def);

try
    project_ID_base=answer{1};
    
    n_prev=length(dir([data_directory,'/',project_ID_base,'*']));
    
    
    ID=[project_ID_base,'_',num2str(n_prev+1)];
    
    mkdir(ID);
    
    infofilename=[data_directory,'/',ID,'_info.txt'];
    fid_info=fopen(infofilename,'w');
catch
    return
end

if image_sel_mode==1
    [grid_image,corners_x,corners_y,bestangle]=make_grid_v4(gridimage_filename,source_plate_format);
    corners_x=round(corners_x);
    corners_y=round(corners_y);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Process image files

%%%collect and process plate images; move originals to project directory;
%save processed images in .mat file in proj directory


if image_sel_mode==1
    
    plate_images=collect_plate_images_v4(image_filenames,plate_indices,bestangle);
    
%     for image_index=1:length(plate_indices)
%         
%         movefile(['image_files/',image_filenames{plate_indices(image_index)}],ID);
%     end
%     movefile(gridimage_filename,ID);
    
    save([data_directory,'/',ID,'_plate_images'],'grid_image','plate_images','ID','corners_x','corners_y','ninespots','-v7.3');
    
    close all;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Select ts candidates
if image_sel_mode==1
    fprintf(fid_info,'Project %s, using plate images %s - %s\n\n____________________________________________\n\n',ID,image_filenames{plate_indices(1)},image_filenames{plate_indices(2)});
else
    fprintf(fid_info,'Project %s, using stored images in %s\n\n____________________________________________\n\n',ID,mat_filename);
end

n_images=length(plate_images);

n_pairs=(n_images/2);

%if target_format==100
%    target_row=3;  %flag for first plate going into 10x10 square in 384 array
%else
target_row=0;
%end
keep_picking=1;
for pair_index=1:n_pairs
    if ~keep_picking
        break
    end
    close all;
    im21=plate_images{(2* (pair_index-1)+1)};
    im33=plate_images{(2* pair_index)};
    
    source_plate=pair_index;
    
    fprintf('Selecting ts candidates from plate %d of project %s\n\n',source_plate,ID);
    figure(1);clf;subplot(2,1,1);imagesc(im21);colormap gray;subplot(2,1,2);imagesc(im33);colormap gray;
    
    %%%save plate name and info
    prompt = {'Plate_name','Plate_info'};
    num_lines = 2;
    dlg_title=sprintf('PLATE %d NAME and INFO',pair_index);
    def = {sprintf('Source_%d',pair_index),sprintf('%s',ID)};
    answer = inputdlg(prompt,dlg_title,num_lines,def);
    %     try
    %         close(plate_message_box);
    %     catch
    %     end
    if isempty(answer)
        source_plate_name=def{1};
        source_plate_info=def{2};
    else
        source_plate_name=answer{1};
        source_plate_info=answer{2};
    end
    %     plate_message_box=msgbox(source_plate_name,sprintf('PLATE %d',pair_index));
    %     set(plate_message_box,'Position', [  1372         908         589          66]);
    %
    
    fprintf(fid_info,'Plate %d proj %s \n%s\n%s\n',source_plate,ID,source_plate_name,source_plate_info);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%PICK:
    
    fprintf('Picking from source plate %d, %s %s\n',pair_index,source_plate_name,source_plate_info);
    pick_ts_cands_v5(im21,im33);
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%Clean up
fprintf('Done picking ts candidates for project %s\n\n',ID);
try
    close(plate_message_box);
catch
end
close all;
fclose all;
close all hidden;