
% STEP 3: Signature Image Generation

% The "signature" of a given pixel defines that pattern of exon inclusion/exclusion at that position.  We abstract this signature pattern as a binary number consisting of digits 
% corresponding to the 10 individual exons (which are defined by the 10 scans).  A signature of "1000010011" would mean that the 1st, 6th, 9th and 10th variable exons were present.
% This signature can of course be represented as its base-10 equivalent as well.  The previous step defined cutoffs for each image, above which a given pixel on a given image would
% be considered to reflect inclusion of the exon corresponding to that image.  Here we create a signature image, in which the value at each pixel is the base-10 conversion of the 
% base-2 10 digit composite signature at that pixel (determined by thresholding each of the ten images).

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

 clear, close all;

% 2. Load Gel Mask.  

load('GEL_MASK_100');

GEL_MASK = uint16(NEW_GEL_MASK .* 65535);

clear NEW_GEL_MASK;

% 3. Load threshold values (generateed in Step 2)

load('thresholds');

% 4. Iterate over the eight slides (as we are processing all images in parallel).

for D = 1:8

    % Define the directory to be used
    
    DIR_NAME = strcat('./IMAGES/SL',num2str(D),'/');
    
    % Define the list of 10 files in that directory (e.g. files produced by Step 1 that bear the ".sub" extension)
    
    FILELIST = dir(strcat(DIR_NAME,'SL',num2str(D),'*.sub'));
   
    % Initialize signature image, setting all bits to '1'
    
    SIG_IMAGE = uint16(ones(size(GEL_MASK)) .* 1023);
    
    % Iterate over the ten images
    
    for i = 1:10                                        
        
        % Load a given image
        
        IMAGE = imread(strcat(DIR_NAME,FILELIST(i).name));      
   
        % Figure out which exon this file is representing and determine the base-10 value for the appropriate 
       
        index = sscanf(FILELIST(i).name, 'SL%d_SC%d_E%d_');
        
        val = 2^(index(3)-1);
        
        [D i val thresholds(D,i)]
        
        % Threshold the image using the appopriate cutoff
        
        NO_SIGNAL = IMAGE < thresholds(D,i);
                 
        % Decrement below-threshold pixels within the signature image by the appropriate base-10 value
        
        NO_SIGNAL = uint16(double(NO_SIGNAL) .* val);
               
        SIG_IMAGE = imsubtract(SIG_IMAGE,NO_SIGNAL);        
                
    end                                                         % Loop to next image
          
    % Save Signature Image in appropriate directory
    
    save(strcat(DIR_NAME,'signature_image.mat'),'SIG_IMAGE');
   
end

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

clear, close all


