R/write.bedgraph.R

#' Write bedgraph
#'
#' DF in, bg written
#'
#' @param df A dataframe with bedgraph-like first four columns
#' @param path Full path for output
#' @param chromSizesPath The path to a chrom.sizes file
#' @param chr Prepend chr to chromosome-names
#' @param NAnum Number to replace NA's with
#' @param InfCoef Number to multiply the min/max with for infs
#' @return A genomicRanges-object
#' @export
write.bedgraph <- function(df, path, chromSizesPath, chr = T, keepMeta = T, NAnum = 0, InfCoef = 100){
  require(data.table)
  chromSizes <- data.table::fread(chromSizesPath, data.table = F)
  
  
  for(chromosome in unique(df[,1])){
    currChromDF <- df[df[,1] == chromosome,]
    chromSize <- chromSizes[chromSizes[,1] == chromosome,2]
    
    bigger <- currChromDF[,3] > chromSize
    currChromDF[bigger,3] <- chromSize
    df[df[,1] == chromosome,]  <-  currChromDF
  }
  
  df[is.na(df[,4]),4] <- NAnum
  
  df[df[,4] == -Inf,] <-  min(df[is.finite(df[,4]),4] )*InfCoef
  df[df[,4] == Inf,] <-  max(df[is.finite(df[,4]),4] )*InfCoef
  
  # Sort
  o = order(df[, 1])
  bed = df[o,]
  
  od = 1:ncol(bed)
  chroms <- unique(df[,1])
  for (i in chroms)
  {
    chr.bed = bed[bed[, 1]==i,]
    o = order (as.integer(as.character(chr.bed[,2])))
    od = rbind (od, chr.bed[o, ])
  }
  od = od [-1,]
  colnames (od)= colnames(bed)
  
  od <- od[!is.na(od[,4]),]
  options(scipen = 99999999)
  write.table(od, file = path, quote = F, sep = "\t", row.names = F, col.names = F)
  
  invisible(od)
  
}
robinweide/RHWlib documentation built on May 7, 2019, 8:03 a.m.