


maxScore(EI,EI-TIsMaxScore):-
	findall(Score,candidateH_score(EI,TI,EIList1,Score),Scores), % gather a list first, TI-Score
	max(Scores,-1000000,MaxScore), 
	findall(TI2,candidateH_score(EI,TI2,EIList2,MaxScore),TIsMaxScore),
	write(EI), write(' maximum TI are '), write(TIsMaxScore),nl.


		
	



trainANDcount(PosEIs,FinalTI):-
	tell('tomato_candidateH_constrainIncorporated1.pl'),	%tell('grammar_candidateH.pl'),
	genAll(PosEIs,AllTs,TotalNum),%genAll_EIsigned(PosEIs,AllTs,TotalNum),
	write('%Total is '), write(TotalNum),nl,
	%print_list(AllTs),nl,
	told,
	consult('tomato_candidateH_constrainIncorporated1.pl'),	%consult('grammar_candidateH.pl'),
	

	rb_new(Tree0),
	insertTheories(PosEIs,Tree0,Tree), % insertTheories(AllTs,Tree0,Tree) *** be careful about the function you are using
	rb_visit(Tree,Clas), 
	
	write('/******************Cla Counted********************/'),
	print_list(Clas),nl,nl,nl,
	selectlist(claLengthCheck, Clas, NonOneClas0),
	print_list(NonOneClas0),nl,
	maplist(extractCla,NonOneClas0,NonOneClas),
	FinalTI=NonOneClas.


extractCla(NonOneClas-EIList,NonOneClas).

claLengthCheck([rs-{RID,enzymeLimiting,Change,Time}]-EIList):-
	length(EIList,K),K>1.



/*consult('tomatoCountedResult.pl'),
maplist(maxScore,PosEIs,EIsTIsMaxScore),
write('*** Here is the input for cross product'), write(EIsTIsMaxScore), % nothing was left because of the constraint, 
crossProductlyCombine_and_check(EIsTIsMaxScore,[[]],FinalTIs), write(FinalTIs),
print_list(FinalTIs), told,
*/

/*
	%tell('tomato_countingResult.txt'),rb_visit(Tree,Ts), print_list(Ts),nl,	
	%maplist(scoreByCountingSharedExNum,Ts,ScoredHs0), % score Hs
	%sort(ScoredHs0,ScoredHs),
	%nl,write('--------New Round--------'),nl,
	%print_list(ScoredHs),
	%checklist(claDetail,ScoredHs),
	tell('tomatoDAG.pl'),
	forAll(candidateH0(EI_any,TI_any,T),assertOneHypothesizedTheory(T,Tree,start,EI_any-TI_any)),
	listing,
	told.
*/




scoreByCountingSharedExNum(RS-ExList,ExNum-RS-ExList):-
	length(ExList,ExNum).


%1-100-([reaction_state-{100,'5426','1.5.3.11',activated,'RIN_L'}]-[365])

% agreed with GE? which pathway.
claDetail(ScoredCla):-
	ScoredCla=Coverage-[rs-{ReactionID,LimitingType,HypothesizedState,Time}]-CoveredEIs,
	
	write(Coverage-[rs-{ReactionID,LimitingType,HypothesizedState,Time}]),
	checkItsGEData(ReactionID,HypothesizedState,Time,Score_GE), write(Score_GE),
	
	((member_of(ReactionID,PathwayID,'IMPD'),concept_name(PathwayID,PathwayName,true))->
 			write('(Pathway '), write(PathwayName), write(') ');
			write(' No pathway annotated')
	),
	nl.
		

/*
	writeReaction(ReactionID),
	maplist(compountNameMaping,CoveredEIs,CoveredEINames),
	nl, write('Covered Metabolites '),writeq(CoveredEINames),
	nl,nl.
*/

checkItsGEData(ReactionID,HypothesizedState,Time,Score_GE):-
	catalyzed_by_ECclass(ReactionID,EC_Number),!,	
	write(EC_Number),
	(concentration_enzClass(EC_Number,GEChange,Time)->
		(propotional(HypothesizedState,GEChange)->
				Score_GE=agreed;%write('Yes, agreed'),nl;  % whatever up/down/no_change, as long as they agree with each other
				Score_GE=disagreed %,write('No, disagreed'),nl
			);
		Score_GE=notMeasured
		).


checkItsGEData(ReactionID,HypothesizedState,Time,notAnnotated).



