% 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: definition of options of simulations to run for paper
% 
%
% Inputs: below
%
% Outputs: saved simulations, and writtingprogress output if requested
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Notes: 
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

% organismModels: Names of parsed .mat files, must be exact, (and in this folder)
%
% organismIDs: Name of the field/structure of simulation that contains information about
%              the model and simulation. This should be unique if mutliple instances of
%              one model is to be used in one simulation/environemnt/coculture
%
% organismInitialBiomass: initial concentration of cells, in gDCW/L
%
% initializerHandles: function to initialize organism state
%                               - FBA_EB_intitializerPreculture
%
% fluxConstraintHandles: function to limit host fluxes ( by media, regulation)
%                               - FBA_EB_rFBAapplyConstraints
%
% accumulationSetHandles: function that allows accumulation of some (if any) metabolites
%                               - FBA_EB_accumulationViralHostMetConnect
%                               - FBA_EB_accumulationNoHostPooling
%
% viralConstraintHandles: function to call in the case that the virus draws sufficient
%                         recourse to cause glpk failure, this determines how resources are
%                         allocated to the virus
%
% media: media file, .txt, including exhaustable and replenished nutrients. Don't include
%        "Media" part of name nameMedia.
%
% viralOutputSubSteps: number of time data points to save for viral runs in between each
%                      FBA run. 
%
% viralODEsolverHandles: matlab ODE solver
%                               - see matlab documentation
% tTotal
% tStepSec
% mediaAllocationHandle
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

function [] = FBA_EB_batchRun( showProgress )


% User options:
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% organismModels, organismIDs (or now set to models?), media, tTotal, tStep, organismInitialBiomass,
% viralOutputSubSteps, viralConstraintHandles, accumulationSetHandles, initializerHandles, 
% fluxConstraintHandles, viralODEsolverHandles, viralOutputIDs, mediaAllocationHandle, dynamicFlag

batchName = 'simulationsMatlab';

optionsSim = {...
... simulationLocation(folder, name),                   organismModels,          organismIDs(not used),	media,      tTotal, tStepSec,   ...InitialBiomass,      viral...SubSteps,       viralConstraintHandles,                             accumulationSetHandles,                   viralRequestHandle,        modelarSingleStepHandle           
... (infected)
 	{'simulationsMatlab','acetateInfectedSimulation'},      {'Ecoli_EB_T7_37Growth_EB_'},         {'ecoliEB'},           'Media_EB_acetateAerobic',    	0.4,     	10,         {0.05},                5,                       {'FBA_EB_limitViralMetToMaxDNTPsAfterMaxHostBiomass'},    {'FBA_EB_accumulationViralHostMetConnect'}, {'FBA_EB_viralRequestMax'}, {'FBA_EB_modularSingleStepPremtiveLimit'},{ {} };
	{'simulationsMatlab','succinateInfectedSimulation'},    {'Ecoli_EB_T7_37Growth_EB_'},         {'ecoliEB'},           'Media_EB_succinateAerobic',    	0.4,     	10,         {0.05},                5,                	{'FBA_EB_limitViralMetToMaxDNTPsAfterMaxHostBiomass'},    {'FBA_EB_accumulationViralHostMetConnect'}, {'FBA_EB_viralRequestMax'}, {'FBA_EB_modularSingleStepPremtiveLimit'},{ {} };
 	{'simulationsMatlab','glucoseInfectedSimulation'},      {'Ecoli_EB_T7_37Growth_EB_'},         {'ecoliEB'},           'Media_EB_glucoseAerobic',        0.4,     	10,         {0.05},                5,                	{'FBA_EB_limitViralMetToMaxDNTPsAfterMaxHostBiomass'},    {'FBA_EB_accumulationViralHostMetConnect'}, {'FBA_EB_viralRequestMax'}, {'FBA_EB_modularSingleStepPremtiveLimit'}, { {} };
 	{'simulationsMatlab','tryptoneInfectedSimulation'},     {'Ecoli_EB_T7_37Growth_EB_'},         {'ecoliEB'},           'Media_EB_tryptoneAerobic',           0.4,     10,        	{0.05},                5,                	{'FBA_EB_limitViralMetToMaxDNTPsAfterMaxHostBiomass'},    {'FBA_EB_accumulationViralHostMetConnect'}, {'FBA_EB_viralRequestMax'}, {'FBA_EB_modularSingleStepPremtiveLimit'},{ {} };
... short, uninfected
    {'simulationsMatlab','tryptoneUninfectedSimulation'},    {'Ecoli_EB_'},	{'ecoliEB'},	'Media_EB_tryptoneAerobic',     7,         60,         {0.0039},                 5,                	{'FBA_EB_limitViralMetToMaxDNTPsAfterMaxHostBiomass'},    {'FBA_EB_accumulationViralHostMetConnect'}, {'FBA_EB_viralRequestMax'}, {'FBA_EB_modularSingleStepPremtiveLimit'}, { {} };
    {'simulationsMatlab','glucoseUninfectedSimulation'},     {'Ecoli_EB_'},	{'ecoliEB'},	'Media_EB_glucoseAerobic',      7,         60,         {0.0025},                 5,                	{'FBA_EB_limitViralMetToMaxDNTPsAfterMaxHostBiomass'},    {'FBA_EB_accumulationViralHostMetConnect'}, {'FBA_EB_viralRequestMax'}, {'FBA_EB_modularSingleStepPremtiveLimit'}, { {} };
    {'simulationsMatlab','acetateUninfectedSimulation'},     {'Ecoli_EB_'},	{'ecoliEB'},	'Media_EB_acetateAerobic',      7,         60,         {0.0122},                 5,                	{'FBA_EB_limitViralMetToMaxDNTPsAfterMaxHostBiomass'},    {'FBA_EB_accumulationViralHostMetConnect'}, {'FBA_EB_viralRequestMax'}, {'FBA_EB_modularSingleStepPremtiveLimit'}, { {} };
    {'simulationsMatlab','succinateUninfectedSimulation'},   {'Ecoli_EB_'},	{'ecoliEB'},	'Media_EB_succinateAerobic',	7,         60,         {0.0084},                 5,                	{'FBA_EB_limitViralMetToMaxDNTPsAfterMaxHostBiomass'},    {'FBA_EB_accumulationViralHostMetConnect'}, {'FBA_EB_viralRequestMax'}, {'FBA_EB_modularSingleStepPremtiveLimit'}, { {} };
   };

