R/bed2granges.R

Defines functions bed2granges

Documented in bed2granges

#' bed to GRanges
#'
#' This function converts bed3-like format object to a GRanges object
#' @param df bed3 like dataframe
#'
#' @import GenomicRanges
#' @importFrom IRanges IRanges
#'
#' @return a GRanges object
#' 
#' @keywords DMR Promoter GRanges
#' @export

bed2granges <- function(df){
    df <- df
    if(length(df) > 6){
        df <- df[,-c(7:length(df))]
    }
    if(length(df)<3){
        stop("File has less than 3 columns")
    }

    header <- c('chr','start','end','id','score','strand')
    names(df) <- header[1:length(names(df))]
    if('strand' %in% colnames(df)){
        df$strand <- gsub(pattern="[^+-]+", replacement = '*', x = df$strand)
    }

    #library("GenomicRanges")
    if(length(df)==3){
        gr <- with(df, GRanges(chr, IRanges(start, end)))
    } else if (length(df)==4){
        gr <- with(df, GRanges(chr, IRanges(start, end), id=id))
    } else if (length(df)==5){
        gr <- with(df, GRanges(chr, IRanges(start, end), id=id, score=score))
    } else if (length(df)==6){
        gr <- with(df, GRanges(chr, IRanges(start, end), id=id, score=score, strand=strand))
    }
    return(gr)
}
takahirosuzuki0626/mTFTarget documentation built on Jan. 4, 2022, 9:44 a.m.