R/dramms.R

Defines functions dramms

Documented in dramms

#' @title Run DRAMMS
#'
#' @description Runs DRAMMS on source and target
#' @param source Filename (or nifti) to match to target
#' @param target Filename (or nifti) to match source to to target
#' @param outfile Output filename
#' @param outdef output deformation field
#' @param retimg return nifti object versus output image
#' @param opts Extra arguments to pass to \code{dramms}
#' @param verbose (logical) print out command before running 
#' @importFrom neurobase checkimg check_outfile
#' @importFrom oro.nifti readNIfTI
#' @importFrom fslr flirt
#' 
#' @export
#' @return If \code{retimg = TRUE}, a \code{nifti} object.  
#' If \code{retimg = FALSE}, a the filename of the output image.
#' @examples \dontrun{
#' require(fslr)
#' temp = file.path(fsldir(), "data", "standard", "MNI152_T1_1mm.nii.gz")
#' # get file
#' #  https://github.com/muschellij2/FSLR_Data/raw/master/01-Baseline_T1.nii.gz
#' brain = file.path(fsldir(), "data", "standard", "MNI152_T1_1mm_brain.nii.gz")
#' res = dramms(source = brain, target = temp, retimg = TRUE)
#' }
dramms <- function(
  source, # Filename (or nifti) to match to target
  target, # Filename (or nifti) to match source to to target
  outfile = NULL, # Output filename
  outdef = NULL, # output deformation field
  retimg = FALSE, # return nifti object versus output image
  opts = NULL,
  verbose = TRUE
  ){
  source = checkimg(source)
  target = checkimg(target)
  outfile = check_outfile(outfile = outfile, retimg = retimg, fileext = ".nii.gz")
  
  args = c("--source" = source, 
           "--target" = target,
           "--outimg" = outfile,
           "--outdef" = outdef,
           opts)
  cmd = "dramms"
  cmd = dramms_cmd_maker(cmd = cmd, args = args)

  if (verbose) {
    cat(cmd, "\n")
  }  
  res = system(cmd)
  
  if (res != 0) {
    stop("DRAMMS command failed")
  }
  
  if (retimg) {
    img = readNIfTI(outfile, reorient = FALSE)
    return(img)
  }
  return(outfile)
}
muschellij2/drammsr documentation built on Jan. 18, 2021, 11:24 a.m.