%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%          Read and organize the data         %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[libAss]                    = loadLibFun(user.assNameConvention);
[libPdb]                    = loadLibFun(user.pdbNameConvention);

pdb                         = pdbread(user.pdbFileName, 'ModelNum', 1);
[pdbNumCode, libPdb]        = pdb2numCode(pdb, libPdb, user);

rawData                     = prepData(libAss, pdbNumCode, user);

%%%%%%%%%%%%%%%%%%%%%%%% filtering assignments %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%% this has to happen before uniqueFlagFun %%%%%%%%%%%%%%%%%s

rawData                     = assignSeqFilterFun(rawData, user);

rawData                     = backboneFilterFun(rawData...
                                    , arrayfun(@(x) x.procAss.ATOMi_NUMCODE, rawData)...
                                    , arrayfun(@(x) x.procAss.ATOMj_NUMCODE, rawData),user); 

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

%write the sigmaData structure, also read intensity file if no tab file is used

[sigmaData, diagDecay]      = input2sigmaDataFun(rawData, user);

%%%%%%%%%           secondary structure                          %%%%%%%%%%

[sigmaData, diagDecay]      = secondStructFun(sigmaData, diagDecay);

%%%%%%%%%%%%%%%     split pseudo into individual atoms      %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%     and add stereo partner to sigmaData     %%%%%%%%%%%%%%% 

sigmaData                   = stereoAndQ2HFun(sigmaData, pdbNumCode, conditions);

%%%%%%%%%  determining the scaling factors for sigma effective   %%%%%%%%%%
%%%%%%%%%         determining the scaling factors for Izero      %%%%%%%%%%
%%%%%%%%%%       determining the HN scaling for D2O exchange     %%%%%%%%%%

sigmaData                   = scalingFactorsFun(sigmaData, conditions);

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

sigmaData                   = rPdb(pdbNumCode, sigmaData);

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