function [sigmaData, user] = integrityCheckFun(sigmaData, user, conditions, work)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%   Checking sigmaData integrity  %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%run before uniqueFlagFun

if user.checkIntegrity == 1
    
    deleteScreenMsg                         = '';
    
    allIndices                              = 1:length(sigmaData);
    labileIndx                              = [];
    labileIndxCount                         = 1;
    
    fprintf('\n\nBasic checks of assignment\n')

    for j = 1:length(sigmaData)
        
        if (mod(j,50) == 0) || j == length(sigmaData) || j == 1
            screenMsg = sprintf(['\n', num2str(length(sigmaData) + 1 - j), ' sigmaData entries remaining to check for labile atom assignments.\n']);
            fprintf([deleteScreenMsg, screenMsg])
            deleteScreenMsg = repmat(sprintf('\b'), 1, length(screenMsg));
            
            if j == length(sigmaData)
                fprintf(deleteScreenMsg)
                fprintf('\nDONE\n')
            end
        end
        
        if ~isempty(find(ismember([sigmaData(j).numcode.ATOMi sigmaData(j).numcode.ATOMj], conditions.all.labile)))
            
            labileIndx(labileIndxCount)         = j;
            labileIndxCount                     = labileIndxCount + 1;
            
        end
        
        
    end
    
        
    diary on    
    
    fprintf(['\nThere was/were ', num2str(length(labileIndx)),...
             ' labil atoms in the assignment that were removed','\n\n'])
    
    for j = labileIndx
      fprintf( '%5d%5s%5s%5d%5s%5s\n' , sigmaData(j).ASSi, sigmaData(j).ASSi_resName, sigmaData(j).ATOMi...
                                      , sigmaData(j).ASSj, sigmaData(j).ASSj_resName, sigmaData(j).ATOMj);
    end
        
    diary off
    
    labileIndx                                  = unique(labileIndx);
    sigmaData                                   = sigmaData(setxor(labileIndx,allIndices));
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    deleteScreenMsg                         = '';
    double_count                            = 1;
    doubleIndx                              = [];
    out.double_count                        = 0;
    
    for j = 1:length(sigmaData)
        
        if (mod(j,50) == 0) || j == length(sigmaData) || j == 1
            screenMsg = sprintf(['\n', num2str(length(sigmaData) + 1 - j), ' sigmaData entries remaining to check for double assignments.\n']);
            fprintf([deleteScreenMsg, screenMsg])
            deleteScreenMsg = repmat(sprintf('\b'), 1, length(screenMsg));
            
            if j == length(sigmaData)
                fprintf(deleteScreenMsg)
                fprintf('\nDONE\n')
            end
        end
    
        for i = 1:length(sigmaData)
            
            if sigmaData(i).numcode.ASSi    == sigmaData(j).numcode.ASSi ...
            && sigmaData(i).numcode.ATOMi   == sigmaData(j).numcode.ATOMi...
            && sigmaData(i).numcode.ASSj    == sigmaData(j).numcode.ASSj ...
            && sigmaData(i).numcode.ATOMj   == sigmaData(j).numcode.ATOMj...
            && i ~= j
 
            doubleIndx(double_count)  = j;
            double_count                    	= double_count + 1;
                    
            end
        end
    end
    
    
    if ~isempty(doubleIndx)
        out.double_count                        = length(doubleIndx)/2;
    end

    diary on    
    
    fprintf(['\nThere is/are ', num2str(out.double_count), ' double ASSignment(s)',...
        '\nSee doubleAssign.txt for a list','\n'])
        
    diary off
    
    if ~isempty(doubleIndx) && ~isempty(user.nmrDrawTabFileName)
        
        file    = fopen (work.doubleOutFileName, 'w');
        
        for i   = doubleIndx
    
            fprintf (file, '%d  %s\n', sigmaData(i).input.INDEX, sigmaData(i).input.ASS);
        
        end
        
        fclose(file);

        while (~exist(work.doubleOutFileName, 'file'))
            sleep(0.5);
        end
        
    end
    
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%