% Author: Elsa W. Birch, ewbirch@stanford.edu, Covert Lab - Stanford Bioengineering
% Date: July 27th 2012
% Version: associated with production of the figures for Birch et al. 2012
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Summary: This function loads in a .mat file and writes and tab dlimited text 
% 			file .txt containing a table of reactiosn and the metabolis class in the FBA definition
% 
%
% Inputs: 	folderReadStr - A string contianing the folder path from which file is to be read.
%			fileString -  A string contianing the name of the file to be read.
%			fileSaveStr - A string with the path to and name of the .txt file where output is to be saved
%
% Outputs: text format table saved in <fileSaveStr>
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Notes: > Only intended for use with a single host organism FBA siulation, first organism in list is used.
% 		 > Currently this function is used only once as all organisms metabolic definition contains the same reactions and associated classes, 
%			and the outuput is only used in the analysis shownin figure S5
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function [] = FORMAT_EB_mat2textMetClass( folderReadStr, fileString, fileSaveStr)

simFields = {};
viralConcentrations = {};


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

% General preparation for output
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

load( fullfile(folderReadStr,fileString ) )

idxUnderscores = strfind( fileString, '_' );

modelString = simulation.organismList{1};

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


% prepare to output viral timescale varibles


% prepare to output host timescale variables
timeHost = simulation.t';
if simulation.(modelString).infectionFlag
    time = simulation.(modelString).simulation.tViralSubStep';
    idxHostData = 1 : simulation.(modelString).simulation.nViralSubSteps : length(time) ;
    % check host/virus time lineup
    idxCheck1 = length( idxHostData ) == simulation.nTimesteps;
    if idxCheck1
        idxCheck2 = all((timeHost - time( idxHostData )) < 1e-6) ;
    else
        idxCheck2 = false;
    end
    hostReactions = simulation.(modelString).modelInfected.rxnList;
    model2use = 'modelInfected';
else
    time = simulation.t';
    idxHostData = 1 : 1 : length(time) ;
    hostReactions = simulation.(modelString).model.rxnList;
    model2use = 'model';
end
 
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

% output host
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
headerString = '';
classString = '';
subClassString ='';
hostTableValues = zeros(length(time),0);
hostOutputIndicies = [];

nHostOut = 0;
for iHostOut = 1: length(hostReactions)
    rxnString = hostReactions{iHostOut};
    rxnID = strrep([rxnString(1),upper( rxnString(2:end))], '-','_');
    if isfield(  simulation.(modelString).(model2use).rxns, rxnID)
        idxRxn = simulation.(modelString).(model2use).rxns.(rxnID).idxRxn;
        nHostOut  = nHostOut  + 1;
        hostOutputIndicies( nHostOut ) = idxRxn;
        headerString = [ headerString, '\t',rxnID];
        classString = [ classString, '\t',simulation.(modelString).(model2use).rxns.(rxnID).metabolicClass];
        subClassString = [ subClassString, '\t',simulation.(modelString).(model2use).rxns.(rxnID).metabolicSubClass];

    end
end % for iHostOut

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



% combine & output
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

fid = fopen([fullfile(fileSaveStr,['metClass_',fileString]),'.txt'],'w');
fprintf(fid, [headerString(3:end), '\n']);
fprintf(fid, [classString(3:end), '\n']);
fprintf(fid, [subClassString(3:end), '\n']);
fclose(fid);

end