/*
claDetail(ScoredCla):-
	nl,write(ScoredCla),
	ScoredCla=Coverage-Score-([reaction_state-{Sign,ReactionID,EC,Change,Time}]-CoveredEIs),
	
	writeReaction(ReactionID),
	maplist(compountNameMaping,CoveredEIs,CoveredEINames),
	nl, write('Covered Metabolites '),writeq(CoveredEINames),
	nl,nl.
*/

compountNameMaping(EI,Name-Change):-
	ex0(EI,concentration(Name,Change,Time),1).

	%sort(Ts,Ts1),print_list(Ts1),nl, % this is just to check the result

/*
	sort(PosEIs,PosEIs1), % the reason to sort it is to facilitate the subtract later

	%FinalTI=Ts.
	chooseH(PosEIs1,Ts,[],FinalTI),
	write('check here'),write(FinalTI),
	testPoint(1),
	print_list(FinalTI),nl.
*/

	
testPoint(1).

chooseH([],_,FinalH,FinalH):- !.
chooseH(UncoverExs,Hs,HSofar,FinalH):-
	maplist(scoreCountedT,Hs,ScoredHs0), % score Hs
	sort(ScoredHs0,ScoredHs),
	nl,write('--------New Round--------'),nl,
	print_list(ScoredHs),

	getLast(ScoredHs,RestHs,Coverage-Score-(Cla-ExList)), % check when you record
	
	portray_clause(Coverage-Score-(Cla-ExList)),nl,
	
	ExList=CoveredExs0, %extractCoveredEx(ExList,CoveredExs0), 

	sort(CoveredExs0,CoveredExs), %ordered setsubstract
	ord_subtract(UncoverExs,CoveredExs,NewUncoverExs),

	testPoint(1),write(NewUncoverExs),nl,
	
	updataHcovergae(CoveredExs0,RestHs,NewRestHs), % remove covered
	
	chooseH(NewUncoverExs,NewRestHs,[Cla|HSofar],FinalH).	



getLast([LastEle],[],LastEle).
getLast([Coverage-Score-T|List],[T|Rest],LastEle):-
	getLast(List,Rest,LastEle).

extractCoveredEx(ExList,CoveredExs):-
	maplist(extractEI,ExList,CoveredExs).

extractEI({EI,Length},EI).


%updataHcovergae(CoveredExs0,RestHs,NewRestHs):- 
%	maplist(updateEIList(CoveredExs0),RestHs,NewRestHs).


updataHcovergae(CoveredExs,[],[]).
updataHcovergae(CoveredExs,[Cla-LengthedEI|Hs],NewHs):-
	updataHcovergae(CoveredExs,Hs,HsSoFar),
	updateEIList(CoveredExs,Cla-LengthedEI,Cla-NewLengthedEI),
	(NewLengthedEI==[] ->
		NewHs=HsSoFar;
		NewHs=[Cla-NewLengthedEI|HsSoFar]
	).
	


updateEIList(CoveredExs,[enzyme_state-{Sign,EC_Number,Change}]-LengthedEI,[enzyme_state-{Sign,EC_Number,Change}]-NewLengthedEI):-
	removeCoveredEx(CoveredExs,LengthedEI,[],NewLengthedEI).

/*
removeCoveredEx(CoveredExs,[],Final,Final).

removeCoveredEx(CoveredExs,[{EI,ExNum}|Rest],Sofar,Final):-
	member(EI,CoveredExs), !,  
	removeCoveredEx(CoveredExs,Rest,Sofar,Final).

removeCoveredEx(CoveredExs,[{EI,ExNum}|Rest],Sofar,Final):-
	removeCoveredEx(CoveredExs,Rest,[{EI,ExNum}|Sofar],Final).	

% I can't remember why this one is retained
removeCoveredEx([],_,Final,Final):- !.
removeCoveredEx(CoveredExs,[],Final,Final):- !.
removeCoveredEx(CoveredExs,[{EI,ExNum}|Rest],Sofar,Final):-
	append(Pre,[EI|Post],CoveredExs), !, %member(EI,CoveredExs), 
	removeCoveredEx(Post,Rest,Sofar,Final).
removeCoveredEx(CoveredExs,[{EI,ExNum}|Rest],Sofar,Final):-
	removeCoveredEx(CoveredExs,Rest,[{EI,ExNum}|Sofar],Final).
*/

removeCoveredEx(CoveredExs,[],Final,Final).

removeCoveredEx(CoveredExs,[EI|Rest],Sofar,Final):-
	member(EI,CoveredExs), !,  
	removeCoveredEx(CoveredExs,Rest,Sofar,Final).

removeCoveredEx(CoveredExs,[EI|Rest],Sofar,Final):-
	removeCoveredEx(CoveredExs,Rest,[EI|Sofar],Final).	


