OBOL_NOGO=obol -r obol_av -r go_synonyms  -r ubo -r relationship -r spatial -table_pred user:process5/3 -table_pred user:process/3 -table_pred classdef_parser:any_kind_of/3 -table_pred user:continuant/3 -table_pred ontol_db:subclassT/2 -table_pred user:cell/3 -table_pred user:cell5/3  -table_pred user:cellular_component/3 -table_pred user:cellular_component5/3 -table_pred user:chemical/3 -table_pred user:quality_in_bearer/3  -table_pred user:quality/3 obol-parse -xp_policy newonly -i ro_test.obo
OBOL=$(OBOL_NOGO) -r go

ANAT= -r fly_anatomy -r plant_anatomy -r zebrafish_anatomy -r mouse_anatomy -r xenopus_anatomy -r caro


BP_XP=biological_process_xp-obol.obo
BP_XP_AN=biological_process_xp_anatomy-obol.obo
BP_XP_CL=biological_process_xp_cell-obol.obo
BP_XP_CC=biological_process_xp_cellular_component-obol.obo
BP_XP_MF=biological_process_xp_molecular_function-obol.obo
BP_XP_BP=biological_process_xp_self-obol.obo
BP_XP_MOP=biological_process_xp_multi_organism_process-obol.obo
BP_XP_CC_MOP=biological_process_xp_cellular_component_multispecies-obol.obo
BP_XP_R=biological_process_xp_regulation-obol.obo
BP_XP_RoP=biological_process_xp_regulation_of_process-obol.obo
BP_XP_RoF=biological_process_xp_regulation_of_function-obol.obo
BP_XP_Q=biological_process_xp_quality-obol.obo
BP_XP_SQ=biological_process_xp_sequence-obol.obo
BP_XP_CH=biological_process_xp_chebi-obol.obo
BP_XP_PRO=biological_process_xp_protein-obol.obo
BP_XP_SIG=biological_process_xp_signaling-obol.obo
BP_XP_ANU=biological_process_xp_uber_anatomy-obol.obo
BP_XP_QANU=biological_process_xp_quality-uber_anatomy-obol.obo
BP_XP_ANF=biological_process_xp_fly_anatomy-obol.obo
BP_XP_ANFU=biological_process_xp_fungal_anatomy-obol.obo
BP_XP_ANX=biological_process_xp_xenopus-obol.obo
BP_XP_ANDD=biological_process_xp_dicty_anatomy-obol.obo
BP_XP_ANZ=biological_process_xp_zebrafish_anatomy-obol.obo
BP_XP_ANM=biological_process_xp_mouse_anatomy-obol.obo
BP_XP_ANP=biological_process_xp_plant_anatomy-obol.obo
BP_XP_ANH=biological_process_xp_human_anatomy-obol.obo
BP_XP_Dev=biological_process_xp_development-obol.obo
BP_XP_STIM=biological_process_xp_stimulus-obol.obo

MF_XP_CH=molecular_function_xp_chebi-obol.obo
MF_XP_CH_PRO=molecular_function_xp_chebi_pro-obol.obo
MFCat_XP_CH=catalysis_xp_chebi-obol.obo
# MF_XP_BP?
MF_XP_GO=molecular_function_xp_go-obol.obo
MF_XP_MF=molecular_function_xp_self-obol.obo
MF_XP_ANU=molecular_function_xp_uberon-obol.obo
MF_XP_PRO=molecular_function_xp_protein-obol.obo
CC_XP_CC=cellular_component_xp_self-obol.obo
CC_XP_GO=cellular_component_xp_go-obol.obo
CC_XP_CL=cellular_component_xp_cell-obol.obo
CC_XP_PRO=cellular_component_xp_protein-obol.obo
CC_XP_PRO_GREP=cellular_component_xp_protein-grep-obol.obo
CC_XP_GENE=cellular_component_xp_gene-obol.obo
CC_XP_FAO=cellular_component_xp_fungal_anatomy-obol.obo

MF_XP_MF_EDIT=../xps/molecular_function_xp_regulators.obo
MF_XP_PRO_EDIT=../xps/molecular_function_xp_protein.obo
#MF_XP_GO_EDIT=../xps/molecular_function_xp_go.obo
MF_XP_CC_EDIT=../xps/molecular_function_xp_cellular_component.obo
MF_XP_CH_EDIT=../xps/molecular_function_xp_chebi.obo
MF_XP_CH_PRO_EDIT=../xps/molecular_function_xp_chebi_protein.obo
BP_XP_BP_EDIT=../xps/biological_process_xp_self.obo
BP_XP_CC_EDIT=../xps/biological_process_xp_cellular_component.obo
BP_XP_MF_EDIT=../xps/biological_process_xp_molecular_function.obo
BP_XP_CL_EDIT=../xps/biological_process_xp_cell.obo
BP_XP_CH_EDIT=../xps/biological_process_xp_chebi.obo
BP_XP_PRO_EDIT=../xps/biological_process_xp_protein.obo
BP_XP_MOP_EDIT=../xps/biological_process_xp_multi_organism_process.obo
CC_XP_CC_EDIT=../xps/cellular_component_xp_self.obo
CC_XP_CC_UNVETTED=../xps/cellular_component_xp_self-unvetted.obo
CC_XP_GO_EDIT=../xps/cellular_component_xp_go.obo
CC_XP_CL_EDIT=../xps/cellular_component_xp_cell.obo
CC_XP_CH_EDIT=../xps/biological_process_xp_chebi.obo

BP_XP_ANU_EDIT=../xps/biological_process_xp_uber_anatomy.obo
BP_XP_ANF_EDIT=../xps/biological_process_xp_fly_anatomy.obo
BP_XP_ANFU_EDIT=../xps/biological_process_xp_fungal_anatomy.obo
BP_XP_ANDD_EDIT=../xps/biological_process_xp_dicty_anatomy.obo
BP_XP_ANZ_EDIT=../xps/biological_process_xp_zebrafish_anatomy.obo
BP_XP_ANM_EDIT=../xps/biological_process_xp_mouse_anatomy.obo
BP_XP_ANP_EDIT=../xps/biological_process_xp_plant_anatomy.obo
BP_XP_ANH_EDIT=../xps/biological_process_xp_human_anatomy.obo

BP_SRC= $(BP_XP_CL) $(BP_XP_BP) $(BP_XP_Q) $(BP_XP_ANU) $(BP_XP_ANP) $(BP_XP_ANDD)
BP_OBOL=$(patsubst %,%-obol.obo,$(BP_SRC))
BP_SRC_INPUT=$(patsubst %,-i %,$(BP_OBOL))
CC_SRC= $(CC_XP_CC) $(CC_XP_GO) $(CC_XP_CL) $(CC_XP_CH)
MF_SRC= $(MF_XP_MF) $(MF_XP_CC) $(MF_XP_CH) $(MF_XP_CH_PRO)
ALL_SRC=$(BP_SRC) $(CC_SRC) $(MF_SRC)
ALL_IMPLIED=$(patsubst %-obol.obo,%-implied.obo,$(ALL_SRC))

INC_BP_Main= -i $(BP_XP_CL_EDIT) -i $(BP_XP_CC_EDIT) -i $(BP_XP_BP_EDIT) -i $(BP_XP_R) -i $(BP_XP_Q) -i $(BP_XP_CH_EDIT) -i $(BP_XP_ANU_EDIT) -i $(BP_XP_ANZ_EDIT) -i $(BP_XP_ANP_EDIT) -i $(BP_XP_ANF_EDIT) -i $(BP_XP_ANFU_EDIT) -i $(BP_XP_PRO_EDIT)

GO=../../ontology/editors/gene_ontology_write.obo
#REGLIVE=../regulates_xp_live/go_reglive_withPosNeg_withPF.obo
REGLIVE=../regulates_xp_live/go_reglive_withPosNeg_withPF-bpgenus.obo

TAXDIR=../../quality_control/annotation_checks/taxon_checks/
GO2TAX=$(TAXDIR)/taxon_go_triggers.obo
TAXSLIM=$(TAXDIR)/ncbi_taxon_slim.obo

