%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: 2017-01-27 (Anya)

%Input: single data base of pointers (compiled) (mat)
%Output: data base with data

clear;

%Create the path direcotry based on where the gene mutation drive is
for i=['X','Y','Z']
    temp = [i,':\DataBase_Input_Code'];
    if (exist(temp, 'dir')) == 7
        path_base = temp;
        path_db = [i,':\DataBase'];
        Drive_Letter = i;
    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
for i=1:Num_Enteries
    %Some of the data should just be repeated between the two structures
    Data_Structure(i).CellTypeDesignator=Data_Loc_Comp(i).CellTypeDesignator;
    Data_Structure(i).DataVar =Data_Loc_Comp(i).DataVar;
    Data_Structure(i).PatVar = 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 the drive mapped currently is where the file is being
    %located
    filename2(1) = Drive_Letter;
    if exist(filename2,'file')==2 %only go here if the file exists
        load(filename2);
        %Single file actin OOP or fibronectin OOP or Nuc OOP
        if (((Data_Structure(i).DataVar == 'Act_OOP') || (Data_Structure(i).DataVar == 'FN_OOP') || (Data_Structure(i).DataVar == 'Nuc_OOP'))...
                && (Data_Loc_Comp(i).DataFoldBoolian == 1) &&(Data_Loc_Comp(i).DataComboBoolian == 0))
            if isscalar(OOP_VEC)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
                i
            else
                Data_Structure(i).Parameter = OOP_VEC;
            end
        end
        
        %A single file with both actin and fibronectin OOP, pull out actin data
        if ((strcmp(char(Data_Structure(i).DataVar),'Act_OOP')==1)...
                && (Data_Loc_Comp(i).DataFoldBoolian == 1) && (Data_Loc_Comp(i).DataComboBoolian == 1))
            for j=1:length(OOP_VEC)                
                if strcmp(condname_str(j,:),'oop_actin')
                    Data_Structure(i).Parameter = OOP_VEC(j);
                else
                    disp('WARNING SOMETHING IS WRONG WITH THE Variable name, look-up variation and add to code');
                    filename2
                    i
                end
            end
        end
        
        %A single file with both actin and fibronectin OOP, pull out FN data
        if ((strcmp(char(Data_Structure(i).DataVar),'FN_OOP')==1)...
                && (Data_Loc_Comp(i).DataFoldBoolian == 1) && (Data_Loc_Comp(i).DataComboBoolian == 1))
            for j=1:length(OOP_VEC)
                if strcmp(condname_str(j,:),'oop_fibro')
                    Data_Structure(i).Parameter = OOP_VEC(j);
                else
                    disp('WARNING SOMETHING IS WRONG WITH THE Variable name, look-up variation and add to code');
                    filename2
                    i
                end
            end
        end
        
        %%%%%%%%%% Nuclei
        
        %Single file with average coverslip nuclei aspect ratio
        if ((strcmp(char(Data_Structure(i).DataVar),'Nuc_AR')==1) && (Data_Loc_Comp(i).DataFoldBoolian == 1) &&(Data_Loc_Comp(i).DataComboBoolian == 0))
            if isscalar(avg_eccentricities_coverslip)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2                
            else
                Data_Structure(i).Parameter = avg_eccentricities_coverslip;
            end
        end
        
        %Single file with average coverslip nuclear area
        if ((strcmp(char(Data_Structure(i).DataVar),'Nuc_Area')==1) && (Data_Loc_Comp(i).DataFoldBoolian == 1) &&(Data_Loc_Comp(i).DataComboBoolian == 0))
            if isscalar(avg_areas_coverslip)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2                
            else
                Data_Structure(i).Parameter = avg_areas_coverslip;
            end
        end
        
       
        %Single file with average coverslip nuclear MNC 
        if ((strcmp(char(Data_Structure(i).DataVar),'Nuc_MNC')==1) && (Data_Loc_Comp(i).DataFoldBoolian == 1) &&(Data_Loc_Comp(i).DataComboBoolian == 0))
            if isscalar(avg_MNC_coverslip_adj)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
            else
                Data_Structure(i).Parameter = avg_MNC_coverslip_adj;
            end
        end
        
        %Single file with percentage of defective nuclei
        if ((strcmp(char(Data_Structure(i).DataVar),'Nuc_Def')==1) && (Data_Loc_Comp(i).DataFoldBoolian == 1) &&(Data_Loc_Comp(i).DataComboBoolian == 0))
            if isscalar(normal_defective_percent)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
            else
                Data_Structure(i).Parameter = normal_defective_percent;
            end
        end
        
        %Single file with average coverslip defective nuclear area
        if ((strcmp(char(Data_Structure(i).DataVar),'Def_Nuc_Area')==1) && (Data_Loc_Comp(i).DataFoldBoolian == 1) &&(Data_Loc_Comp(i).DataComboBoolian == 0))
            if isscalar(avg_def_areas_coverslip)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
            else
                Data_Structure(i).Parameter = avg_def_areas_coverslip;
            end
        end
        
        %Single file with average coverslip defective nuclear eccentricity
        if ((strcmp(char(Data_Structure(i).DataVar),'Def_Nuc_AR')==1) && (Data_Loc_Comp(i).DataFoldBoolian == 1) &&(Data_Loc_Comp(i).DataComboBoolian == 0))
            if isscalar(avg_def_eccentricities_coverslip)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
            else
                Data_Structure(i).Parameter = avg_def_eccentricities_coverslip;
            end
        end
        
        %Single file with average coverslip defective nuclear MNC
        if ((strcmp(char(Data_Structure(i).DataVar),'Def_Nuc_MNC')==1) && (Data_Loc_Comp(i).DataFoldBoolian == 1) &&(Data_Loc_Comp(i).DataComboBoolian == 0))
            if isscalar(avg_def_MNC_coverslip)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
            else
                Data_Structure(i).Parameter = avg_def_MNC_coverslip;
            end
        end
        
        %%%%%%%%%%%% COOP files load data %%%%%%%%%%%%%%%%
        if (strcmp(char(Data_Structure(i).DataVar),'COOP_c_A-FN')==1)
            if isscalar(CorCOOP)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
            else
                Data_Structure(i).Parameter = CorCOOP;
            end
        end
        if (strcmp(char(Data_Structure(i).DataVar),'COOP_u_A-FN')==1)
            if isscalar(UncorCOOP)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
            else
                Data_Structure(i).Parameter = UncorCOOP;
            end
        end
        if (strcmp(char(Data_Structure(i).DataVar),'COOP_A-FN')==1)
            if isscalar(COOP)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
            else
                Data_Structure(i).Parameter = COOP;
            end
        end
        if (strcmp(char(Data_Structure(i).DataVar),'COOP_theta_A-FN')==1)
            if isscalar(COOP_theta)==0
                disp('WARNING SOMETHING IS WRONG WITH THE DATABASE, probably data folder or combo boolian wrongly designated in the following folder');
                filename2
            else
                Data_Structure(i).Parameter = COOP_theta;
            end
        end
        clear OOP* *COOP*
    else
        disp('WARNING this filename does not exist');
        filename2
    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 '\DataValues_' time_stamp '.mat'];
save(filename_data_out,'Data_Structure');

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