# s(CASP) biological example

**URL:** <https://swi-prolog.discourse.group/t/s-casp-biological-example/4570>\
**Category:** Help!\
**Created:** [October 20, 2021, 8:44am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570 "2021-10-20T08:44:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam.neaves](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sam.neaves/32/967_2.png) [@sam.neaves](https://swi-prolog.discourse.group/u/sam.neaves)\
**Post date:** [October 20, 2021, 8:44am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/1 "2021-10-20T08:44:37Z")

</div>

I put together this notebook which demonstrates how haplotype inference can be done in s(CASP). I think it is very elegant.

[https://swish.swi-prolog.org/?code=https://raw.githubusercontent.com/samwalrus/scasp\_haplotype/master/swish.swinb](https://swish.swi-prolog.org/?code=https://raw.githubusercontent.com/samwalrus/scasp_haplotype/master/swish.swinb)

A couple of questions, would the query `?- ? genotype(1,[2,1,2]),genotype(2,[1,2,1]).` always find the model with the smallest number of `haplotype/1` literals first? If not how do I use `scasp_model/2` to return the model for me to count the number of `haplotype/1` literals in the model?

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [October 20, 2021, 9:22am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/2 "2021-10-20T09:22:53Z")

</div>

FYI for others.

* * *

[pubmed.ncbi.nlm.nih.gov](http://pubmed.ncbi.nlm.nih.gov)

Haplotype inference

([ref](https://pubmed.ncbi.nlm.nih.gov/22665282/))

> The information carried by combination of alleles on the same chromosome, called haplotypes, is of crucial interest in several fields of modern genetics as population genetics or association studies. However, this information is usually lost by sequencing and needs, therefore, to be recovered by inference.

* * *

[www.genome.gov](http://www.genome.gov)

haplotype

([ref](https://www.genome.gov/genetics-glossary/haplotype))

> A haplotype is a set of DNA variations, or polymorphisms, that tend to be inherited together. A haplotype can refer to a combination of alleles or to a set of single nucleotide polymorphisms (SNPs) found on the same chromosome.

allele

([ref](https://www.genome.gov/genetics-glossary/Allele))

> An allele is one of two or more versions of a gene. An individual inherits two alleles for each gene, one from each parent. If the two alleles are the same, the individual is homozygous for that gene. If the alleles are different, the individual is heterozygous. Though the term allele was originally used to describe variation among genes, it now also refers to variation among non-coding DNA sequences.

* * *

Wikipedia

([ref](https://en.wikipedia.org/wiki/Haplotype))

haplotype

> A haplotype ([haploid](https://en.wikipedia.org/wiki/Haploid) [genotype](https://en.wikipedia.org/wiki/Genotype)) is a group of [alleles](https://en.wikipedia.org/wiki/Allele) in an [organism](https://en.wikipedia.org/wiki/Organism) that are inherited together from a single parent

([ref](https://en.wikipedia.org/wiki/Allele))

allele

> An allele is one of two, or more, forms of a given [gene](https://en.wikipedia.org/wiki/Gene) variant.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [October 20, 2021, 10:00am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/3 "2021-10-20T10:00:28Z")

</div>

If you don’t mind answering this question. I know I would find it helpful and maybe others would too.

The code you posted

```prolog
%conflation.
conflation(2,0,1). %Heterozygous
conflation(2,1,0). %Heterozygous
conflation(0,0,0). %Homozygous
conflation(1,1,1). %Homozygous.

conflation_seq([],[],[]).
conflation_seq([G|Gs],[HA1|HAs],[HB1|HBs]):-
       conflation(G,HA1,HB1),
       conflation_seq(Gs,HAs,HBs).

genotype(_G,Genotype):-
    haplotype(Hap1),
    haplotype(Hap2),
    conflation_seq(Genotype,Hap1,Hap2).

haplotype(_X).

```

looks like standard Prolog. Except for `:- use_module(library(scasp)).` I would not have guessed it being s(CASP).

Can you demonstrate what the answer would be with normal Prolog and how using s(CASP) changes the results. 🙂

Also, can conflation\_seq/3 be changed out for one of the predicates from [library(apply)](https://www.swi-prolog.org/pldoc/man?section=apply), or does using s(CASP) cause problems?

---

<div class="post-metadata">

**Author:** ![sam.neaves](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sam.neaves/32/967_2.png) [@sam.neaves](https://swi-prolog.discourse.group/u/sam.neaves)\
**Post date:** [October 20, 2021, 10:04am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/4 "2021-10-20T10:04:50Z")

</div>

You can query with prolog

```prolog
?- genotype(1,[2,1,2]),genotype(2,[1,2,1]).
true.

```

Not very helpful.

But query with scasp (note the ? op):

```prolog
?- ? genotype(1,[2,1,2]),genotype(2,[1,2,1]).
#### s(CASP) model

* haplotype holds for [0, 1, 0]
* haplotype holds for [1, 0, 1]
* haplotype holds for [1, 1, 1]
* genotype holds for 1, and [2, 1, 2]
* genotype holds for 2, and [1, 2, 1]
* conflation holds for 1, 1, and 1
* conflation holds for 2, 0, and 1
* conflation_seq holds for [], [], and []
* conflation_seq holds for [1], [1], and [1]
* conflation_seq holds for [1, 2], [1, 0], and [1, 1]
* conflation_seq holds for [1, 2, 1], [1, 0, 1], and [1, 1, 1]
* conflation_seq holds for [2], [0], and [1]
* conflation_seq holds for [2, 1], [0, 1], and [1, 1]
* conflation_seq holds for [2, 1, 2], [0, 1, 0], and [1, 1, 1]

#### s(CASP) justification

Expand All +1 -1 Collapse All

* genotype holds for 1, and [2, 1, 2], because

  * haplotype holds for [0, 1, 0], and

  * haplotype holds for [1, 1, 1], and

  * conflation_seq holds for [2, 1, 2], [0, 1, 0], and [1, 1, 1], because

```

Also, can conflation\_seq/3 be changed out for one of the predicates from [library(apply)](https://www.swi-prolog.org/pldoc/man?section=apply), or does using s(CASP) cause problems?

Not sure, I had to redefine some basic predicates before so just keeping it simple.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 21, 2021, 7:38am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/5 "2021-10-21T07:38:27Z")

</div>

> [@sam.neaves](#):
>
> Also, can conflation\_seq/3 be changed out for one of the predicates from [library(apply)](https://www.swi-prolog.org/pldoc/man?section=apply), or does using s(CASP) cause problems?

As is, s(CASP) doesn’t allow high-order calling, e.g., call/N. I don’t know how feasible it would be to add that, neither how useful. s(CASP) only gets interesting when negation is involved. Without negation its semantics is no different from Prolog and it is not rocket science to build up a justification and model from standard Prolog SLD resolution.

To deal with negation, s(CASP) uses _constructive negation_. From the positive rules it derives negative rules that do not work using NAF (Negation As Failure), but build up a model of the world where some goal is known to be not true. Constraints come handy there. This allows the system to derive from p(a) that not p(X) may be true for any X not equal to `a`. Where ASP easily explodes during the _grounding phase_, s(CASP) easily explodes in the complexity of (notably) the negative world. Well, it works differently, but it is surprisingly easy to create what seems to be rather simple programs that perform unexpectedly poor.

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [October 21, 2021, 7:54am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/6 "2021-10-21T07:54:02Z")

</div>

> [@jan](#):
>
> but it is surprisingly easy to create what seems to be rather simple programs that perform unexpectedly poor.

(off-topic) you just summarized my whole career in one sentence

---

<div class="post-metadata">

**Author:** ![sam.neaves](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sam.neaves/32/967_2.png) [@sam.neaves](https://swi-prolog.discourse.group/u/sam.neaves)\
**Post date:** [October 21, 2021, 8:08am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/7 "2021-10-21T08:08:08Z")

</div>

> [@jan](#):
>
> As is, s(CASP) doesn’t allow high-order calling, e.g., call/N. I don’t know how feasible it would be to add that, neither how useful. s(CASP) only gets interesting when negation is involved. Without negation its semantics is no different from Prolog and it is not rocket science to build up a justification and model from standard Prolog SLD resolution.

Yes the idea here would be to try and add constraints on what haplotypes are admitable which might use negation… but complexity will prob make it impractical for real tasks.

> [@sam.neaves](#):
>
> `would the query ?- ? genotype(1,[2,1,2]),genotype(2,[1,2,1]).` always find the model with the smallest number of `haplotype/1` literals first? If not how do I use `scasp_model/2` to return the model for me to count the number of `haplotype/1` literals in the model?

Still trying to understand this though…?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 21, 2021, 8:14am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/8 "2021-10-21T08:14:30Z")

</div>

> [@sam.neaves](#):
>
> If not how do I use `scasp_model/2` to return the model for me to count the number of `haplotype/1` literals in the model?

The s(CASP) model is just a list of terms. You can use Prolog to inspect it. There is no magic there. Possibly we can define some more abstract operations on them. I do not see much more than “give me all model terms about p/1)”, which would return p(X), not p(X), -p(X) and not -p(X) instances.

---

<div class="post-metadata">

**Author:** ![sam.neaves](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sam.neaves/32/967_2.png) [@sam.neaves](https://swi-prolog.discourse.group/u/sam.neaves)\
**Post date:** [October 21, 2021, 8:19am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/9 "2021-10-21T08:19:49Z")

</div>

I am just confused how to actually get that list… ?

```prolog
?- ? genotype(1,[2,1,2]),genotype(2,[1,2,1]),scasp_model(M).
o permission to scasp procedure `scasp_embed:scasp_model/1'
In:
  [15] throw(error(permission_error(scasp,procedure,...),_1806))
  [10] scasp_dyncall:body_calls_pi((genotype(1,...),...,...),'9cbd6aed-557a-48e7-b8ae-cdb040dce973',_1866) at /scratch/swish/src/swish/pack/sCASP/prolog/scasp/dyncall.pl:166
   [9] findall_loop(_1922,scasp_dyncall:body_calls_pi(...,'9cbd6aed-557a-48e7-b8ae-cdb040dce973',_1942),_1926,[]) at /home/swish/lib/swipl/boot/bags.pl:99
   [8] setup_call_catcher_cleanup('$bags':'$new_findall_bag','$bags':findall_loop(_2006,...,_2010,[]),_1988,'$bags':'$destroy_findall_bag') at /home/swish/lib/swipl/boot/init.pl:646
   [4] scasp_dyncall:query_callees((...,...),_2068) at /scratch/swish/src/swish/pack/sCASP/prolog/scasp/dyncall.pl:161
   [3] scasp_dyncall:scasp_query_clauses((...,...),_2132) at /scratch/swish/src/swish/pack/sCASP/prolog/scasp/dyncall.pl:113
   [2] scasp_dyncall:scasp((...,...)) at /scratch/swish/src/swish/pack/sCASP/prolog/scasp/dyncall.pl:67
   [1] setup_call_catcher_cleanup(scasp:set_prolog_flag(scasp_show_justification,false),scasp:scasp(...),_2248,scasp:set_prolog_flag(scasp_show_justification,unicode)) at /home/swish/lib/swipl/boot/init.pl:646

Note: some frames are missing due to last-call optimization.
Re-run your program in debug mode (:- debug.) to get more detail.

```

(In swish).

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 21, 2021, 8:24am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/10 "2021-10-21T08:24:45Z")

</div>

You are hit by operator precedence. `?` is a high priority operator, so `? a,b.` is executed as a s(CASP) conjunction. scasp\_model/1 however is Prolog, so you need to write

```
?- (? a,b), scasp_model(M).
```

---

<div class="post-metadata">

**Author:** ![sam.neaves](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sam.neaves/32/967_2.png) [@sam.neaves](https://swi-prolog.discourse.group/u/sam.neaves)\
**Post date:** [October 21, 2021, 8:27am UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/11 "2021-10-21T08:27:40Z")

</div>

ah ok 🙂 thanks!

---

<div class="post-metadata">

**Author:** ![sam.neaves](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sam.neaves/32/967_2.png) [@sam.neaves](https://swi-prolog.discourse.group/u/sam.neaves)\
**Post date:** [October 21, 2021, 12:34pm UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/13 "2021-10-21T12:34:25Z")

</div>

I would say this is abduction, but there is also abducible/1 in scasp but I am not sure how this is meant to be used.

---

<div class="post-metadata">

**Author:** ![sam.neaves](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sam.neaves/32/967_2.png) [@sam.neaves](https://swi-prolog.discourse.group/u/sam.neaves)\
**Post date:** [October 21, 2021, 12:39pm UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/14 "2021-10-21T12:39:08Z")

</div>

See also :  
example from the book simply logical:

> <https://github.com/samwalrus/scasp_tests/blob/a449df12fa1fe6f82afc49129281b446c92bc873/simply_logical/adder.scasp>

and a simple ilp with negation example.

> <https://github.com/samwalrus/scasp_tests/blob/a449df12fa1fe6f82afc49129281b446c92bc873/simply_logical/negation_ilp_simple.scasp>

---

<div class="post-metadata">

**Author:** ![JasonMorris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jasonmorris/32/3824_2.png) [@JasonMorris](https://swi-prolog.discourse.group/u/JasonMorris)\
**Post date:** [November 2, 2021, 3:19pm UTC](https://swi-prolog.discourse.group/t/s-casp-biological-example/4570/15 "2021-11-02T15:19:30Z")

</div>

```prolog
#abducible p(X).

```

gets implemented in s(CASP) as

```prolog
p(X) :- not -p(X).
-p(X): not p(X).

```

if I’m not mistaken. So it creates two different “worlds,” one in which p(X) holds, and one in which it is known not to hold.