INC_TAX= -i $(GO2TAX) -i $(TAXSLIM) -table_pred ontol_db:bf_parentRT/2

all: $(ALL_SRC)
all_implied: $(ALL_IMPLIED)

TABLE_ANATOMY=-table_pred user:gross_anatomical/3 -table_pred user:gross_anatomical5/3

# ===================
# biological process
# ===================

# BP all
$(BP_XP): $(BP_OBOL) $(GO)
	$(OBOL) -u obol_go_xp_all -r cell -r xchebi -r pato  -parse_rule process $(BP_SRC_INPUT) "belongs(ID,biological_process)" >& $@.tmp && mv $@.tmp $@

# BP regulation terms
# this is generated regularly as a separate process in the above directory
# note: changed to RoBP+MF
#$(BP_XP_RoP): ../gene_ontology_xp.obo
$(BP_XP_R): $(REGLIVE)
	obo-grep.pl -r intersection_of $< | obo-filter-tags.pl -t id -t intersection_of - > $@.tmp && mv $@.tmp $@

$(BP_XP_RoP): $(BP_XP_R)
# TODO

# multi-organism process
# biological_role included in MOP_EDIT
$(BP_XP_MOP): $(BP_XP_R) $(BP_XP_CC_EDIT) $(BP_XP_MOP_EDIT)  $(GO)
	$(OBOL) -i $< -i $(BP_XP_CC_EDIT) -i $(BP_XP_MOP_EDIT) -u obol_biological_process_xp_self -parse_rule process  "parentT(ID,'GO:0051704')" >& $@.tmp && mv $@.tmp $@

# one-off
#PAMGO = PAMGO-Sep3.obo
PAMGO_EDIT = ../xps/biological_process_xp_pamgo_new.obo
pamgo-obol.obo: $(BP_XP_R) $(BP_XP_CC_EDIT) $(BP_XP_MOP_EDIT) $(PAMGO_EDIT)
	$(OBOL) -i $< -i $(BP_XP_CC_EDIT) -i $(BP_XP_MOP_EDIT) -i $(PAMGO_EDIT) -u obol_biological_process_xp_self -parse_rule process  "(class(ID),sub_atom(ID,_,_,_,'GO:0075'))" >& $@.tmp && mv $@.tmp $@
pamgo-reasoned.obo: obo2obo


$(BP_XP_CC_MOP): $(BP_XP_R) $(BP_XP_CC_EDIT) $(BP_XP_MOP_EDIT) $(GO)
	$(OBOL) -i $< -i $(BP_XP_CC_EDIT) -i $(BP_XP_MOP_EDIT) -u obol_biological_process_xp_anatomy_multispecies -parse_rule process  "subclassT(ID,'GO:0051704')" >& $@.tmp && mv $@.tmp $@

# BP x BP
# extends MOP and regulation xps
$(BP_XP_BP): $(BP_XP_R) $(BP_XP_CC_EDIT) $(BP_XP_MOP_EDIT) $(GO)
	$(OBOL) -i $< -i $(BP_XP_CC_EDIT) -i $(BP_XP_MOP_EDIT)  -i $(BP_XP_BP_EDIT) -u obol_biological_process_xp_self -parse_rule process  "(belongs(ID,biological_process),\+genus(ID,_))" >& $@.tmp && mv $@.tmp $@

plus-$(BP_XP_BP): $(BP_XP_R) $(BP_XP_CC_EDIT) $(BP_XP_MOP_EDIT) $(GO)
	obol obol-suggest -i $< -i $(BP_XP_CL_EDIT)  -i $(BP_XP_CC_EDIT)  -i $(BP_XP_CH_EDIT)  -i $(BP_XP_BP_EDIT) -i $(BP_XP_MOP_EDIT) -r go  "belongs(ID,biological_process)" >& $@.tmp && mv $@.tmp $@

involved_in-$(BP_XP_BP):
	blip-findall -u ontol_writer_obo -r go -u metadata_nlp "term_split_over_strict(E,A,B,'involved in'),id_idspace(A,'GO'),id_idspace(B,'GO'),write_cdef(obo,E,cdef(A,[part_of=B])),fail" > $@

# BP x PATO (regulation)
# Example: regulation of size
# Note: this will make more sense when we have a shared anatomy ontology, so we can have regulation of BLOOD pressure
# We include CL so we can have "establishment and/or maintenance of neuroblast polarity"
$(BP_X_Q):  $(GO)
	$(OBOL) -u obol_biological_process_xp_quality -u obol_biological_process_xp_cell  -r pato -r cell -r xchebi -parse_rule process -i $(BP_XP_BP) -i $(BP_XP_RoP)  -i $(BP_XP_RoF) -i $(BP_XP_CC) -i $(BP_XP_CL) -i $(BP_XP_CH)   -xp_ontology quality  "belongs(ID,'biological_process')" >& $@.tmp && mv $@.tmp $@
####	$(OBOL) -u obol_biological_process_xp_quality -u obol_biological_process_xp_cell  -r pato -r cell -r xchebi -parse_rule process -i $(BP_XP_BP) -i $(BP_XP_RoP) -i $(BP_XP_CC) -i $(BP_XP_CL) -i $(BP_XP_CH)  "(belongs(ID,'biological_process'),subclassT(ID,Root),class(Root,'regulation of biological quality'))" >& $@.tmp && mv $@.tmp $@

# BP x MF (regulation)
# todo: other BP-MF links?
$(BP_XP_RoF):  $(BP_XP_CL) $(BP_XP_BP) $(GO)
	$(OBOL) -u obol_biological_process_xp_function  -parse_rule process -i $(BP_XP_BP)  "(belongs(ID,biological_process),subclassRT(ID,Root),class(Root,'regulation of a molecular function'))" >& $@.tmp && mv $@.tmp $@

# BP x CC - any biological_process that refers to a cellular_component
# eg U12-dependent spliceosome disassembly
$(BP_XP_CC): $(BP_XP_CC_EDIT) $(GO)
	$(OBOL) -u obol_biological_process_xp_cellular_component  -parse_rule process -i $< -i $(BP_XP_BP_EDIT) -i $(BP_XP_CL_EDIT) -i $(BP_XP_R) -i $< -xp_ontology cellular_component  "(belongs(ID,biological_process))" >& $@.tmp && mv $@.tmp $@

#$(BP_XP_CC): bp-cc-aln.tbl $(BP_XP_CC_EDIT)
#	$(OBOL) -u obol_biological_process_xp_cellular_component  -parse_rule process -i $< -i $(BP_XP_BP_EDIT) -i $(BP_XP_CL_EDIT) -i $(BP_XP_RoP) -i $< -i $(BP_XP_CC_EDIT) -xp_ontology cellular_component  "(substr(_,XID,ID,_,_,_,_),belongs(ID,biological_process))" >& $@.tmp && mv $@.tmp $@

#organization-$(BP_XP_CC): $(BP_XP_CC_EDIT) $(GO)
#	$(OBOL) -u obol_biological_process_xp_cellular_component  -parse_rule process -i $< -i $(BP_XP_BP_EDIT) -i $(BP_XP_CL_EDIT) -i $(BP_XP_R) -i $< -xp_ontology cellular_component  "(belongs(ID,biological_process),entity_label_scope(ID,N,_),(sub_atom(N,_,_,0,organization);sub_atom(N,_,_,0,biogenesis)))" >& $@.tmp && mv $@.tmp $@

organization-$(BP_XP_CC): $(BP_XP_CC_EDIT) $(GO)
	$(OBOL) -u obol_biological_process_xp_cellular_component  -parse_rule process -i $< -i $(BP_XP_BP_EDIT) -i $(BP_XP_CL_EDIT) -i $(BP_XP_R) -i $< -xp_ontology cellular_component  "(subclassRT(ID,'GO:0016043'))" >& $@.tmp && mv $@.tmp $@

endmatch-%-$(BP_XP_CC): $(BP_XP_CC_EDIT) $(GO)
	$(OBOL) -u obol_biological_process_xp_cellular_component  -parse_rule process -i $< -i $(BP_XP_BP_EDIT) -i $(BP_XP_CL_EDIT) -i $(BP_XP_R) -i $< -xp_ontology cellular_component  "(belongs(ID,biological_process),entity_label_scope(ID,N,_),sub_atom(N,_,_,0,'$*'))" >& $@.tmp && mv $@.tmp $@

