%% Bargrouping
% You can find all this in DOGMA or just use it here
% You need barcodeGen and oS from assembly_pipeline_yeast
% You need 'alphaN','alphav','betaN','betanu' from parametrization
% Output from here are many, cGenAll works fine for most downstream
% analysis 
% one can align bargroups to reference afterwards using many of the
% functions in figs

alphaNu1=alphav;
alphaNu2=betanu;
alphaN1=alphaN;
alphaN2=betaN;


sets.minOverlap = 150; %w 120 or 150

% For consistency check
sets.scDiffSetting = 0.05; % local stretch when adding a barcode to a group 0.01 antes
sets.pxDifSetting = 10; % dist of pixels from actual bargroup 100
pthresh =  0.05; % false positive control


showFig = 1; % whether to show output figure



timestamp = datestr(clock(), 'yyyy-mm-dd_HH_MM_SS'); % timestamp for results

%
% both on local and global. Only uses the oS structure as input +
% parameters from table 2
[sortedVals, sortedIds,localScore,partialScore,lenA,lenB,partialLength,pvalLocal,pvalLeftOver, pvalCombined, sortedValsBad, sortedIdsBad] = ...
    calculate_sorted_pvals(oS,sets.minOverlap, alphaNu1, pthresh,[],alphaN1,alphaNu2,alphaN2); %localCCStruct

% [sortedVals, sortedIds,localScore,partialScore,lenA,lenB,partialLength,pvalLocal,pvalLeftOver, pvalCombined, sortedValsBad, sortedIdsBad] = ...
%     calculate_sorted_pvals(oS,sets.minOverlap, alphaNu,pthresh,[],alphaN); %localCCStruct

%This block is enough for nth best plot

%% barcode islands plots

% number of minimum amount of bargroup members
nbar=3;

sortedValsGood = sortedVals(~isnan(sortedVals));
sortedIdsGood  = sortedIds(~isnan(sortedVals));
localScore = localScore(~isnan(sortedVals));

% sortedValsGood(4190:end) = nan; % change alphaNu instead of this

% Main barcode islands script:
import Core.barcode_island_output;
[barsetGen, outConsensus, coverage, consensus, islandElts, islandPx,cGenAll,barcodeIslandsData, barStruct,barIslands,bargroup_meancov,meanCC,varCC,SNR] =...
    barcode_island_output(sortedValsGood,sortedIdsGood, oS, barcodeGen,timestamp,sets.scDiffSetting,sets.pxDifSetting, [],showFig,nbar,pxCov);

