
# do a single conditional independence test.
ci.test = function(x, y, z, data, test, ...) {

  if (missing(x))
    stop("one or both of the variables to test are missing.")

  # normalize the arguments into a data set with the tested variables in the
  # first two columns and the conditioning variables in the following ones.
  if (is.string(x)) {

    args = check.ci.test.character(x = x, y = y, z = z, data = data)

  }#THEN
  else if (is.data.frame(x)) {

    if (!missing(y) || !missing(z))
      warning("'y' and 'z' will be ignored.")
    if (!missing(data))
      warning("'data' will be ignored.")
    if (ncol(x) < 2)
      stop("'x' must have at least two columns.")

    nodes = names(x)
    args = check.ci.test.character(x = nodes[1], y = nodes[2],
             z = nodes[-(1:2)], data = x)

  }#THEN
  else if (is.vector(x)) {

    if (!missing(data))
      warning("'data' will be ignored.")

    args = check.ci.test.vector(x = x, y = y, z = z,
             xlab = deparse(substitute(x)), ylab = deparse(substitute(y)),
             zlab = deparse(substitute(z)))

  }#THEN
  else {

    stop("x must be either a factor object, a numeric vector, a character string or a data frame.")

  }#ELSE

  return(ci.test.backend(data = args$data, test = test,
           extra.args = list(...), data.name = args$data.name))

}#CI.TEST

# compute a single conditional independence test between the first two columns
# of the data, conditioning on the remaining ones, and assemble the htest.
ci.test.backend = function(data, test, extra.args, data.name) {

  # check the test label and its optional arguments.
  test = check.test(test, data = data)
  extra.args = check.test.args(test = test, extra.args = extra.args, data = data)

  # the tested variables are in columns 1 and 2, the conditioning ones follow.
  nodes = names(data)

  # create the htest object.
  htest = indep.test(x = nodes[1], y = nodes[2], sx = nodes[-(1:2)],
            data = data, test = test, extra.args = extra.args, alpha = 1,
            learning = FALSE)
  htest$method = test.labels[test]
  htest$data.name = data.name

  return(htest)

}#CI.TEST.BACKEND
