Local similarity (sequence alignment)
ADP Source Code Try DP online
Haskell header: showcode
> module LocSim where

> import Array
> import ADPCombinators
> import List
The signature: showcode
> data Alignment = Nil (Int, Int)         |
>                  D  Char Alignment      |
>                  I  Alignment Char      |
>                  R  Char Alignment Char 
>                                            deriving (Eq, Show)
The yield grammar: showcode
> 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: showcode
>   z         = mk f
>   (_,n)     = bounds z
>   achar     = acharSep' z '$'
>   tabulated = table n
>   axiom     = axiom' n
Enumeration algebra: showcode
> 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: showcode
> 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: showcode
> 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: showcode
> 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 type: showcode
> type Locsim_Algebra alphabet answer = (
>   (Int, Int) -> answer,                     -- nil
>   alphabet -> answer -> answer,             -- d
>   answer -> alphabet -> answer,             -- i
>   alphabet -> answer -> alphabet -> answer, -- r
>   [answer] -> [answer]                      -- h
>   )
Algebra cross product: showcode
> infix ***
> (***) :: Eq answer1 =>
>          Locsim_Algebra alphabet answer1 ->
>          Locsim_Algebra alphabet answer2 ->
>          Locsim_Algebra alphabet (answer1, answer2)
> 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]]
For usage on your local machine:
background image
university bielefeld AG PI BiBiServ
ambient picture