$(BP_XP_MF): $(BP_XP_MF_EDIT) $(GO)
	$(OBOL) -u obol_biological_process_xp_function  -parse_rule process -i $< -i $(BP_XP_MF_EDIT) -i  $(BP_XP_CC_EDIT) -i  $(BP_XP_BP_EDIT) -i $(BP_XP_CL_EDIT) -i $(BP_XP_CH_EDIT) -i $(BP_XP_R) -i $< -xp_ontology molecular_function  "(subclassT(ID,'GO:0008152'))" >& $@.tmp && mv $@.tmp $@

# BP x CL
$(BP_XP_CL):  $(BP_XP_CL_EDIT) $(GO)
	$(OBOL) -u obol_biological_process_xp_cell -u obol_biological_process_xp_cellular_component -r cell -r hemo_CL  -parse_rule process  -i $(BP_XP_RoP) -i $(BP_XP_CL_EDIT) -i $(BP_XP_CH_EDIT) -i $(BP_XP_BP_EDIT) -i $(BP_XP_CC_EDIT)  "belongs(ID,biological_process)" >& $@.tmp && mv $@.tmp $@

## NEW
new-CL.txt:
	 obol qobol-newterms -ontology GO -r cell  -tag bp -tag generic -tag cell -subclass GO:0009987 -taxon NCBITaxon:33208 > $@
	# (obol qobol-newterms -ontology GO -xont CL -subclass GO:0009987 -tag bp -tag generic -tag cell -undefined_only true -export obo > $@) 2> $@.err

new-CL-cd.txt:
	 obol qobol-newterms -ontology GO -r CL -tag generic -subclass GO:0030154  > $@

new-RoBQ.txt:
	 obol qobol-newterms -ontology GO -tag bp -tag regulation -tag quality -subclass GO:0065008  > $@

new-INVOLVED.txt:
	 obol qobol-newterms -ontology GO -tag bp -tag involved   > $@

x-bq-obol.obo:
	obol qobol  -ontology GO -i bbq.obo -subclass GO:0065008 -tag quality -tag regulation -undefined_only true -export obo -scope all > $@

bbq-obol.obo:
	obol qobol  -ontology BBQ -tag trait -tag bbq  -undefined_only true -export obo -scope all > $@

new-UBERON.txt:
	 obol qobol-newterms -ontology GO -r uberonp -r goxp/biological_process_xp_uber_anatomy  -r goxp/biological_process_xp_fly_anatomy  -r goxp/biological_process_xp_fungal_anatomy -tag bp -tag generic -tag anatomy -subclass GO:0032502 -not_subclass GO:0048468 -taxon NCBITaxon:33208 > $@
new-GOCHE.txt:
	 obol qobol-newterms -ontology GO   -tag bp -tag generic -tag chemical -subclass GO:0008152 > $@
new-PR.txt:
	 obol qobol-newterms -ontology GO -r protein -r goxp/molecular_function_xp_protein -tag binding -subclass GO:0005515 -not_subclass GO:0032403 -not_subclass GO:0019904 > $@

new-CC-assembly.txt:
	 obol qobol-newterms -ontology GO -r go_xp -tag generic -subclass GO:0022607 > $@

ESTAB_LOC = GO:0051234
MAINT_LOC = GO:0051235


bp_xp_generic-obol.obo:
	(obol qobol -ontology GO  -r chebi -r ions -r go_transport  -tag generic -tag bp -undefined_only true -export obo > $@) >& $@.err


bp_xp_transport-obol.obo:
	(obol qobol  -ontology GO -r go_xp -r chebi -r ions -r go_transport -subclass $(ESTAB_LOC) -tag transport -undefined_only true -export obo > $@) >& $@.err
bp_xp_transport-obol-allsyns.obo:
	obol qobol  -ontology GO -r go_xp -r chebi -r ions -r go_transport -subclass $(ESTAB_LOC) -tag transport -undefined_only true -export obo -scope all > $@

porter.obo:
	./hack-porter.pl  ../../ontology/editors/gene_ontology_write.obo > $@
mf_xp_transport-obol.obo:
	(obol qobol  -ontology GO  -r chebi -r ions -i porter.obo -subclass GO:0005215 -tag transport -undefined_only true -export obo > $@) >& $@.err
mf_xp_transport-obol-allsyns.obo:
	obol qobol  -ontology GO  -r chebi -r ions  -i porter.obo -subclass GO:0005215 -tag transport -undefined_only true -export obo -scope all > $@

# DEP
go-t.obo:
	blip-findall -r go ontol-query -query "parentRT(ID,part_of,'GO:0051179')" -to obo > go-t.obo 

go-p.obo:
	owltools http://purl.obolibrary.org/obo/go.owl --make-subset-by-properties part_of // -o -f obo $@

go-xp-t-all.obo: go-p.obo
	( obol qobol -debug foo -idspace GO -scope all -i $< -r go_xp_chebi -r chebi -r ions -r go_transport -oneof transport -oneof generic -undefined_only true -export obo -union GO:0051179 -union GO:0051234 -union GO:0051235 > $@ ) >& $@.err

bp_xp_uberon-obol.obo:
	obol qobol -ontology GO -xont UBERON -tag bp -tag anatomy -undefined_only true -export obo > $@
bp_xp_cl-obol.obo:
	(obol qobol -debug qobol -ontology GO -xont CL -subclass GO:0009987 -tag bp -tag generic -tag cell -undefined_only true -export obo > $@) 2> $@.err
bp_xp_involved-obol.obo:
	obol qobol -ontology GO -tag bp -tag involved -undefined_only true -export obo > $@
#bp_xp_involved-obol.obo:
#	obol qobol -ontology GO -tag bp -tag involved -undefined_only true -export obo > $@
bp_xp_po-obol.obo:
	obol qobol -ontology GO -xont PO -tag bp -tag generic  -tag anatomy -undefined_only true -export obo > $@
bp_xp_po_cl-obol.obo:
	obol qobol -ontology GO -xont PO -tag bp -tag cell  -tag generic -undefined_only true -subclass GO:0009987 -export obo > $@
mf_xp_pro-obol.obo:
	obol qobol -ontology GO -xont PR -subclass GO:0005515  -tag binding  -undefined_only true -export obo > $@
mf_xp_pro-RA-obol.obo:
	obol qobol -ontology GO -xont PR -subclass GO:0004872  -tag receptor_activity  -undefined_only true -export obo > $@
bp_xp_fao-obol.obo:
	obol qobol -debug qobol -ontology GO -r FAO -r goxp/biological_process_xp_fungal_anatomy -r goxp/biological_process_xp_uber_anatomy -tag bp -tag generic  -tag anatomy -undefined_only true -export obo > $@
bp_xp_zfa-obol.obo:
	obol qobol -ontology GO -r zebrafish_anatomy -r goxp/biological_process_xp_zebrafish_anatomy -tag bp -tag generic  -tag anatomy -undefined_only true -export obo > $@
bp_xp_xao-obol.obo:
	obol qobol -ontology GO -r xenopus_anatomy -r goxp/biological_process_xp_uber_anatomy -tag bp -tag generic  -tag anatomy -undefined_only true -export obo > $@
bp_xp_wbbt-obol.obo:
	obol qobol -debug qobol -ontology GO -r worm_anatomy -subclass GO:0032502 -r goxp/biological_process_xp_worm_anatomy -r goxp/biological_process_xp_uber_anatomy -tag bp -tag generic  -tag anatomy -undefined_only true -export obo > $@
bp_xp_chebi-obol.obo:
	obol  qobol -ontology GO -xont CHEBI -tag bp -tag generic  -tag chemical -undefined_only true -export obo > $@
bp_xp_allchebi-obol.obo:
	obol -i chebi_lite.obo  qobol -ontology GO -xont CHEBI -tag bp -tag generic  -tag chemical -undefined_only true -export obo > $@
bp_xp_cc-occurs_in-obol.obo:
	obol qobol -ontology GO -tag bp -tag cc -tag in -undefined_only true -export obo > $@


