@echo off
SETLOCAL

rem Command-line arguments: SkylineAnalysis.bat <data-directory> <model sub-folder name> <m/z filter> <time filter> <skyline file>
set DATA_DIR=%1
set MODEL_NAME=%2
set FILTER_RES=%3
set FILTER_TIME=%4
set SKYLINE_FILE=%5
set SKYLINE_FILE=%SKYLINE_FILE:"=%

rem Because of an isotope distribution calculation bug in Skyline, avoid going lower than 5 ppm
rem for precursor filter tolerance
set PRECURSOR_RES=%FILTER_RES%
if %FILTER_RES% LSS 4 set PRECURSOR_RES=4

rem Report 1 exported from Skyline
set REPORT_NAME1=SWATHbenchmark
set REPORT_TEMPLATE1=lfqbench\SWATHbenchmark_report.skyr
set REPORT_FILE1=SWATHbenchmark.csv

rem Report 2 exported from Skyline (set to blank to skip)
set REPORT_NAME2=Precursor Peak Areas Scored HUMAN
set REPORT_TEMPLATE2=scripts\PeakAreasHUMAN.skyr
set REPORT_FILE2=PeakAreas.csv

set STARTTIME=%TIME%

set ROOT_DIR=%ROOT_ANALYSIS_DIR%
set SKYD_FILE="%ROOT_ANALYSIS_DIR%\%SKYLINE_FILE%d"

echo [%STARTTIME%] Running trial %MODEL_NAME%...
echo [%STARTTIME%] Running trial %MODEL_NAME%... >> %LOG%

rem GOTO IMPORT
rem GOTO REPORT
rem GOTO RSCRIPT

rem Get rid of the skyd file from previous analysis, so it doesn't get re-used in this analysis
rem if exist %SKYD_FILE% del /f /q %SKYD_FILE%

rem Save to new location to allow parallel processing
echo %SKYLINE_RUNNER% --timestamp --dir="%ROOT_DIR%" --in="%SKYLINE_FILE%" --out="%DATA_DIR%\%MODEL_NAME%\%SKYLINE_FILE%" --full-scan-product-res=%FILTER_RES% --full-scan-precursor-res=%PRECURSOR_RES% --full-scan-rt-filter-tolerance=%FILTER_TIME% >> %LOG%
%SKYLINE_RUNNER% --timestamp --dir="%ROOT_DIR%" --in="%SKYLINE_FILE%" --out="%DATA_DIR%\%MODEL_NAME%\%SKYLINE_FILE%" --full-scan-product-res=%FILTER_RES% --full-scan-precursor-res=%PRECURSOR_RES% --full-scan-rt-filter-tolerance=%FILTER_TIME% >> %LOG%

rem Do the analysis in the new location and export the first report
echo %SKYLINE_RUNNER% --timestamp --dir="%ROOT_DIR%" --in="%DATA_DIR%\%MODEL_NAME%\%SKYLINE_FILE%" --import-process-count=%PROCESS_COUNT% --import-all-files="%DATA_DIR%" --import-naming-pattern="(._[^_]*)$" --reintegrate-model-name="%MODEL_NAME%" --reintegrate-create-model --reintegrate-overwrite-peaks --save --report-add="%ROOT_ANALYSIS_DIR%\%REPORT_TEMPLATE1%" --report-conflict-resolution=overwrite --report-name="%REPORT_NAME1%" --report-file="%DATA_DIR%\%MODEL_NAME%\%REPORT_FILE1%" --report-invariant >> %LOG%
%SKYLINE_RUNNER% --timestamp --dir="%ROOT_DIR%" --in="%DATA_DIR%\%MODEL_NAME%\%SKYLINE_FILE%" --import-process-count=%PROCESS_COUNT% --import-all-files="%DATA_DIR%" --import-naming-pattern="(._[^_]*)$" --reintegrate-model-name="%MODEL_NAME%" --reintegrate-create-model --reintegrate-overwrite-peaks --save --report-add="%ROOT_ANALYSIS_DIR%\%REPORT_TEMPLATE1%" --report-conflict-resolution=overwrite --report-name="%REPORT_NAME1%" --report-file="%DATA_DIR%\%MODEL_NAME%\%REPORT_FILE1%" --report-invariant >> %LOG%
if %ERRORLEVEL% NEQ 0 GOTO END

:REPORT

rem If there is a second report, reopen the file and export the second report
if "%REPORT_NAME2%" == "" goto RSCRIPT

rem Output second report only for completed analysis
echo %SKYLINE_RUNNER% --timestamp --dir="%ROOT_DIR%" --in="%DATA_DIR%\%MODEL_NAME%\%SKYLINE_FILE%" --report-add="%ROOT_ANALYSIS_DIR%\%REPORT_TEMPLATE2%" --report-conflict-resolution=overwrite --report-name="%REPORT_NAME2%" --report-file="%DATA_DIR%\%MODEL_NAME%\%REPORT_FILE2%" --report-invariant >> %LOG%
%SKYLINE_RUNNER% --timestamp --dir="%ROOT_DIR%" --in="%DATA_DIR%\%MODEL_NAME%\%SKYLINE_FILE%" --report-add="%ROOT_ANALYSIS_DIR%\%REPORT_TEMPLATE2%" --report-conflict-resolution=overwrite --report-name="%REPORT_NAME2%" --report-file="%DATA_DIR%\%MODEL_NAME%\%REPORT_FILE2%" --report-invariant >> %LOG%
if %ERRORLEVEL% NEQ 0 GOTO END

:RSCRIPT

rem Run analysis scripts in R
set Analysis_Script="%ROOT_ANALYSIS_DIR%\lfqbench\generateReport.R"

%R_Script% %Analysis_Script% "%ROOT_DIR%\%DATA_DIR%\%MODEL_NAME%" "%DATA_DIR%" >> %LOG% 2>&1

rem Only do the second analysis if there is a second report
if "%REPORT_NAME2%" == "" goto END

set Analysis_Script="%ROOT_ANALYSIS_DIR%\scripts\detection.R"

%R_Script% %Analysis_Script% "%ROOT_DIR%\%DATA_DIR%\%MODEL_NAME%\%REPORT_FILE2%" >> %LOG% 2>&1

:END

set ENDTIME=%TIME%

rem Change formatting for the start and end times
for /F "tokens=1-4 delims=:.," %%a in ("%STARTTIME%") do (
   set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
for /F "tokens=1-4 delims=:.," %%a in ("%ENDTIME%") do (
   set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Calculate the elapsed time by subtracting values
set /A elapsed=end-start
rem we might have measured the time inbetween days
if %end% LSS %start% set /A elapsed=(24*60*60*100 - start) + end

rem Format the results for output
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%

set DURATION=%hh%:%mm%:%ss%.%cc%

echo. >> %LOG%
echo [%ENDTIME%] Completed trial %MODEL_NAME%... >> %LOG%
echo [%ENDTIME%] =^> Elapsed time: %DURATION%
echo [%ENDTIME%] =^> Elapsed time: %DURATION% >> %LOG%
echo. >> %LOG%
echo. >> %LOG%

ENDLOCAL