%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%   write out cyana restraints  %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%  eNOES are set to 8 Angstroms if any proton involved is a aromatic  %%%
%%%  und der NOE uni-directional                                        %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sigmaData = cyanaUplScaling(sigmaData); % sigmaData(x).numcode.ATOMmultiplicity is the same!!!!!

user.toleranceUplUniue2     = 1;
user.toleranceLolUniue2     = 1;
user.toleranceUplUniue1     = 1.20;
user.toleranceLolUniue1     = 0.80;

% use default value or let user specify

user.cutoffDefault          = input('Use the default value of 20% error(1) for uni-directional eNOEs or specify your own(2)?: ');

if isequal(double(user.cutoffDefault), 2)
    
	user.cutoffValue        = input('Enter the percent for uni-directional eNOEs you would like to use (i.e. 15, for 15%): ');
   	user.toleranceUplUniue1	= 1 + user.cutoffValue/100;
  	user.toleranceLolUniue1	= 1 - user.cutoffValue/100; 
        
end

%methyl are allways applied also for bi-directional eNOEs!!!
%8.5% communication Beat V.
user.toleranceUplMeth       = 1.085;
user.toleranceLolMeth       = 0.915;

%methylene pseudo are allways applied also for bi-directional eNOEs!!!
%5.0% communicaton Beat V.
user.toleranceUplMethylenePseudo       = 1.05;
user.toleranceLolMethylenePseudo       = 0.95;

cyanaOutput                 = [];
cyanaUpl                    = [];
cyanaLol                    = [];

clearvars cyanaOutput cyanaUpl cyanaLol

user.chiNcutOff             = 0;

user.chiNcutOff             = input('Specify a chiN cut off value: ');
user.bi_eNOE                = input('Output eNOEs derrived from two cross peaks only (2) or also if only one cross peak was computed (1)? Choose (2/1): ');

user.fixedNOES              = input('Remove all fixed distances (intra methelene, rings etc) (1) or keep all eNOEs (2)? Choose (1/2): ');

count.cyana                 = 1;

