% record the length of each



lazyAllTsTranslation(AllTs,NewAllClas):- maplist(lazyTsTranslation,AllTs,NewAllClas).

lazyTsTranslation(EI-Ts,EI-Clas):- maplist(listHead,Ts,Clas).
					%Clas0),remove_duplicates(Clas0,Clas),
					%length(Clas0,K0),length(Clas,K),
					%nl,write('Comparing redundancy within each example'),write({K0,K}).

%lazyTranslation(Ts,PrunedTs):- maplist(listHead,Ts,PrunedTs).

listHead([Head|List],Length-Head):- length([Head|List],Length). % [Head]? Head -- depend later


counting([],Tree,Tree).
counting([EI-OneEITRecord|All_T_Records],TreeSoFar,Tree):-
	insertOneEI_Ts(OneEITRecord,EI,TreeSoFar,NTreeSoFar),
	counting(All_T_Records,NTreeSoFar,Tree).


insertOneEI_Ts([],EI,Tree,Tree).

insertOneEI_Ts([Length-TI|TIs],EI,TreeSoFar,Tree):-
	(rb_lookup(TI,EI_List,TreeSoFar)->
		%TI is there-> previous example, or the same example
		(append(Pre,[{EI,Length0}|Post],EI_List)->
			(Length < Length0 ->
				append(Pre,[{EI,Length}|Post],NewEI_List),
				rb_update(TreeSoFar,TI,NewEI_List,NTreeSoFar);
				NTreeSoFar=TreeSoFar
			);
			rb_update(TreeSoFar,TI,[{EI,Length}|EI_List],NTreeSoFar)
		);
				
		rb_insert(TreeSoFar,TI,[{EI,Length}],NTreeSoFar)
	),	
	insertOneEI_Ts(TIs,EI,NTreeSoFar,Tree).

turnLengthCoverage(Length-Score-Ts,Score-Length-Ts).

%scoreCountedT([],[]).
%scoreCountedT([T|Ts],[ScoredT|ScoredTs]):-
scoreCountedT(T,ScoredT):-
		T=[enzyme_state-{Sign,EC_Number,Change}]-LengthedEI,
		length(LengthedEI,Coverage),
		Score0 is Coverage*Sign,
		divide_by_length(LengthedEI,Score0,Score),
		ScoredT= Coverage-Score-T.

divide_by_length([],Score,Score).
divide_by_length([{EI,Length}|LengthedEI],ScoredSoFar,Score):-
	NewScoredSoFar is ScoredSoFar/Length,
	divide_by_length(LengthedEI,NewScoredSoFar,Score).


% remove the contradict part. -- prefer the high mark, then the whole is used for prediction -- probability. --still low PA, unless that assumption is added.
% GE won't improve much, since with GE,the coverage is still low, the coverage depend on the structure of network itself. 
% choosingH(ScoredTs,FinalT)
