{-# LANGUAGE OverloadedStrings #-}
module Bio.GO
( GO(..)
, GOId
, GOMap
, getGOLevel
) where
import qualified Data.HashMap.Strict as M
import Data.Maybe
import qualified Data.Text as T
data GO = GO
{ GO -> GOId
_oboId :: !GOId
, GO -> Text
_label :: !T.Text
, GO -> [GOId]
_subProcessOf :: ![GOId]
, GO -> Text
_oboNS :: !T.Text
} deriving (GOId -> GO -> ShowS
[GO] -> ShowS
GO -> String
(GOId -> GO -> ShowS)
-> (GO -> String) -> ([GO] -> ShowS) -> Show GO
forall a.
(GOId -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GO] -> ShowS
$cshowList :: [GO] -> ShowS
show :: GO -> String
$cshow :: GO -> String
showsPrec :: GOId -> GO -> ShowS
$cshowsPrec :: GOId -> GO -> ShowS
Show, ReadPrec [GO]
ReadPrec GO
GOId -> ReadS GO
ReadS [GO]
(GOId -> ReadS GO)
-> ReadS [GO] -> ReadPrec GO -> ReadPrec [GO] -> Read GO
forall a.
(GOId -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GO]
$creadListPrec :: ReadPrec [GO]
readPrec :: ReadPrec GO
$creadPrec :: ReadPrec GO
readList :: ReadS [GO]
$creadList :: ReadS [GO]
readsPrec :: GOId -> ReadS GO
$creadsPrec :: GOId -> ReadS GO
Read)
type GOId = Int
type GOMap = M.HashMap GOId GO
type TermCount = M.HashMap GOId Int
getGOLevel :: GOId -> GOMap -> Int
getGOLevel :: GOId -> GOMap -> GOId
getGOLevel GOId
gid GOMap
gm = GOId -> [GOId] -> GOId
forall t. Num t => t -> [GOId] -> t
loop GOId
0 [GOId
gid]
where
loop :: t -> [GOId] -> t
loop t
l [GOId]
ids | [GOId] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [GOId]
parents = t
l
| Bool
otherwise = t -> [GOId] -> t
loop (t
lt -> t -> t
forall a. Num a => a -> a -> a
+t
1) [GOId]
parents
where
parents :: [GOId]
parents = (GO -> [GOId]) -> [GO] -> [GOId]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap GO -> [GOId]
_subProcessOf ([GO] -> [GOId]) -> [GO] -> [GOId]
forall a b. (a -> b) -> a -> b
$ ((GOId -> Maybe GO) -> [GOId] -> [GO])
-> [GOId] -> (GOId -> Maybe GO) -> [GO]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (GOId -> Maybe GO) -> [GOId] -> [GO]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe [GOId]
ids ((GOId -> Maybe GO) -> [GO]) -> (GOId -> Maybe GO) -> [GO]
forall a b. (a -> b) -> a -> b
$ \GOId
i -> GOId -> GOMap -> Maybe GO
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup GOId
i GOMap
gm