for j=find(ismember([sigmaData.unique], [1 2]))
        
        if sigmaData(j).chiN <= user.chiNcutOff
            
            if sigmaData(j).unique == 1 && abs(sigmaData(j).buildups.rawIzero) ~= 999
                
                cyanaOutput(count.cyana).numcode            = sigmaData(j).numcode;
                cyanaOutput(count.cyana).ASSi_resName       = sigmaData(j).ASSi_resName;
                cyanaOutput(count.cyana).ATOMi              = sigmaData(j).ATOMi;
                cyanaOutput(count.cyana).ASSj_resName       = sigmaData(j).ASSj_resName;
                cyanaOutput(count.cyana).ATOMj              = sigmaData(j).ATOMj;
                cyanaOutput(count.cyana).ASSnorm            = sigmaData(j).ASSnorm;
                cyanaOutput(count.cyana).ATOMnorm           = sigmaData(j).ATOMnorm;
                
                cyanaOutput(count.cyana).rEff               = sigmaData(j).rEffij;
                cyanaOutput(count.cyana).rCyana             = sigmaData(j).rEffij     * (1/sigmaData(j).numcode.cyanaUplScaling)^(1/6);
                cyanaOutput(count.cyana).rCyanaCorr         = [];
                
                cyanaOutput(count.cyana).unique             = 1;
                
                count.cyana                                 = count.cyana + 1;
                
            
            elseif sigmaData(j).unique == 2
                
                
                if sigmaData(sigmaData(j).indxji).chiN <= user.chiNcutOff && abs(sigmaData(j).buildups.rawIzero) ~= 999 && abs(sigmaData(sigmaData(j).indxji).buildups.rawIzero) ~= 999
                    
                    cyanaOutput(count.cyana).numcode            = sigmaData(j).numcode;
                    cyanaOutput(count.cyana).ASSi_resName       = sigmaData(j).ASSi_resName;
                    cyanaOutput(count.cyana).ATOMi              = sigmaData(j).ATOMi;
                    cyanaOutput(count.cyana).ASSj_resName       = sigmaData(j).ASSj_resName;
                    cyanaOutput(count.cyana).ATOMj              = sigmaData(j).ATOMj;
                    cyanaOutput(count.cyana).ASSnorm            = sigmaData(j).ASSnorm;
                    cyanaOutput(count.cyana).ATOMnorm           = sigmaData(j).ATOMnorm;
                    
                    cyanaOutput(count.cyana).rEff               = sigmaData(j).avgrEff;
                    cyanaOutput(count.cyana).rCyana             = sigmaData(j).avgrEff    * (1/sigmaData(j).numcode.cyanaUplScaling)^(1/6);
                    cyanaOutput(count.cyana).rCyanaCorr         = [];
                    
                    cyanaOutput(count.cyana).unique             = 2;
                    
                    count.cyana                                 = count.cyana + 1;
                    
                end
                
                if abs(sigmaData(j).buildups.rawIzero) ~= 999 && (abs(sigmaData(sigmaData(j).indxji).buildups.rawIzero) == 999 || sigmaData(sigmaData(j).indxji).chiN > user.chiNcutOff)
                
                    cyanaOutput(count.cyana).numcode            = sigmaData(j).numcode;
                    cyanaOutput(count.cyana).ASSi_resName       = sigmaData(j).ASSi_resName;
                    cyanaOutput(count.cyana).ATOMi              = sigmaData(j).ATOMi;
                    cyanaOutput(count.cyana).ASSj_resName       = sigmaData(j).ASSj_resName;
                    cyanaOutput(count.cyana).ATOMj              = sigmaData(j).ATOMj;
                    cyanaOutput(count.cyana).ASSnorm            = sigmaData(j).ASSnorm;
                    cyanaOutput(count.cyana).ATOMnorm           = sigmaData(j).ATOMnorm;
                    
                    cyanaOutput(count.cyana).rEff               = sigmaData(j).rEffij;
                    cyanaOutput(count.cyana).rCyana             = sigmaData(j).rEffij     * (1/sigmaData(j).numcode.cyanaUplScaling)^(1/6);
                    cyanaOutput(count.cyana).rCyanaCorr         = [];
                    
                    cyanaOutput(count.cyana).unique             = 1;
                    
                    count.cyana                                 = count.cyana + 1;
                
                end
            end
            
        elseif sigmaData(j).chiN > user.chiNcutOff             
            
            if sigmaData(j).unique == 2
            
                if sigmaData(sigmaData(j).indxji).chiN <= user.chiNcutOff && abs(sigmaData(sigmaData(j).indxji).buildups.rawIzero) ~= 999
            
                cyanaOutput(count.cyana).numcode                = sigmaData(sigmaData(j).indxji).numcode;
                cyanaOutput(count.cyana).ASSi_resName           = sigmaData(sigmaData(j).indxji).ASSi_resName;
                cyanaOutput(count.cyana).ATOMi                  = sigmaData(sigmaData(j).indxji).ATOMi; 
                cyanaOutput(count.cyana).ASSj_resName           = sigmaData(sigmaData(j).indxji).ASSj_resName;
                cyanaOutput(count.cyana).ATOMj                  = sigmaData(sigmaData(j).indxji).ATOMj;
                
                cyanaOutput(count.cyana).rEff                   = sigmaData(sigmaData(j).indxji).rEffij;
                cyanaOutput(count.cyana).rCyana                 = sigmaData(sigmaData(j).indxji).rEffij * (1/sigmaData(sigmaData(j).indxji).numcode.cyanaUplScaling)^(1/6);
                cyanaOutput(count.cyana).rCyanaCorr             = [];
                
                cyanaOutput(count.cyana).unique                 = 1; % since it is not an avgrEff that is used but only one of them
            
                count.cyana                                     = count.cyana + 1;
                
                end
            end
        end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% scrub

if user.fixedNOES == 1
    
    fprintf('\n\n%s\r\n','REMOVING FIXED DISTANCES, INCLUDING PROLINE INTRA')
     
    % % % scrub intra methylene etc, i.e. HB2, HB3 in same res
    [cyanaOutput, cyanaOutputScrub] = scrubIntraResMethyleneNew(cyanaOutput);
    
    numScrubed = length(cyanaOutputScrub);
    
    fprintf('\n\n%d%s\r\n',numScrubed,' eNOES were scrubed from the final output')
    
