Counting conditional occurrences using Prolog.
I wanted to implement the following rule in prolog for my IISA project.
If the percentage of coil region is greater than 20% the homologue detection algorithms may become unreliable. The coil module of IISA returns prolog database in the following format.
coil(105,a,0.000).
coil(109,g,0.001).
coil(110,l,0.004).
coil(111,l,0.014).
coil(112,v,0.055).
coil(113,g,0.055).
coil(114,s,0.371).
coil(115,e,0.416).
coil(116,k,0.860).
coil(117,v,0.955).
coil(118,t,0.998).
coil(119,m,0.999).
coil(120,q,0.999).
coil(121,n,0.999).
ie coil(position, aminoacid, probability of being part of coil).
I wanted to count the number of facts with the probability exceeding a cutoff value, say 0.7.
Though it is simple problem, I could not find any solution for this even after googling for few hours. Finally I found this code on the net.
%Code to count the number of proofs for a goal. Found on the net.
count_proof(Goal, N) :-
flag(counter, Outer, 0),
( call(Goal),
flag(counter, Inner, Inner+1),
fail;
flag(counter, Count, Outer),
N = Count
).
Though it worked well (I have still not figured out how!) the code is SWI-Prolog specific and did not work in JLog prolog applet I use for IISA web interface.
Finally I learned about findall and bagof
But still it took some time to figure out how to use findall for my purpose because the description read findall (Var, Database, Array) and to use length(Array). Using coil (_,_,A) as database will count only the number of aminoacids and I didn’t know how to implement the condition A>0.7.
Finally somewhere I found the actual definition for findall (Var, Goal, Array) which solved my problem, though I wasted a whole day on this simple problem.
Here is the final code.
%The region is coil if the value is greater than threshold
coil_region(Cutoff, Val):-
coil(_,_,Val),
Val>Cutoff.
coil_percent(Region, Total, Percent):-
setting(coil,X),
findall(Val,coil_region(X,Val),R),
length(R,Region),
findall(C,coil(_,_,C),A),
length(A,Total),
Total>0, %Avoid division by zero
Percent is (Region/Total) * 100,!.
coil_percent(0,0,0).
IISA Home Page.
- Loading MIMIC dataset onto a FHIR server in two easy steps - November 20, 2024
- R&D and Innovation in IT; to or not to combine both - November 15, 2024
- Locally hosted LLMs - July 14, 2024