I want to create a blank GRanges (from package GenomicRanges) object dynamicall.
If we have two alternative options, opt1 and opt2, we can write statically as:
library(GenomicRanges)
GRanges(seqnames=NULL,ranges=NULL,strand=NULL, opt1=NULL, opt2=NULL)
The question is "How can we create the GRanges object dynamically?". Or more specifically, "Is the following functionXX possible?" :
opts=c('opt1','opt2','opt3')
#' dynamic create blank GRanges with optional fields
#' @param opts, a vector containing the colnames of "mcols" of GRanges object
#' @return blank GRanges object
functionXX<-function(opts){
//todo:
}
Thanks!
Only the column names is not enough, every fields should have the defined data types.
Below is the solution:
opts=c('opt1','opt2','opt3')
tp=list(character(),character(),character())
functionXXX<-function(opts,tp){
names(tp)=opts
df=do.call(data.frame,tp)
gr=GRanges(c(seqnames=NULL,ranges=NULL,strand=NULL))
mcols(gr)=df
gr
}
functionXXX(opts,tp)
#GRanges object with 0 ranges and 3 metadata columns:
# seqnames ranges strand | opt1 opt2 opt3
# <Rle> <IRanges> <Rle> | <character> <character> <character>
#-------
#seqinfo: no sequences