## Jobs to validate the GAF submissions.
## Assumptions: 
##  * owltools is in the PATH
##  * only one GAF validation at a time, as the report files are named the same for each job (makes archive and e-mail configuration in Jenkins easier)
##  * delete report files before each run required (also a Jenkins requirement)
##  * use CATALOG_XML environment variable for optional catalog.xml usage (load ontologies from local files)
##  * use REPORT_FOLDER environment variable for specifying the output folder for reports, inferences, and similar outputfiles

## find directory details
current_dir := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

# Handle fix named report files
GAF_REPORT_FOLDER=.
# check environment variable: REPORT_FOLDER
ifdef REPORT_FOLDER
  GAF_REPORT_FOLDER=$(REPORT_FOLDER)
endif

GAF_VALIDATION_REPORT_FILE=$(GAF_REPORT_FOLDER)/gaf-validation-report.txt
GAF_VALIDATION_SUMMARY_FILE=$(GAF_REPORT_FOLDER)/gaf-validation-summary.txt
GAF_PREDICTIONS_REPORT_FILE=$(GAF_REPORT_FOLDER)/gaf-prediction-report.txt
GAF_UNSATISFIABLE_MODULE_FILE=$(GAF_REPORT_FOLDER)/go-taxon-rule-unsatisfiable-module.owl
GAF_EXPERIMENTAL_PREDICTIONS_REPORT_FILE=$(GAF_REPORT_FOLDER)/gaf-prediction-experimental-report.txt

## use these two variables only in a valid target context of a job
GAF_INFERENCE_FILE=$(GAF_REPORT_FOLDER)/gene_association.$@.inf.gaf
GAF_INFERENCE_FILE_NEW=$(GAF_REPORT_FOLDER)/$@.inf.gaf
GAF_EXPERIMENTAL_INFERENCE_FILE=$(GAF_REPORT_FOLDER)/gene_association.$@.inf.experimental.gaf

# Handle catalog xml flags
CATALOG_DETAILS=
ifdef CATALOG_XML
  CATALOG_DETAILS=--catalog-xml $(CATALOG_XML)
endif

## function for a simple GAF validation (old file name layout gene_association.MOD.gz)
define simple-gaf-validation
owltools $(CATALOG_DETAILS) http://purl.obolibrary.org/obo/go/extensions/go-gaf.owl \
--gaf $(current_dir)/gene_association.$@.gz \
--createReport \
--gaf-report-file $(GAF_VALIDATION_REPORT_FILE) \
--gaf-report-summary-file $(GAF_VALIDATION_SUMMARY_FILE) \
--gaf-prediction-file $(GAF_INFERENCE_FILE) \
--gaf-prediction-report-file $(GAF_PREDICTIONS_REPORT_FILE) \
--gaf-validation-unsatisfiable-module $(GAF_UNSATISFIABLE_MODULE_FILE) \
--gaf-run-checks
endef

## function for a simple GAF validation (update file name layout MOD.gaf.gz)
define simple-gaf-validation-new
owltools $(CATALOG_DETAILS) http://purl.obolibrary.org/obo/go/extensions/go-gaf.owl \
--gaf $(current_dir)/$@.gaf.gz \
--createReport \
--gaf-report-file $(GAF_VALIDATION_REPORT_FILE) \
--gaf-report-summary-file $(GAF_VALIDATION_SUMMARY_FILE) \
--gaf-prediction-file $(GAF_INFERENCE_FILE_NEW) \
--gaf-prediction-report-file $(GAF_PREDICTIONS_REPORT_FILE) \
--gaf-validation-unsatisfiable-module $(GAF_UNSATISFIABLE_MODULE_FILE) \
--gaf-run-checks
endef

#clean up files with job specific names
define clean-inf
rm -f $(GAF_INFERENCE_FILE) $(GAF_INFERENCE_FILE_NEW) $(GAF_EXPERIMENTAL_INFERENCE_FILE)
endef

##function for the advanced validation of GAFs with experimental predictions
define experimental-gaf-validation
owltools $(CATALOG_DETAILS) http://purl.obolibrary.org/obo/go/extensions/go-gaf.owl \
--gaf $(current_dir)/gene_association.$@.gz \
--createReport \
--gaf-report-file $(GAF_VALIDATION_REPORT_FILE) \
--gaf-report-summary-file $(GAF_VALIDATION_SUMMARY_FILE) \
--gaf-prediction-file $(GAF_INFERENCE_FILE) \
--gaf-prediction-report-file $(GAF_PREDICTIONS_REPORT_FILE) \
--gaf-validation-unsatisfiable-module $(GAF_UNSATISFIABLE_MODULE_FILE) \
--experimental-gaf-prediction-file $(GAF_EXPERIMENTAL_INFERENCE_FILE) \
--experimental-gaf-prediction-report-file $(GAF_EXPERIMENTAL_PREDICTIONS_REPORT_FILE) \
--gaf-run-checks
endef

