% Search for conserved domains.
% This creates and analyzes signals acording to Beta size windows.
% Based on alignment free method by Spectrum comparison, this function
% constructs a phylogenetic tree. Taking into account the most related
% sequences, it fractions its corresponding signals into \beta size
% windows. After window redundancy elimination, it will progressively
% compare the windows with each of the remaining signals, sliding an
% increase step at a time and evaluate if the comparison is valid
% considering a treshold for the comparison tools score. If so, it will
% tag the position of the signal where the "hit" occurred. The function input are
% the sequence set {seq1;seq2...seqn,1}, the headers set {header1;
% header2...headern,1} and the alpha value decided by sequence similarity,
% the beta size of the windows and the increase step of the sliding window.
% The output is the window sequence, the window hits and the hits position for each window.
% Each column corresponds to each of the original sequences compared, within,
% the cell corresponds to the windows generated, and within the values for
% each variable. In Windowsequence the value will be the window sequence,
% in WindowHits will be a vector with the result of each of the sliding
% comparisons 1 for a hit 0 otherwise; in WindowHitPosition the value will
% be the point of the signal where a hit exist (it corresponds to
% Windowhits).
% 7may11
% B


%% this should be the main function to "Beta progresive alignment".
function [Windowsequence,WindowHits,WindowHitPosition]=progresive2(seq,header,alpha,beta,increase)
%% Generate tree and get treeinfo.pointers && treeinfo.names
% Get distance Matrix From Sequences 
    DistFFTTMFOcorr=FFTNonFiltered(seq,alpha);
% Generate NJ tree
    tree=seqneighjoin(DistFFTTMFOcorr,'equivar',header);
% Get treeinfo.pointers && treeinfo.names
    info=get(tree);
    pointers=info.Pointers;
    names=info.LeafNames;
% Implement Branch column in "pointers" (this will generate a vector in the
% variable containing the branch position corresponding to each pointer
% pairing)
    for m=1:numel(pointers(:,1))
        pointers(m,3)=m+numel(pointers(:,1))+1;
    end
    leavesorder=orderpointers(pointers);
 %% Start progresive alignment
% Get next pairs to consider
    for x=1:numel(seq)
       [seqtreeorder(x),headertreeorder(x)]=getseqpair(leavesorder,names,seq,header,x); 
    end
%% Create windows to work with

[windowsnext,WindowsSeqpdist,WindowsOrSignal,beta]=Createwindowsforprogresive(seq,alpha,beta); 
    
% Start progresive
stag=1;
brk=0;
Hits=WindowsSeqpdist;
for x1=1:numel(windowsnext)
    Hitswin{x1}=1;
end
    for i=1:numel(leavesorder)
       if brk==1
    return
        end
        
        [windowsnext,WindowsSeqpdist,WindowsOrSignal,Hitswin,Hitspos]=localalignmentwindows2(seqtreeorder(i),alpha,beta,increase,windowsnext,WindowsSeqpdist,WindowsOrSignal);
        A{i}=windowsnext;
        B{i}=WindowsSeqpdist;
        C{i}=WindowsOrSignal;
        H1{i}=Hitswin;
        H4{i}=Hitspos;
        stag=stag+1;
        
        for r=1:numel(H1{i})
            if H1{i}{r}<stag-2
                H2{i,r}=1;
                H3{i}=B{i};
            else
                H2{i,r}=0;
            end
                        
        end
     
        stag=stag+1;


    end
    Windowsequence=B;
    WindowHits=H1;
    WindowHitPosition=H4;
end