#!/usr/bin/env bds

#-------------------------------------------------------------------------------
# Integration tests
#
# Some test to make sure SnpEff & SnpSift co-exists woth other tools
#
#																Pablo Cingolani
#-------------------------------------------------------------------------------

# Default genome
genome		:= 'GRCh37.75'

# Integration test dir
intTestDir	:= "$HOME/workspace/SnpEff/tests/integration"

# Programs
picardDir	:= "$HOME/tools/picard"
gatkDir		:= "$HOME/tools/gatk"
snpeffDir	:= "$HOME/snpEff"

#-------------------------------------------------------------------------------
# Prepare FASTA reference to be used with GATK
#-------------------------------------------------------------------------------
void prepareRefGatk(string ref) {
	dict := ref.swapExt('.fa', '.dict')
	if( dict <- ref ) {
		println "Creating dictionary file '$dict'"
		sys java -jar $picardDir/CreateSequenceDictionary.jar R= $ref O= $dict
	}

	fai := "$ref.fai"
	if( fai <- ref ) {
		println "Indexing reference '$ref'"
		sys samtools faidx $ref
	}
}

#-------------------------------------------------------------------------------
# Integration test: GATK's VariantAnnotator
#-------------------------------------------------------------------------------
void test_Gatk_VariantAnnotator() {
	refFasta	:= "$snpeffDir/data/genomes/hs37d5.fa"	# URL http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/technical/reference/phase2_reference_assembly_sequence/hs37d5.fa.gz

	# VCF files
	base		:= "$intTestDir/test_gatk"
	vcf			:= "$base.vcf"
	effVcf		:= "$base.eff.vcf"
	effGatk		:= "$base.eff.gatk.vcf"

	# Prepare FASTA file to b used in GATK
	prepareRefGatk( refFasta )

	# Annotate
	println "Annotating file '$vcf'"
	sys java -Xmx4G -jar $snpeffDir/snpEff.jar -v -o gatk $genome $vcf > $effVcf

	# Run GATK
	sys java -Xmx4G -jar $gatkDir/GenomeAnalysisTK.jar \
		-T VariantAnnotator \
		-R $refFasta \
		-A  SnpEff \
		--variant $vcf \
		--snpEffFile $effVcf  \
		-L $vcf \
		-o $effGatk 
}

#-------------------------------------------------------------------------------
# Integration test: Annotate Effect
#-------------------------------------------------------------------------------
void test_Annotate_Eff() {
	println "Annotating"
	base := "$intTestDir/integration_1"
	vcf	:= "$base.vcf"
	out := "$base.eff.vcf"
	out.rmOnExit()

	sys java -Xmx4g -jar $snpeffDir/snpEff.jar -v $genome $vcf > $out 
}
#-------------------------------------------------------------------------------
# Integration test: Annotate using ClinVar
#-------------------------------------------------------------------------------
void test_Annotate_ClinVar() {
	println "Annotating with ClinVar"
	base := "$intTestDir/test_clinvar"
	vcf	:= "$base.vcf"
	out := "$base.dnsnp.vcf"
	out.rmOnExit()

	sys java -Xmx4g -jar $snpeffDir/SnpSift.jar annotate -v -clinvar $vcf > $out 
}

#-------------------------------------------------------------------------------
# Integration test: Annotate using dbSnp
#-------------------------------------------------------------------------------
void test_Annotate_dbSnp() {
	println "Annotating with dbSNP"
	base := "$intTestDir/test_dbSnp"
	vcf	:= "$base.vcf"
	out := "$base.dnsnp.vcf"
	out.rmOnExit()

	sys java -Xmx4g -jar $snpeffDir/SnpSift.jar annotate -v -dbsnp $vcf > $out 
}

#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------

# Run tests
test_Annotate_Eff()
test_Annotate_ClinVar()
test_Annotate_dbSnp()
test_Gatk_VariantAnnotator()