nSim = size( optionsSim, 1 );  

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

fileLabels = optionsSim(:,1);
optionsSim = optionsSim(:,2:end);

% Folder for storage
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
% data information 
timeVect = clock;
% prepare month string
if timeVect(2) < 10
    monthStr = ['0',num2str(timeVect(2))];
else
    monthStr = num2str(timeVect(2));
end
% prepare day string
if timeVect(3) < 10
    dayStr = ['0',num2str(timeVect(3))];
else
    dayStr = num2str(timeVect(3));
end
% prepare date string
dateStr = [ num2str(timeVect(1)), monthStr, dayStr ];

folderStr = [ dateStr, strrep(batchName, ' ', '') ];

noLabel = false;
for iLabel = 1 :nSim
    noLabel = noLabel || isempty( fileLabels{iLabel} );
end

if ~exist(folderStr,'dir') && noLabel
    mkdir(folderStr)
end % if folder does not exist

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



% Viral reffernce runs
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%

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



% Loop through entering options in simulation form
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
parfor iSim = 1 : nSim
    
    runOneSim( fileLabels, optionsSim, showProgress, folderStr, iSim)  
    
end % iSim

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

end % function

function [] = runOneSim( fileLabels, optionsSim, showProgress, folderStr, iSim)  
    
    % options from user input
    simulation.setup.organismModels = optionsSim{iSim, 1 };
    simulation.setup.organismIDs = optionsSim{iSim, 1 }; % replaceIDs entered with model names
    simulation.setup.media = optionsSim{iSim, 3 };
    simulation.tTotal = optionsSim{iSim, 4 };
    simulation.tStep = optionsSim{iSim, 5 } / 60 / 60; % sec to hour
    simulation.setup.organismInitialBiomass = optionsSim{iSim, 6 };
    simulation.setup.viralOutputSubSteps = optionsSim{iSim, 7 };
    simulation.setup.viralConstraintHandles = optionsSim{iSim, 8 };
    simulation.setup.accumulationSetHandles = optionsSim{iSim, 9 };
    simulation.setup.viralRequestHandle = optionsSim{iSim, 10 };
    simulation.setup.modularSingleStepHandle = optionsSim{iSim, 11 };
    simulation.setup.knockouts = optionsSim{iSim, 12 };
    
    % options that do not change between runs
    simulation.setup.initializerHandles = { 'FBA_EB_intitializerPreculture' };
    simulation.setup.fluxConstraintHandles = { 'FBA_EB_applyRegulationAndBounds' };
    simulation.setup.viralOutputIDs = { {'vMRNA3o5','vGP1','vGP10A','vGenome','vProcapsid','vT7'} };
    simulation.setup.viralODEsolverHandles = { 'ode15s' };
    simulation.mediaAllocationHandle = @FBA_EB_mediaAllocateSimpleProportion;
    simulation.dynamicFlag = true;
    
    % deal with viral reffernce runs
    simulation.setup.viralReferenceRunFlag = [ true ];
    
    % run it
    folderPath = folderStr; 
    if showProgress
       nUpdates = 10;
    else
       nUpdates = 0;
    end
       
    FBA_EB_simulationPrepRunSave(simulation, folderPath, nUpdates, fileLabels{iSim}, showProgress);
end % % function runOneSim



