R/dwi2fod.R

Defines functions dwi2fod dwi2fod_with_response

Documented in dwi2fod dwi2fod_with_response

#' Estimate fibre orientation distributions
#' from diffusion data using spherical deconvolution
#'
#' @param infile the input DWI image containing volumes
#' that are both diffusion weighted and b=0
#' @param response response file, usually generated by \code{\link{dwi2response}}
#' @param algorithm the algorithm to use for fibre orientation distributions estimation
#' @param bvecs The file containing the b-vectors.
#' @param bvals The file containing the b-values.
#' @param scheme The file containing a 4xT text file,
#' instead of bvals/bvecs
#' @param mask initial mask for response voxel selection
#' @param lmax The maximum harmonic degree(s) of response function
#' estimation (single value for single-shell response,
#' comma-separated values for multi-shell response)
#' @param shells The b-value shell(s) to use in response
#' function estimation (single value for single-shell response,
#' comma-separated values for multi-shell response)
#' @param outfile Output response file
#' @param opts Additional options to pass
#' @param verbose Print diagnostic output
#' @param ... arguments to pass to \code{\link{std_opts}}
#'
#' @return Output filename
#' @export
#'
#' @importFrom neurobase checkimg check_outfile
#' @examples \dontrun{
#' infile = "~/Downloads/data.nii.gz"
#' bvals = "~/Downloads/bvals"
#' bvecs = "~/Downloads/bvecs"
#' response = dwi2response(infile, bvals = bvals, bvecs = bvecs)
#' fod = dwi2fod(infile, response = response,
#' bvals = bvals, bvecs = bvecs,
#' algorithm = "csd")
#' fod_make_resp = dwi2fod_with_response(infile = infile,
#' bvals = bvals, bvecs = bvecs)
#'
#' }
dwi2fod = function(
  infile,
  response,
  algorithm = c("csd", "msmt_csd"),
  bvals = NULL,
  bvecs = NULL,
  scheme = NULL,
  mask = NULL,
  lmax = NULL,
  shells = NULL,
  outfile = NULL,
  opts = "",
  verbose = TRUE,
  ...
) {

  cmd = "dwi2fod"
  cmd = system.file("bin", cmd, package = "mrtrix")

  infile = checkimg(infile)
  algorithm = match.arg(algorithm)

  outfile = check_outfile(
    outfile = outfile,
    retimg = TRUE)

  scheme_opts = scheme_or_bvals(
    bvals = bvals,
    bvecs = bvecs,
    scheme = scheme)
  opts = c(opts, scheme_opts)

  if (!is.null(mask)) {
    mask = checkimg(mask)
    opts = c(opts, paste0("-mask ", mask))
  }

  if (!is.null(lmax)) {
    lmax = paste(lmax, collapse = ",")
    opts = c(opts, paste0("-lmax ", lmax))
  }

  if (!is.null(shells)) {
    shells = paste(shells, collapse = ",")
    opts = c(opts, paste0("-shell ", shells))
  }

  opts = std_opts(opts = opts,
                  verbose = verbose,
                  use_tempdir = FALSE, # no tempdir available
                  ...)

  opts = paste(opts, collapse = " ")

  cmd = paste(cmd, algorithm, opts,
              infile, response,outfile)
  if (verbose) {
    message(cmd)
  }
  res = system(cmd)
  if (res != 0) {
    warning("Result not zero, may be a problem!")
  }
  return(outfile)
}


#' @export
#' @rdname dwi2fod
#' @importFrom methods formalArgs
dwi2fod_with_response = function(
  infile,
  ...
) {

  args = list(...)
  args$infile = infile

  nargs = names(args)
  response_args = formalArgs(dwi2response)
  response_args = args[ nargs %in% response_args ]
  response = do.call(dwi2response, args = response_args)

  args$response = response
  nargs = names(args)
  fod_args = formalArgs(dwi2fod)
  fod_args = args[ nargs %in% fod_args ]
  ret = do.call(dwi2fod, args = response_args)
  return(ret)
}
muschellij2/mrtrix documentation built on July 9, 2019, 9:15 a.m.