task quartet_mendelian {
	Array[File] summary
	Array[File] LCL5_hap
	Array[File] LCL6_hap
	Array[File] LCL7_hap
	Array[File] LCL8_hap
	String docker
	String project
	String cluster_config
	String disk_size

	command <<<
		cat ${sep=" " summary} | grep -v 'Family'> mendelian.summary
		sed '1iFamily\tTotal_Variants\tMendelian_Concordant_Variants\tMendelian_Concordance_Rate' mendelian.summary > mendelian.txt

		cat mendelian.txt | grep 'INDEL' | cut -f4 | grep -v 'Mendelian_Concordance_Rate' | awk '{for(i=1;i<=NF;i++) {sum[i] += $i; sumsq[i] += ($i)^2}} 
          END {for (i=1;i<=NF;i++) {
          printf "%f %f \n", sum[i]/NR, sqrt((sumsq[i]-sum[i]^2/NR)/NR)}
         }' >> quartet_indel_aver-std.txt

		cat mendelian.txt | grep 'SNV' | cut -f4 | grep -v 'Mendelian_Concordance_Rate' | awk '{for(i=1;i<=NF;i++) {sum[i] += $i; sumsq[i] += ($i)^2}} 
          END {for (i=1;i<=NF;i++) {
          printf "%f %f \n", sum[i]/NR, sqrt((sumsq[i]-sum[i]^2/NR)/NR)}
         }' >> quartet_snv_aver-std.txt

		cat ${sep=" " LCL5_hap} ${sep=" " LCL6_hap} ${sep=" " LCL7_hap} ${sep=" " LCL8_hap} | grep ALL | sed s'/,/\t/g' > hap.summary
		sed '1i\Type\tFilter\tTRUTH.TOTAL\tTRUTH.TP\tTRUTH.FN\tQUERY.TOTAL\tQUERY.FP\tQUERY.UNK\tFP.gt\tMETRIC.Recall\tMETRIC.Precision\tMETRIC.Frac_NA\tMETRIC.F1_Score\tTRUTH.TOTAL.TiTv_ratio\tQUERY.TOTAL.TiTv_ratio\tTRUTH.TOTAL.het_hom_ratio\tQUERY.TOTAL.het_hom_ratio' hap.summary > precision_recall

		python /opt/hap_summary.py -hap precision_recall -name ${project}

		cat variants.calling.qc.txt | cut -f12- | grep -v 'SNV' | awk '{for(i=1;i<=NF;i++) {sum[i] += $i; sumsq[i] += ($i)^2}} 
          END {for (i=1;i<=NF;i++) {
          printf "%f %f \n", sum[i]/NR, sqrt((sumsq[i]-sum[i]^2/NR)/NR)}
         }' >> reference_datasets_aver-std.txt

	>>>

	runtime {
		docker:docker
    	cluster:cluster_config
    	systemDisk:"cloud_ssd 40"
    	dataDisk:"cloud_ssd " + disk_size + " /cromwell_root/"
	}
	output {
		File mendelian_summary = "mendelian.txt"
		File snv_aver_std = "quartet_snv_aver-std.txt"
		File indel_aver_std = "quartet_indel_aver-std.txt"
		File pr = "precision_recall"
		File hap_summary = "variants.calling.qc.txt"
		File precision_recall_aver_std = "reference_datasets_aver-std.txt"
	}
}