else
    
    fprintf('\n\n%s\r\n','KEEPING ALL eNOEs')
    
end

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

count.cyana = 1;

for j = 1:length(cyanaOutput)
    
    if user.bi_eNOE == 2 && cyanaOutput(j).unique == 2
        
        cyanaUpl(count.cyana) = cyanaOutput(j);
        cyanaLol(count.cyana) = cyanaOutput(j);
            
    	cyanaUpl(count.cyana).rCyanaCorr  = cyanaUpl(count.cyana).rCyana * user.toleranceUplUniue2;
        cyanaLol(count.cyana).rCyanaCorr  = cyanaLol(count.cyana).rCyana * user.toleranceLolUniue2;
        
        count.cyana = count.cyana + 1;
        
    elseif user.bi_eNOE == 1
        
        cyanaUpl(count.cyana) = cyanaOutput(j);
        cyanaLol(count.cyana) = cyanaOutput(j);
        
            if cyanaOutput(j).unique == 2 
        
            cyanaUpl(count.cyana).rCyanaCorr  = cyanaUpl(count.cyana).rCyana * user.toleranceUplUniue2;
            cyanaLol(count.cyana).rCyanaCorr  = cyanaLol(count.cyana).rCyana * user.toleranceLolUniue2;
                    
            elseif cyanaOutput(j).unique == 1
        
            cyanaUpl(j).rCyanaCorr  = cyanaUpl(count.cyana).rCyana * user.toleranceUplUniue1;
            cyanaLol(j).rCyanaCorr  = cyanaLol(count.cyana).rCyana * user.toleranceLolUniue1;
            end
        
        count.cyana = count.cyana + 1;
                    
    end
    
end

count.cyana =  count.cyana - 1;


%methyl are allways applied also for bi-directional eNOEs!!!

for j = 1:length(cyanaUpl)
    
        cyanaUpl(j).bothAtoms = [cyanaUpl(j).numcode.ATOMi, cyanaUpl(j).numcode.ATOMj];
                
    if  size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.methylPseudo(1:8))), 2) == 1
        
        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr * user.toleranceUplMeth;
    
    elseif  size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.methylPseudo(9:10))), 2) == 1
        
        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr * (user.toleranceUplMeth *  user.toleranceUplMethylenePseudo);
        
    elseif size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.methylPseudo(1:8))), 2) == 2
        
        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr * user.toleranceUplMeth^2;
        
    elseif size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.methylPseudo(9:10))), 2) == 2
        
        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr * (user.toleranceUplMeth *  user.toleranceUplMethylenePseudo)^2;
        
    elseif size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.methylPseudo)), 2) == 0
        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr;
        
    end
end

for j = 1:length(cyanaLol)
    
        cyanaLol(j).bothAtoms = [cyanaLol(j).numcode.ATOMi, cyanaLol(j).numcode.ATOMj];
                
    if  size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.methylPseudo(1:8))), 2) == 1
        
        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr * user.toleranceLolMeth;
        
    elseif  size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.methylPseudo(9:10))), 2) == 1
        
        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr * (user.toleranceLolMeth * user.toleranceLolMethylenePseudo);
        
    elseif size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.methylPseudo(1:8))), 2) == 2
        
        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr * user.toleranceLolMeth^2;
        
    elseif size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.methylPseudo(9:10))), 2) == 2
        
        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr * (user.toleranceLolMeth * user.toleranceLolMethylenePseudo)^2;
        
    elseif size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.methylPseudo)), 2) == 0
        
        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr;
        
    end
end

% methylene Pseudo corrections are allways applied also for bi-directional eNOEs!!!

for j = 1:length(cyanaUpl)
    
        cyanaUpl(j).bothAtoms = [cyanaUpl(j).numcode.ATOMi, cyanaUpl(j).numcode.ATOMj];
                
    if  size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.methylenePseudo)), 2) == 1
        
        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr * user.toleranceUplMethylenePseudo;
        
    elseif size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.methylenePseudo)), 2) == 2
        
        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr * user.toleranceUplMethylenePseudo^2;
        
    elseif size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.methylenePseudo)), 2) == 0
        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr;
        
    end
end

