{-# language OverloadedStrings #-}
{-# options_ghc -Wno-unused-imports #-}
module Algebra.Graph.IO.SV (
tsvSink
) where
import Control.Monad (when)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Void (Void)
import qualified Algebra.Graph as G (Graph, edge, empty, overlay)
import Data.ByteString (ByteString)
import Conduit (MonadUnliftIO(..), MonadResource, runResourceT)
import Data.Conduit (runConduit, ConduitT, (.|), yield, await)
import qualified Data.Conduit.Combinators as C (print, sourceFile, sinkFile, map, mapM, foldM, mapWhile)
import Data.Conduit.Zlib (ungzip)
import Data.CSV.Conduit (CSV(..), CSVSettings(..), Row)
import Control.Monad.Catch (MonadThrow(..))
import Network.HTTP.Simple (httpSource, getResponseBody, Response, Request, parseRequest, setRequestMethod)
import Text.Megaparsec (parse)
import Text.Megaparsec.Char.Lexer (decimal)
import Control.Monad.Combinators (count)
import Control.Monad.Primitive (PrimMonad(..))
import Data.Conduit.Tar (Header(..), untarChunks, TarChunk, withEntries, headerFileType, FileType(..), headerFilePath)
import Data.Text (Text)
import Data.Text.Encoding (decodeUtf8)
import Algebra.Graph.IO.Internal.Megaparsec (Parser, ParseE)
import Algebra.Graph.IO.Internal.Conduit (unTarGz, fetch)
tsvSink :: (MonadThrow m) => ConduitT ByteString o m (G.Graph Int)
tsvSink :: ConduitT ByteString o m (Graph Int)
tsvSink = ConduitT ByteString (Row Text) m ()
forall (m :: * -> *).
MonadThrow m =>
ConduitT ByteString (Row Text) m ()
parseTSV ConduitT ByteString (Row Text) m ()
-> ConduitM (Row Text) o m (Graph Int)
-> ConduitT ByteString o m (Graph Int)
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| (Row Text -> Maybe (Edge Int))
-> ConduitT (Row Text) (Maybe (Edge Int)) m ()
forall (m :: * -> *) a b. Monad m => (a -> b) -> ConduitT a b m ()
C.map Row Text -> Maybe (Edge Int)
edgeP ConduitT (Row Text) (Maybe (Edge Int)) m ()
-> ConduitM (Maybe (Edge Int)) o m (Graph Int)
-> ConduitM (Row Text) o m (Graph Int)
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| ConduitM (Maybe (Edge Int)) o m (Graph Int)
forall (m :: * -> *) a o.
Monad m =>
ConduitT (Maybe (Edge a)) o m (Graph a)
accGraph
parseTSV :: MonadThrow m => ConduitT ByteString (Row Text) m ()
parseTSV :: ConduitT ByteString (Row Text) m ()
parseTSV = CSVSettings -> ConduitT ByteString (Row Text) m ()
forall s r (m :: * -> *).
(CSV s r, MonadThrow m) =>
CSVSettings -> ConduitM s r m ()
intoCSV CSVSettings
tsvSettings
edgeP :: [Text] -> Maybe (Edge Int)
edgeP :: Row Text -> Maybe (Edge Int)
edgeP Row Text
t =
case (Text -> Either ParseE Int) -> Row Text -> Either ParseE [Int]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (Parsec Void Text Int -> String -> Text -> Either ParseE Int
forall e s a.
Parsec e s a -> String -> s -> Either (ParseErrorBundle s e) a
parse Parsec Void Text Int
forall e s (m :: * -> *) a.
(MonadParsec e s m, Token s ~ Char, Num a) =>
m a
decimal String
"" :: Text -> Either ParseE Int) Row Text
t of
Left ParseE
_ -> Maybe (Edge Int)
forall a. Maybe a
Nothing
Right (Int
a:Int
b:Int
c:[Int]
_) -> Edge Int -> Maybe (Edge Int)
forall a. a -> Maybe a
Just (Edge Int -> Maybe (Edge Int)) -> Edge Int -> Maybe (Edge Int)
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int -> Edge Int
forall a. a -> a -> a -> Edge a
Edge Int
a Int
b Int
c
Right [Int]
_ -> Maybe (Edge Int)
forall a. Maybe a
Nothing
data Edge a = Edge a a a deriving (Edge a -> Edge a -> Bool
(Edge a -> Edge a -> Bool)
-> (Edge a -> Edge a -> Bool) -> Eq (Edge a)
forall a. Eq a => Edge a -> Edge a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Edge a -> Edge a -> Bool
$c/= :: forall a. Eq a => Edge a -> Edge a -> Bool
== :: Edge a -> Edge a -> Bool
$c== :: forall a. Eq a => Edge a -> Edge a -> Bool
Eq, Int -> Edge a -> ShowS
[Edge a] -> ShowS
Edge a -> String
(Int -> Edge a -> ShowS)
-> (Edge a -> String) -> ([Edge a] -> ShowS) -> Show (Edge a)
forall a. Show a => Int -> Edge a -> ShowS
forall a. Show a => [Edge a] -> ShowS
forall a. Show a => Edge a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Edge a] -> ShowS
$cshowList :: forall a. Show a => [Edge a] -> ShowS
show :: Edge a -> String
$cshow :: forall a. Show a => Edge a -> String
showsPrec :: Int -> Edge a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Edge a -> ShowS
Show)
accGraph :: (Monad m) => ConduitT (Maybe (Edge a)) o m (G.Graph a)
accGraph :: ConduitT (Maybe (Edge a)) o m (Graph a)
accGraph = ((Graph a -> Maybe (Edge a) -> m (Graph a))
-> Graph a -> ConduitT (Maybe (Edge a)) o m (Graph a))
-> Graph a
-> (Graph a -> Maybe (Edge a) -> m (Graph a))
-> ConduitT (Maybe (Edge a)) o m (Graph a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Graph a -> Maybe (Edge a) -> m (Graph a))
-> Graph a -> ConduitT (Maybe (Edge a)) o m (Graph a)
forall (m :: * -> *) a b o.
Monad m =>
(a -> b -> m a) -> a -> ConduitT b o m a
C.foldM Graph a
forall a. Graph a
G.empty ((Graph a -> Maybe (Edge a) -> m (Graph a))
-> ConduitT (Maybe (Edge a)) o m (Graph a))
-> (Graph a -> Maybe (Edge a) -> m (Graph a))
-> ConduitT (Maybe (Edge a)) o m (Graph a)
forall a b. (a -> b) -> a -> b
$ \Graph a
acc Maybe (Edge a)
m ->
case Maybe (Edge a)
m of
Just (Edge a
a a
b a
_) -> Graph a -> m (Graph a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Graph a -> m (Graph a)) -> Graph a -> m (Graph a)
forall a b. (a -> b) -> a -> b
$ (a
a a -> a -> Graph a
forall a. a -> a -> Graph a
`G.edge` a
b) Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
`G.overlay` Graph a
acc
Maybe (Edge a)
Nothing -> Graph a -> m (Graph a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Graph a
acc
tsvSettings :: CSVSettings
tsvSettings :: CSVSettings
tsvSettings = Char -> Maybe Char -> CSVSettings
CSVSettings Char
'\t' Maybe Char
forall a. Maybe a
Nothing