module Utility ( addLengthHeader
, addFillerGermlines
, replaceChars
, getField
) where
import qualified Data.Map as M
import qualified Data.Text as T
import Data.Fasta.Text
import TextShow
addLengthHeader :: FastaSequence -> FastaSequence
addLengthHeader fSeq = fSeq { fastaHeader = fastaHeader fSeq
`mappend` "|"
`mappend` (showt . T.length . fastaSeq $ fSeq)
}
addFillerGermlines :: [FastaSequence] -> CloneMap
addFillerGermlines = M.fromList . labelGermlines . map insertDummy
where
labelGermlines = map (\(x, (y, z)) -> ((x, y), z)) . zip [0..]
insertDummy x = (dummy, [x])
dummy = FastaSequence {fastaHeader = "filler", fastaSeq = "---"}
zipWithRetain :: (a -> a -> a) -> [a] -> [a] -> [a]
zipWithRetain _ [] [] = []
zipWithRetain _ xs [] = xs
zipWithRetain _ [] ys = ys
zipWithRetain f (x:xs) (y:ys) = f x y : zipWithRetain f xs ys
zipWithRetainText :: (Char -> Char -> Char) -> T.Text -> T.Text -> T.Text
zipWithRetainText _ (T.uncons -> Nothing) (T.uncons -> Nothing) = T.empty
zipWithRetainText _ xs (T.uncons -> Nothing) = xs
zipWithRetainText _ (T.uncons -> Nothing) ys = ys
zipWithRetainText f (T.uncons -> Just (x, xs)) (T.uncons -> Just (y, ys))
= f x y `T.cons` zipWithRetainText f xs ys
replaceChars :: Char -> T.Text -> T.Text -> T.Text
replaceChars c = zipWithRetainText changeChar
where
changeChar a b = if a == c && (not . T.isInfixOf (T.singleton b)) ".-"
then b
else a
getField :: Int -> FastaSequence -> T.Text
getField f fs = (T.splitOn "|" . fastaHeader $ fs) !! (f 1)