#### Local similarity (sequence alignment)

Read the full description in Towards a discipline of dynamic programming.

Sequence 1: Sequence 2:

Input:

Algebra: ***            Algebra explanation

Grammar:   locsim

Output:

#### For usage on your local machine:

```> module LocSim where

> import Data.Array
> import Data.List

```
The signature:
```> data Alignment = Nil (Int, Int)         |
>                  D  Char Alignment      |
>                  I  Alignment Char      |
>                  R  Char Alignment Char
>                                            deriving (Eq, Show)

```
Algebra type:
```> type Locsim_Algebra alphabet answer = (
>   (Int, Int) -> answer,                     -- nil
>   )

```
Enumeration algebra:
```> enum :: Locsim_Algebra Char Alignment
> enum = (nil, d, i, r, h) where
>   nil = Nil
>   d   = D
>   i   = I
>   r   = R
>   h   = id

```
Pretty printing algebra:
```> prettyprint :: Locsim_Algebra Char (String, String)
> prettyprint = (nil, d, i, r, h) where
>   nil _       = ("","")
>   d x (l,r)   = (x:l, gap:r)
>   i (l,r) x   = (gap:l, x:r)
>   r x (l,r) y = (x:l,y:r)
>   h           = id
>   gap         = '-'

```
Counting Algebra:
```> count :: Locsim_Algebra Char Int
> count = (nil, d, i, r, h) where
>   nil s   = 1
>   d x s   = s
>   i s x   = s
>   r a s b = s
>   h []    = []
>   h xs    = [sum xs]

```
The scoring algebras:
```> wgap :: Locsim_Algebra Char Int
> wgap = (nil, d, i, r, h) where
>   nil a   = 0
>   d x s   = s + gap x
>   i s y   = s + gap y
>   r a s b = s + w a b
>   h []    = []
>   h xs    = [maximum xs]
>
>  -- simple definitions for gap and w:
>   gap x = (-1)
>   w a b = if a==b then 1 else (-1)

> unit :: Locsim_Algebra Char Int
> unit = (nil, d, i, r, h) where
>   nil a   = 0
>   d x s   = s - 1
>   i s y   = s - 1
>   r a s b = if a==b then s+1 else s-1
>   h []    = []
>   h xs    = [maximum xs]

```
Algebra product operation:
```> infix ***
> (***) :: Eq answer1 =>
> alg1 *** alg2 = (nil, d, i, r, h) where
>    (nil1, d1, i1, r1, h1) = alg1
>    (nil2, d2, i2, r2, h2) = alg2
>
>    nil a = (nil1 a, nil2 a)
>    d x (s1,s2)   = (d1 x s1, d2 x s2)
>    i   (s1,s2) y = (i1 s1 y, i2 s2 y)
>    r x (s1,s2) y = (r1 x s1 y, r2 x s2 y)
>
>    h xs = [(x1,x2)| x1 <- nub \$ h1 [ y1 | (y1,y2) <- xs],
>                     x2 <-       h2 [ y2 | (y1,y2) <- xs, y1 == x1]]

```
The yield grammar:
```> locsim alg f = axiom skipR where
>   (nil, d, i, r, h) = alg

>   skip_right a b = a
>   skip_left  a b = b

>   skipR     = skip_right <<<           skipR ~~- achar |||
>               skipL                                    ... h

>   skipL     = skip_left  <<< achar -~~ skipL           |||
>               alignment                                ... h

>   alignment  = tabulated(
>                 nil <<< astring                       |||
>                 d   <<< achar -~~ alignment           |||
>                 i   <<<           alignment ~~- achar |||
>                 r   <<< achar -~~ alignment ~~- achar ... h)

```
Bind input:
```>   z         = mk f
>   (_,n)     = bounds z
>   achar     = acharSep' z '\$'
>   tabulated = table n
>   axiom     = axiom' n

```

 | Author: mruether | Date: 2004/10/01 15:52:12 |