%This code will create two files (.mat and .txt or .scv), which are meant
%to list directories with all the data

clear;

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


choose_another_cell_line = 1; %let user decide when to stop
counter = 0; %initiate counter

while choose_another_cell_line==1
    counter = counter+1;
    %Select the number of the cell line you would like to input
    Combo_For_Disp = table(Cell_Line_Number, Designator)
    Cell_Line_Index = input('From the list above, \n select the number of the cell line for \n which you are entering data: ');
    disp(['Chosen cell line: ' Designator(Cell_Line_Index)]);
    
    %Select the pattern type you are inputing
    Combo_For_Disp = table(Pat_Number, Pattern_Name)
    Pattern_Type_Index = input('From the list above, \n select the number of the pattern type for \n which you are entering data: ');
    disp(['Chosen pattern type: ' Pattern_Name(Pattern_Type_Index)]);
    
    %Select the data type you are inputing
    %Did you analyze a single type of construct or two simultaneously
    Data_Combo = input('Did you run the OOP code on a single construct at a time [0] or multiple constructs simultaneously [1]: ');
    
    % Display the table
    Combo_For_Disp = table(Var_Number, Variable_Name)
    if Data_Combo==0
        Data_Type_Vector(1) = input('From the list above, \n select the number of the data type for \n which you are entering data: ');
        disp(['Chosen data type: ' Variable_Name(Data_Type_Vector(1))]);
        Disp_Data_Portion = [char(Variable_Name(Data_Type_Vector(1))) ' '];
    else
        clear Disp_Data_Portion
        Disp_Data_Portion = [];
        for i=1:(Data_Combo+1)
            Data_Type_Vector(i) = input(['From the list above, \n select the number of the data type ' num2str(i) ' for \n which you are entering data: ']);
            disp(['Chosen data type: ' Variable_Name( Data_Type_Vector(i))]);
            st1 = Disp_Data_Portion;
            st2 = char(Variable_Name(Data_Type_Vector(1)));
            st3 = ' ';
            Disp_Data_Portion = strcat(st1,st2,st3);
        end
    end
    
    %Cover-slip number
    Num_CS = input('How many data sets (i.e. cover-slips) are there for this data/pattern/cell line: ');
    
    %Different folders or the same folder for different cover-slips
    Data_Fold = input('Is there a single data file for all the cover slips [0] or multiple files [1]: ');
    
    if Data_Fold == 1 %if there are multiple data folders cycle to locate each
        counter = counter-1; %Need to set the counter back otherwise it will skip a number
        for i=1:Num_CS
            %Ask the user for the file for each cover-slip
            Display_string = ['Select the file with the the data for cell line/data ' char(Designator(Cell_Line_Index))...
                Disp_Data_Portion char(Pattern_Name(Pattern_Type_Index)) ' c-s ' num2str(i)];
            [filename_data, data_path] = ...
                uigetfile('*.mat', Display_string , path_base);
            path_base = data_path; %Reset the path_base to correspond to the last folder used.
            PathFileVec = [data_path filename_data];
            for j=1:(Data_Combo+1)
                counter = counter+1;
                Data_Type_Index=Data_Type_Vector(j);
                Data_Loc_Set(counter).CellTypeDesignator = Designator(Cell_Line_Index);
                Data_Loc_Set(counter).DataVar = Variable_Name(Data_Type_Index);
                Data_Loc_Set(counter).PatVar = Pattern_Name(Pattern_Type_Index);
                Data_Loc_Set(counter).Tot_Num_CS = Num_CS;
                Data_Loc_Set(counter).DataFoldBoolian = Data_Fold;
                Data_Loc_Set(counter).DataComboBoolian = Data_Combo;
                Data_Loc_Set(counter).CS_Num = i;
                Data_Loc_Set(counter).PathFiles = PathFileVec;
            end
        end
    else %otherwise ask for the single file
        %Ask the user for the file with the single data file
        Display_string = ['Select the SINGLE file with the All the data for cell line/data ' char(Designator(Cell_Line_Index))...
            Disp_Data_Portion char(Pattern_Name(Pattern_Type_Index))];
        [filename_data, data_path] = ...
            uigetfile('*.mat', Display_string , path_base);
        PathFileVec = [data_path filename_data];
        path_base = data_path; %Reset the path_base to correspond to the last folder used.
        for j=1:(Data_Combo+1)
            counter = counter+(j-1);
            Data_Type_Index=Data_Type_Vector(j);
            Data_Loc_Set(counter).CellTypeDesignator = Designator(Cell_Line_Index);
            Data_Loc_Set(counter).DataVar = Variable_Name(Data_Type_Index);
            Data_Loc_Set(counter).PatVar = Pattern_Name(Pattern_Type_Index);
            Data_Loc_Set(counter).Tot_Num_CS = Num_CS;
            Data_Loc_Set(counter).DataFoldBoolian = Data_Fold;            
            Data_Loc_Set(counter).DataComboBoolian = Data_Combo;
            Data_Loc_Set(counter).CS_Num = 1;
            Data_Loc_Set(counter).PathFiles = PathFileVec;
        end
    end
    %Ask the user if there is another set they would like to enter
    choose_another_cell_line = input('Would you like to enter another data location? 0=No, 1=Yes: ');
end

%Ask the user to pick an output directory
path_base = uigetdir(path_base,'Please pick an output directory...');

%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_' time_stamp '.mat'];
save(filename_data_out,'Data_Loc_Set');

%save the table as a csv file for visual checks
T = struct2table(Data_Loc_Set);
filename_table_out = [path_base '\DataLocation_' time_stamp '.csv'];
writetable(T,filename_table_out);