# This makefile runs all tests, then it builds the paper

SRC_DIR = src
KBMF2K_DIR = kbmf2k
INTERACTION_INFO = scripts/interactions-info.pl
INTERACTION_INFO_ARGS = 
INTERACTION_INFO_SUMMARY = scripts/interactions-info-summary.pl

DATASETS = nr gpcr ic e
METHODS = gip wnn wnn-gip const const-gip kwnn kwnn-gip kwnn-near kwnn-near-gip kbmf2k

#MATLAB = matlab -r
MATLAB = octave -q --eval

.PHONY: all
#.DELETE_ON_ERROR:

all: raw-cv-results new-interactions

#-------------------------------------------------------------------------------
# Calculating raw cross validation results
#-------------------------------------------------------------------------------

results:
	mkdir -p $@

# for each dataset and method, run cross validate in two forms

.PHONY: raw-cv-results
raw-cv-results: results

define RAW_CV_RESULTS_template
raw-cv-results: results/cv-loo-pairs-$(1)-$(2).mat
raw-cv-results: results/cv-5fold-cols-$(1)-$(2).mat
results/cv-loo-pairs-$(1)-$(2).mat:
	$(MATLAB) "addpath $(SRC_DIR);\
	           addpath $(KBMF2K_DIR);\
	           results=run_cross_validate('$(2)','$(1)','num_folds','loo', 'cv_type','pairs');\
	           save -v6 $(pwd)$$@ results;\
	           exit"
results/cv-5fold-cols-$(1)-$(2).mat:
	$(MATLAB) "addpath $(SRC_DIR);\
	           addpath $(KBMF2K_DIR);\
	           results=run_cross_validate('$(2)','$(1)','num_folds',5,'num_repetitions',5, 'cv_type','cols');\
	           save -v6 $$@ results;\
	           exit"
endef

$(foreach dataset,$(DATASETS), \
 $(foreach method,$(METHODS), \
  $(eval $(call RAW_CV_RESULTS_template,$(dataset),$(method))) \
 ))

#-------------------------------------------------------------------------------
# Summarize cross validation results
#-------------------------------------------------------------------------------


#-------------------------------------------------------------------------------
# New predicted interactions
#-------------------------------------------------------------------------------

.PHONY: new-interactions

# New interactions for each dataset
.PHONY: new-interactions-raw
new-interactions-raw: $(foreach dataset,$(DATASETS), results/new-interactions-$(dataset).txt)
results/new-interactions-%.txt: results
	$(MATLAB) "addpath $(SRC_DIR);\
	           result = find_new_interactions('$*',1000);\
	           print_structs(result,'$@');\
	           exit"

# Convert to html
.PHONY: new-interactions-html
new-interactions: new-interactions-html
new-interactions-html: $(foreach dataset,$(DATASETS), results/new-interactions-$(dataset).html)
results/new-interactions-%.html: results/new-interactions-%.txt $(INTERACTION_INFO)
	perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS) --mode=html < $< > $@

# Convert to latex
.PHONY: new-interactions-latex
new-interactions: new-interactions-latex
new-interactions-latex: $(foreach dataset,$(DATASETS), paper/new-interactions-$(dataset)-5.tex)
paper/new-interactions-%-5.tex: results/new-interactions-%.txt $(INTERACTION_INFO)
	perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS) --max-rank=5 --mode=latex < $< > $@

#-------------------------------------------------------------------------------
# Convert to latex / html
#-------------------------------------------------------------------------------

#.PHONY: paper/results
#paper/results: paper/results-loo-small.tex paper/results-loo-full.tex paper/results-10cv-full.tex
##paper/results-loo-small.tex: results/all-tests-loo.out results/results-by09.txt
##	perl scripts/make-results-table.pl 0 $^ > $@
#paper/results-loo-full.tex: results/all-tests-loo.out
	#perl scripts/make-results-table.pl 1 $^ > $@
#paper/results-10cv-full.tex: results/all-tests-10cv.out
	#perl scripts/make-results-table.pl 1 $^ > $@

#.PHONY: new-interactions new-interactions-html new-interactions-latex new-interactions-summary
#new-interactions: new-interactions-html new-interactions-latex new-interactions-summary

#new-interactions-raw: $(foreach x,nr gpcr ic e,results/new-interactions-$(x).txt)
#results/new-interactions-%.txt:
	#cd ../src && octave -q --eval "find_new_interactions('$*',1000)" > ../paper_prib/$@

#new-interactions-html: $(foreach x,nr gpcr ic e,results/new-interactions-$(x).html)
#results/new-interactions-%.html: results/new-interactions-%.txt $(INTERACTION_INFO)
	#perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS) --mode=html < $< > $@

#new-interactions-html: $(foreach x,nr gpcr ic e,results/new-interactions-$(x)-surprise.html)
#results/new-interactions-%-surprise.html: results/new-interactions-%.txt $(INTERACTION_INFO)
	#perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS) --mode=html --only-surprising < $< > $@

##new-interactions-html: $(foreach x,nr gpcr ic e,results/by09-interactions-$(x).html)
##results/by09-interactions-%.html: results/by09_predpairs_def_max_%.txt $(INTERACTION_INFO)
##	perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS) --mode=html --max-rank=1000 < $< > $@

#new-interactions-latex: $(foreach x,nr gpcr ic e,paper/new-interactions-$(x)-50.tex paper/new-interactions-$(x)-20.tex paper/new-interactions-$(x)-10.tex paper/new-interactions-$(x)-5.tex paper/new-interactions-$(x)-wide.tex)

#paper/new-interactions-%-50.tex: results/new-interactions-%.txt $(INTERACTION_INFO)
	#perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS) --max-rank=50 --mode=latex < $< > $@
#paper/new-interactions-%-20.tex: results/new-interactions-%.txt $(INTERACTION_INFO)
	#perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS) --max-rank=20 --mode=latex < $< > $@
#paper/new-interactions-%-10.tex: results/new-interactions-%.txt $(INTERACTION_INFO)
	#perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS) --max-rank=10 --mode=latex < $< > $@
#paper/new-interactions-%-5.tex: results/new-interactions-%.txt $(INTERACTION_INFO)
	#perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS) --max-rank=5 --mode=latex < $< > $@
#paper/new-interactions-%-wide.tex: results/new-interactions-%.txt $(INTERACTION_INFO)
	#perl $(INTERACTION_INFO) $(INTERACTION_INFO_ARGS)--max-rank=20 --mode=latex-wide < $< > $@

#new-interactions-summary: paper/new-interactions-summary.tex
##new-interactions-summary: paper/by09-interactions-summary.tex
#paper/new-interactions-summary.tex: $(foreach x,e ic gpcr nr,results/new-interactions-$(x).txt) $(INTERACTION_INFO_SUMMARY) Makefile
	#perl $(INTERACTION_INFO_SUMMARY) $^ > $@
##paper/by09-interactions-summary.tex: $(foreach x,e ic gpcr nr,results/by09_predpairs_def_max_$(x).txt) $(INTERACTION_INFO_SUMMARY) Makefile
##	perl $(INTERACTION_INFO_SUMMARY) $^ > $@
