function [nmrDrawTabUnique, nmrDrawTabOverlap]   = nmrDrawTabFun(user)

%This function will read the nmrPipe.tab file to an array of cells:
% and converts the array of cells to a structure

%Read the tab file
FID             = fopen(user.nmrDrawTabFileName);
if FID == -1, error('Cannot open file'), end;
Data            = textscan(FID, '%s', 'delimiter', '\n', 'whitespace', '');
fclose(FID);

%Find header line FORMAT, get its index and prepare formating
all_index_FORMAT = strfind(Data{1}, 'FORMAT');
index_FORMAT     = find(~cellfun('isempty', all_index_FORMAT), 1);

frmt            = regexprep(Data{1,1}{index_FORMAT},'(..e)','f');
frmt            = regexprep(frmt,'(...f)','f');
frmt            = regexprep(frmt,'FORMAT','');
frmt            = strtrim(frmt);

ind_Cols        = textscan(Data{1,1}{index_FORMAT},'%s');
num_Cols        = size(ind_Cols{1,1},1) - 1;


%Read in the VARS header to create field names

all_index_VARS  = strfind(Data{1}, 'VARS');
index_VARS      = find(~cellfun('isempty', all_index_VARS), 1);
str_VARS        = Data{1,1}{index_VARS};
str_VARS        = regexprep(str_VARS,'VARS','');
str_VARS        = strtrim(str_VARS);

[del_str_VARS,VARS_cell] = regexp(str_VARS,'[ ]','match','split');

all_index_ASS = strfind(VARS_cell, 'ASS');
index_ASS     = find(~cellfun('isempty', all_index_ASS), 1);

%Read Data into an array of cells
count.peaks = 1;
for j=index_FORMAT+2:length(Data{1,1})
    tabRaw(count.peaks,1) = textscan(Data{1,1}{j},'%s');
    count.peaks = count.peaks + 1;
end

for j=1:length(tabRaw)
        for i = 1:num_Cols
            if i~=index_ASS
                tabRaw2{j,i} = str2double(tabRaw{j,1}{i,1});
            elseif i==index_ASS
                tabRaw2{j,i} = tabRaw{j,1}{i,1};
            end
                
        end
end

%Write the nmrDrawTab structure
nmrDrawTabRaw = cell2struct(tabRaw2, VARS_cell, 2);

count.assPeaks = 1;
for j = 1:length(nmrDrawTabRaw)
    
    if ~strcmp(nmrDrawTabRaw(j).ASS, ';')
        nmrDrawTab(count.assPeaks) = nmrDrawTabRaw(j);
        count.assPeaks = count.assPeaks + 1;
    end
end
count.assPeaks = count.assPeaks - 1;

%Separate unique and non-unique assigned peaks (overlap)

count.unique    = 1;
count.overLap   = 1;

if user.specDimens == 3
    work.detOverlap = 6;
elseif user.specDimens == 2
    work.detOverlap = 4;
end

for j = 1:length(nmrDrawTab)
           
    [del_ass{j,1}, ass{j,1}] = regexp(nmrDrawTab(j).ASS,'[.,;]','match','split');                        
    
        if  size(ass{j,1},2) > work.detOverlap
                nmrDrawTabOverlap(count.overLap)   = nmrDrawTab(j);
                count.overLap = count.overLap + 1;
        end
         
        if size(ass{j,1},2) == work.detOverlap && isfield(user, 'nmrDrawTabFileName')
                nmrDrawTabUnique(count.unique)   = nmrDrawTab(j);
                count.unique = count.unique + 1;
        end
        
end

count.overLap   = count.overLap - 1;
count.unique    = count.unique -1;

clearvars del_ass ass ans

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   splitt unique assignments

for j=1:length(nmrDrawTabUnique)
            
    [del_ass{j,1}, ass{j,1}]        = regexp(nmrDrawTabUnique(j).ASS,'[.;]','match','split');
            
    if user.specDimens      == 3
        nmrDrawTabUnique(j).procAss.ASSk        = str2double(ass{j,1}{5});
        nmrDrawTabUnique(j).procAss.ATOMk       = ass{j,1}{6};
    end
    
    if user.magOriginSpin        == 1
    
        nmrDrawTabUnique(j).procAss.ASSi        = str2double(ass{j,1}{1});
        nmrDrawTabUnique(j).procAss.ATOMi       = ass{j,1}{2};
    
        %if size(ass{j,1},2) == work.detOverlap
            nmrDrawTabUnique(j).procAss.ASSj    = str2double(ass{j,1}{3});
            nmrDrawTabUnique(j).procAss.ATOMj   = ass{j,1}{4};
        %end
    
    elseif user.magOriginSpin    == 2
    
        nmrDrawTabUnique(j).procAss.ASSj        = str2double(ass{j,1}{1});
        nmrDrawTabUnique(j).procAss.ATOMj       = ass{j,1}{2};
    
        %if size(ass{j,1},2) == work.detOverlap
            nmrDrawTabUnique(j).procAss.ASSi    = str2double(ass{j,1}{3});
            nmrDrawTabUnique(j).procAss.ATOMi   = ass{j,1}{4};
        %end
    
    end
    
end


if ~exist('nmrDrawTabOverlap')
    nmrDrawTabOverlap = struct;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%