R/GsnapParam-class.R
b22faf0d
 ### =========================================================================
 ### GsnapParam class
 ### -------------------------------------------------------------------------
 ###
 ### High-level interface to gsnap. As a complex operation, we need a
 ### formal parameter object. It should only formally represent the
 ### most commonly used parameters. The rest fall into the 'extra'
 ### list.
 ###
 
 setClass("GsnapParam",
e1b17d35
          representation(max_mismatches = "integer_OR_NULL",
b22faf0d
                         suboptimal_levels = "integer",
                         novelsplicing = "logical",
e1b17d35
                         splicing = "character_OR_NULL",
28398387
                         terminal_threshold = "integer",
119984f9
                         gmap_mode = "character",
ead94b30
                         clip_overlap = "logical"),
          contains="GmapAlignerParam")
b22faf0d
 
 ### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ### Accessors
 ###
 
 gsnap_part <- function(x) {
   x@part
 }
 gsnap_batch <- function(x) {
   x@batch
 }
 gsnap_max_mismatches <- function(x) {
   x@max_mismatches
 }
 gsnap_suboptimal_levels <- function(x) {
   x@suboptimal_levels
 }
 gsnap_use_snps <- function(x) {
   x@use_snps
 }
 gsnap_snpsdir <- function(x) {
   x@snpsdir
 }
 gsnap_mode <- function(x) {
   x@mode
 }
 gsnap_nthreads <- function(x) {
   x@nthreads
 }
 gsnap_novelsplicing <- function(x) {
   x@novelsplicing
 }
 gsnap_splicing <- function(x) {
   x@splicing
 }
 gsnap_npaths <- function(x) {
   x@npaths
 }
 gsnap_quiet_if_excessive <- function(x) {
   x@quiet_if_excessive
 }
 gsnap_nofails <- function(x) {
   x@nofails
 }
 gsnap_split_output <- function(x) {
   x@split_output
 }
 gsnap_extra <- function(x) {
   x@extra
 }
 
 `gsnap_part<-` <- function(x, value) {
   x@part <- value
   x
 }
 `gsnap_batch<-` <- function(x, value) {
   x@batch <- value
   x
 }
 `gsnap_max_mismatches<-` <- function(x, value) {
   x@max_mismatches <- value
   x
 }
 `gsnap_suboptimal_levels<-` <- function(x, value) {
   x@suboptimal_levels <- value
   x
 }
 `gsnap_use_snps<-` <- function(x, value) {
   x@use_snps <- value
   x
 }
 `gsnap_snpsdir<-` <- function(x, value) {
   x@snpsdir <- value
   x
 }
 `gsnap_mode<-` <- function(x, value) {
   x@mode <- value
   x
 }
 `gsnap_nthreads<-` <- function(x, value) {
   x@nthreads <- value
   x
 }
 `gsnap_novelsplicing<-` <- function(x, value) {
   x@novelsplicing <- value
   x
 }
 `gsnap_splicing<-` <- function(x, value) {
   x@splicing <- value
   x
 }
 `gsnap_npaths<-` <- function(x, value) {
   x@npaths <- value
   x
 }
 `gsnap_quiet_if_excessive<-` <- function(x, value) {
   x@quiet_if_excessive <- value
   x
 }
 `gsnap_nofails<-` <- function(x, value) {
   x@nofails <- value
   x
 }
 `gsnap_split_output<-` <- function(x, value) {
   x@split_output <- value
   x
 }
 `gsnap_extra<-` <- function(x, value) {
   x@extra <- value
   x
 }
 
 ## etc..
 
 ### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ### Constructor
 ###
 
ead94b30
 newGmapAlignerParam <- function(Class, genome, snps) {
     if (missing(genome))
         stop("The 'genome' must be specified (and coercible to GmapGenome)")
     args <- formals(sys.function(sys.parent(1L)))
     params <- mget(names(args), parent.frame())
     params$unique_only <- NULL
     paramClasses <- getSlots(Class)
     paramClasses <- paramClasses[setdiff(names(paramClasses),
                                          c("extra", "snps"))]
     params <- mapply(as, params[names(paramClasses)], paramClasses,
                      SIMPLIFY = FALSE)
     if (!is.null(snps)) {
         if (!is(snps, "GmapSnps")) {
             snps <- GmapSnps(snps, genome)
         }
         params$snps <- snps
     }
     params$extra <- evalq(list(...), parent.frame())
     do.call(new, c(Class, params))
 }
 
28398387
 GsnapParam <- function(genome, unique_only = FALSE, molecule = c("RNA", "DNA"),
b22faf0d
                        max_mismatches = NULL,
                        suboptimal_levels = 0L, mode = "standard",
                        snps = NULL,
                        npaths = if (unique_only) 1L else 100L,
                        quiet_if_excessive = unique_only, nofails = unique_only,
                        split_output = !unique_only,
fb3917e8
                        novelsplicing = FALSE, splicing = NULL, 
28398387
                        nthreads = 1L, part = NULL, batch = "2",
ead94b30
                        terminal_threshold =
                            if (molecule == "DNA") 1000L else 2L,
                        gmap_mode = if (molecule == "DNA") "none"
                                    else "pairsearch,terminal,improve",
0379507d
                        clip_overlap = FALSE, ...)
119984f9
 {
28398387
   molecule <- match.arg(molecule)
ead94b30
   newGmapAlignerParam("GsnapParam", genome, snps)
b22faf0d
 }
 
 ### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ### Coercion
 ###
 
ead94b30
 GmapAlignerParam_asList <- function(from) {
b22faf0d
   to <- lapply(slotNames(from), slot, object = from)
   names(to) <- slotNames(from)
ead94b30
   to$split_output <- if (to$split_output) tolower(sub("Param", "", class(from)))
b22faf0d
   to$db <- genome(to$genome)
   to$dir <- path(directory(to$genome))
   to$genome <- NULL
e2b4bdb1
   to$use_snps <- name(to$snps)
b22faf0d
   to$snpsdir <- path(directory(to$snps))
e8f9c3a7
   extras <- to$extra
   to <- c(to, extras)
ead94b30
   to$extra <- NULL  
b22faf0d
   to
ead94b30
 }
 
 setAs("GsnapParam", "list", function(from) {
           to <- GmapAlignerParam_asList(from)
           to$novelsplicing <- as.integer(to$novelsplicing)
           to <- rename(to, splicing = "use_splicing")
           to
       })
b22faf0d
 
f053c5f3
 as.list.GmapAlignerParam <- function(x, ...) as(x, "list")
b22faf0d
 
e1b17d35
 setAs("ANY", "character_OR_NULL", function(from) {
9facdd9a
   if (is.null(from))
     NULL
   else as.character(from)
 })
e1b17d35
 setAs("ANY", "integer_OR_NULL", function(from) {
9facdd9a
   if (is.null(from))
     NULL
   else as.integer(from)
 })
 
 
b22faf0d
 ### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ### Show
 ###
 
ead94b30
 setMethod("show", "GmapAlignerParam", function(object) {
b22faf0d
   slots <- lapply(slotNames(object), slot, object = object)
   names(slots) <- slotNames(object)
   slots$genome <- paste0(slots$genome@name,
                          " (", path(directory(slots$genome)), ")")
   if (!is.null(slots$snps)) {
     slots$snps <- paste0(name(slots$snps),
                          " (", path(directory(slots$snps)), ")")
   }
ead94b30
   cat("A ", class(object), " object\n",
b22faf0d
       paste0(names(slots), ": ", slots, collapse = "\n"), "\n", sep = "")
 })