#!/usr/bin/make -rRf

# Input files
seqs_to_polish=None
seqs_to_polish_notdir=$(notdir $(seqs_to_polish))
polishing_seqs=None
polishing_seqs_notdir=$(notdir $(polishing_seqs))

# Common parameters
K=32 28 24 20 # k values should be descending
bfs=None
t=4

# ntEdit parameters
NTEDIT_X=0.5
NTEDIT_Y=0.5

# Sealer parameters
SEALER_P=10
SEALER_B=1000

# ntLink mapping parameters (used if --ntlink specified)
k_ntLink=32
w_ntLink=100

# Set shell
SHELL=bash -e -o pipefail
ifeq ($(shell zsh -e -o pipefail -c 'true' 2>/dev/null; echo $$?), 0)
# Set pipefail to ensure that all commands of a pipe succeed.
SHELL=zsh -e -o pipefail
# Report run time and memory usage with zsh.
export REPORTTIME=1
export TIMEFMT=time user=%U system=%S elapsed=%E cpu=%P memory=%M job=%J
endif

# Record run time and memory usage in a file using GNU time
ifeq ($(time),true)
log_time=command time -v -o $@.time
else
log_time=
endif

.PHONY: clean
.DELETE_ON_ERROR:
.SECONDARY:

%.upper.fa: %.fa
	goldpolish-to-upper $< $@

%.index: %
	goldpolish-index $< $@

$(seqs_to_polish_notdir).k$(k_ntLink).w$(w_ntLink).z1000.verbose_mapping.tsv: $(seqs_to_polish) $(polishing_seqs)
	$(log_time) ntLink t=$(t) target=$(seqs_to_polish) reads=$(polishing_seqs) pair verbose=True k=$(k_ntLink) w=$(w_ntLink) sensitive=True

$(seqs_to_polish_notdir).%.mapping.tsv: $(seqs_to_polish_notdir).%.verbose_mapping.tsv
	awk '{ print $$1" "$$2" "$$3 }' $< >$@

$(seqs_to_polish_notdir).$(polishing_seqs_notdir).paf: $(seqs_to_polish) $(polishing_seqs)
	minimap2 -t$(t) $(seqs_to_polish) $(polishing_seqs) >$@

%.ntedited.fa: %.fa $(bfs)
	goldpolish-ntedit $(basename $<) "$(bfs)" "$(K)" $(NTEDIT_X) $(NTEDIT_Y) $(t) $@

%.prepd.fa: %.fa
	goldpolish-mask -s -k$(firstword $(K)) $< >$@

%.sealer_scaffold.fa: %.fa $(bfs)
	sealer-gr -v -S $< \
	-o $*.sealer -L$(firstword $(K)) -j$(t) -P$(SEALER_P) -B$(SEALER_B) --lower \
	$(foreach k,$(K),-k$(k)) \
	$(foreach bf,$(bfs),--input-bloom=$(bf))

clean:
	rm -f *.index *.mapping.tsv
	rm -f *.k(k_ntLink).w$(w_ntLink).z1000.verbose_mapping.tsv *.k$(k_ntLink).w$(w_ntLink).tsv *.k$(k_ntLink).w$(w_ntLink).z1000.n1.scaffold.dot *.k$(k_ntLink).w$(w_ntLink).z1000.pairs.tsv