##

# we seed this with an alignment between BP and CL
# also include chebi xps to avoid false +ves (eg spermine transport)
# we DON'T include curated go_xp_cell for now, as the goal is to redo this

differentiation-$(BP_XP_CL): $(BP_XP_CL_EDIT) $(GO)
	$(OBOL) -u obol_biological_process_xp_cell -u obol_biological_process_xp_cellular_component -r cell  -parse_rule process -i $(BP_XP_RoP) -i $(BP_XP_CL_EDIT) -i $(BP_XP_CH_EDIT) -i $(BP_XP_BP_EDIT) -i $(BP_XP_CC_EDIT) "(subclassT(ID,'GO:0030154'))" >& $@.tmp && mv $@.tmp $@

# BP x CHEBI
# we seed this with an alignment between BP and CHEBI
# we exclude anything part of curated go-xp-chebi links
$(BP_XP_CH):  $(GO)
	$(OBOL) -u obol_biological_process_xp_chemical -r xchebi  -parse_rule process -i $(BP_XP_R) -i $(BP_XP_BP_EDIT) -i $(BP_XP_CC_EDIT) -i $(BP_XP_CH_EDIT)  "belongs(ID,biological_process)" >& $@.tmp && mv $@.tmp $@
#$(BP_XP_CH): bp-chebi-aln.tbl 
#	$(OBOL) -u obol_biological_process_xp_chemical -r xchebi  -parse_rule process -i $(BP_XP_RoP) -i $(BP_XP_RoF) -i $(BP_XP_Q) -i $(BP_XP_BP) -i $(BP_XP_CC) -i $(BP_XP_CH_EDIT) -i bp-chebi-aln.tbl "(substr(_,XID,ID,_,_,_,_),belongs(ID,biological_process))" >& $@.tmp && mv $@.tmp $@

transport-$(BP_XP_CH):  $(GO)
	$(OBOL) -u obol_biological_process_xp_chemical -r xchebi  -parse_rule process -i $(BP_XP_RoP) -i $(BP_XP_RoF) -i $(BP_XP_BP_EDIT) -i $(BP_XP_CC_EDIT) -i $(BP_XP_CH_EDIT)  "(subclassT(ID,'GO:0006810'))" >& $@.tmp && mv $@.tmp $@

metabolic-$(BP_XP_CH):  $(GO)
	$(OBOL) -u obol_biological_process_xp_chemical -r xchebi  -parse_rule process -i $(BP_XP_R) -i $(BP_XP_RoF) -i $(BP_XP_BP_EDIT) -i $(BP_XP_CC_EDIT) -i $(BP_XP_CH_EDIT)  "(subclassT(ID,'GO:0008152'))" >& $@.tmp && mv $@.tmp $@


fixed-pro.obo: pro.obo
	./remove-protein.pl $< > $@
no-pro-go.obo: gene_ontology_write.obo
	./remove-protein.pl $< > $@

# BP x PRO
$(BP_XP_PRO): $(GO)
	$(OBOL)  -i fixed-pro.obo -i no-pro-go.obo -parse_rule process  $(INC_BP_Main) "(belongs(ID,biological_process))" >& $@.tmp && mv $@.tmp $@

force-$(BP_XP_PRO): $(GO)
	$(OBOL) -debug protein -u obol_biological_process_xp_protein_with_force -r protein -parse_rule process -i $(BP_XP_R) -i $(BP_XP_Q) -i $(BP_XP_BP_EDIT) -i $(BP_XP_CC_EDIT) -i $(BP_XP_CH_EDIT) -i $(BP_XP_PRO_EDIT) "(belongs(X,biological_process),differentium(X,_,P),id_idspace(P,'PRO'),debug(protein,'P:~w',[X]),subclassRT(ID,X))" >& $@.tmp && mv $@.tmp $@


# BP x MF - signaling (relies on PRO / BF x MF)
$(BP_XP_SIG): $(GO)
	$(OBOL) -u obol_biological_process_xp_signaling -r protein -parse_rule process -i $(BP_XP_R) -i $(BP_XP_Q) -i $(BP_XP_BP_EDIT) -i $(BP_XP_CC_EDIT) -i $(BP_XP_CH_EDIT) -i $(MF_XP_PRO_EDIT) -i $(BP_XP_PRO_EDIT) "(belongs(ID,biological_process),class(ID,N),sub_atom(N,_,_,0,'signaling pathway'))" >& $@.tmp && mv $@.tmp $@

# BP x anatomy - development EXPERIMENTAL
# use the _with_force version to force materialization of anatomical continuant anonymous classes
# at this stage this will mostly generate anon terms, as we are lacking CARO+
# todo: first rerun sc anatomy xps, exclude uberon. manually edit so that it's species centric
$(BP_XP_Dev): $(GO)
	$(OBOL) -r uberon  $(BP_ANAT_PARAMS)  -u obol_biological_process_xp_anatomy_with_force -u obol_biological_process_xp_self -r caro -r cell -parse_rule process $(INC_BP_Main)  "(belongs(ID,biological_process),parentT(ID,Root),class(Root,'developmental process'))" >& $@.tmp && mv $@.tmp $@

$(BP_XP_STIM): $(GO) stimulus.obo
	$(OBOL) -u obol_biological_process_xp_stimulus_force -parse_rule process $(INC_BP_Main)  "(belongs(ID,biological_process),subclassT(ID,Root),class(Root,'response to stimulus'))" >& $@.tmp && mv $@.tmp $@

stimulus.obo: $(BP_XP_STIM) $(GO)
	blip-ddb -r go -i $< -u ontol_db io-convert -to obo -u ontol_manifest_abduced_links -include ontology//stimulus -midgoal manifest -o $@

# BP x anatomy - using ssAOs
# examples: wing disc pattern formation; central nervous system neuron axonogenesis
# we overpredict: treat related synonyms as exact (TODO: only for anatomies); this is because many AOs don't use synonym scopes yet
# we also overstate defs, making them more specific than is the case, as we are limited by ssAOs
# - this can result in unusual xps, like optic placode formation = formation of fly:primordium zf:optic
# we also throw in PATO: eg e.g. regulation of blood pressure
$(BP_XP_AN): bp-anat-aln.tbl $(GO)
	$(OBOL)  -u ontol_manifest_exact_from_related_synonym -u obol_biological_process_xp_anatomy_with_force -u obol_biological_process_xp_self -u obol_biological_process_xp_quality -u obol_biological_process_xp_cell  -r cell -r pato $(ANAT) $(TABLE_ANATOMY) -parse_rule process $(INC_BP_Main) -i $<  "(class(Root,'developmental process'),belongs(ID,biological_process),(subclassT(ID,Root) ; substr(_,XID,ID,_,_,_,_),\+belongs(XID,biological_process)))" >& $@.tmp && mv $@.tmp $@

BP_ANAT_PARAMS= -u obol_biological_process_xp_anatomy -u obol_biological_process_xp_self -u obol_biological_process_xp_quality -u obol_biological_process_xp_cell  -parse_rule process $(INC_BP_Main) -r cell -r pato   -newonly

# uberon
$(BP_XP_ANU): $(GO)
	$(OBOL) -r uberon $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) -u obol_biological_process_xp_quality -i $< $(INC_BP_Main) "belongs(ID,biological_process),\+genus(ID,_),subclassT(ID,'GO:0032502')" >& $@.tmp && mv $@.tmp $@
#	$(OBOL) -u metadata_nlp -r uberon $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) -u obol_biological_process_xp_quality -i $< $(INC_BP_Main) -index "metadata_db:entity_label/2" -goal "index_labels(false)" "belongs(ID,biological_process),\+genus(ID,_),entity_pair_nlp_subset_of(U,ID,1),id_idspace(U,'UBERON')" >& $@.tmp && mv $@.tmp $@
#	$(OBOL) -r uberon $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) -u obol_biological_process_xp_quality -i $< $(INC_BP_Main) "belongs(ID,biological_process)" >& $@.tmp && mv $@.tmp $@

