/* Modularization analysis:


*/

% How many pathways?
pathways:-
	findall(PathKeggName,
	(pathway(PathwayID,Annotated,'','KEGG','IMPD'),
	concept_accession(PathwayID,'KEGG',PathKeggName,true)),
	PathKeggNames
	),write(PathKeggNames).



% connections among pathways
/* connections:-
	tell('pathwayConnections.txt'),
	findall(MKeggName-PathwayKeggName1-PathwayKeggName2,
		(produced_by(M,ProducingRID,_),member_of(ProducingRID,PathwayID1,'IMPD'),concept_accession(PathwayID1,'KEGG',PathwayKeggName1,true),%concept_name(PathwayID1,PathwayName1,true),
		consumed_by(M,ConsumingRID,_),member_of(ConsumingRID,PathwayID2,'IMPD'),concept_accession(PathwayID2,'KEGG',PathwayKeggName2,true),%concept_name(PathwayID2,PathwayName2,true),
		PathwayID1\==PathwayID2,
		concept_accession(M,'KEGG',MKeggName,true)
		% portray_clause(pathwayConnection(MKeggName,PathwayKeggName1,PathwayKeggName2)
		),
	Connections0),
	remove_duplicates(Connections0,Connections),
	length(Connections0,L0),
	length(Connections,L),
write(Connections),
write({L0,L}),
	told. */

connections:-
	tell('pathwayConnections.txt'),
	findall(MKeggName-PathwayKeggName1-PathwayKeggName2,
		( 
producedSamePathwayReactions(M,ProducedReactions,PathwayID1),
consumedSamePathwayReactions(M,ConsumedReactions,PathwayID2),
PathwayID1\==PathwayID2,
concept_accession(PathwayID1,'KEGG',PathwayKeggName1,true),
concept_accession(PathwayID2,'KEGG',PathwayKeggName2,true),
concept_accession(M,'KEGG',MKeggName,true)
		% portray_clause(pathwayConnection(MKeggName,PathwayKeggName1,PathwayKeggName2)
		),
	Connections0),
	remove_duplicates(Connections0,Connections),
	length(Connections0,L0),
	length(Connections,L),
write(Connections),
write({L0,L}),
	told.

% no input for this program
producedSamePathwayReactions(M,[ProducingRIDKeggName1|ProducedReactions],PathwayID):-
produced_by(M,ProducingRID1,_),member_of(ProducingRID1,PathwayID,'IMPD'),concept_accession(ProducingRID1,'KEGG',ProducingRIDKeggName1,true) % M and PathwayID are instantiated
findall(
ProducingRIDKeggName,
	(produced_by(M,ProducingRID,_),
	member_of(ProducingRID,PathwayID,'IMPD'),
	%concept_accession(PathwayID,'KEGG',PathwayKeggName,true),
	concept_accession(ProducingRID,'KEGG',ProducingRIDKeggName,true)
	),
ProducedReactions).


consumedSamePathwayReactions(M,ConsumedReactions,PathwayID):-
findall(
ConsumingRIDKeggName,
	(consumed_by(M,ConsumingRID,_),
	member_of(ConsumingRID,PathwayID,'IMPD'),
	%concept_accession(PathwayID,'KEGG',PathwayKeggName,true),
	concept_accession(ConsumingRID,'KEGG',ConsumingRIDKeggName,true)
	),
ConsumedReactions).
