#!/bin/bash
tool_directory=$1
galaxy_tabular_file=$2
galaxy_replication_vector_column_index=$3
galaxy_genotype_vector_column_index=$4
galaxy_first_y_vector_column_index=$5
galaxy_last_y_vector_column_index=$6
galaxy_design=$7
galaxy_summarize_by=$8
galaxy_summarize_by_vector_column_index=$9
galaxy_group_variable_1=${10}
galaxy_group_variable_1_vector_column_index=${11}
galaxy_group_variable_2=${12}
galaxy_group_variable_2_vector_column_index=${13}
galaxy_output_file_path=${14}
block_vector_column_index=${15}

directory=`dirname $0`
mkdir tmpdir$$


counter=$galaxy_first_y_vector_column_index

previous="tmpdir$$/output"
echo '' >$previous
while [ $counter -le $galaxy_last_y_vector_column_index ]
do
Rscript --vanilla $directory/blupcal_wrapper.R --tool_directory $tool_directory --tabular_file $galaxy_tabular_file --replication_vector_column_index $galaxy_replication_vector_column_index --genotype_vector_column_index $galaxy_genotype_vector_column_index --y_vector_column_index $counter  --design $galaxy_design --summarize_by $galaxy_summarize_by --summarize_by_vector_column_index $galaxy_summarize_by_vector_column_index --group_variable_1 $galaxy_group_variable_1 --group_variable_1_vector_column_index $galaxy_group_variable_1_vector_column_index --group_variable_2 $galaxy_group_variable_2 --group_variable_2_vector_column_index $galaxy_group_variable_2_vector_column_index --output_file_path tmpdir$$/output.$counter --block_vector_column_index $block_vector_column_index
#cat tmpdir$$/output.$counter >>$galaxy_output_file_path
if [ "$counter" -gt "$galaxy_first_y_vector_column_index" ];then
if [[ "$galaxy_summarize_by" == "true" ]];then 
cut -f 3-7 tmpdir$$/output.$counter >>tmpdir$$/output.$counter.cut
fi
if [[ "$galaxy_summarize_by" == "false" ]];then
cut -f 2-6 tmpdir$$/output.$counter >>tmpdir$$/output.$counter.cut
fi
mv tmpdir$$/output.$counter.cut tmpdir$$/output.$counter
fi
if [ "$counter" -gt "$galaxy_first_y_vector_column_index" ];then
paste $previous tmpdir$$/output.$counter >>tmpdir$$/output.$counter.$counter
fi
if [ "$counter" -eq "$galaxy_first_y_vector_column_index" ];then
cp tmpdir$$/output.$counter tmpdir$$/output.$counter.$counter
fi
previous="tmpdir$$/output.$counter.$counter"
cp -rf tmpdir$$/output.$counter.$counter $galaxy_output_file_path
#paste $galaxy_output_file_path tmpdir$$/output.$counter >>$galaxy_output_file_path
#cut -f 3-7 tmpdir$$/output.$counter >tmpdir$$/output.$counter.cut
((counter++))
done


echo $galaxy_summarize_by



