% This code will scan a compiled database of pointers for pointers that may
% be incorrect (designated as lines but pointing to an iso data file, vice
% versa) 

%Initial Version written by:
% Jason Core 
% Cardiovascular Modeling Group
% University of CA, Irvine
%Initial Version Date: 2017-03-18
%Last Version Edit Date: 2016-03-18

%Input: single data base of pointers (compiled) (mat)
%Output: Displays row numbers and file names of potentially incorrect
%pointers in command line 

%Create the path direcotry based on where the gene mutation drive is
for i=['W','X','Y','Z']    
    temp = [i,':\DataBase_Input_Code'];
    if (exist(temp, 'dir')) == 7
        path_base = temp;
        path_db = [i,':\DataBase'];
    end
end


%Ask the user for all the files that contain pointer information
[file, path] = ...
    uigetfile('*.mat','Select the single compiled data base of pointers in mat format...', path_db);

path_and_filename = [path file];
filename = path_and_filename;
%load the file
load(filename)

%Determine the number of enteries in the data base
[X,Num_Enteries] = size(Data_Loc_Comp);
incorrect_indices = 0;

% Scan through all pointers
for i=1:(Num_Enteries)
    
    % Assign all data entries associated to a pointer to non-categorical
    % variables 
    Data_Structure(i).CellTypeDesignator=cellstr(Data_Loc_Comp(i).CellTypeDesignator);
    Data_Structure(i).DataVar =cellstr(Data_Loc_Comp(i).DataVar);
    Data_Structure(i).PatVar = cellstr(Data_Loc_Comp(i).PatVar);
    Data_Structure(i).Tot_Num_CS = Data_Loc_Comp(i).Tot_Num_CS;
    Data_Structure(i).CS_Num = Data_Loc_Comp(i).CS_Num;
    filename2 = Data_Loc_Comp(i).PathFiles;
    
    % Check for mismatching pattern designation and filename
    if ~isempty(strfind(char(Data_Structure(i).PatVar),'Iso'))
        if ~isempty(strfind(filename2,'Lin'))||~isempty(strfind(filename2,'LIN'))||~isempty(strfind(filename2,'lin'))
            cmdtext = ['Entry ' num2str(i+1) ' in excel sheet may be incorrect.\nPossible lines data file with iso designation.'];
            disp(cmdtext)
            incorrect_indices = 1;
        end
    end
    if ~isempty(strfind(char(Data_Structure(i).PatVar),'Lines'))
        if ~isempty(strfind(filename2,'Iso'))||~isempty(strfind(filename2,'ISO'))||~isempty(strfind(filename2,'iso'))
            cmdtext = ['Entry ' num2str(i+1) ' in excel sheet may be incorrect.\nPossible iso data file with lines designation.'];
            disp(cmdtext)
            incorrect_indices = 1;
        end
    end
    
    % Check for mismatching data type designation and filename
    if ~isempty(strfind(char(Data_Structure(i).CellTypeDesignator),'D'))
         if ~isempty(strfind(filename2,'atient'))
            cmdtext = ['Entry ' num2str(i+1) ' in excel sheet may be incorrect. Designation as Patient but patient file.'];
            disp(cmdtext)
            incorrect_indices = 1;
        end
        if ~isempty(strfind(filename2,'ontrol'))
            cmdtext = ['Entry ' num2str(i+1) ' in excel sheet may be incorrect. Designation as Donor but control file.'];
            disp(cmdtext)
            incorrect_indices = 1;
        end
    end
    if ~isempty(strfind(char(Data_Structure(i).CellTypeDesignator),'P'))
        if ~isempty(strfind(filename2,'onor'))
            cmdtext = ['Entry ' num2str(i+1) ' in excel sheet may be incorrect. Designation as Patient but donor file.'];
            disp(cmdtext)
            incorrect_indices = 1;
        end
        if ~isempty(strfind(filename2,'ontrol'))
            cmdtext = ['Entry ' num2str(i+1) ' in excel sheet may be incorrect. Designation as Donor but control file.'];
            disp(cmdtext)
            incorrect_indices = 1;
        end
    end
    if ~isempty(strfind(char(Data_Structure(i).CellTypeDesignator),'C'))&&isempty(strfind(char(Data_Structure(i).CellTypeDesignator),'PC'))
         if ~isempty(strfind(filename2,'atient'))
            cmdtext = ['Entry ' num2str(i+1) ' in excel sheet may be incorrect. Designation as Patient but patient file.'];
            disp(cmdtext)
            incorrect_indices = 1;
         end
         if ~isempty(strfind(filename2,'onor'))
            cmdtext = ['Entry ' num2str(i+1) ' in excel sheet may be incorrect. Designation as Patient but donor file.'];
            disp(cmdtext)
            incorrect_indices = 1;
        end
    end
end

% Check if no bad pointers were found 
if incorrect_indices == 0
    disp('No bad pointers found')
end