under-$(PARENT)-$(BP_XP_ANU): $(GO)
	$(OBOL) -table_pred ontol_db:bf_parentRT/2 -r uberon -r cell $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) -u obol_biological_process_xp_quality -i $< $(INC_BP_Main) "belongs(ID,biological_process),bf_parentRT(ID,'GO:$(PARENT)')" >& $@.tmp && mv $@.tmp $@


# uberon+pato : e.g. regulation of blood vessel diameter
$(BP_XP_QANU):  $(GO)
	$(OBOL) -r uberon $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) -u obol_biological_process_xp_quality -r pato -i $<  "belongs(ID,biological_process),subclassRT(ID,'GO:0065008')" >& $@.tmp && mv $@.tmp $@

development-$(BP_XP_ANU):  $(GO)
	$(OBOL) -r uberon $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) -u obol_biological_process_xp_quality -r pato -i $<  "(belongs(ID,biological_process),subclassT(ID,'GO:0032502'))" >& $@.tmp && mv $@.tmp $@

$(BP_XP_ANM): bp-mouse_anat-aln.tbl $(GO)
	$(OBOL) -r mouse_anatomy $(TABLE_ANATOMY) $(BP_ANAT_PARAMS)  -i $<  "(substr(_,XID,ID,_,_,_,_),belongs(ID,biological_process),belongs(XID,'adult_mouse_anatomy.gxd'),\+class(XID,cell),\+genus(ID,_))" >& $@.tmp && mv $@.tmp $@

#$(BP_XP_ANP): bp-plant_anat-aln.tbl $(GO)
#	$(OBOL) -r plant_anatomy $(TABLE_ANATOMY) $(BP_ANAT_PARAMS)  -i $<  "(substr(_,XID,ID,_,_,_,_),belongs(ID,biological_process),belongs(XID,'plant_structure'),\+class(XID,cell),\+genus(ID,_))" >& $@.tmp && mv $@.tmp $@
$(BP_XP_ANP): $(GO)
	$(OBOL) -u ontol_manifest_exact_from_related_synonym  -r plant_anatomy $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) $(INC_TAX) "bf_parentRT(ID,'GO:0032502')" >& $@.tmp && mv $@.tmp $@
##	$(OBOL) -u ontol_manifest_exact_from_related_synonym  -r plant_anatomy $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) $(INC_TAX) "class(X,'Viridiplantae'),belongs(ID,biological_process),bf_parentRT(ID,X)" >& $@.tmp && mv $@.tmp $@


# fly anatomy
#  only BP terms unique to fly
# use related synonyms? only for fly?
$(BP_XP_ANF): $(GO)
	$(OBOL) -u ontol_manifest_exact_from_related_synonym  -r fly_anatomy $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) $(INC_TAX) "class(X,'Arthropoda'),belongs(ID,biological_process),bf_parentRT(ID,X)" >& $@.tmp && mv $@.tmp $@

$(BP_XP_ANFU): $(GO)
	$(OBOL) -u ontol_manifest_exact_from_related_synonym -r fungal_anatomy $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) $(INC_TAX) "bf_parentRT(ID,'GO:0048856')" >& $@.tmp && mv $@.tmp $@
##	$(OBOL) -u ontol_manifest_exact_from_related_synonym -r fungal_anatomy $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) $(INC_TAX) "class(X,'Fungi'),belongs(ID,biological_process),bf_parentRT(ID,X)" >& $@.tmp && mv $@.tmp $@

$(BP_XP_ANZ): $(GO)
	$(OBOL) -u ontol_manifest_exact_from_related_synonym  -r zebrafish_anatomy $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) $(INC_TAX) "class(X,'Teleosti'),belongs(ID,biological_process),bf_parentRT(ID,X)" >& $@.tmp && mv $@.tmp $@

# DEPRECATED:
# fma alignment has lots of true negatives; also include everything in the developmental branch, or reg of BQ
$(BP_XP_ANH): bp-fma_anat-aln.tbl $(GO)
	$(OBOL) -r fma_downcase $(TABLE_ANATOMY) $(BP_ANAT_PARAMS)  -i $<  "(((substr(_,XID,ID,_,_,_,_),belongs(XID,'fma'),\+class(XID,cell)) ; (class(Root,'anatomical structure development'),parentT(ID,Root)) ; (class(Root,'regulation of biological quality'),parentT(ID,Root))),belongs(ID,biological_process),\+genus(ID,_))" >& $@.tmp && mv $@.tmp $@

$(BP_XP_ANDD): $(GO)
	$(OBOL) -u ontol_manifest_exact_from_related_synonym  -r zebrafish_anatomy $(TABLE_ANATOMY) $(BP_ANAT_PARAMS) $(INC_TAX) "class(X,'Amoebozoa'),belongs(ID,biological_process),bf_parentRT(ID,X)" >& $@.tmp && mv $@.tmp $@


# BP x SO -- experimental
# only use children of LSF
$(BP_XP_SQ): bp-sequence-aln.tbl $(GO)
	$(OBOL) -u obol_biological_process_xp_sequence -r sequence -r xchebi  -i $(BP_XP_CC_EDIT)  -parse_rule process -i $(BP_XP_RoP) -i bp-sequence-aln.tbl "(substr(_,XID,ID,_,_,_,_),belongs(ID,biological_process),class(XRootID,'sequence_feature'),subclassT(XID,XRootID))" >& $@.tmp && mv $@.tmp $@


# generic
# do NOT us related synonyms
biological_process_xp_%_anatomy-obol.obo: $(GO)
	$(OBOL) -r $*_anatomy $(BP_ANAT_PARAMS) $(INC_BP_Main) -i $< -i ../xps/biological_process_xp_$*_anatomy.obo "parentT(ID,'GO:0048856')" >& $@.tmp && mv $@.tmp $@


# ===================
# molecular function
# ===================

MF_ARGS = -u ontol_manifest_synonym_from_molecular_function -table_pred metadata_db:entity_synonym/2 -table_pred metadata_db:entity_synonym_scope/3

# MF x CHEBI
$(MF_XP_CH):  $(GO)
	$(OBOL) -i $(MF_XP_CH_EDIT) -i $(MF_XP_MF_EDIT) -i $(MF_XP_CC_EDIT) -i $(BP_XP_CH_EDIT) -u obol_molecular_function_xp_chemical -r xchebi  -parse_rule molecular_function "belongs(ID,molecular_function)" >& $@.tmp && mv $@.tmp $@

# MF x (CHEBI u PRO)
$(MF_XP_CH_PRO):  $(GO)
	$(OBOL) -i $(MF_XP_CH_EDIT) -i $(MF_XP_CH_PRO_EDIT) -i $(MF_XP_MF_EDIT) -i $(MF_XP_CC_EDIT) -i $(BP_XP_CH_EDIT) -u obol_molecular_function_xp_chemical -r xchebi -r protein  -parse_rule molecular_function "belongs(ID,molecular_function)" >& $@.tmp && mv $@.tmp $@

# deprec?
$(MFCat_XP_CH):  $(GO)
	$(OBOL) -i $(MF_XP_MF_EDIT) -i $(MF_XP_CC_EDIT)  -i $(MF_XP_CH_EDIT) -u obol_molecular_function_xp_chemical -r xchebi -parse_rule def "subclassT(ID,'GO:0003824')" >& $@.tmp && mv $@.tmp $@

# this this is the best way...
catalysis-chebi.txt:
	(blip-findall -debug chebi -r xchebi -r go -u query_go function_participants/4 -label > $@ ) >& $@.err

# MF x Uberon: mostly structural constituents
$(MF_XP_ANU):  $(GO)
	$(OBOL) -i $(MF_XP_CC_EDIT) -i $(MF_XP_MF_EDIT) -u obol_molecular_function_xp_cellular_component -r uberon -parse_rule molecular_function "belongs(ID,molecular_function)" >& $@.tmp && mv $@.tmp $@

# MF x PRO
$(MF_XP_PRO):  $(GO)
	$(OBOL)  -i $(MF_XP_CH_EDIT)  -i $(MF_XP_PRO_EDIT) -i $(MF_XP_MF_EDIT) -i $(MF_XP_CC_EDIT) -u obol_molecular_function_xp_chemical -i fixed-pro.obo -i no-pro-go.obo -r xchebi -parse_rule molecular_function "belongs(ID,molecular_function)" >& $@.tmp && mv $@.tmp $@

