#!/usr/bin/make -rf

SHELL=/bin/bash -o pipefail

ifndef n
error::
	@>&2 echo 'dida: missing required parameter `n`'
endif

ifndef l
error::
	@>&2 echo 'dida: missing required parameter `l`'
endif

ifndef d
error::
	@>&2 echo 'dida: missing required parameter `d`'
endif

ifndef query 
error::
	@>&2 echo 'dida: missing required parameter `query`'
endif

ifndef ref 
error::
	@>&2 echo 'dida: missing required parameter `ref`'
endif

.PHONY: partition index dispatch align merge clean debug

default:

error::
	@>&2 echo 'Try `dida help` for more information.'
	@false

help:
	@printf '\
		| Usage: dida name=<job_name> n=<num_partitions> l=<min_align_length> d=<bloom_filter_match_len> \\ \n\
		|             query=<fastq_or_bam_file> ref=<fasta_file>\n\
		| Example: dida name=myjob n=10 l=30 d=10 query=reads.bam ref=genome.fa\n\
		| \n\
		| Mandatory parameters:\n\
		| \n\
		|    n=<num_partitions>         number of partitions for dividing up the alignments\n\
		|    l=<min_align_len>          discard all alignments shorter than this length (bp)\n\
		|    d=<bloom_filter_match_len> minimum length of match for bloom filter hit (bp)\n\
		|    query=<fastq_or_bam_file>  align these sequences to the reference \n\
		|                               (FASTQ or BAM; must be sorted by read name!)\n\
		|    ref=<fasta_file>           the reference sequence (FASTA)\n\
		| \n\
		| Make targets (in order of execution):\n\
		| \n\
		|    partition                  split the reference into n partitions\n\
		|    index                      index the reference partitions\n\
		|    dispatch                   dispatch reads to partitions, using Bloom filters\n\
		|    align                      align query to each reference partition\n\
		|    merge [default]            align query to ref (output: <job_name>.sam.gz)\n\
		| \n\
		| Other Make targets:\n\
		|    clean                      delete intermediate files\n'

default: merge 

ref_adj:=$(basename $(ref)).adj
ref_dot:=$(basename $(ref)).dot

split_fm_indices:=$(foreach i,$(shell seq 1 $n),mref-$i.fa.fm)
split_fai_indices:=$(foreach i,$(shell seq 1 $n),mref-$i.fa.fai)

split_sam_files:=$(foreach i,$(shell seq 1 $n),aln-$i.sam)

bytes_per_gb:=1073741824
ref_size_bytes:=$(shell du -b $(ref) | cut -f1)
ref_size_gb:=$(shell echo '($(ref_size_bytes) + $(bytes_per_gb)) / $(bytes_per_gb)' | bc)
split_ref_size_gb:=$(shell echo '($(ref_size_gb) + $n) / $n' | bc 2>/dev/null)

partition_mem_gb:=$(shell echo '($(ref_size_gb) + 2) / 2' | bc 2>/dev/null)
index_mem_gb:=$(shell echo '$(split_ref_size_gb) * 10 + 5' | bc 2>/dev/null)
dispatch_mem_gb:=3.7
align_mem_gb:=3.7
merge_mem_gb:=25
adj_mem_gb:=28

debug:
	@ echo split_fm_indices: $(split_fm_indices)
	@ echo ref_size_gb: $(ref_size_gb)
	@ echo split_ref_size_gb: $(split_ref_size_gb)
	@ echo partition_mem_gb: $(partition_mem_gb)
	@ echo index_mem_gb: $(index_mem_gb)
	@ echo dispatch_mem_gb: $(dispatch_mem_gb)
	@ echo align_mem_gb: $(align_mem_gb)
	@ echo merge_mem_gb: $(merge_mem_gb)

partition: partition-complete

partition-complete: $(ref) $(ref_adj)
	SGE_RREQ="-N DIDA-PARTITION -l mem_token=$(partition_mem_gb)G,mem_free=$(partition_mem_gb)G,h_vmem=$(partition_mem_gb)G" && \
	prt -p$n $(ref) && \
	touch partition-complete

index: $(split_fm_indices) $(split_fai_indices)

mref-%.fa.fai mref-%.fa.fm: partition-complete
	SGE_RREQ="-N DIDA-INDEX -l mem_token=$(index_mem_gb)G,mem_free=$(index_mem_gb)G,h_vmem=$(index_mem_gb)G" && \
	abyss-index mref-$*.fa

dispatch: dispatch-complete

dispatch-complete: partition-complete
	SGE_RREQ="-N DIDA-DISPATCH -pe ncpus 12 -l mem_token=$(dispatch_mem_gb)G,mem_free=$(dispatch_mem_gb)G,h_vmem=$(dispatch_mem_gb)G" && \
	dsp -b$d -p$n $(query) && \
	touch dispatch-complete

align: $(split_sam_files)

aln-%.sam: mref-%.fa.fai mref-%.fa.fm dispatch-complete
	SGE_RREQ="-N DIDA-ALIGN -pe ncpus 12 -l mem_token=$(align_mem_gb)G,mem_free=$(align_mem_gb)G,h_vmem=$(align_mem_gb)G" && \
	abyss-map -l$l -j12 --order mreads-$*.fa mref-$*.fa > aln-$*.sam && \
	rm mreads-$*.fa

merge: aln.sam

aln.sam: $(split_sam_files)
	SGE_RREQ="-N DIDA-MERGE -l mem_token=$(merge_mem_gb)G,mem_free=$(merge_mem_gb)G,h_vmem=$(merge_mem_gb)G" && \
	mrg -p $n -a abyss-map -m mem && \
#rm aln-*.sam

clean:
	rm -f maxinf *.fai *.fm partition-complete mreads-*.fa dispatch-complete aln-*.sam
