A quadratic version for a simplistic cost function (Waterman) (sequence alignment)
This is Waterman's reformulation of the Needleman-Wunsch algorithm for a simple additive gap function, running in O(n^2). Read the full description in Systematic Dynamic Programming in Bioinformatics.
ADP Source Code Try DP online
Haskell header: showcode
> module Waterman where

> import Array
> import List
> import TTCombinators
The signature: showcode
> data Alignment = Nil                   |
>                  D  Char Alignment     |
>                  I  Alignment Char     |
>                  R  Char Alignment Char 
>                                        deriving (Eq, Show)
The yield grammar: showcode
> waterman_alignments alg inpX inpY = axiom alignment where
>   (nil, d, i, r, h)    = alg
> 
>   alignment  = tabulated (
>                 nil ><< empty                         |||
>                 r   <<< xbase -~~ alignment ~~- ybase |||
>                 d   <<< xbase -~~ alignment           |||
>                 i   <<<           alignment ~~- ybase ... h)
Bind input: showcode
>   infixl 7  -~~, ~~-
>   (_, _, xbase, ybase, empty, _, _, (-~~), (~~-), tabulated) 
>     = bindParserCombinators inpX inpY 
Enumeration algebra: showcode
> enum :: Waterman_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 :: Waterman_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 :: Waterman_Algebra Char Int
> count = (nil, d, i, r, h) where
>   nil     = 1
>   d x s   = s
>   i s x   = s
>   r a s b = s
>   h []    = []
>   h xs    = [sum xs]
The scoring algebras: showcode
> wgap :: Waterman_Algebra Char Int
> wgap = (nil, d, i, r, h) where
>   nil     = 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 :: Waterman_Algebra Char Int
> unit = (nil, d, i, r, h) where
>   nil     = 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 Waterman_Algebra alphabet answer = (
>   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 =>
>          Waterman_Algebra alphabet answer1 ->
>          Waterman_Algebra alphabet answer2 ->
>          Waterman_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           = (nil1, nil2)
>    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