binding-$(MF_XP_PRO):  $(GO)
	$(OBOL) -i $(MF_XP_CH_EDIT) -i $(MF_XP_PRO_EDIT) -i $(MF_XP_MF_EDIT) -i $(MF_XP_CC_EDIT) -u obol_molecular_function_xp_binding -r protein -r xchebi -parse_rule molecular_function "belongs(ID,molecular_function),class(ID,N),sub_atom(N,_,_,0,binding)" >& $@.tmp && mv $@.tmp $@


# MF x GO
# eg structural constituent of cell wall; lamelin-1 binding [in chemical grammar]
$(MF_XP_GO):  $(GO)
	$(OBOL) -i $(MF_XP_MF_EDIT) -i $(MF_XP_CC_EDIT) -i $(BP_XP_CH_EDIT) -u obol_molecular_function_xp_cellular_component -u obol_molecular_function_xp_chemical  -parse_rule molecular_function "belongs(ID,molecular_function)" >& $@.tmp && mv $@.tmp $@

# MF regulator activity
$(MF_XP_MF):  $(GO).noactivity.obo
	$(OBOL_NOGO) -i $<  -i $(MF_XP_MF_EDIT) $(MF_ARGS) -u obol_molecular_function_xp_self -parse_rule molecular_function "belongs(ID,molecular_function)" >& $@.tmp && mv $@.tmp $@


# ===================
# cellular component
# ===================

# CC x CC
# self/intra-cc
$(CC_XP_CC): $(CC_XP_CC_EDIT) $(GO)
	$(OBOL_NOGO) -i $< -i $(CC_XP_CC_UNVETTED) -r obo/cellular_component -u ontol_manifest_synonym_from_molecular_function -u obol_cellular_component_xp -parse_rule cellular_component  "belongs(ID,cellular_component)" >& $@.tmp && mv $@.tmp $@

plus-$(CC_XP_CC): $(CC_XP_CC_EDIT) $(GO)
	obol obol-suggest -i $< -i $(CC_XP_CL_EDIT) -i $(CC_XP_GO_EDIT) -r obo/cellular_component  "belongs(ID,cellular_component)" >& $@.tmp && mv $@.tmp $@

# CC x GO
# both self-xps and xps to function
# redundant with CC x CC
# examples:
#  Golgi apparatus membrane (CC x CC)
#  virion transport vesicle (CC x CC)
#  DNA helicase complex
$(CC_XP_GO):  $(CC_XP_CC_EDIT) $(CC_XP_CL_EDIT) $(GO)
	$(OBOL) -u ontol_manifest_synonym_from_molecular_function -u obol_cellular_component_xp -u obol_cellular_component_xp_function -u obol_cellular_component_xp_biological_process -i $(CC_XP_CC_EDIT) -i $(CC_XP_CL_EDIT) -i $(CC_XP_GO_EDIT)  -parse_rule cellular_component  "belongs(ID,cellular_component)" >& $@.tmp && mv $@.tmp $@

# CC x CL
# examples: B cell receptor complex
# exclude cc_xp_go
$(CC_XP_CL): $(CC_XP_GO) $(CC_XP_CC_EDIT) $(GO)
	$(OBOL) -u obol_cellular_component_xp_cell  -parse_rule cellular_component -r cell -i $(CC_XP_GO) -i $(CC_XP_CC_EDIT) -i $(CC_XP_CL_EDIT) -i $< "belongs(ID,cellular_component)" >& $@.tmp && mv $@.tmp $@

$(CC_XP_PRO):  $(CC_XP_CC_EDIT) $(GO)
	$(OBOL) -u obol_cellular_component_xp_protein  -parse_rule cellular_component -r protein  -i $(CC_XP_CC_EDIT) -i $(CC_XP_CL_EDIT) -i $< "belongs(ID,cellular_component)" >& $@.tmp && mv $@.tmp $@

$(CC_XP_PRO_GREP):  $(CC_XP_CC_EDIT) $(GO)
	obol -u onto_grep onto-grep -r protein -r go -query "belongs(ID,cellular_component)"  >& $@.tmp && mv $@.tmp $@

$(CC_XP_GENE):  $(CC_XP_CC_EDIT) $(GO)
	obol -u onto_grep onto-grep -r gene/10090 -r go -query "belongs(ID,cellular_component)"  >& $@.tmp && mv $@.tmp $@

# e.g.: sexual spore wall
$(CC_XP_FAO): $(CC_XP_GO_EDIT) $(CC_XP_CC_EDIT) $(CC_XP_CL_EDIT) $(GO)
	$(OBOL) -u obol_cellular_component_xp_cell  -parse_rule cellular_component -r fungal_anatomy -i $(CC_XP_GO) -i $(CC_XP_CC_EDIT) -i $< "belongs(ID,cellular_component)" >& $@.tmp && mv $@.tmp $@

NIF-Subcellular-entity.obo: NIF-Subcellular.obo
	blip -i $< ontol-query -query "subclassRT(ID,'NIF_Subcellular:nlx_subcell_100315')" -to obo > $@

cc.obo:
	blip -r go ontol-query -query "subclassRT(ID,'GO:0005575')" -to obo > $@

cellular_component-align-NIF.obo: NIF-Subcellular-entity.obo
	blip -i NIF-Subcellular.obo -i cc.obo -u onto_grep onto-exact-align -exclude_xref -exclude_xref_strict -disp 'format(obo)' > $@.tmp && obo-simple-merge.pl $@.tmp > $@
#	obol -debug obol  -i $< -r obo/cellular_component ontol-align -stem -shownames > $@.tmp && mv $@.tmp $@
#	obol -debug obol -u onto_grep -i $< -r obo/cellular_component onto-exact-align -ont birnlex_anatomy -ont2 cellular_component -exclude_xref_strict -exclude_xref -disp 'format(obo)' > $@.tmp && mv $@.tmp $@

# from NIF:
cellular_component_links_cell.obo:
	blip-findall -r go -i nif_subcellular_xp.obo -i nif_subcellular.obo -i nif_cell.obo -r cell -u query_go "part_of_from_xref('GO','CL',A,B)" | cut -f4,5 | tbl2obolinks.pl --rel part_of -

signaling-obol.obo:
	blip -r go ontol-query -u obol_signaling -n '%signaling pathway' -to obo | obo-grep.pl -r intersection_of - | obo-filter-tags.pl -t id -t def -t intersection_of > $@


# ===================
# comparison
# ===================
go_xp_cell.diff: $(GO)
	obol -i ro_test.obo -r relationship -r cell -r go -i ../go_xp_cell.obo -i $(BP_XP_CL) ontol-diff-cdef > $@.tmp && mv $@.tmp $@

timing-bp-cell.txt: $(GO)
	blip -r implied/go -r implied/cell -i biological_process_xp_cell-obol.obo -u query_go_xp findall formation2/7 > $@.tmp && mv $@.tmp $@

timing-bp-%.txt: $(GO)
	blip -r implied/go -r implied/$*_anatomy -i biological_process_xp_$*_anatomy-obol.obo -u query_go_xp findall formation2/7 > $@.tmp && mv $@.tmp $@

go_xp_regulation_new.obo: $(REGLIVE)
	 obol obol-generate-xps -idspace GO: -id_offset 9000000 -debug obol -u xp_reg_go -i $< > $@.tmp && mv $@.tmp $@
.PRECIOUS: go_xp_regulation_new.obo

%.noactivity.obo: %
	./fix-mf.pl $< > $@

%_wn1.obo: %.obo
	obol -i $< -r pato -r go -u obol_biological_process_xp_regulation obol-generate-names -write_synonyms > $@.tmp && mv $@.tmp $@
.PRECIOUS: %_wn1.obo

%_wn.obo: %_wn1.obo
	obo2obo -o $@ $< $*.obo

%_plusgo.obo: %_wn.obo $(REGLIVE)
	obo2obo -o -saveimpliedlinks $@ $< $(REGLIVE)

# ===================
# chebi-hacking
# ===================
CHEBI=$(HOME)/cvs/obo/ontology/chemical/chebi.obo

