%This code will take the output of the comilatin pointer and generate the
%actual data base file in mat and csv
%Initial Version written by:
% Anna Grosberg
% Cardiovascular Modeling Group
% University of CA, Irvine
%Initial Version Date: 2016-11-03
%Last Version Edit Date: 2016-11-03


%Input: single data base of pointers (compiled) (mat)
%Output: single data base of pointers with all duplicate rows removed (compiled) (mat)

clear;

%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);

%Cycle through each entery
count = 0;
duplicate_indices = [];
for i=1:(Num_Enteries-1)
    % Make sure all enteries are strings
    i
    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;
    
    % Make sure duplicate entries that are pointed to differently based on 
    % drive letter are checked 
    if strfind(filename2, 'Y:')
        filename2 = regexprep(filename2,'Y:','Z:');
    elseif strfind(filename2, 'X:')
        filename2 = regexprep(filename2,'X:','Z:');
    end
    
    % Go through the second loop looking for doubles
    for ii=(i+1):Num_Enteries
        CellTypeDesignator_check =cellstr(Data_Loc_Comp(ii).CellTypeDesignator);
        DataVar_check =cellstr(Data_Loc_Comp(ii).DataVar);
        PatVar_check = cellstr(Data_Loc_Comp(ii).PatVar);
        Tot_Num_CS_check = Data_Loc_Comp(ii).Tot_Num_CS;
        CS_Num_check = Data_Loc_Comp(ii).CS_Num;
        filename_check = Data_Loc_Comp(ii).PathFiles;
        
        % Make sure duplicate entries that are pointed to differently based on 
        % drive letter are checked 
        if strfind(filename_check, 'Y:')
            filename_check = regexprep(filename_check,'Y:','Z:');
        elseif strfind(filename_check, 'X:')
            filename_check = regexprep(filename_check,'X:','Z:');
        end
        
        if strcmp(Data_Structure(i).CellTypeDesignator,CellTypeDesignator_check) && ...
           strcmp(Data_Structure(i).DataVar,DataVar_check) && ...
           strcmp(Data_Structure(i).PatVar,PatVar_check) && ...
           strcmp(filename2,filename_check)
           count = count + 1; 
           duplicate_indices(count) = ii;
        end
       
    end
    
end

% Check if no duplicates were found 
if ~isempty(duplicate_indices)
    Data_Loc_Comp(duplicate_indices) = [];
    any_duplicates = 1;
else
    disp('No duplicates found')
    any_duplicates = 0;
end

if any_duplicates == 1
    %Ask the user to pick an output directory
    path_base = path;

    %Generate a time stamp for the file name
    c=clock;
    cc = fix(c);
    time_stamp = [num2str(cc(1)) '_' num2str(cc(2)) '_' num2str(cc(3)) '_' num2str(cc(4)) '_' num2str(cc(5))];
    %Save a mat file of the data
    filename_data_out = [path_base '\DataLocation_Comp_' time_stamp '_Duplicates_Removed.mat'];
    save(filename_data_out,'Data_Loc_Comp');

    %save the talbe as a csv file for visual checks
    T = struct2table(Data_Loc_Comp);
    filename_table_out = [path_base '\DataLocation_Comp_' time_stamp '_Duplicates_Removed.csv'];
    writetable(T,filename_table_out);
end