#' Write bed
#'
#' DF in, bed written
#'
#' @param df A dataframe with bed-like first three columns
#' @param path Full path for output
#' @param chromSizesPath The path to a chrom.sizes file
#' @param chr Prepend chr to chromosome-names
#' @param sort Do you want a sorted df?
#' @return A genomicRanges-object
#' @export
write.bed <- function(df, path, chromSizesPath, chr = T, sort = F){
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
df[bigger,3] <- chromSize
}
if(sort == T){
# 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)
options(scipen = 99999999)
write.table(od, file = path, quote = F, sep = "\t", row.names = F, col.names = F)
invisible(od)
} else {
write.table(df, file = path, quote = F, sep = "\t", row.names = F, col.names = F)
invisible(df)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.