R/single_network.R

Defines functions single_network

Documented in single_network

#' @title Construct network for single target gene
#'
#' @inheritParams inferCSN
#' @param matrix An expression matrix.
#' @param target The target gene.
#'
#' @return A data frame of the single target gene network.
#'  The data frame has three columns:
#'  \item{regulator}{The regulator genes.}
#'  \item{target}{The target gene.}
#'  \item{weight}{The weight of the regulator gene on the target gene.}
#' @export
#' @examples
#' data("example_matrix")
#' head(
#'   single_network(
#'     example_matrix,
#'     regulators = colnames(example_matrix),
#'     target = "g1"
#'   )
#' )
#' head(
#'   single_network(
#'     example_matrix,
#'     regulators = colnames(example_matrix),
#'     target = "g1",
#'     cross_validation = TRUE
#'   )
#' )
#'
#' single_network(
#'   example_matrix,
#'   regulators = c("g1", "g2", "g3"),
#'   target = "g1"
#' )
#' single_network(
#'   example_matrix,
#'   regulators = c("g1", "g2"),
#'   target = "g1"
#' )
single_network <- function(
    matrix,
    regulators,
    target,
    cross_validation = FALSE,
    seed = 1,
    penalty = "L0",
    r_squared_threshold = 0,
    n_folds = 5,
    verbose = TRUE,
    ...) {
  regulators <- setdiff(regulators, target)
  if (length(regulators) < 2) {
    log_message(
      "less than 2 regulators found while modeling: ", target,
      message_type = "warning",
      verbose = verbose
    )
    return()
  }
  x <- matrix[, regulators]
  y <- matrix[, target]

  result <- fit_srm(
    x, y,
    cross_validation = cross_validation,
    seed = seed,
    penalty = penalty,
    n_folds = n_folds,
    verbose = verbose,
    ...
  )

  r_squared <- result$metrics$r_squared
  if (r_squared > r_squared_threshold) {
    coefficients <- result$coefficients$coefficient |>
      normalization(
        method = "unit_vector",
        ...
      )

    if (length(coefficients) != ncol(x)) {
      coefficients <- rep(0, ncol(x))
    }
  } else {
    coefficients <- rep(0, ncol(x))
  }

  return(
    data.frame(
      regulator = regulators,
      target = target,
      weight = coefficients
    )
  )
}

Try the inferCSN package in your browser

Any scripts or data that you put into this service are public.

inferCSN documentation built on April 13, 2025, 5:11 p.m.