#######################################################################################
# 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 sfleoo
import sys

Import( "*" )
# EndImport

oo = sfleoo.ooSfle(  fileDirOutput = fileDirOutput, fileDirTmp = fileDirTmp )


# StartInFiles
fileInputDataPCL = oo.fin( "data.pcl" )
fileInputMetadataPCL = oo.fin( "metadata.pcl" )
# EndInFiles

# StartOutFiles
fileReportTXT = oo.fout( "report.txt" )
fileMergedPCL = oo.fout( "merged.pcl" )
fileNormalizedPCL = oo.ftmp( oo.rebase( fileInputDataPCL ) )
fileTSV = oo.ftmp( oo.rebase( fileMergedPCL, sfle.c_strSufPCL, sfle.c_strSufTSV ) )
# EndOutFiles

# StartPrograms
fileProgNormalize = oo.fsrc("normalize.py") 
fileProgReport = oo.fsrc( "report.py" )
# EndPrograms

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

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

# StartNormalize
oo.ex( fileInputDataPCL, fileNormalizedPCL, fileProgNormalize, pipe = True )
# 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
afilePCLs = [fileInputMetadataPCL, fileNormalizedPCL]
afileTSVs = [ oo.ftmp( oo.rebase( fileIn, sfle.c_strSufPCL, sfle.c_strSufTSV ) ) 
			for fileIn in afilePCLs ]
for fin,fout in zip( afilePCLs, afileTSVs ):
	oo.ex( fin, fout, c_fileProgTranspose, pipe = True, verbose = True )
# EndTranspose

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

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

# StartMerge
oo.ex( afileTSVs, fileTSV, c_fileProgMergeTables, outpipe = True, verbose = True )
# 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
oo.ex( fileTSV, fileMergedPCL, c_fileProgTranspose, pipe = True )
oo.ex( fileMergedPCL, fileReportTXT, fileProgReport, pipe = True )
Default( fileReportTXT  )
# EndOutput


"""
Processing modules 6 
======================

Very simple example showing how in the object oriente framework
the programs can be specified. 
"""

# User defined function
# 'io' is a class for input/output and argument container:
# - io.inpf and io.outf are the arrayes of input/output file names
# - io.inp_tab contains the generator of tab-tokenized lines
#   reading each file sequentially
# - io.out_tab() writes to the next output file the generator
#   as a tab-delimited text file
# - io.args is a dictionary containing all passed arguments
# - others functions and features...
def cut( io ):
    inp = io.inp_tab(all_inp = True)
    fs = [int(a) for a in io.args['f'].split(',')]
    out = [l for i,l in enumerate(zip(*inp)) if i+1 in fs]
    out = zip(*out)
    io.out_tab( out )


# StartInFiles
file_in1  = oo.fin( "f1.txt" )
file_in2  = oo.fin( "f2.txt" )
# EndInFiles

# StartPrograms
cut_local_functin = cut
cut_unix_prog = "cut"
# EndOutFiles


# StartOutFiles
file_out1  = oo.fout( "fout1.txt" )
file_out2  = oo.fout( "fout2.txt" )
file_out3  = oo.fout( "fout3.txt" )
file_out4  = oo.fout( "fout4.txt" )
file_out5  = oo.fout( "fout5.txt" )
file_out6  = oo.fout( "fout6.txt" )
file_out7  = oo.fout( "fout7.txt" )
file_out8  = oo.fout( "fout8.txt" )
# EndOutFiles

# Apply the 'cut' function defined above
oo.f( file_in1, file_out1, cut_local_functin, f = '1,3' )

# Apply the external 'cut' program. The arguments are still
# passed in a function-style manner
oo.ex( file_in1, file_out2, cut_unix_prog, f = '1,3', outpipe = True )

# Apply the external 'cut' program using pipes for input/output
oo.ex( file_in1, file_out3, cut_unix_prog, f = '1,3', pipe = True )

# Apply the 'registered' cut function implemented inside sfleoo.py
oo.cut( file_in1, file_out4, f = '1,3' ) 

# Apply the local function / external command / external pipe  on two input files
oo.f( [file_in1,file_in2], file_out5, cut_local_functin, f = '1,3' )
oo.ex( [file_in1,file_in2], file_out6, cut_unix_prog, f = '1,3', pipe = True )
oo.cut( [file_in1,file_in2], file_out7, f = '1,3' ) 


pipe_out  = oo.fout( "pout.txt" )

c1 = oo.chain( "curl", url = "http://www.huttenhower.org/webfm_send/73" )
oo.chain( "grep", e = "Bifidobacterium",in_pipe = c1 )
oo.chain( "cut", f = 1, complement = ""  ,in_pipe = c1 )
oo.chain( "sed", e = "s/\t/+/g"  ,in_pipe = c1 )
oo.chain( "bc", in_pipe = c1, stop = pipe_out )



Default( [ file_out1, file_out2, file_out3, file_out4, file_out5, file_out6,
           file_out7, pipe_out ] ) 






