#######################################################################################
# This file is provided under the Creative Commons Attribution 3.0 license.
#
# You are free to share, copy, distribute, transmit, or adapt this work
# PROVIDED THAT you attribute the work to the authors listed below.
# For more information, please see the following web page:
# http://creativecommons.org/licenses/by/3.0/
#
# This file is a component of the SflE Scientific workFLow Environment for reproducible 
# research, authored by the Huttenhower lab at the Harvard School of Public Health
# (contact Curtis Huttenhower, chuttenh@hsph.harvard.edu).
#
# If you use this environment, the included scripts, or any related code in your work,
# please let us know, sign up for the SflE user's group (sfle-users@googlegroups.com),
# pass along any issues or feedback, and we'll let you know as soon as a formal citation
# is available.
#######################################################################################
# StartImport
import sfle
import sys

Import( "*" )
pE = DefaultEnvironment( )
# EndImport

"""
Header
======

Constant and file definitions.
"""

# StartConstants
c_strPreDemo			= "demo_"
# EndConstants

# StartInFiles
c_fileInputDataPCL		= sfle.d( pE, fileDirInput, "data.pcl" )
c_fileInputMetadataPCL	= sfle.d( pE, fileDirInput, "metadata.pcl" )
# EndInFiles

# StartOutFiles
c_fileNormalizedPCL		= sfle.d( pE, fileDirTmp, sfle.rebase( c_fileInputDataPCL ) )
c_fileMergedPCL			= sfle.d( pE, fileDirOutput, "merged.pcl" )
c_fileReportTXT			= sfle.d( pE, fileDirOutput, "report.txt" )
# EndOutFiles

# StartPrograms
c_fileProgNormalize		= sfle.d( pE, sfle.c_strDirSrc, "normalize.py" )
c_fileProgReport		= sfle.d( pE, sfle.c_strDirSrc, "report.py" )
# EndPrograms

"""
Processing module 1
===================

Generate an intermediate normalized PCL file from the input data.
"""

# StartNormalize
sfle.pipe( pE, c_fileInputDataPCL, c_fileProgNormalize, c_fileNormalizedPCL )
# EndNormalize

"""
Processing module 2
===================

Generate intermediate transposed TSV files from the normalized data and the
input metadata.  Store the results in ``afileTSVs`` for convenience, since
A) the filenames are autogenerated, and B) they're provided as inputs to
``merge_tables.py`` below.  
"""

# StartTranspose
afileTSVs = []
for fileIn in (c_fileInputMetadataPCL, c_fileNormalizedPCL):
	fileOut = sfle.d( pE, fileDirTmp, sfle.rebase( fileIn, sfle.c_strSufPCL, sfle.c_strSufTSV ) )
	afileTSVs += sfle.pipe( pE, fileIn, c_fileProgTranspose, fileOut )
# EndTranspose

"""
Processing module 3
===================

Generate an intermediate (still transposed) merge file from the transposed data and
metadata.
"""

# StartMerge
fileTSV = sfle.d( pE, fileDirTmp, sfle.rebase( c_fileMergedPCL, sfle.c_strSufPCL, sfle.c_strSufTSV ) )
sfle.cmd( pE, c_fileProgMergeTables, fileTSV, [[False, f] for f in afileTSVs] )
# EndMerge

"""
Processing modules 4-5
======================

Create our two main output files, a merged normalized PCL and a plain text
report describing it.  Critical that the report is ``Default``, which
means it will be produced as a target, as will all files it depends on.
"""

# StartOutput
sfle.pipe( pE, fileTSV, c_fileProgTranspose, c_fileMergedPCL )

sfle.pipe( pE, c_fileMergedPCL, c_fileProgReport, c_fileReportTXT )
Default( c_fileReportTXT )
# EndOutput
