%MakeGlimFile_generic.m
%
%Create a glimfile for either a single output folder or up to 2 output folders / variables
%
%You will also need to download the "cell2csv.m" for this code to run:
%
%http://www.mathworks.com/matlabcentral/fileexchange/7601-cell2csv


clear all;
% Define variables/pathnames
StudyFolder = input('What is the location of your study folder? (e.g. /home/user/surfstat/StudyName): ', 's');
StudyFolder = strtrim(StudyFolder);
MultipleFolders = input('Do you have multiple variables / subfolders in your StudyName? Y or N): ', 's');
thickness_metric = input ('Which thickness metric?  (e.g., tlink, tlaplace, or tfs): ', 's');
blur = input ('What is the blur level in mm?  (e.g., 30): ', 's');
output = input ('Output glimfile name .csv?  (e.g., glimfile): ', 's');

if MultipleFolders == 'Y'
    Variable_1_short = input ('What is the subdirectory of your Variable1? (e.g. Variable1 where data for Variable1 is located in /home/user/surfstat/StudyName/Variable1): ', 's');
    Variable_2_short = input ('What is the subdirectory of your Variable2? (e.g. Variable2 where data for Variable2 is located in /home/user/surfstat/StudyName/Variable2): ', 's');
    Variable_1 = [StudyFolder '/' Variable_1_short];
    Variable_2 = [StudyFolder '/' Variable_2_short];

    for v=1:2
        eval(['cd(Variable_' num2str(v) ')'])
        files = dir;

        % Find all files in the directory that contains an underscore ('_'), meaning
        % they are probably a CIVET output directory and not something else like a QC
        % folder

        for i=1:length(files)
            if cell2mat(strfind({files(i).name}, '_')) > 0
                eval(['subjects_' num2str(v)  '(i) = {files(i).name};']);
            end
        end
        % Get rid of empty cells
        eval(['subjects_' num2str(v) ' = subjects_' num2str(v) '(find(~cellfun(@isempty,subjects_' num2str(v) ')))'';']);
    end

    for i=1:length(subjects_1)
        cd([Variable_1 '/' subjects_1{i} '/thickness'])
        thickfiles=dir;
        for j=1:length(thickfiles)
            if cell2mat(strfind({thickfiles(j).name}, ['native_rms_rsl_' thickness_metric '_' blur 'mm_left'])) > 0
                thickleft(i) = {thickfiles(j).name};
            elseif cell2mat(strfind({thickfiles(j).name}, ['native_rms_rsl_' thickness_metric '_' blur 'mm_right'])) > 0
                thickright(i) = {thickfiles(j).name};
            end
        end
        Row(i).str= [subjects_1{i} ' ' Variable_1_short ' ' StudyFolder '/' Variable_1_short '/' subjects_1{i} '/thickness/' thickleft{i} ' ' StudyFolder '/' Variable_1_short '/' subjects_1{i} '/thickness/' thickright{i}];
    end

    for i=1:length(subjects_2)
        cd([Variable_2 '/' subjects_2{i} '/thickness'])
        thickfiles=dir;
        for j=1:length(thickfiles)
            if cell2mat(strfind({thickfiles(j).name}, ['native_rms_rsl_' thickness_metric '_' blur 'mm_left'])) > 0
                thickleft(i) = {thickfiles(j).name};
            elseif cell2mat(strfind({thickfiles(j).name}, ['native_rms_rsl_' thickness_metric '_' blur 'mm_right'])) > 0
                thickright(i) = {thickfiles(j).name};
            end
        end
        Row(i+length(subjects_1)).str= [subjects_2{i} ' ' Variable_2_short ' ' StudyFolder '/' Variable_2_short '/' subjects_2{i} '/thickness/' thickleft{i} ' ' StudyFolder '/' Variable_2_short '/' subjects_2{i} '/thickness/' thickright{i}];
    end

else
    cd(StudyFolder)
    files = dir;

    % Find all files in the directory that contains an underscore ('_'), meaning
    % they are probably a CIVET output directory and not something else like a QC
    % folder

    for i=1:length(files)
        if cell2mat(strfind({files(i).name}, '_')) > 0
            subjects(i) = {files(i).name};
        end
    end
    % Get rid of empty cells
    subjects = subjects(find(~cellfun(@isempty,subjects)))';
    for i=1:length(subjects)
        cd([StudyFolder '/' subjects{i} '/thickness'])
        thickfiles=dir;
        for j=1:length(thickfiles)
            if cell2mat(strfind({thickfiles(j).name}, ['native_rms_rsl_' thickness_metric '_' blur 'mm_left'])) > 0
                thickleft(i) = {thickfiles(j).name};
            elseif cell2mat(strfind({thickfiles(j).name}, ['native_rms_rsl_' thickness_metric '_' blur 'mm_right'])) > 0
                thickright(i) = {thickfiles(j).name};
            end
        end
        Row(i).str= [subjects{i} ' ' StudyFolder '/' subjects{i} '/thickness/' thickleft{i} ' ' StudyFolder '/' subjects{i} '/thickness/' thickright{i}];
    end
end

% Convert struct to cell array
Row=struct2cell(Row');
Row=Row';

% Print to GLIM file
cell2csv([output '.csv'],Row,' ');