R/spacing.r

Defines functions interval_spacing

Documented in interval_spacing

#' Calculate interval spacing.
#'
#' Spacing for the first interval of each chromosome is undefined (`NA`). The
#' leading interval of an overlapping interval pair has a negative value.
#'
#' @param x [ivl_df]
#'
#' @return [ivl_df] with `.spacing` column.
#'
#' @family utilities
#'
#' @examples
#' x <- tibble::tribble(
#'   ~chrom, ~start, ~end,
#'   "chr1", 1,      100,
#'   "chr1", 150,    200,
#'   "chr2", 200,    300
#' )
#'
#' interval_spacing(x)
#'
#' @export
interval_spacing <- function(x) {
  x <- check_interval(x)

  res <- bed_sort(x)

  gx <- groups(x)

  res <- group_by(res, chrom)
  res <- mutate(res, .spacing = start - lag(end))

  res <- group_by(res, !!!gx)

  res
}
jayhesselberth/valr documentation built on April 24, 2024, 7:15 a.m.