#!/usr/bin/env bds

#-------------------------------------------------------------------------------
#
# Download genome data
#
#-------------------------------------------------------------------------------

urlClinVarGrch37   := "ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh37/clinvar.vcf.gz"
urlClinVarGrch38   := "ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh38/clinvar.vcf.gz"
urlDbSnpGrch37     := "https://ftp.ncbi.nih.gov/snp/latest_release/VCF/GCF_000001405.25.gz"
urlDbSnpGrch38     := "https://ftp.ncbi.nih.gov/snp/latest_release/VCF/GCF_000001405.39.gz"
urlGwasCatalog     := "https://www.ebi.ac.uk/gwas/api/search/downloads/alternative"
urlJaspar          := "https://jaspar.genereg.net/download/data/2022/CORE/JASPAR2022_CORE_non-redundant_pfms_jaspar.txt"
urlNextProt        := "https://download.nextprot.org/pub/current_release/xml/nextprot_all.xml.gz"
urlPdb             := "rsync.wwpdb.org::ftp_data/structures/divided/pdb/"

#-------------------------------------------------------------------------------
# Download ClinVar
#-------------------------------------------------------------------------------
void downloadClinvar() {
	println "Download Clinvar"

	# GRCh37
	dir := "$dirDb/GRCh37/clinvar/"
	dir.mkdir()
    clinvar := "$dir/clinvar.vcf.gz"
	wget(urlClinVarGrch37, clinvar, false, false, true)

	# GRCh38
	dir = "$dirDb/GRCh38/clinvar/"
	dir.mkdir()
    clinvar = "$dir/clinvar.vcf.gz"
	wget(urlClinVarGrch38, clinvar, false, false, true)

	wait
}

#-------------------------------------------------------------------------------
# Download and build dbNsfp
#-------------------------------------------------------------------------------
void downloadDbNsfp() {
	# Download and create
	dbNsfp38 := new DbNsfp(dirDb, "GRCh38")
	dbNsfp38.download()
	dbNsfp38File := dbNsfp38.create('')
	wait

	# Create dbNsfp37 using 'dbNsfp38File'
	dbNsfp37 := new DbNsfp(dirDb, "GRCh37")
	dbNsfp37.create(dbNsfp38File)
}

#-------------------------------------------------------------------------------
# Download dbSnp
#-------------------------------------------------------------------------------
void downloadDbSnp() {
	println "Download dbSnp"

	dbsnp := "dbsnp.vcf.gz"

	# GRCh37
	dir := "$dirDb/GRCh37/dbSnp/"
	dir.mkdir()
    wget(urlDbSnpGrch37, "$dir/$dbsnp")

	# GRCh38
	dir = "$dirDb/GRCh38/dbSnp/"
	dir.mkdir()
    wget(urlDbSnpGrch38, "$dir/$dbsnp")

	wait
}

#-------------------------------------------------------------------------------
# Download GWAS catalog
#-------------------------------------------------------------------------------
void downloadGwasCatalog() {
	println "Download Gwas-Catalog"

	dir := "$dirDb/gwasCatalog"
	dir.mkdir()
	gwasTxt := "$dir/gwasCatalog.txt"
	gwasVcf := "$dir/gwasCatalog.vcf"

	if( wget(urlGwasCatalog, gwasTxt) ) {
		sys cat '$gwasTxt' | '$dirScriptsBuild/gwascatalog2vcf.pl' > '$gwasVcf'
	}

	wait
}

#-------------------------------------------------------------------------------
# Download Jaspar
#-------------------------------------------------------------------------------
void downloadJaspar() {
	println "Download Jaspar"

	dir := "$dirDb/jaspar"
	dir.mkdir()
	jaspar := "$dir/jaspar.pwm"

	if( wget(urlJaspar, jaspar) ) {
		sys gzip -c '$jaspar' > '$dir/pwms.bin'
		println "Jaspar: File $dir/pwms.bin created"
	}
}

#-------------------------------------------------------------------------------
# Download NextProt
#-------------------------------------------------------------------------------
void downloadNextProt() {
	println "Download NextProt"

	dir := "$dirDb/nextProt"
	dir.mkdir()
	dir.chdir()
	
	downloadOk := "download.ok"
	if( ! downloadOk.exists() ) {
		println "Download NextProt"

		# Download each chromosome's XML file
        wgetTask(urlNextProt)

		wait
		downloadOk.write("OK")	# Mark as downlaoded OK
	} else println "downloadNextProt: Done file '$dir/$downloadOk' exists, skippping"
}

#-------------------------------------------------------------------------------
# Download PDB
#-------------------------------------------------------------------------------
void downloadPdb() {
	println "Download PDB"

	dir := "$dirDb/pdb"
	dir.mkdir()
	dir.chdir()

	downloadOk := "download.ok"
	if( ! downloadOk.exists() ) {
		println "Download PDB"
		sys rsync -rlpt -v -z --delete --port=33444 $urlPdb .
		downloadOk.write("OK")	# Mark as downlaoded OK
	} else println "downloadPdb: Done file '$dir/$downloadOk' exists, skippping"
}

