R/tad2border.R

#' tad2border
#'
#' TAD-bed to border-Granges
#'
#' @param df A dataframe with bed-like three columns
#' @param chr Prepend chr to chromosome-names?
#' @param whichBorders Generate 5', 3' of both borders? [5,3,8]
#' @param padding Padding left and right of a border in bp.
#' @return A genomicRanges-object of borders
#' @export
tad2border <- function(df, chr = T, keepMeta = T, whichBorders = 8, padding = 10e3){
  require(GenomicRanges)
  colnames(df) <- paste0('V',1:ncol(df))
  df <- as.data.frame(df)

  B1 <- df[,c(1,2)]
  B1$start <- B1[,2] - padding
  B1$end <- B1[,2] + padding
  B1 <- B1[,-2]
  colnames(B1)[1:3] <- c('seqnames','start','end')

  B2 <- df[,c(1,3)]
  B2$start <- B2[,2] - padding
  B2$end <- B2[,2] + padding
  B2 <- B2[,-2]
  colnames(B2)[1:3] <- c('seqnames','start','end')

  borders <- NULL
  if(whichBorders == 5){
    borders <- B1
  } else if(whichBorders == 3){
    borders <- B2
  } else if(whichBorders == 8){
    borders <- rbind(B1,B2)
  }

  borders$seqnames <- gsub(borders$seqnames, pattern = '^chr', replacement = '')
  if(chr == T){
    borders$seqnames <- gsub(borders$seqnames, pattern = '^', replacement = 'chr')
  }

  GR <- GenomicRanges::makeGRangesFromDataFrame(borders)
  return(GR)
}
robinweide/RHWlib documentation built on May 7, 2019, 8:03 a.m.