% Author: Elsa W. Birch, ewbirch@stanford.edu, Covert Lab - Stanford Bioengineering
% Date: 4/24/2011
% Version: associated with production of the figures for Birch et al. 2012 
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Summary: 
% 
%
% Inputs: 
%
% Outputs: 
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Notes: 
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


function [] = FBA_EB_organismSetupAndSave( hostModel, virusModel )

% User deifnitions/input
% ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 

% user defined list of organism(s)
% Assume for now that model folder is in this folder, and prep input accordingly


organismFolders = hostModel{1};% {'Ecoli_EB_Model'}; % this has to be the folder name, in format nameModel
organismIDs = hostModel(2);%{organismFolders{1}(1:end-5)}; % this becomes the mat file name with virus if applicable
% organismFolderPaths = {pwd};

organismModelName = hostModel{2};%'Ecoli_EB_Model';

organismModelFolder = hostModel{1};%'modelsFunctionsAndFlatFiles';
 
% % user defined options for each organism constrction
organismDefaultFluxBounds = {[-1000 1000]};
organismDefaultAutoAddGenesFlag = { true }; % not needed for JR, needed for JK
organismDefaultAutoAddGenesMaxBlattner = {5000}; % not used if flag is false
organismDefaultAutoAddGenesDelay = { 0.25 };
organismRegulationParse = { true };
organismTFgeneActivityInstant = { true }; % this sets up rules so that TF's when they 
% reference a gene it is the *current* activity and not the gene product activity which is
% delayed


viralFolders = {}; % this has to be the folder name, in format nameModel
viralIDs = {}; % this becomes the mat file name if applicable
viralDefaultFluxBounds = {};

if ~isempty( virusModel )
    % viral options
%     viralFolders = {'T7_37Growth_EB_VirusModel'}; % this has to be the folder name, in format nameModel
    viralFolders = virusModel(1);%{'modelsFunctionsAndFlatFiles'}; % this has to be the folder name, in format nameModel
    viralNames = virusModel(2);%{'T7_37Growth_EB_'}; % this has to be the folder name, in format nameModel

    viralIDs = viralNames ; % {viralFolders{1}(1: end - 10)}; % this becomes the mat file name if applicable
    viralDefaultFluxBounds = {[-500 500]};

end







% ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

% Temp variable
iOrgSetup = 1;

% organism full path
% [organismPath1, organismPath2, ~] = fileparts( organismFolderPaths{ iOrgSetup } );
% organismFullPath = fullfile(organismPath1, organismPath2, organismFolders{ iOrgSetup} );

% set up  parsing options
organismParseOptions.fluxBoundsDefault = organismDefaultFluxBounds{ iOrgSetup  };
organismParseOptions.geneAutoAddOptions.flag = organismDefaultAutoAddGenesFlag{ iOrgSetup  }; 
organismParseOptions.geneAutoAddOptions.maxGeneNumber = organismDefaultAutoAddGenesMaxBlattner{ iOrgSetup  }; 
organismParseOptions.geneAutoAddOptions.delayDefault = organismDefaultAutoAddGenesDelay{ iOrgSetup  }; 
organismParseOptions.regulationFlag = organismRegulationParse{ iOrgSetup };
organismParseOptions.TFgeneActivityInstant = organismTFgeneActivityInstant{ iOrgSetup };

% call organism parser organismModelFolder, organismModelName,
[model, organismModelWarnings] = FBA_EB_organismFileParse( organismModelFolder, organismModelName, organismParseOptions );
% sotre for ref
% model.fullPath = organismFullPath; % store full path, just for reference
model.parsed = now; % store dat & time for ref

% call, if given, parser on virus
if ~isempty( viralIDs )
    
    
    % set up  parsing options
    viralParseOptions.fluxBoundsDefault = viralDefaultFluxBounds{ iOrgSetup };
    
    % call viral parser
    %
    [modelInfected, viralParseWarnings] = ...
        FBA_EB_viralFileParse( viralFolders{ iOrgSetup}, viralNames{iOrgSetup} , model, viralParseOptions );

    
    % Unit conversions 
    % ( hard codded for now assuming standard units for FBA and that ODE model is in molecules/cell/sec )
    % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    % From ODEs:            | Conversion using:
    %                       |
    %   molecules rxn       |          mmol rxn                   cell
    % ( ------------- )     | ( ----------------------- ), ( ---------------- )
    %    cell * sec         |   NA x10^-3 molecules rxn     2.8x10^-13 gdcw
    %
    modelInfected.ode2fbaMetUnits = 5.9306e-9;
    % NOTE:  importantly this does not include the time conversion, as that will not always be
    % used concurrently with this conversion
    % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    
    % save

    save( fullfile(organismModelFolder, [organismIDs{ iOrgSetup } ,viralIDs{ iOrgSetup } ]), 'model','modelInfected')
    
else
    
    % save
    save(fullfile(organismModelFolder, organismIDs{ iOrgSetup } ), 'model')
    
end

% disp('Organism setup and save complete!');