:- dynamic enzyme_state/2.
:-['topCla'].
:-['b'].

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





/*********** 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 ******************/
% 12,15,18 enzyme inhibited
concentration(DepthLimit,TrSoFar,Tr,VisitedNodes,PID,down):-
	produced_by(PID,ReactionID,'IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	enzyme_state(TrSoFar,Tr,EC_Number,inhibited).


/*----------Change in Substrates ----------*/
% Substrates are consumed more or less with the change of reaction speed 

% enzyme inhibited
concentration(DepthLimit,TrSoFar,Tr,VisitedNodes,SID,up):-				%concept_name(S,SID,true),	
	consumed_by(SID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	enzyme_state(TrSoFar,Tr,EC_Number,inhibited).


/****************** 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), 
	enzyme_state(TrSoFar,Tr1,EC_Number,no_change),
	consumed_by(SID,ReactionID,'IMPD'), \+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,up).

% enzyme no_change -- 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), 
	enzyme_state(TrSoFar,Tr1,EC_Number,no_change),
	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. 

enzyme_state(TrSoFar,Tr,EC_Number,State):-
	(appeared(EC_Number,TrSoFar,State2)->
		State2==State,
		Tr=TrSoFar;
		Tr=[[enzyme_state-{EC_Number,State}]|TrSoFar]
	).


appeared(EC_Number,TrSoFar,State):-
	member([enzyme_state-{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).




