packages feed

convert-annotation-0.4.0.0: src/MSigDBRDataConvert.hs

{- MSigDBRDataConvert
Gregory W. Schwartz

Collections the functions pertaining to converting certain annotations into
pathways using the MSigDB rdata files (tested with
http://bioinf.wehi.edu.au/software/MSigDB/).
-}

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}

module MSigDBRDataConvert
    ( getRData
    , toMSigDBPathways
    ) where

-- Standard

-- Cabal
import qualified Data.Text as T

import qualified Foreign.R as R
import Foreign.R (SEXP, SEXPTYPE)
import Language.R.Instance as R
import Language.R.QQ
import Language.R.Literal as R
import H.Prelude

-- Local
import Types
import RGeneConvert
       
-- | Get the RData object.
getRData :: File -> String -> R s (RData s)
getRData (File file) object = fmap RData
                            $ [r| load(file_hs)
                                  res = get(object_hs)
                                  res
                              |]

-- | Get the R mapping of gene to gene.
toMSigDBPathways
    :: RData s
    -> RMart s
    -> MSigDBType
    -> UnknownAnn
    -> IO (Maybe Desc)
toMSigDBPathways _ _ _ (UnknownAnn "")            = return Nothing
toMSigDBPathways rData rMart (MSigDBType (_, _, !from)) query =
    (fmap . fmap) Desc $ R.runRegion $ do
      entrez <- io . toRGeneAnn rMart (RType (from, "entrezgene")) $ query
      let object = unRData rData

      case entrez of
          Nothing            -> return Nothing
          (Just (Ann gText)) -> do
              let g = T.unpack gText
              res <- [r| pathNames = names(object_hs[unlist(lapply(object_hs, function(x) (g_hs %in% unlist(x))))]) |]
              let pathNames = R.fromSomeSEXP res :: [String]
              if null . drop 1 $ pathNames
                  then return Nothing
                  else
                    return . Just . T.intercalate "/" . fmap T.pack $ pathNames