R/ants_apply_transforms.R

#' @name ants_apply_transforms-methods
#' @docType methods 
#' @aliases ants_apply_transforms 
#' @title Apply ANTs transforms to nifti images
#'
#' @description Wraps \code{\link{antsApplyTransforms}} but allows for 
#' \code{\link{nifti}} objects from the \code{oro.nifti} package
#' @param fixed image defining domain into which the moving image is transformed.
#' @param moving image to be mapped to fixed space.
#' @param transformlist character vector of transforms generated by 
#' \code{\link{antsRegistration}}
#' @param ret_ants if \code{TRUE}, the returned object is an \code{antsImage},
#' as opposed to a \code{nifti}
#' @param ... Arguments to be passed to \code{\link{antsApplyTransforms}}. 
#' @export 
#' @return Either \code{nifti} object or list of nifti objects
#' @seealso \code{\link{antsApplyTransforms}} 
#' @import methods
#' @author John Muschelli \email{muschellij2@@gmail.com}  
setGeneric("ants_apply_transforms", function(fixed, moving, transformlist, ...)  {
  standardGeneric("ants_apply_transforms")
})

#' @rdname ants_apply_transforms-methods
#' @aliases ants_apply_transforms,ANY,list,ANY-method
#' @export
setMethod("ants_apply_transforms", 
          signature("ANY", "list", "ANY"), 
          function(fixed, moving, transformlist, ret_ants = FALSE, ...) { 
            fixed = check_ants(fixed)
            moving = check_ants(moving)
            res = lapply(moving, ants_apply_transforms, 
                         fixed = fixed, transformlist = transformlist,
                         ... = ...)
            rm(list = c("fixed", "moving"))
            for (i in 1:10) {
              gc();
            }            
            return(res)
          })

#' @rdname ants_apply_transforms-methods
#' @aliases ants_apply_transforms,ANY,character,ANY-method
#' @export
setMethod("ants_apply_transforms", 
          signature("ANY", "character", "ANY"), 
          function(fixed, moving, transformlist, ret_ants = FALSE, ...) { 
            fixed = check_ants(fixed)
            moving = check_ants(moving)
            if (length(moving) > 1) {
              res = lapply(moving, ants_apply_transforms, 
                           fixed = fixed, transformlist = transformlist,
                           ... = ...)
            } else {
              res = ants_apply_transforms(fixed = fixed, 
                                          moving = moving, 
                                          transformlist = transformlist,
                                          ... = ...)
            }
            rm(list = c("fixed", "moving"))
            for (i in 1:10) {
              gc();
            }            
            return(res)
          })


#' @rdname ants_apply_transforms-methods
#' @aliases ants_apply_transforms,ANY,ANY,"ANY"-method
#' @export
setMethod("ants_apply_transforms", 
          signature("ANY", "ANY", "ANY"), 
          function(fixed, moving, transformlist, ret_ants = FALSE, ...) { 
            
            fixed = check_ants(fixed)
            moving = check_ants(moving)
            tclasses = sapply(transformlist, class)
            if (!all(tclasses %in% "character")) {
              transformlist = sapply(transformlist, function(x){
                if (is.nifti(x) | is.antsImage(x)) {
                  x = check_ants(x)
                }
                if (is.matrix(x)) {
                  x = write_transformlist(x)
                }
                x
              })
            }
            tclasses = sapply(transformlist, class)
            stopifnot(all(tclasses %in% "character"))
            ptype = ANTsRCore::pixeltype(moving)
            if (ptype %in% "unsigned char") {
              warning(paste0(
                "Moving had pixeltype unsigned char, cloning", 
                ", see https://github.com/ANTsX/ANTsRCore/issues/120")
              )
              moving = ANTsRCore::antsImageClone(
                moving, 
                out_pixeltype = "float")
            }
            moving_to_fixed = antsApplyTransforms(
              fixed = fixed, 
              moving = moving,
              transformlist = transformlist,
              ...)
            if (!is.antsImage(moving_to_fixed)) {
              warning("Output is not an antsImage object, results may be wrong")
            }
            if (!ret_ants) {
              moving_to_fixed = ants2oro(moving_to_fixed)
            }
            rm(list = c("fixed", "moving", "transformlist"))
            for (i in 1:10) {
              gc();
            }
            return(moving_to_fixed)            
          })
neuroconductor/extrantsr documentation built on Sept. 28, 2020, 11:31 a.m.