%This code will check if there is a full data set and print out a table of
%missing data
%It will also check for any possible doubles


%Input: compiled data base (mat)
%Output: data base of missing and spreadsheet of missing data as well as
%possible doubles

clear;

%Ask the user for a compiled file that contain pointer information
[file, path] = ...
    uigetfile('*.mat','Select a mat files that has your compiled data base pointers...', 'Z:');

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

disp('You will be asked for the cell line names file.')
disp('It needs to be in the same format as the original file, or the code will need to be updated.');
%Format of each column of the cell name file: Designator, Property, Family,
%Number, Age, Gender, Relation
formatSpec = '%C%C%C%C%C%C%C';
%Ask the user for the file with cell line names
[filename_cell_lines, cell_path] = ...
    uigetfile('*.csv','Select a csv file with the cell line names and types...', 'Z:');

path_base = cell_path; %set the previous folder to be the current base folder

%Read the cell types data file ignoring the first row
path_filename_cell_lines = [cell_path filename_cell_lines];
T_Cell = readtable(path_filename_cell_lines,'Delimiter',',', ...
    'Format',formatSpec);
%Create a vector with cell line designators
Designator = T_Cell.Designator;
%Number of cell lines
N_Cell_Lines = length(Designator);
Cell_Line_Number = (1:1:N_Cell_Lines)';

%Data type
fprintf('You will be asked for the data types file. \n It needs to be in the same format as the original file, or the code will need to be updated.');
%Format of each column of the cell name file: Data, Variable_Name
formatSpec = '%C%C';
%Ask the user for the file with variable names
[filename_data_type, data_type_path] = ...
    uigetfile('*.csv','Select a csv file with the data types and variable names...', path_base);

%Read the variable types data file ignoring the first row
path_filename_data_type = [data_type_path filename_data_type];
T_Data = readtable(path_filename_data_type,'Delimiter',',', ...
    'Format',formatSpec);
%Create a vector with variable names
Variable_Name = T_Data.Variable_Name;
%Number of variables
N_Var = length(Variable_Name);
Var_Number = (1:1:N_Var)';

%Pattern type
fprintf('You will be asked for the pattern types file. \n It needs to be in the same format as the original file, or the code will need to be updated.');
%Format of each column of the cell name file: Pattern, Pattern_Name
formatSpec = '%C%C';
%Ask the user for the file with pattern names
[filename_pattern_type, pattern_type_path] = ...
    uigetfile('*.csv','Select a csv file with the pattern types and pattern names...', path_base);

%Read the patttern types data file ignoring the first row
path_filename_pattern_type = [pattern_type_path filename_pattern_type];
T_Pattern = readtable(path_filename_pattern_type,'Delimiter',',', ...
    'Format',formatSpec);
%Create a vector with variable names
Pattern_Name = T_Pattern.Pat_Name;
%Number of patterns
N_Pat = length(Pattern_Name);
Pat_Number = (1:1:N_Pat)';


%Determine the number of items in this set
[X,Num_Data_Entries] = size(Data_Loc_Comp);
%Assign set to vectors
CellTypeDesignator = Data_Loc_Comp.CellTypeDesignator;
DataVar= Data_Loc_Comp.DataVar;
PatVar= Data_Loc_Comp.PatVar;
Tot_Num_CS= Data_Loc_Comp.Tot_Num_CS;
DataFoldBoolian= Data_Loc_Comp.DataFoldBoolian;
DataComboBoolian= Data_Loc_Comp.DataComboBoolian;
CS_Num= Data_Loc_Comp.CS_Num;
PathFiles= Data_Loc_Comp.PathFiles;

%Big Matrix of Possible Data
Total_Data_Num = N_Cell_Lines*N_Var*N_Pat;
%The number of data points for each data line
NCLData = N_Var*N_Pat;

%Go through each cell line checking for completness
for i=1:N_Cell_Lines
   Cell_Line_Ind = find(CellTypeDesignator == Designator(i));
        
   if isempty(Cell_Line_Ind)
    
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);