for j = 1:length(cyanaLol)
    
        cyanaLol(j).bothAtoms = [cyanaLol(j).numcode.ATOMi, cyanaLol(j).numcode.ATOMj];
                
    if  size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.methylenePseudo)), 2) == 1
        
        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr * user.toleranceLolMethylenePseudo;
        
    elseif size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.methylenePseudo)), 2) == 2
        
        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr * user.toleranceLolMethylenePseudo^2;
        
    elseif size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.methylenePseudo)), 2) == 0
        
        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr;
        
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%  eNOES are set to 8 Angstroms if any proton involved is a aromatic  %%%
%%%  and if the data is uni-directional, except for TRP and HIS (ring flip)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% conditions.all.aromaticsP

for j = 1:length(cyanaUpl)
    
        cyanaUpl(j).plotFlag = 1;
    
        cyanaUpl(j).bothAtoms = [cyanaUpl(j).numcode.ATOMi, cyanaUpl(j).numcode.ATOMj];
                
    if  size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.aromaticsP)), 2) == 1 ...
        || size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.aromaticsP)), 2) == 2 
    
        if cyanaUpl(j).unique == 1 % if aromatic distance is uni-directional set to 8 angstroem
            
            if size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.aromaticsP)), 2) == 1
            
                if  ismember(cyanaUpl(j).numcode.ATOMi, conditions.all.aromaticsP) && ~ismember(cyanaUpl(j).numcode.ATOMj, conditions.all.aromaticsP)
                    
                    if strcmp(cyanaUpl(j).ASSi_resName, 'TRP') || strcmp(cyanaUpl(j).ASSi_resName,'HIS')

                        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr;
                
                    else
                    
                        cyanaUpl(j).rCyanaCorr = 8;
                    
                    end
                    
                elseif ismember(cyanaUpl(j).numcode.ATOMj, conditions.all.aromaticsP) && ~ismember(cyanaUpl(j).numcode.ATOMi, conditions.all.aromaticsP) 
                    
                    if strcmp(cyanaUpl(j).ASSj_resName, 'TRP') || strcmp(cyanaUpl(j).ASSj_resName,'HIS')

                        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr;
                        
                    elseif cyanaUpl(j).numcode.ASSj == cyanaUpl(j).numcode.ASSnorm && cyanaUpl(j).numcode.ATOMj == cyanaUpl(j).numcode.ATOMnorm
                    
                        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr;
                                        
                    else
                    
                    cyanaUpl(j).rCyanaCorr = 8;
                    
                    end
                    
                end
             
            elseif size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.aromaticsP)), 2) == 2 
                if strcmp(cyanaUpl(j).ASSi_resName,'TRP') || strcmp(cyanaUpl(j).ASSi_resName,'HIS') ...
                && (strcmp(cyanaUpl(j).ASSj_resName,'TRP') || strcmp(cyanaUpl(j).ASSj_resName,'HIS'))

                    cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr;
                
                else
                    cyanaUpl(j).rCyanaCorr = 8;
                end
            end
        end
        
    elseif size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.aromaticsP)), 2) == 0
        
        cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr;
        
    end
end

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

