% difference with that in t_SID_0 is that, s(DepthLimit) in those direct effect rules
% no_change is still recorded, but not translated.
% Reasons for recording no_change: avoid hypothesize to two states. 
% the sign is given 100 for those agree, since it is not compression, but counting the number


:- dynamic enzyme_state/2.

:-['topCla'].
%:-['counting.pl'].
%:-['counting_includeLength'].
:-['b_noCycle'].

%set(depthLimit,s(s(s(s(s(s(s(s(0))))))))). %PianoNumDepthLimit):- numMap(6,PianoNumDepthLimit).
set(depthLimit,s(0)).
%set(depthLimit,s(s(0))).
%set(depthLimit,s(s(s(s(0))))).
%set(depthLimit,s(s(s(s(s(0)))))).
%set(depthLimit,s(s(s(s(s(s(s(s(s(s(0))))))))))).
set(etoB,yes).




peanoMinus1(s(X),X).

peanoGth(X,X).
peanoGth(s(Y),X):-peanoGth(Y,X).


/*********** using other examples ***********/
concentration(DepthLimit,Tr,Tr,VisitedNodes,X,Change):-	
	set(depthLimit,DepthLimit0),
	DepthLimit \== DepthLimit0,
	ex(EI,concentration(X,Change),1),!,
	write('***using this example***'), write(X),nl.


concentration(DepthLimit,Tr,Tr,VisitedNodes,X,Change):- 
	neg(Change,OppositeChange),
	ex(EI,concentration(X,OppositeChange),1),%write('!!! you are looking for sth inconsistent'),
	!,
	fail.

neg(up,down).
neg(down,up).



/***************************** Regulation Rules ******************************/
/****************** Direct Effect ******************/

/*---------- Blocked/Slower down ----------*/
% 12,15,18 enzyme inhibited
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,down):-
	%write('go down this way'),
	produced_by(PID,ReactionID,'IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	enzyme_state(TrSoFar,Tr,EC_Number,inhibited),write(ReactionID),portray_clause(VisitedNodes).

%/*
% enzyme inhibited
concentration(s(CurrentDepth),TrSoFar,Tr,VisitedNodes,SID,up):-				%concept_name(S,SID,true),
	%write('go down this way'),	
	set(depthLimit,DepthLimit),peanoMinus1(DepthLimit,DepthLimit1),peanoGth(CurrentDepth,DepthLimit1),write('succeed'),
	consumed_by(SID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	enzyme_state(TrSoFar,Tr,EC_Number,inhibited),write(ReactionID),portray_clause(VisitedNodes).
%*/

/*---------- Speeded Up ----------*/
% 10,13,16 enzyme activated
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,up):-
	write('go down this way'),
	produced_by(PID,ReactionID,'IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	enzyme_state(TrSoFar,Tr,EC_Number,activated),write(ReactionID),portray_clause(VisitedNodes).

%/*
% enzyme activated
concentration(s(CurrentDepth),TrSoFar,Tr,VisitedNodes,SID,down):-				%concept_name(S,SID,true),	
	%write('go down this way'),	
	set(depthLimit,DepthLimit),peanoMinus1(DepthLimit,DepthLimit1),peanoGth(CurrentDepth,DepthLimit1),write('succeed'),
	consumed_by(SID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	enzyme_state(TrSoFar,Tr,EC_Number,activated),write(ReactionID),portray_clause(VisitedNodes).
%*/


/*---------- No change ----------*/
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,no_change):-
	write('go down this way'),
	produced_by(PID,ReactionID,'IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	enzyme_state(TrSoFar,Tr,EC_Number,no_change),write(ReactionID),portray_clause(VisitedNodes).


concentration(s(CurrentDepth),TrSoFar,Tr,VisitedNodes,SID,no_change):-				%concept_name(S,SID,true),	
	%write('go down this way'),	
	set(depthLimit,DepthLimit),peanoMinus1(DepthLimit,DepthLimit1),peanoGth(CurrentDepth,DepthLimit1),write('succeed'),
	consumed_by(SID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	enzyme_state(TrSoFar,Tr,EC_Number,no_change),write(ReactionID),portray_clause(VisitedNodes).


concentration(s(CurrentDepth),TrSoFar,Tr,VisitedNodes,SID,no_change):-				%concept_name(S,SID,true),	
	%write('go down this way'),	
	set(depthLimit,DepthLimit),peanoMinus1(DepthLimit,DepthLimit1),peanoGth(CurrentDepth,DepthLimit1),write('succeed'),
	consumed_by(SID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	\+produced_by(SID,ReactionID_Any,'IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	enzyme_state(TrSoFar,Tr,EC_Number,inhibited),write(ReactionID),portray_clause(VisitedNodes).





/****************** Indirect Effect ******************/
/*------------------------- enzyme No_Change ------------------------*/
% S up
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,up):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number), 
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,no_change),
	consumed_by(SID,ReactionID,'IMPD'), \+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,up).

% S down
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,down):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number), 
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,no_change),
	consumed_by(SID,ReactionID,'IMPD'), \+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,down).



% S no change
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,no_change):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,no_change),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,no_change).


/*------------------------- enzyme UP ------------------------*/
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,up):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number), 
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,activated),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,no_change).

concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,up):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,activated),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,down).

concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,no_change):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,activated),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,down).


/*------------------------- enzyme DOWN ------------------------*/
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,no_change):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number), 
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,inhibited),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,up).

concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,down):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,inhibited),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,up).

concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,down):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,inhibited),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,no_change).






concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,up):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,activated),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,up).


concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,no_change):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,no_change),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,up).


concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,no_change):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,no_change),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,down).



% enzyme limiting -- last line
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,down):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	%\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,inhibited),
	consumed_by(SID,ReactionID,'IMPD'),\+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,down).









/*************** Abduce enzyme_state	****************/
/* Simple version -- no incorporation of integrity constraint
enzyme_state(TrSoFar,Tr,EC_Number,State):-
	Tr=[[enzyme_state-{EC_Number,State}]|TrSoFar].
*/


% incorporate integrity constraint
% 1. one enzyme not to be hypothesized to several states: (1) the existing B; (2) the same 
% 2. the gene expression data. 

propotional(activated,up).
propotional(inhibited,down).
propotional(no_change,noChange).

% This is not suitable when there are alternative similar rules 



enzyme_state(TrSoFar,Tr,EC_Number,State):-
	(appeared(EC_Number,TrSoFar,State2)->
		State2==State, %write('not contradiction'),nl,
		Tr=TrSoFar;
		(concentration_enzClass(EC_Number,Change)->
			nl,write('----- genomic data available'), write({EC_Number,State,Change}),
			(propotional(State,Change)->
				Sign=100,write('Yes, agreed'),nl;  % whatever up/down/no_change, as long as they agree with each other
				Sign=1,write('No, disagreed'),nl
			);
			Sign=10
		),
		Tr=[[enzyme_state-{Sign,EC_Number,State}]|TrSoFar]
	).




appeared(EC_Number,TrSoFar,State):-
	member([enzyme_state-{Sign,EC_Number,State}],TrSoFar),nl,write('--Aha, appear before!--'),nl.
% appeared(EC_Number,TrSoFar,State):-	% won't happen since no inhibited is known, nor added to the background knowledge
%	enzyme_state(EC_Number,State).



