%mMasterParser.m    %%%%% Ali 2005_11_06 %%%%%

if ~exist('RunAftermMasterParserGUI','var') %Variable created in mMasterParserGUI (if run) overwrite variables here
    %%%%%%%%%%%%%%%%%%%%%%%%%User Defined%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %FastaFilename = strcat('C:\AliRes\_MatlabWork\MATLAB programs\Alignment_Analysis\Ribostral\Alignment_Parser\Nov1motif\Work done after\Nov1motif55_StructuralAlignment_Hand.fasta');   % This must be with full path.
    %motifs = {'Nov1_55'};  %This is the LAST PART of the name of the .m file containing Nodes information. The FIRST PART of the name must always be "Nodes_" (For organization purposes). Make sure you do not include .m into this name.
    FastaFilename = strcat('C:\AliRes\_MatlabWork\MATLAB programs\Alignment_Analysis\Ribostral\Alignment_Parser\Nov1motif\Work done after\Nov1motif55_StructuralAlignment_random.fasta');   % This must be with full path.
    motifs = {'Nov1_55'};  %This is the LAST PART of the name of the .m file containing Nodes information. The FIRST PART of the name must always be "Nodes_" (For organization purposes). Make sure you do not include .m into this name.
    numtodo = Inf;      %How many sequences to align. Use Inf to align all
    LoadPreviousParses = 0;
    %%%%%%%%%%%%%%%%%%%%%%%%%User Defined%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

warning off all




%motifs={'m5S'};
%motifs={'h5IL'};
%motifs={'h6IL'};
%motifs={'h7IL1'};
%motifs={'h7IL2'};
%motifs={'h8IL'};
%motifs={'h11IL1'};
%motifs={'h11IL2'};
%motifs={'h12IL'};
%motifs={'h15IL'};  %1 right insertion after the Motif
%motifs={'h16IL'};  %new kind of error at sequence 60... made that sequence exactly like crystal structure
%motifs={'h17IL1'}; %1 right and 1 left insertion after the Motif %%also gives same error as h16IL but at sequence 71

% for mot=1:length(motifs)
mot=1;
Filename = strcat(motifs{mot},'.mat');

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

    % -----------------------------------------

    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



    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),'_Parsed.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

if ~exist('Audio','var'),Audio=0;end
if Audio == 1
    try
        [Ys,Fs] = wavread(['..',filesep,'Audio',filesep,'done5']);
        sound(Ys,Fs); %wavplay(Ys,Fs,'async')
    end
end