for j = 1:length(cyanaLol)
    
        cyanaLol(j).plotFlag = 1;
    
        cyanaLol(j).bothAtoms = [cyanaLol(j).numcode.ATOMi, cyanaLol(j).numcode.ATOMj];
                
    if  size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.aromaticsP)), 2) == 1 ...
        || size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.aromaticsP)), 2) == 2 
    
        if cyanaLol(j).unique == 1
            
            if size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.aromaticsP)), 2) == 1
            
                if  ismember(cyanaLol(j).numcode.ATOMi, conditions.all.aromaticsP) && ~ismember(cyanaLol(j).numcode.ATOMj, conditions.all.aromaticsP)
                    
                    if strcmp(cyanaLol(j).ASSi_resName, 'TRP') || strcmp(cyanaLol(j).ASSi_resName,'HIS')
                        
                        cyanaLol(j).plotFlag = 1;       % plotFlag = 0;
                        cyanaLol(j).rCyanaCorr  = cyanaLol(j).rCyanaCorr;
                
                    else
                        
                        cyanaLol(j).plotFlag = 1;       % plotFlag = 0;
                        cyanaLol(j).rCyanaCorr  = cyanaLol(j).rCyanaCorr;
                    
                    end
                    
                elseif ismember(cyanaLol(j).numcode.ATOMj, conditions.all.aromaticsP) && ~ismember(cyanaLol(j).numcode.ATOMi, conditions.all.aromaticsP) 
                    
                    if strcmp(cyanaLol(j).ASSj_resName, 'TRP') || strcmp(cyanaLol(j).ASSj_resName,'HIS')

                        cyanaLol(j).plotFlag = 1;       % plotFlag = 0;
                        cyanaLol(j).rCyanaCorr  = cyanaLol(j).rCyanaCorr;
                        
                    elseif cyanaLol(j).numcode.ASSj == cyanaLol(j).numcode.ASSnorm && cyanaLol(j).numcode.ATOMj == cyanaLol(j).numcode.ATOMnorm
                    
                        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr;
                
                    else
                    
                        cyanaLol(j).plotFlag = 1;       % plotFlag = 0;
                        cyanaLol(j).rCyanaCorr  = cyanaLol(j).rCyanaCorr;
                    
                    end
                    
                end
             
            elseif size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.aromaticsP)), 2) == 2 
                if strcmp(cyanaLol(j).ASSi_resName,'TRP') || strcmp(cyanaLol(j).ASSi_resName,'HIS') ...
                && (strcmp(cyanaLol(j).ASSj_resName,'TRP') || strcmp(cyanaLol(j).ASSj_resName,'HIS'))

                    cyanaLol(j).plotFlag = 1;           % plotFlag = 0;
                    cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr;
                
                else
                    cyanaLol(j).plotFlag = 1;           % plotFlag = 0;   
                    cyanaLol(j).rCyanaCorr  = cyanaLol(j).rCyanaCorr;
                end
            end
        end
        
    elseif size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.aromaticsP)), 2) == 0
        
        cyanaLol(j).plotFlag = 1; 
        cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr;
        
    end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% we shift aromaticsP by 11% to shorter distance if  ATOMj is aromatic and 
% normalized to non aromatic proton and uni-directional, since in HSQC aromats 
% are 50% lower in intensity due to pulse imperfection and the distance therefore appears to long
% REMEMBER, the HSQC follows the mixing time so if we start on aromat and
% go to other proton, there is no problem

for j = 1:length(cyanaUpl)
    
 if cyanaUpl(j).rCyanaCorr ~= 8
        
     cyanaUpl(j).bothAtoms = [cyanaUpl(j).numcode.ATOMi, cyanaUpl(j).numcode.ATOMj];
        
    if size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.aromatics)), 2) == 1 ...
        || size(find(ismember(cyanaUpl(j).bothAtoms, conditions.all.aromatics)), 2) == 2
       	if  ~ismember(cyanaUpl(j).numcode.ATOMi, conditions.all.aromatics) && ismember(cyanaUpl(j).numcode.ATOMj, conditions.all.aromatics) ...
                && cyanaUpl(j).unique == 1
          	if cyanaUpl(j).numcode.ASSj == cyanaUpl(j).numcode.ASSnorm && cyanaUpl(j).numcode.ATOMj == cyanaUpl(j).numcode.ATOMnorm
              	cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr;
            else
              	cyanaUpl(j).rCyanaCorr = cyanaUpl(j).rCyanaCorr * 0.89;
          	end
     	end
    end
 end
end

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

for j = 1:length(cyanaLol)        
     cyanaLol(j).bothAtoms = [cyanaLol(j).numcode.ATOMi, cyanaLol(j).numcode.ATOMj];
                
    if size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.aromatics)), 2) == 1 ...
        || size(find(ismember(cyanaLol(j).bothAtoms, conditions.all.aromatics)), 2) == 2
        if  ~ismember(cyanaLol(j).numcode.ATOMi, conditions.all.aromatics) && ismember(cyanaLol(j).numcode.ATOMj, conditions.all.aromatics)...
                && cyanaLol(j).unique == 1
            if cyanaLol(j).numcode.ASSj == cyanaLol(j).numcode.ASSnorm && cyanaLol(j).numcode.ATOMj == cyanaLol(j).numcode.ATOMnorm
                cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr;
            else
                cyanaLol(j).rCyanaCorr = cyanaLol(j).rCyanaCorr * 0.89;
            end
        end
    end

