
% STEP 6: Isoform Validation

% The focus of Step 6 is facilitate the visual validation / rejection of isoforms that were 'automatically' identified by Steps 1 through 5.  We want to ensure that the polony-calling
% algorithm is not inaccurately claiming polonies with a specific rare signature that are not actually polonies but instead border-elements or junk (e.g. false positives).  This semi-
% automated process permits visual evaluation and confirmation of each isoform claimed by Step 4 of the software. 

% 1. Reset variables & close image windows to free up memory.

clear, close all;

format short g;

% 2. Load list of called polonies

load ('final_polony_list.mat');

P = FINAL_POLONY_LIST;

S = size(FINAL_POLONY_LIST);

% 3. Set parameters

% These parameters merely control the size of windows around the polony that will be presented to the human evaluator as s/he considers whether or not a polony is valid.

PB1 = 30;
PB2 = 100;

% 4. Create variable to contain positive validation counts

VALID_COUNTS = zeros(1023,1);

% 5. Iterate over the full set of images

for D = 1:8

    % Define the directory to be used
    
    DIR_NAME = strcat('./IMAGES/SL',num2str(D),'/');
        
    % Load signature image for current slide under consideration
    
    load(strcat(DIR_NAME,'signature_image.mat'));
    
    % Run through full list of idenfitied polonies
    
    for j = 1:S(1)
                   
        % Check only polonies on this slide and isoforms that have not already been validated twice
        
        if (P(j,1) == D & VALID_COUNTS(P(j,2)) < 2)
                            
            % Pull out a subregion that surrounds the bounding-box-coordinates of this proposed polony
            
            Q1 = SIG_IMAGE((P(j,12)-PB1):(P(j,12)+P(j,14)+PB1),(P(j,11)-PB1):(P(j,11)+P(j,13)+PB1));                
            Q3 = SIG_IMAGE((P(j,12)-PB2):(P(j,12)+P(j,14)+PB2),(P(j,11)-PB2):(P(j,11)+P(j,13)+PB2));      
            
            Q2 = (1 .* ones(size(Q1)) .* double((Q1 == P(j,2)))) + (2 .* ones(size(Q1)) .* double((Q1 > 0 )));
            Q4 = (1 .* ones(size(Q3)) .* double((Q3 == P(j,2)))) + (2 .* ones(size(Q3)) .* double((Q3 > 0 )));
            Q2 = Q2 + (2 .* ones(size(Q1)) .* double((Q1 == 960))) + (3 .* ones(size(Q1)) .* double((Q1 == 896 )));
            Q4 = Q4 + (2 .* ones(size(Q3)) .* double((Q3 == 960))) + (3 .* ones(size(Q3)) .* double((Q3 == 896 )));
            Q2 = Q2 + (4 .* ones(size(Q1)) .* double((Q1 == 992))) + (5 .* ones(size(Q1)) .* double((Q1 == 1016)));
            Q4 = Q4 + (4 .* ones(size(Q3)) .* double((Q3 == 992))) + (5 .* ones(size(Q3)) .* double((Q3 == 1016)));
            
            % Plot a set of images such that the human evaluator can decide whether this is a valid polony
                        
            A = colormap(hsv(7));
            A(1,1) = 0;
            A(2,3) = 1;
            COLORMAP(A)
            
            subplot(2,2,1), imagesc(Q1);
            subplot(2,2,2), image(Q2);
            subplot(2,2,3), imagesc(Q3);
            subplot(2,2,4), image(Q4);
                       
            P(j,:)
            
            n = input('Is this a valid polony?', 's')
                
            if (strcmp(n,'y'))
                    
                VALID_COUNTS(P(j,2)) = VALID_COUNTS(P(j,2)) + 1;               
                            
            end
            
        end

    end
    
end

% 6. Load list of isoforms

SIG_STRINGS = cell(1023,1);
fid = fopen('isoform_strings.txt','rt')
for i = 1:1023
    SIG_STRINGS{i} = fscanf(fid,'%s',1);
end
fclose(fid);

% 7. Output list of validated isoforms

fid = fopen('validated_isoform_list.txt','w');

for i = 1:1023
    if (VALID_COUNTS(i) > 0) 
        fprintf(fid,'%d\t%s\n',i,SIG_STRINGS{i});
    end
end

fclose(fid);

% 4. Reset variables & close image windows to free up memory.

clear, close all

