% This code will remove pointers, either individually (enter in row
% numbers), by folder (delete all pointers pointing to files in a
% particular folder), or by individual (CA1, PA1, etc.) 
% Jason Core
% Cardiovascular Modeling Group
% University of CA, Irvine
%Initial Version Date: 2017-03-07
%Last Version Edit Date: 2016-03-07


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


%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);
specific_remove = input('To remove individual pointers, enter 0.\nTo remove all pointers corresponding to a folder/coverslip, enter 1\nTo remove all pointer corresponding to an individual, enter 2.\n');

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

if specific_remove == 1
    validcheck = 0;
    while validcheck<1
        % Select numbers of pointers to exclude from excel sheet 
        remove_path = input('Enter address of folder to remove all pointers from: ', 's');
        counter = length(Data_Loc_Comp);
        counting = 0;
        for i=1:counter
            % Make sure all enteries are strings
            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

            if strfind(filename2,remove_path)
                counting = counting+1;
                remove_ind(counting) = i;
            end 
        end
        Data_Loc_Comp(remove_ind) = [];
        w = input('Are you done removing pointers? yes = 0 no = 1: ');
        if w == 0 
            disp('Done')
            validcheck = 1;
        end
        clear remove_path remove_ind 
    end
elseif specific_remove == 0
   remove_ind = input('Enter row indices of pointers to manually delete: ');
   Data_Loc_Comp(remove_ind) = [];
elseif specific_remove == 2
    validcheck = 0;
    while validcheck<1
        % Select numbers of pointers to exclude from excel sheet 
        remove_person = input('Enter designation of individual to remove pointers for (CA1, PA1, D3, etc.): ', 's');
        counter = length(Data_Loc_Comp);
        counting = 0;
        for i=1:counter
            % Make sure all enteries are strings
            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

            if strfind(char(Data_Structure(i).CellTypeDesignator),remove_person)
                counting = counting+1;
                remove_ind(counting) = i;
            end 
        end
        Data_Loc_Comp(remove_ind) = [];
        w = input('Are you done removing pointers? yes = 0 no = 1: ');
        if w == 0 
            disp('Done')
            validcheck = 1;
        end
        clear remove_path remove_ind 
    end
end

%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 '.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 '.csv'];
writetable(T,filename_table_out);
