import "./tasks/mapping.wdl" as mapping
import "./tasks/Dedup.wdl" as Dedup
import "./tasks/deduped_Metrics.wdl" as deduped_Metrics
import "./tasks/sentieon.wdl" as sentieon
import "./tasks/Realigner.wdl" as Realigner
import "./tasks/BQSR.wdl" as BQSR
import "./tasks/Haplotyper_gVCF.wdl" as Haplotyper_gVCF
import "./tasks/GVCFtyper.wdl" as GVCFtyper
import "./tasks/split_gvcf_files.wdl" as split_gvcf_files
import "./tasks/benchmark.wdl" as benchmark
import "./tasks/multiqc.wdl" as multiqc
import "./tasks/merge_sentieon_metrics.wdl" as merge_sentieon_metrics
import "./tasks/mendelian.wdl" as mendelian
import "./tasks/merge_mendelian.wdl" as merge_mendelian
import "./tasks/quartet_mendelian.wdl" as quartet_mendelian
import "./tasks/fastqc.wdl" as fastqc
import "./tasks/fastqscreen.wdl" as fastqscreen
import "./tasks/extract_tables.wdl" as extract_tables
import "./tasks/D5_D6.wdl" as D5_D6
import "./tasks/merge_family.wdl" as merge_family


workflow {{ project_name }} {

	File inputSamplesFile
	Array[Array[File]] inputSamples = read_tsv(inputSamplesFile)

	String SENTIEON_INSTALL_DIR
	String SENTIEON_LICENSE
	String SENTIEONdocker
	String FASTQCdocker
	String FASTQSCREENdocker
	String QUALIMAPdocker
	String BENCHMARKdocker
	String MENDELIANdocker
	String DIYdocker
	String MULTIQCdocker

	String fasta
	File ref_dir
	File dbmills_dir
	String db_mills
	File dbsnp_dir
	String dbsnp

	File screen_ref_dir
	File fastq_screen_conf
	File benchmarking_dir

	String project

	String disk_size
	String BIGcluster_config
	String SMALLcluster_config

	scatter (quartet in inputSamples){

		call mapping.mapping as mapping {
			input: 
			SENTIEON_INSTALL_DIR=SENTIEON_INSTALL_DIR,
			SENTIEON_LICENSE=SENTIEON_LICENSE,
			group=quartet[2],
			sample=quartet[2],
			pl="ILLUMINAL",
			fasta=fasta,
			ref_dir=ref_dir,
			fastq_1=quartet[0],
			fastq_2=quartet[1],
			docker=SENTIEONdocker,
			disk_size=disk_size,
			cluster_config=BIGcluster_config
		}

		call fastqc.fastqc as fastqc {
			input:
			read1=quartet[0],
			read2=quartet[1],
			docker=FASTQCdocker,
			cluster_config=BIGcluster_config,
			disk_size=disk_size
		}

		call fastqscreen.fastq_screen as fastqscreen {
			input:
			read1=quartet[0],
			read2=quartet[1],
			screen_ref_dir=screen_ref_dir,
			fastq_screen_conf=fastq_screen_conf,
			docker=FASTQSCREENdocker,
			cluster_config=BIGcluster_config,
			disk_size=disk_size
		}

		call Dedup.Dedup as Dedup {
			input:
			SENTIEON_INSTALL_DIR=SENTIEON_INSTALL_DIR,
			sorted_bam=mapping.sorted_bam,
			sorted_bam_index=mapping.sorted_bam_index,
			sample=quartet[2],
			docker=SENTIEONdocker,
			disk_size=disk_size,
			cluster_config=BIGcluster_config
		}

		call deduped_Metrics.deduped_Metrics as deduped_Metrics {
			input:
			SENTIEON_INSTALL_DIR=SENTIEON_INSTALL_DIR,
			fasta=fasta,
			ref_dir=ref_dir,
			Dedup_bam=Dedup.Dedup_bam,
			Dedup_bam_index=Dedup.Dedup_bam_index,
			sample=quartet[2],
			docker=SENTIEONdocker,
			disk_size=disk_size,
			cluster_config=BIGcluster_config
		}

		call sentieon.sentieon as sentieon {
			input:
			quality_yield=deduped_Metrics.deduped_QualityYield,
			wgs_metrics_algo=deduped_Metrics.deduped_wgsmetrics,
			aln_metrics=deduped_Metrics.dedeuped_aln_metrics,
			is_metrics=deduped_Metrics.deduped_is_metrics,
			sample=quartet[2],
			docker=SENTIEONdocker,
			cluster_config=SMALLcluster_config,
			disk_size=disk_size		
		}

		call Realigner.Realigner as Realigner {
			input:
			SENTIEON_INSTALL_DIR=SENTIEON_INSTALL_DIR,
			fasta=fasta,
			ref_dir=ref_dir,
			Dedup_bam=Dedup.Dedup_bam,
			Dedup_bam_index=Dedup.Dedup_bam_index,
			db_mills=db_mills,
			dbmills_dir=dbmills_dir,
			sample=quartet[2],
			docker=SENTIEONdocker,
			disk_size=disk_size,
			cluster_config=BIGcluster_config
		}

		call BQSR.BQSR as BQSR {
			input:
			SENTIEON_INSTALL_DIR=SENTIEON_INSTALL_DIR,
			fasta=fasta,
			ref_dir=ref_dir,
			realigned_bam=Realigner.realigner_bam,
			realigned_bam_index=Realigner.realigner_bam_index,
			db_mills=db_mills,
			dbmills_dir=dbmills_dir,
			dbsnp=dbsnp,
			dbsnp_dir=dbsnp_dir,
			sample=quartet[2],
			docker=SENTIEONdocker,
			disk_size=disk_size,
			cluster_config=BIGcluster_config
		}

		call Haplotyper_gVCF.Haplotyper_gVCF as Haplotyper_gVCF {
			input:
			SENTIEON_INSTALL_DIR=SENTIEON_INSTALL_DIR,
			fasta=fasta,
			ref_dir=ref_dir,
			recaled_bam=BQSR.recaled_bam,
			recaled_bam_index=BQSR.recaled_bam_index,
			sample=quartet[2],
			docker=SENTIEONdocker,
			disk_size=disk_size,
			cluster_config=BIGcluster_config
		}
	}

	call GVCFtyper.GVCFtyper as GVCFtyper {
		input:
		ref_dir=ref_dir,
		SENTIEON_INSTALL_DIR=SENTIEON_INSTALL_DIR,
		fasta=fasta,
		vcf=Haplotyper_gVCF.vcf,
		vcf_idx=Haplotyper_gVCF.vcf_idx,
		project=project,
		docker=SENTIEONdocker,
		cluster_config=BIGcluster_config,
		disk_size=disk_size
	}

	call split_gvcf_files.split_gvcf_files as split_gvcf_files {
		input:
		gvcf=GVCFtyper.gvcf,
		docker=DIYdocker,
		project=project,
		cluster_config=SMALLcluster_config,
		disk_size=disk_size
	}


	Array[File] single_gvcf = split_gvcf_files.splited_vcf

	scatter (idx in range(length(single_gvcf))) {
		call benchmark.benchmark as benchmark {
			input:
			vcf=single_gvcf[idx],
			benchmarking_dir=benchmarking_dir,
			ref_dir=ref_dir,
			fasta=fasta,
			docker=BENCHMARKdocker,
			cluster_config=BIGcluster_config,
			disk_size=disk_size,
		}

	}

	call multiqc.multiqc as multiqc {
		input:
		read1_zip=fastqc.read1_zip,
		read2_zip=fastqc.read2_zip,
		txt1=fastqscreen.txt1,
		txt2=fastqscreen.txt2,
		summary=benchmark.summary,
		docker=MULTIQCdocker,
		cluster_config=SMALLcluster_config,
		disk_size=disk_size
	}

	call merge_sentieon_metrics.merge_sentieon_metrics as merge_sentieon_metrics {
		input:
		quality_yield_header=sentieon.quality_yield_header,
		wgs_metrics_algo_header=sentieon.wgs_metrics_algo_header,
		aln_metrics_header=sentieon.aln_metrics_header,
		is_metrics_header=sentieon.is_metrics_header,
		quality_yield_data=sentieon.quality_yield_data,
		wgs_metrics_algo_data=sentieon.wgs_metrics_algo_data,
		aln_metrics_data=sentieon.aln_metrics_data,
		is_metrics_data=sentieon.is_metrics_data,
		project=project,
		docker=MULTIQCdocker,
		cluster_config=SMALLcluster_config,
		disk_size=disk_size	
	}

	call extract_tables.extract_tables as extract_tables {
		input:
		quality_yield_summary=merge_sentieon_metrics.quality_yield_summary,
		wgs_metrics_summary=merge_sentieon_metrics.wgs_metrics_summary,
		aln_metrics_summary=merge_sentieon_metrics.aln_metrics_summary,
		is_metrics_summary=merge_sentieon_metrics.is_metrics_summary,
		fastqc=multiqc.fastqc,
		fastqscreen=multiqc.fastqscreen,
		hap=multiqc.hap,
		docker=MULTIQCdocker,
		cluster_config=SMALLcluster_config,
		disk_size=disk_size
	}	

	Boolean sister_tag = read_boolean(split_gvcf_files.sister_tag)
	Boolean quartet_tag = read_boolean(split_gvcf_files.quartet_tag)

	if (sister_tag) {
		call D5_D6.D5_D6 as D5_D6 {
			input:
			splited_vcf=split_gvcf_files.splited_vcf,
			project=project,
			docker=DIYdocker,
			cluster_config=SMALLcluster_config,
			disk_size=disk_size,
		}
	}

	if (quartet_tag) {
		call merge_family.merge_family as merge_family {
			input:
			splited_vcf=split_gvcf_files.splited_vcf,
			project=project,
			docker=DIYdocker,
			cluster_config=SMALLcluster_config,
			disk_size=disk_size,
		}

		Array[File] family_vcfs = merge_family.family_vcf
		scatter (idx in range(length(family_vcfs))) {
			call mendelian.mendelian as mendelian {
				input:
				family_vcf=family_vcfs[idx],
				ref_dir=ref_dir,
				fasta=fasta,
				docker=MENDELIANdocker,
				cluster_config=BIGcluster_config,
				disk_size=disk_size		
			}

			call merge_mendelian.merge_mendelian as merge_mendelian {
				input:
				D5_trio_vcf=mendelian.D5_trio_vcf,
				D6_trio_vcf=mendelian.D6_trio_vcf,
				family_vcf=family_vcfs[idx],
				docker=DIYdocker,
				cluster_config=SMALLcluster_config,
				disk_size=disk_size
			}
		}

		call quartet_mendelian.quartet_mendelian as quartet_mendelian {
			input:
			project_mendelian_summary=merge_mendelian.project_mendelian_summary,
			project=project,
			docker=DIYdocker,
			cluster_config=SMALLcluster_config,
			disk_size=disk_size
		}
	}
}


