
filterIsolated(OrderedGroupedCandidateHs):-
	maplist(eachExCandidate,OrderedGroupedCandidateHs).

eachExCandidate(EI-TIs):-
	maplist(hTranslate_mappedToClaID1(EI),TIs).

hTranslate_mappedToClaID1(EI,TI):-
	candidateH0(EI,TI,OneH),
	hTranslate_mappedToClaID(EI,TI,OneH).


gen_output_readIn_candiateH(PosEIs):-
	tell('candidateH.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('candidateH.pl').	%consult('grammar_candidateH.pl'),

% this also include generating candidates
indentifySharedHs(PosEIs,T_EITIs):-	%,OrderedGroups
	rb_new(Tree0),
	reverse(PosEIs,PosEIsReversedOrder),
	insertTheories(PosEIsReversedOrder,Tree0,Tree), % insertTheories(AllTs,Tree0,Tree) *** be careful about the function you are using
	%scoreAll(AllTs,Tree,ScoredEIsTs).
	rb_visit(Tree,T_EITIs), %print_list(T_EITIs),
	maplist(outputSharedInfo,T_EITIs).

/*	maplist(outputSharedInfo,T_EITIs), %listing,
	findall(EITIs_any,sharedHs(ClaID,EITIs_any),AllShared0),
	flatten(AllShared0,AllShared1),sort(AllShared1,AllShared),
	ti_GroupedByEI(AllShared,Grouped),
	order_by_candidatesNum(Grouped,OrderedGroups),
	print_list(OrderedGroups). */


order_by_candidatesNum(Grouped,OrderedGroups):-
    maplist(assignNum,Grouped,NumberedGroups),
    sort(NumberedGroups,OrderedGroups0),
    maplist(removeNum,OrderedGroups0,OrderedGroups,Nums),
    write(Nums),
    multiplyList(Nums,TotalSearchSpace),write(TotalSearchSpace),nl.

assignNum(EI-TIs,Num-EI-TIs):-
    length(TIs,Num).
removeNum(Num-EI-TIs,EI-TIs,Num).

outputSharedInfo([rs-{RID,enzymeLimiting,Change,Time}]-EITIs):-    
	exExtraction(EITIs,EIs),
     	length(EIs,EINum),EINum>1,!,
	asserta(sharedControlReation([rs-{RID,enzymeLimiting,Change,Time}],EITIs)).
outputSharedInfo(ClaID-EITIs).



/*ti_GroupedByEI([],[]).
ti_GroupedByEI([EI-TI|Record],EIs):- 
	ti_GroupedByEI(Record,PreEIs),
	(append(Pre,[EI-TIs|Rest],PreEIs) ->
		EIs=[EI-[TI|TIs][EI-TIs|Rest],PreEIs);
		EIs=[EI-[TI]|PreEIs]
	).*/
% since you have sorted before grouping, so
ti_GroupedByEI([],[]).
ti_GroupedByEI([EI-TI|Record],EIs):- 
	ti_GroupedByEI(Record,PreEIs),
	(PreEIs=[EI-TIs|Rest] ->
		EIs=[EI-[TI|TIs]|Rest];
		EIs=[EI-[TI]|PreEIs]
	).

exExtraction([],[]).
exExtraction([EI-TI|Record],EIs):- 
	exExtraction(Record,PreEIs),
	(PreEIs=[EI|Rest] ->
		EIs=PreEIs;
		EIs=[EI|PreEIs]
	).
