%mMasterParser_16sILs.m
warning off all

%motifs={'h5IL','h6IL','h7IL1','h7IL2','h8IL','h11IL1','h11IL2','h12IL','h15IL','h16IL','h17IL1'};
motifs={'h17IL2'};

Nodes_h17IL2

for mot=1:length(motifs)

    Filename = strcat(motifs{mot},'.mat');

    %     if exist(Filename,'file') > 0,            % load previous work, if it exists
    %         load(Filename);
    %         k = k + 1;                              % go to next sequence
    %         fprintf('Loaded previous computations\n');
    %     else                                      % define model, load sequences

    % -----------------------------------------
    p = [1 1 1 15 1 1 15 1 1 15 1 8 15 1 8 1]; % canonical state distn
    % preference for AU, CG, GU
    canonical = [0.99*p/sum(p) 0.01];  % normalize & deletion

    clear Node
    %Nodes_23S_1850_1881
    eval(['Nodes_',motifs{mot}])
    mpErrorCheck

    %%%%%%%%%%%%%%%%%%%%BE SURE TO MAKE THESE MUTUALLY EXCLUSIVE%%%%%%%%%Ali%%%%%%%%%
    %     %The following 2 lines used to browse for FASTA file to be aligned:
    %     [name,path] = uigetfile({'*.*','*.*'},'Pick FASTA file to be aligned');
    %     FastaFilename=strcat(path,name);

    %The following 2 lines used in case the browsing code above is commented out (it works in a particular case only):
    FastaPath='C:\AliRes\_MatlabWork\MATLAB programs\Alignment_Analysis\_TripleAlignmentAnalysis\Alignment_Parser\16S_Sequences\FastaExtracts\';
    FastaFilename=strcat(FastaPath,'16SelectedABEAllEc_',motifs{mot},'.fasta');
    %%%%%%%%%%%%%%%%%%%%BE SURE TO MAKE THESE MUTUALLY EXCLUSIVE%%%%%%%%%Ali%%%%%%%%%

    clear Sequence
    Sequence = pReadFASTA(FastaFilename);
    % -----------------------------------------

    k = 1;
    % end

    numtodo = 100;

    for j=k:min(length(Sequence),(k+numtodo-1)),
        [Node,Sequence] = pParseSequences(Node,Sequence,j,j);
        k = j;
        %         pDisplayMultipleAlignment(Node,Sequence);
        %         drawnow
        %   if mod(j,10) == 0,
        %     pDisplayMultipleAlignment(1,Node,Sequence);
        %     save(Filename,'Node','Sequence','k');
        %   end
    end

    ind= findstr('.',FastaFilename);
    RealignedFastaFilename = strcat(FastaFilename(1:ind(length(ind))-1),'_Realigned.fasta');
    fidOUT = fopen(RealignedFastaFilename,'w+');
    mpDisplayMultipleAlignment(fidOUT,Node,Sequence);%%%Ali, this will print all at once and only once
    save(Filename,'Node','Sequence','k');
    fclose(fidOUT);
    % save(Filename,'Node','Sequence','k');

    %     return

    %     for j=1:length(Sequence),
    %         %   fprintf('%s\t%s\n', Sequence(j).Fasta(6206:6311),Sequence(j).Organism);
    %         fprintf('  %g %s\t%s\n', j, Sequence(j).Fasta, Sequence(j).Organism);
    %     end
    %     end
end