xchebi.obo: $(CHEBI)
	(./go-transform-chebi.pl $< > $@.tmp) >& $@.log && mv $@.tmp $@

# remove xrefs as these don't parse correctly in CHEBI
chebi_lite.obo: $(CHEBI)
	perl -ne 'print unless /^relationship: (\S+)/ && $$1 !~ /conjug/ && $$1 !~ /parent_hydride/' $< | grep -v ^xref > $@
#	grep -v ^relationship: $< > $@

chebi_lite2.obo: $(CHEBI)
	grep -v ^relationship: $< > $@

chebi_ultralite.obo: $(CHEBI)
	grep -v ^relationship: $< | grep -v ^is_a > $@

chebi_minilite.obo: chebi_lite.obo
	blip-findall -i goche.obo "class(X)" -select "goche_id(X)" -write_prolog > goche_id.pro && blip -i $< -i goche_id.pro ontol-query -table_pred subclassT/2 -query "goche_id(X),subclassRT(X,ID)" -to obo > $@

chebi_with_formula.obo:
	blip -r chebi -u ontol_manifest_cdef_from_chebi_formula io-convert -to obo -o $@.tmp && mv $@.tmp $@
.PRECIOUS: chebi_with_formula.obo

chebi_formula.obo: chebi_with_formula.obo
	obo-grep.pl -r 'id: FORMULA' $< > $@

chebi_isa_formula.obo: chebi_with_formula.obo
	obo-filter-tags.pl -t namespace -i id: $< | grep -v 'is_a: CHEBI' | obo-grep.pl -r 'id: CHEBI' - | obo-grep.pl -r 'is_a: FORMULA' - > $@

# inverse entailment
chebi_inv-ontol_db.pro:
#	blip-findall -r goxp/biological_process_xp_chebi -r go -u query_chebi "inverse_entail(A,B)" -select "subclass(A,B)" -write_prolog > $@
	blip-findall -table_pred user:inverse_entail/2 -r goxp/biological_process_xp_chebi -r implied/go -u query_chebi "inverse_entail_nr(A,B)" -select "subclass(A,B)" -write_prolog > $@
.PRECIOUS: chebi_inv-ontol_db.pro

# chego is now goche
#chego.obo: chebi_inv-ontol_db.pro
#	blip -i chebi_ultralite.obo -i $< ontol-query -query "subclass(ID,_);subclass(_,ID)" -to obo > $@
#.PRECIOUS: chego.obo

# no MF for now...
go_xp_goche-newlinks.txt: goche.obo
	blip-reasoner -explain genus_differentia -explain genus -r go -i goche.obo -i ../xps/biological_process_xp_chebi.obo > $@.tmp

go_xp_goche-implied.obo: goche.obo
	obo2obo -o -saveimpliedlinks $@ ../../ontology/editors/gene_ontology_write.obo goche.obo ../xps/biological_process_xp_chebi.obo

go_xp_goche-merged.obo: goche.obo
	obo2obo -o $@ ../../ontology/editors/gene_ontology_write.obo goche.obo ../xps/biological_process_xp_chebi.obo 

go_xp_goche-mireot.obo: go_xp_goche-merged.obo
	blip -i $< -table_pred "ontol_db:bf_parentRT/2" ontol-query -query "differentium(X,_,Y),(bf_parentRT(X,ID);bf_parentRT(Y,ID))" -to obo > $@

%-abduced.txt: %.obo
	blip-findall -r implied/$<  -u query_chebi "inverse_entail_nr(A,B),\+subclass(A,B)" -select "subclass(A,B)" -label > $@

#chebi_inv-nr.obo: chebi_inv.obo
#	obo2obo -o -saveimpliedlinks -filterredundantlinks $@ $<

cvs_update:
	cvs update ; cd ../../ontology/ && cvs update ; echo updated
goche_all: goche-uniq-subclass.txt goche-uniq-subclass-filtered.txt goche-uniq-subclass-summary.txt goche-diff-labels.txt
goche_all_cycle: cvs_update goche_all

goche-diff.txt: goche.obo chebi_lite.obo
	blip-findall -i $< -i chebi_lite.obo -table_pred src_subclassT/3 -u ontol_diff "diff_subclass/8" > $@.tmp && sort -u $@.tmp > $@

goche-uniq-subclass.txt: goche.obo $(GO) chebi_lite.obo
	blip-findall -u ontol_diff -r implied/goche.obo -r implied/chebi_lite.obo  uniq_subclass/3 -label | sort -u > $@
goche-uniq-subclass2.txt: goche.obo $(GO) chebi_lite.obo
	blip-findall -u ontol_diff -r implied/goche.obo -r implied/chebi_lite.obo  uniq_subclass/5 -label | sort -u > $@

goche-uniq-subclass-filtered.txt: goche-uniq-subclass.txt
	blip-findall -ff tbl^goche-uniq-subclass.txt -ff tbl^goche-chebi-mixed-rel.txt "uniq_subclass(A,B,S),\+r(A,_,B)" -select "uniq_subclass(A,B,S)" > goche-uniq-subclass-filtered.txt

goche-uniq-subclass-partitioned.txt: goche.obo $(GO)
	blip-findall -i goche_chooser.pro -u ontol_diff -r implied/goche.obo -r implied/chebi_lite.obo "uniq_subclass(X,Y,S),goche_set(Y,C)" -select "uniq_subclass(C,X,Y,S)"  -label | sort -u > $@

goche-uniq-subclass-with-defs.txt: goche.obo $(GO)
	blip-findall -u ontol_diff -r implied/goche.obo -r implied/chebi_lite.obo  uniq_subclass_with_defs/5 -label | sort -u > $@

goche-uniq-subclass-summary.txt: goche-uniq-subclass.txt
	cut -f4,5 $< | count-occ.pl > $@

#?
goche-uniq-subclass-defs.txt: goche.obo $(GO)
	blip-findall -u ontol_diff -i goche.obo -i chebi_lite.obo  "uniq_subclass(A,B,S),(X=A;X=B),G=def(X,D),fact_clausesource(G,S)" -select "def(X,D,S)"  -label | sort -u > $@

goche-diff-labels.txt: goche.obo $(GO)
	blip-findall -u ontol_diff -i goche.obo -i chebi_lite.obo  diff_label/5 > $@

goche-switched-labels.txt: goche.obo $(GO)
	blip-findall -u ontol_diff -i goche.obo -i chebi_lite.obo  "fact_clausesource(entity_label(X1,N1),S1),fact_clausesource(entity_label(X1,N2),S2),N1\=N2,fact_clausesource(entity_label(X2,N2),S1),X1\=X2,fact_clausesource(entity_label(X2,N1),S2)" -select "switch(X1,N1,X2,N2,S1,S2)" > $@

goche-diff-defs.txt: goche.obo $(GO)
	blip-findall -u ontol_diff -i goche.obo -i chebi_lite.obo  "diff_def(X,D1,D2,S1,S2),D1\='x.',D2\='x.'" -select "diff_def(X,D1,D2,S1,S2)" -label > $@

goche-orphan.txt: goche.obo
	blip-findall -i $< "class(X),\+subclass(X,_)" -select X -label > $@

goche-partition.txt: goche.obo
	blip-findall -i $< "class(X),subclassT(X,Y),\+subclass(Y,_)" -select "partition(X,Y)" -label > $@

goche-cycles.txt: goche.obo
	blip-findall -i $< "subclass_cycle(X,Y)" -select X-Y -label > $@

goche-chebi-cycles.txt: goche.obo
	blip-findall -i chebi_lite.obo -i $< "subclass_cycle(X,Y)" -select X-Y -label > $@

CHREL= -rel has_functional_parent -rel is_conjugate_base_of -rel is_conjugate_acid_of -rel subclass
goche-n-%.png:
	blip ontol-subset  $(CHREL) -i goche.obo -r chebi -u ontol_config_goche -n "$*" -to png > "$@"

goche-id-%.png:
	blip ontol-subset -i goche.obo -r chebi -u ontol_config_goche -id "CHEBI:$*" -to png > $@

go-abduce-propagation-rel-from-chebi.txt:
	blip-findall -r go -r chebi -r goxp/biological_process_xp_chebi -u query_chebi "abduce_inference_over/6" -label -use_tabs > $@