# clean fix named report files
.PHONY: clean
clean:
	rm -f $(GAF_VALIDATION_REPORT_FILE) $(GAF_VALIDATION_SUMMARY_FILE) $(GAF_PREDICTIONS_REPORT_FILE) $(GAF_UNSATISFIABLE_MODULE_FILE) $(GAF_EXPERIMENTAL_PREDICTIONS_REPORT_FILE)

GeneDB_Lmajor: clean
	$(clean-inf)
	$(simple-gaf-validation)

GeneDB_Pfalciparum: clean
	$(clean-inf)
	$(simple-gaf-validation)

GeneDB_Tbrucei: clean
	$(clean-inf)
	$(simple-gaf-validation)

# no longer supported
#GeneDB_tsetse: clean
#	$(clean-inf)
#	$(simple-gaf-validation)

PAMGO_Atumefaciens: clean
	$(clean-inf)
	$(simple-gaf-validation)

PAMGO_Ddadantii: clean
	$(clean-inf)
	$(simple-gaf-validation)

PAMGO_Mgrisea: clean
	$(clean-inf)
	$(simple-gaf-validation)

PAMGO_Oomycetes: clean
	$(clean-inf)
	$(simple-gaf-validation)

aspgd: clean
	$(clean-inf)
	$(simple-gaf-validation)

cgd: clean
	$(clean-inf)
	$(simple-gaf-validation)

dictyBase: clean
	$(clean-inf)
	$(simple-gaf-validation)

ecocyc: clean
	$(clean-inf)
	$(simple-gaf-validation)

fb: clean
	$(clean-inf)
	$(simple-gaf-validation)

goa_chicken: clean
	$(clean-inf)
	$(simple-gaf-validation-new)

goa_cow: clean
	$(clean-inf)
	$(simple-gaf-validation-new)

goa_dog: clean
	$(clean-inf)
	$(simple-gaf-validation-new)

goa_human: clean
	$(clean-inf)
	$(simple-gaf-validation-new)

goa_pig: clean
	$(clean-inf)
	$(simple-gaf-validation-new)

goa_uniprot_noiea: clean
	$(clean-inf)
	mkdir -p download
	wget -O download/gene_association.goa_uniprot.gz ftp://ftp.geneontology.org/go/gene-associations/gene_association.goa_uniprot.gz
	owltools $(CATALOG_DETAILS) http://purl.obolibrary.org/obo/go/extensions/go-gaf.owl \
	--gaf download/gene_association.goa_uniprot.gz \
	--no-iea \
	--createReport \
	--gaf-report-file $(GAF_VALIDATION_REPORT_FILE) \
	--gaf-report-summary-file $(GAF_VALIDATION_SUMMARY_FILE) \
	--gaf-prediction-file $(GAF_INFERENCE_FILE) \
	--gaf-prediction-report-file $(GAF_PREDICTIONS_REPORT_FILE) \
	--gaf-validation-unsatisfiable-module $(GAF_UNSATISFIABLE_MODULE_FILE) \
	--gaf-run-checks

gramene_oryza: clean
	$(clean-inf)
	$(simple-gaf-validation)

jcvi: clean
	$(clean-inf)
	$(simple-gaf-validation)

mgi: clean
	$(clean-inf)
#	OWLTOOLS_MEMORY=12G $(simple-gaf-validation)
	OWLTOOLS_MEMORY=12G $(experimental-gaf-validation)

pombase: clean
	$(clean-inf)
	$(experimental-gaf-validation)

pseudocap: clean
	$(clean-inf)
	$(simple-gaf-validation)

reactome: clean
	$(clean-inf)
	$(simple-gaf-validation)

rgd: clean
	$(clean-inf)
	$(simple-gaf-validation)

sgd: clean
	$(clean-inf)
	$(simple-gaf-validation)

sgn: clean
	$(clean-inf)
	$(simple-gaf-validation)

tair: clean
	$(clean-inf)
	$(simple-gaf-validation)

wb: clean
	$(clean-inf)
	$(simple-gaf-validation)

zfin: clean
	$(clean-inf)
	$(simple-gaf-validation)