packages feed

convert-annotation-0.4.0.0: src/RGeneConvert.hs

{- RGeneConvert
Gregory W. Schwartz

Collections the functions pertaining to converting certain annotations into
other annotations using biomart from R
(http://bioconductor.org/packages/release/bioc/html/biomaRt.html).
-}

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

module RGeneConvert
    ( getRMart
    , toRGeneAnn
    ) 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

-- Local
import Types

-- | Get the R mapping of gene to gene.
getRMart :: R s (RMart s)
getRMart = fmap RMart
         $ [r| library(biomaRt)
               mart = useMart("ensembl", dataset="hsapiens_gene_ensembl")
           |]

-- | Get the R mapping of gene to gene.
toRGeneAnn :: RMart s -> RType -> UnknownAnn -> IO (Maybe Ann)
toRGeneAnn _ _ (UnknownAnn "")            = return Nothing
toRGeneAnn rMart (RType (!from, !to)) (UnknownAnn textQuery) =
    (fmap . fmap) Ann $ R.runRegion $ do
      let query = T.unpack textQuery
          mart  = unRMart rMart
      res <- [r| map = getBM( attributes = c(from_hs, to_hs)
                            , filters = from_hs
                            , values = c(query_hs)
                            , mart = mart_hs
                            , uniqueRows=FALSE
                            )
                as.character(map[1,2])
            |]

      let naCheck "NA" = Nothing
          naCheck x    = Just x
      return . fmap T.pack . naCheck $ (R.fromSomeSEXP res :: String)