% this file is to use with the t.pl that has constraint about consistent GE. 

claLength(startNode,ClaEICoverage,0):-!.
claLength(endNode-EITIs,ClaEICoverage,0):-!.
claLength(ClaID,ClaEICoverage,ClaDescriptionLength):-
	ClaID=[rs-{ReactionID,LimitingType,HypothesizedState,Time}],
	scoreByGEData({ReactionID,LimitingType,HypothesizedState,Time},ClaEICoverage,Score_GE),
	ClaDescriptionLength=Score_GE.


% no degree, but simply agree or not
% agree -- no description score%enzymlimiting,cataIncreased,
% not agree -- 2
% not annotated or not measured -- 1

% when enzyme limiting, it is the same as before
scoreByGEData({ReactionID,enzymeLimiting,HypothesizedState,MutantTypeTime},ClaEICoverage,Score):-
	catalyzed_by_ECclass(ReactionID,EC_Number),
	timeEquivenlent(Time,DoseTime), %cancer also need to take care of the following DoseTime.
	concentration_e(EC_Number,GEChange,Degree,DoseTime),!, 	
	compute_MetsGE_consistentDegree_AlongTime(ClaEICoverage,EC_Number,Scale_byTime),	% no need of MutantType			
	Score=10*Scale_byTime.

	

compute_MetsGE_consistentDegree_AlongTime(EIs,EC_Number,Scale):-
	maplist(compute_eachMetGE_consistentDegree_AlongTime(EC_Number,EIs,ConsistentDegrees),
	multiplyList(ConsistentDegrees,Scale).	

compute_eachMetGE_consistentDegree_AlongTime(EC_Number,EI,ConsistentDegree):-
	getMETChangesAlongTime(EI,Met_ChangesAlongTime),
	getGEChangesAlongTime(MutantType,EC_Number,GE_ChangesAlongTime),
	(consistentMetGE(Met_ChangesAlongTime,GE_ChangesAlongTime)->
		ConsistentDegree is 1;
		((\+sameList(Met_ChangesAlongTime),partialConsistentMetGE(Met_ChangesAlongTime,GE_ChangesAlongTime))->
			ConsistentDegree is 2;
			ConsistentDegree=3
		)
	).
%change at the same point -- when it is no Change it is difficult to ask no change as well, but the one *change* (rather simply not the same)

getMETChangesAlongTime(EI,Met_Changes):-
	getAllTimePoints(AllTimePoints),
	ex0(EI,concentration(MetName,Change,Degree,EI_MutantTypeTime),1),
	maplist(getMetChange_fromEI(MetName),AllTimePoints,Met_ChangesAcrossMutants).

getMetChange_fromEI(MetName,Day,Met_Change):-
	ex0(EI,concentration(MetName,Met_Change,Degree,Day),1).

getGEChangesAlongTime(EC_Number,GE_Changes):-
	getAllTimePoints(AllTimePoints),maplist(timeEquivenlent,AllTimePoints,DoseTimes),
	maplist(getGEChange(EC_Number),DoseTimes,GE_Changes).

getGEChange(EC_Number,MutantTypeTime,Change):-
	concentration_e(EC_Number,Change,Degree,MutantTypeTime).
	


partialConsistentMetGE([no_change,no_change,MetChange_Late],[GE_Early,GE_Mid,GE_Late]):-!. %,GE_Early\==GE_Late,GE_Mid\==GE_Late.
partialConsistentMetGE([no_change,MetChange_Mid,MetChange_Late],[GE_Early,GE_Mid,GE_Late]):-
		GE_Early\==GE_Mid,
		consistentMetGE([MetChange_Mid,MetChange_Late],[GE_Mid,GE_Late]).
partialConsistentMetGE([MetChange_Early,no_change,MetChange_Late],[GE_Early,GE_Mid,GE_Late]):-
		GE_Early\==GE_Mid, %GE_Mid\==GE_Late, -- no need, since you require them to be the same
		consistentMetGE([MetChange_Early,MetChange_Late],[GE_Early,GE_Late]).

consistentMetGE(MetChangesAlongTime,GEChangesAlongTime):-
	maplist(propotionalChange,MetChangesAlongTime,GEChangesAlongTime).
consistentMetGE(MetChangesAlongTime,GEChangesAlongTime):-
	maplist(oppositeChange,MetChangesAlongTime,GEChangesAlongTime).

% similar to the propotionalBelow, but that is the reaciton state with GE.
propotionalChange(up,up).
propotionalChange(no_change,noChange).
propotionalChange(down,down).

oppostieChange(up,down).
oppostieChange(no_change,noChange).
oppostieChange(down,up).


getAllTimePoints(['Day1','Day3','Day7','Day14']).



scoreByGEData({ReactionID,substrateLimiting,HypothesizedState,Time},ClaEICoverage,Score):-
	catalyzed_by_ECclass(ReactionID,EC_Number),
	timeEquivenlent(Time,DoseTime),
	concentration_e(EC_Number,GEChange,Degree,DoseTime),!,
	
getMutantType(Time,MutantType),
compute_nonRegulatorDegree_AlongTime(MutantType,EC_Number,HypothesizedState,NonRegulatorDegree),		
	Score=1*NonRegulatorDegree.



compute_nonRegulatorDegree_AlongTime(MutantType,EC_Number,HypothesizedState,NonRegulatorDegree):-
	getAllTimePoints(MutantType,[Early,Mid,Late]),
	concentration_e(EC_Number,Change_E,Degree1,Early),
	concentration_e(EC_Number,Change_M,Degree2,Mid),
	concentration_e(EC_Number,Change_L,Degree3,Late),
	maplist(compute_nonRegulatorDegree(HypothesizedState),[Change_E,Change_M,Change_L],[DE,DM,DL]),
	NonRegulatorDegree is DE*DE*DM*DL. % early stage has higer effect


compute_nonRegulatorDegree(HypothesizedState,noChange,0.5):-!.
compute_nonRegulatorDegree(HypothesizedState,GEChange,1):-
	nonContradict(HypothesizedState,GEChange),!.
compute_nonRegulatorDegree(HypothesizedState,GEChange,2):-!.




scoreByGEData({ReactionID,enzymeLimiting,HypothesizedState,Time},EIs,Score):- 
	length(EIs,NumEI), Score is 20/NumEI.
scoreByGEData({ReactionID,substrateLimiting,HypothesizedState,Time},EIs,Score):-
	length(EIs,NumEI), Score is 2/NumEI.


proportional(cataIncreased,up).
proportional(cataDecreased,down).
proportional(cataNoChange,noChange).


nonContradict(sUp,GEChange):- GEChange\==down.
nonContradict(sDown,GEChange):- GEChange\==up.
nonContradict(sNoChange,GEChange):- GEChange=='noChange'.



/*
1. Enzyme Limiting is more expensive then substrate limiting. 
For two alternatives, when their enzyme limiting reactions has the same score, and their substrate limiting reaction differentiate them out. 

substrate limiting still counts for DL -- make the shorter explanation/closer effect more preferable


2. The unknown is double, this gives it a chance to beat those known. 

3. the Shared number is taken into account, this is reasonable from information theory point -- more frequent one are given shorter DL.

4. noContradict constrain for substrateLimiting, is this reasonable? --- that's more appropriate for early stage, So can you refer to the early stage to differentiate them

*/