%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%         search sigmaData using assignment           %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clearvars srch

sigmaDataUniqueIndx = [];

prompt = {'Using letter code (y/n):','RES1:','ATOM1:','RES2:','ATOM2:'};
dlg_title = 'Search sigmaData';
num_lines = 1;
def = {'y','25','H', '14','H'};

srch.input = inputdlg(prompt,dlg_title,num_lines,def);

if isempty(srch.input)
    break
elseif ~isempty(srch.input)
    
        srch.index1 = find(ismember(arrayfun(@(x) x.numcode.ASSi, sigmaData), str2num(srch.input{2})));
        srch.index3 = find(ismember(arrayfun(@(x) x.numcode.ASSj, sigmaData), str2num(srch.input{4})));
        
    if srch.input{1} == 'y'
      
      if isfield(srch, 'index1') && isfield(srch, 'index3')
        
        for i = srch.index1
    
            if strcmp(sigmaData(i).ATOMi, srch.input{3})
        
            srch.index2(i)    = i;
        
            end
        end
    
        for i = srch.index3
    
            if strcmp(sigmaData(i).ATOMj, srch.input{5})
        
            srch.index4(i)        = i;
        
            end
        
        end
        
        if isfield(srch, 'index2') && isfield(srch, 'index4')
        
            sigmaDataUniqueIndx = intersect(intersect(srch.index2, srch.index4), intersect(srch.index1, srch.index3));
            
            
        elseif ~isfield(srch, 'index2') || ~isfield(srch, 'index4')
            
            fprintf(['\n\nThere is no assignment corresponding to: ', srch.input{2}, ' ', srch.input{3}, ' ', srch.input{4}, ' ', srch.input{5} '\n'])
        end
        
      end
        
    elseif srch.input{1} == 'n'
    
        srch.index2 = find(ismember(arrayfun(@(x) x.numcode.ATOMi, sigmaData), str2num(srch.input{3})));
        srch.index4 = find(ismember(arrayfun(@(x) x.numcode.ATOMj, sigmaData), str2num(srch.input{5})));
    
        sigmaDataUniqueIndx = intersect(intersect(srch.index2, srch.index4), intersect(srch.index1, srch.index3));

    end
            
end


if ~isempty(sigmaDataUniqueIndx)
                fprintf(['\n\n', 'SigmaData Index: ',num2str(sigmaDataUniqueIndx)])
                if isfield(sigmaData, 'tab')
                    fprintf(['\n', 'Peak Index (tab file): ',num2str(sigmaData(sigmaDataUniqueIndx).tab.INDEX)])
                end
                sigmaData(sigmaDataUniqueIndx)
            else
                fprintf(['\n\nThere is no assignment corresponding to: ', srch.input{2}, ' ', srch.input{3}, ' ', srch.input{4}, ' ', srch.input{5} '\n'])
end

clearvars prompt ans dlg_title num_lines def i srch sigmaDataUniqueIndx

