function varargout=dopGrpFileRead(fileNameLoc)
% dopOSCCI Mark II: dopGrpFileRead ~ 21-11-2010 (last edit)
%
% read dopOSCCI group file: a tab delimitted file with first column
% of file names (could be full path - file name location) and then a series
% of columns (number of groups) labelled with respect to the group name
%
% critical outputs:
% -
%
fprintf('%s\n\t%s\n',['Running ',mfilename],...
    'Reading dopOSCCI group file:');
try
    if iscell(fileNameLoc)
        fileNameLoc=fileNameLoc{1};
    end
    [dgr.tmp.fl dgr.tmp.fn dgr.tmp.fext]=fileparts(fileNameLoc);
    if exist(fileNameLoc,'file')==2 && strcmp(dgr.tmp.fext,'.txt')
        dgr.fileNameLoc=fileNameLoc;%dtd.stitchFile;
        dgr.out.okay=0;
        dgr.translation='dopOSCCI group read';
        dgr.abbreviation='dgr'; % useful for debugging
        
        %% import the stitchFile
        dgr.im=importdata(dgr.fileNameLoc); % imported data
        % something like:
        %          data: [8x24 double]
        %     textdata: {9x25 cell}
        dgr.dataSize=size(dgr.im.data);
        dgr.textSize=size(dgr.im.textdata);
        dgr.out.n=dgr.textSize(1)-1; % number of rows (people) minus column header row      
        
        %% file names
        i=1;% skip the first row = column header
        dgr.out.list=[];
        while i<dgr.textSize(1); i=i+1;
            dgr.out.list{end+1}=dgr.im.textdata{i};
        end
        
        %% headers (grp names)
        i=1;% ignores first header (should be file names anyway)
        dgr.out.grpNames=[]; 
        dgr.reportFormTxt='%s';        
        while i<dgr.textSize(2); i=i+1;
            dgr.reportFormTxt=[dgr.reportFormTxt,'\t%s'];
            dgr.out.grpNames{end+1}=dgr.im.textdata{1,i};
        end
        
        if ~isempty(dgr.out.grpNames)
            dgr.out.okay=1;
            dgr.out.nGrps=length(cellfun('size',dgr.out.grpNames,1));
        end
        %% sort data
        % put group indices in a single column
        dgr.out.grpNumbers=[];
        if dgr.out.okay
            [dgr.row dgr.out.grpNumbers]=find(dgr.im.data);
        end
        dgr.out.perGrp=[];
        dgr.reportFormNum='%s';
        if ~isempty(dgr.out.grpNumbers)
            dgr.unique=unique(dgr.out.grpNumbers);
            for i=1:length(dgr.unique)
                dgr.out.perGrp(i)=sum(dgr.out.grpNumbers==dgr.unique(i));
                dgr.reportFormNum=[dgr.reportFormNum,'\t%u'];
            end
        else
            dgr.out.okay=0;
        end
        %% list to clearly see groups...
        dgr.out.filesInGrps=[];
        dgr.out.sprintList=[];
        dgr.out.sprintForm=[];
        if dgr.out.okay
            dgr.out.filesInGrps=dgr.out.grpNames;
            for ii=1:dgr.out.nGrps
                dgr.out.sprintList{end+1}=dgr.out.grpNames{ii};
                if ii<dgr.out.nGrps
                    dgr.out.sprintForm=[dgr.out.sprintForm,'%s\t\t'];
                else
                    dgr.out.sprintForm=[dgr.out.sprintForm,'%s\n'];
                end
            end
            dgr.tmp.c=0; % counter
            for i=1:max(dgr.out.perGrp)
                for j=1:dgr.out.nGrps
                    if i<=dgr.out.perGrp(j)
                        dgr.tmp.c=dgr.tmp.c+1;
                        dgr.out.filesInGrps{i+1,j}=dgr.out.list{dgr.tmp.c};
                    else
                        dgr.out.filesInGrps{i+1,j}='-';
                    end
                    dgr.out.sprintList{end+1}=dgr.out.filesInGrps{i+1,j};
                end
            end
            
            
            for i=1:numel(dgr.out.filesInGrps) % number of elements
%                 dgr.out.sprintList{end+1}=dgr.out.filesInGrps{i};
                if mod(i,dgr.out.nGrps)
                    if strcmp(dgr.out.sprintList,'-')
                        % would be nice to get some extra space in here...
                        dgr.out.sprintForm=[dgr.out.sprintForm,'%s\t'];
                    else
                    dgr.out.sprintForm=[dgr.out.sprintForm,'%s\t'];
                    end
                else
                    dgr.out.sprintForm=[dgr.out.sprintForm,'%s\n'];
                end
            end
        end
        %% report
        dgr.out.report=sprintf('%s\n\t%s\n\t%s\n\t%s\n\t%s\n',...
            ['Group File Report: ',dgr.fileNameLoc],...
            [num2str(dgr.out.nGrps),' groups found'],...
            sprintf(dgr.reportFormTxt,'Groups = ',dgr.out.grpNames{1:end}),...
            sprintf(dgr.reportFormNum,'(per group)',dgr.out.perGrp),...
            ['n = ',num2str(dgr.out.n),' individual files']);
            dgr.out.guiReport=sprintf('%s\n',...
            sprintf(dgr.reportFormTxt,[num2str(dgr.out.nGrps),' groups found:'],dgr.out.grpNames{1:end}));
        disp(dgr.out.report);
        disp(dgr.out.filesInGrps);
    else
        fprintf('%s\n\t%s\n\n%s\n','Group file was NOT found:',...
            fileNameLoc,'Therefore, group file can''t be read...');
        dgr.out.okay=0;
    end
    %% set output
    varargout{1}=dgr.out;
    
catch err
    %% catch dopOSCCI error
    % 
save(dopOSCCIdebug(mfilename));throw(err); 
%     dopOSCCIcrash(err,dgr,mfilename);
end
clear dgr