end


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

%%%%%%%%%%%%%%%%%%%%%% upl and lol write out %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if user.bi_eNOE == 1
    fid = fopen(sprintf('./%s/%s_all_aroBiUni8_TRP_HIS_11perShift.upl', work.finalCollected, user.timeStamp),'wt');
elseif user.bi_eNOE == 2
    fid = fopen(sprintf('./%s/%s_bi_eNOE_aroBi_TRP_HIS_11perShift.upl', work.finalCollected, user.timeStamp),'wt');
end
    
for j = 1:length(cyanaUpl)
            
    if cyanaUpl(j).plotFlag == 1
            fprintf(fid, '%4d%5s%5s%8d%5s%5s%16.2f      # rEff= %6.2f       uni-biDir = %4d        Ass-Norm = %4d   %4s\r\n'...
                , cyanaUpl(j).numcode.ASSi,     cyanaUpl(j).ASSi_resName,   cyanaUpl(j).ATOMi...
                , cyanaUpl(j).numcode.ASSj,     cyanaUpl(j).ASSj_resName,   cyanaUpl(j).ATOMj...
                , cyanaUpl(j).rCyanaCorr,           cyanaUpl(j).rEff,           cyanaUpl(j).unique, cyanaUpl(j).ASSnorm, cyanaUpl(j).ATOMnorm);
    end
    
end

fclose(fid);

if user.bi_eNOE == 1
    fid = fopen(sprintf('./%s/%s_all_aroBiUni8_TRP_HIS_11perShift.lol', work.finalCollected, user.timeStamp),'wt');
elseif user.bi_eNOE == 2
    fid = fopen(sprintf('./%s/%s_bi_eNOE_aroBi_TRP_HIS_11perShift.lol', work.finalCollected, user.timeStamp),'wt');
end

for j = 1:length(cyanaLol)
            
    if cyanaLol(j).plotFlag == 1
            fprintf(fid, '%4d%5s%5s%8d%5s%5s%16.2f      # rEff= %6.2f       uni-biDir = %4d         Ass-Norm = %4d   %4s\r\n'...
                , cyanaLol(j).numcode.ASSi,     cyanaLol(j).ASSi_resName,   cyanaLol(j).ATOMi...
                , cyanaLol(j).numcode.ASSj,     cyanaLol(j).ASSj_resName,   cyanaLol(j).ATOMj...
                , cyanaLol(j).rCyanaCorr,           cyanaLol(j).rEff,           cyanaLol(j).unique, cyanaLol(j).ASSnorm, cyanaLol(j).ATOMnorm);
    end
    
end

fclose(fid);

%%%%%%%%%%%%%%%%%%%%%%% chimera eNOE network %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if user.bi_eNOE == 1
    fid = fopen(sprintf('./%s/%s_chimera_all_eNOEnetwork', work.finalCollected, user.timeStamp),'wt');
elseif user.bi_eNOE == 2
    fid = fopen(sprintf('./%s/%s_chimera_bi_eNOE_eNOEnetwork', work.finalCollected, user.timeStamp),'wt');
end

fprintf('\n\n%s\r\n','TO DISPLAY THE eNOE NETOWORK in CHIMERA:')
fprintf('%s\r\n','Edit a copy of your pdb to contain only one confomer')
fprintf('%s\r\n\n','Start chimera; load structure; Actions > Atoms/Bonds > Show then Tools > Depiction > pseudoBond Reader')

for j = 1:length(cyanaUpl)
           
    if cyanaUpl(j).plotFlag == 1
        
        if user.bi_eNOE == 1

            fprintf(fid, ':%d@%s :%d@%s yellow \n', cyanaUpl(j).numcode.ASSi...
                , cyanaUpl(j).ATOMi, cyanaUpl(j).numcode.ASSj, cyanaUpl(j).ATOMj);
        
        elseif user.bi_eNOE == 2

            fprintf(fid, ':%d@%s :%d@%s green \n', cyanaUpl(j).numcode.ASSi...
                , cyanaUpl(j).ATOMi, cyanaUpl(j).numcode.ASSj, cyanaUpl(j).ATOMj);
        
        end
    end
    
end

fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%