{-
# -*- coding: utf-8 -*-

# --------------------------------------------------
# File Name: decode.hs
# Purpose:
# Creation Date: 10-03-2017
# Last Modified: Thu, Mar 23, 2017  9:05:50 PM
# Author(s): Mike Stout 
# Copyright 2017 The Author(s) All Rights Reserved
# Credits: 
# --------------------------------------------------
-}

import System.Environment

import Utilities

import Data.Maybe
import Data.Char
import Data.List

-- import Text.Regex.PCRE



main = do
	{-
	let 	pat = "(foo[a-z]*bar|quux)"
		a =   "before foodiebar after" =~ pat :: (String,String,String)
	let 	stringResult = "hello there" =~ "e" :: AllTextMatches [] String	
		a = getAllTextMatches stringResult
	print a
	-}

	
	interact $ doStuff

doStuff s = unlines $ map show $ proc s

proc s = shw $ map proc1 $ spltAll1 "\n>" s

shw xs = [ a ++ ":\t" ++ b!!i  | i<-[0..m-1], (a,b) <- sortBy cmp xs ]
	where	cmp a b = compare (fst a) (fst b)
		m = length $ snd $ head xs


proc1 s = (a', takes 100 $ decode $ concat xs)
	where	(a:xs) = lines s
		a' = (spltAll1 ".html." a)!!0

--asDNA = True
asDNA = False

bases 
	| asDNA = "ATGC" 
	-- | otherwise = "KHERDQNPSLAGWTFV"
	| otherwise = "ARNDBCEQZGHILKMFPSTWYV"
codons 
	| asDNA = [ [a,b,c,d] | a <- bases, b <- bases , c <- bases, d <- bases] 
	| otherwise = [ [a,b] | a <- bases, b <- bases ] 

geneticCode = zip codons $ [ 0 ..  ] 

decode xs = map dec $ takes n xs -- $ fixGaps xs

n = length $ head codons

-- Ensure all gaps are of even length ...
fixGaps [] = []
fixGaps xs = concat $ map fixGaps' $ groupBy cmp xs
        where   cmp a b = a == b

fixGaps' xs = if head xs=='-' && k/=0 then pad k xs else xs 
	where	k = ((length xs) `mod` n)

pad n xs = (take n $ repeat '-') ++ xs


dec codon = if x==Nothing then '_'  else chr $ fromJust x
	where	x = lookup codon geneticCode