go-abduce-non-propagation-rel-from-chebi.txt:
	blip-findall -r go -r chebi -r goxp/biological_process_xp_chebi -u query_chebi "neg_abduce_inference_over/6" -label -use_tabs > $@

goche-chebi-mixed-rel.txt: goche.obo
	blip-findall -u ontol_diff -i goche.obo -r chebi "src_subclass(goche,A,B),restriction(A,R,Z),src_subclassRT(chebi,Z,B),\+src_subclassT(chebi,A,B)" -select "r(A,R,B)" -label  > $@.tmp && sort -u $@.tmp > $@

goche-chebi-mixed-neg-rel.txt: goche.obo
	blip-findall -u ontol_diff -i goche.obo -r chebi "class_in(A,goche),restriction(A,R,Z),src_subclassRT(chebi,Z,B),class_in(B,goche),\+src_subclass(goche,A,B),\+src_subclassT(chebi,A,B)" -select "r(A,R,B)" -label  > $@.tmp && sort -u $@.tmp > $@

goche-defs.txt: goche.obo
	blip-findall -i goche.obo -r go -u query_chebi chebi_def_from_go/0 > $@

go_goche_split.tbl: goche.obo
	blip-findall -i $< -i go_chebi_genus_label.pro -u metadata_nlp -r go "genus_label(Tail),term_ends_with(E,S,SN,Tail,S1,S2),\+id_idspace(S,'GO')" -select "term_ends_with(E,S,SN,Tail,S1,S2)" -label > $@
.PRECIOUS: go_goche_split.tbl

go_goche_split-summarize.txt: go_goche_split.tbl
	blip-findall -i $< "setof(E-F,term_ends_with(A,B,C,D,E,F),EFs)" -select "term_ends_with(A,B,C,D,EFs)" > $@

go_goche_split-non-exact.txt: go_goche_split.tbl
	blip-findall -u query_chebi -i $< "setof(E-F,term_ends_with_inexact(A,B,C,D,E,F),EFs)" -select "term_ends_with(A,B,C,D,EFs)" > $@

go_oxoacid_ref.txt:
	blip-findall -i go_chebi_genus_label.pro -table_pred ontol_db:subclassT/2 -u query_chebi -r go -r chebi "go_oxoacid_ref/4" -label > $@.tmp && sort -u $@.tmp > $@

goche_labels.obo: goche.obo
	grep -v ^is_a: $< > $@

go_justify_by_chebi.txt: goche_labels.obo
	blip-findall -i go_chebi_genus_label.pro -r implied/biological_process_xp_chebi-minimir.obo -i $< -r go -u query_chebi  justify_subclass/5 -label > $@.tmp && sort -u $@.tmp > $@

go_no_justify_by_chebi.txt: goche_labels.obo
	blip-findall -i go_chebi_genus_label.pro -r implied/biological_process_xp_chebi-minimir.obo -i $< -r go -u query_chebi  no_justify_subclass/4 -label > $@.tmp && sort -u $@.tmp > $@
.PRECIOUS: go_no_justify_by_chebi.txt

go_no_justify_by_chebi-filtered.txt: go_no_justify_by_chebi.txt
	 grep -v GOCHE $< | cut -f4,5 | grep CHEBI | sort -u > $@


go_new_by_chebi.txt: goche_labels.obo
	blip-findall -i go_chebi_genus_label.pro -r implied/biological_process_xp_chebi-minimir.obo -i $< -r go -u query_chebi  new_subclass/5 -label > $@.tmp && sort -u $@.tmp > $@

not_in_xp.txt: 
	blip-findall  -i go_chebi_genus_label.pro -r implied/biological_process_xp_chebi-minimir.obo -r go -u query_chebi "genus_label(GN),atom_concat(' ',GN,Z),class(X,XN),atom_concat(CN,Z,XN),\+quick_def(X,_,_),\+genus(X,_)" -select "noxp(GN,CN,X,XN)" 

# ===================
# plain links
# ===================
cellular_component_links_to_molecular_function.obo: cellular_component_xp_go-obol.obo
	obo-xp-to-link.pl --idspace GO -a -r has_function $< > $@.tmp && mv $@.tmp $@

# ===================
# generic
# ===================

.PRECIOUS: %-obol.obo

%-implied.obo: %-imports.obo %-obol.obo
	obo2obo -o -allowdangling -saveimpliedlinks $@ $<

%-newlinks.txt: %.obo
	blip-reasoner -i $< -import_all -explain genus_differentia -explain genus > $@.tmp && filterreasoner.pl $@.tmp > $@
#%-newlinks.txt: %-imports.obo %-obol.obo
#	blip-reasoner -i $< -import_all -explain genus_differentia -explain genus > $@.tmp && mv $@.tmp $@

%-slurp.obo: %-imports.obo %-obol.obo
	obo2obo -o $@ $<

%-stats: %
	grep -c ^id: $<

%-non-go: %
	obo-grep.pl --neg -r 'id: GO' $<

bp-chebi-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r biological_process -r xchebi biological_process chebi_ontology > $@.tmp && mv $@.tmp $@
.PRECIOUS: bp-chebi-aln.tbl

cc-chebi-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r obo/cellular_component -r xchebi cellular_component chebi_ontology > $@.tmp && mv $@.tmp $@
.PRECIOUS: bp-chebi-aln.tbl

bp-protein-aln.tbl: $(GO)
	obol -debug blip -debug ontol ontol-align -stem -shownames -r go -r protein biological_process pro > $@.tmp && mv $@.tmp $@
.PRECIOUS: bp-protein-aln.tbl

bp-cell-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r go -r cell biological_process cell > $@.tmp && mv $@.tmp $@
.PRECIOUS: bp-cell-aln.tbl

bp-sequence-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r go -r sequence biological_process sequence > $@.tmp && mv $@.tmp $@
.PRECIOUS: bp-sequence-aln.tbl

bp-cc-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r go biological_process cellular_component > $@.tmp && mv $@.tmp $@
.PRECIOUS: bp-cc-aln.tbl

mf-cc-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r go molecular_function cellular_component > $@.tmp && mv $@.tmp $@
.PRECIOUS: mf-cc-aln.tbl

mf-bp-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r go molecular_function biological_process > $@.tmp && mv $@.tmp $@
.PRECIOUS: mf-bp-aln.tbl

bp-anat-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r go $(ANAT) > $@.tmp && mv $@.tmp $@
.PRECIOUS: bp-anat-aln.tbl

bp-fly_anat-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r obo/biological_process -r fly_anatomy biological_process fly_anatomy.ontology > $@.tmp && mv $@.tmp $@

bp-uberon-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r obo/biological_process -r uberon biological_process uberon > $@.tmp && mv $@.tmp $@

bp-mouse_anat-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r obo/biological_process -r mouse_anatomy biological_process adult_mouse_anatomy.gxd > $@.tmp && mv $@.tmp $@

bp-plant_anat-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r obo/biological_process -r plant_anatomy biological_process plant_structure > $@.tmp && mv $@.tmp $@

bp-fma_anat-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r obo/biological_process -r fma biological_process fma > $@.tmp && mv $@.tmp $@

bp-dicty_anat-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r obo/biological_process -r dicty_anatomy biological_process Dictyostelium_discoideum_anatomy > $@.tmp && mv $@.tmp $@

bp-%_anat-aln.tbl: $(GO)
	obol ontol-align -stem -shownames -r obo/biological_process -r $*_anatomy biological_process $*_anatomy > $@.tmp && mv $@.tmp $@
.PRECIOUS: bp-%_anat-aln.tbl

# ===================
# reporting
# ===================

list-defined-ids:
	obo-grep.pl -r intersection_of: *.obo | grep ^id: | perl -ne 'print "$$1\n" if /id: (GO:\d+)/' | sort -u

df-undefined.txt:
	blip-findall -debug foo -r go -r goxp/biological_process_xp_uber_anatomy -r goxp/biological_process_xp_cell "parentRT(ID,'GO:0032502'),debug(foo,'check: ~w',[ID]),\+genus(ID,_)" -select ID -label > $@
