I have a granges object that I'm trying to export as a BigWig. The object looks like this:
> prediction
GRanges object with 4127167 ranges and 1 metadata column:
seqnames ranges strand | score
<Rle> <IRanges> <Rle> | <numeric>
[1] chr1 5201-5400 * | 0.704062535636186
[2] chr1 5401-5600 * | 0.7038285233713
[3] chr1 5601-5800 * | 0.703379269216662
[4] chr1 5801-6000 * | 0.703339211770708
[5] chr1 6001-6200 * | 0.703186993302055
... ... ... ... . ...
[4127163] chrX 155190601-155190800 * | 0.3657433841594
[4127164] chrX 155190801-155191000 * | 0.365798827160496
[4127165] chrX 155191001-155191200 * | 0.366063557897697
[4127166] chrX 155191201-155191400 * | 0.366739565616863
[4127167] chrX 155191401-155191600 * | 0.367196219763251
-------
seqinfo: 23 sequences from hg19 genome
> seqinfo(prediction)
Seqinfo object with 23 sequences from hg19 genome:
seqnames seqlengths isCircular genome
chr1 249250621 <NA> hg19
chr2 243199373 <NA> hg19
chr3 198022430 <NA> hg19
chr4 191154276 <NA> hg19
chr5 180915260 <NA> hg19
... ... ... ...
chr19 59128983 <NA> hg19
chr20 63025520 <NA> hg19
chr21 48129895 <NA> hg19
chr22 51304566 <NA> hg19
chrX 155270560 <NA> hg19
I have been using the export.bw
feature from the rtracklayer
R package in other cases successfully but for this case I run into an error that states:
> rtracklayer::export.bw(prediction, con = "file.bw")
Error in FUN(extractROWS(unlisted_X, IRanges(X_elt_start[i], X_elt_end[i])), :
Features cannot overlap. Note that WIG does not distinguish between strands - try exporting two tracks, one for each strand.
From what I see there aren't overlapping regions? All widths of the ranges are set at 200bps. Is there a way to check and remove/fix overlapping ranges?
EDIT: I've also tried to export by each chromosome split out and was returned a similar error:
prediction_by_chr <- split(prediction, f = seqnames(prediction))
lapply(seq_along(prediction_by_chr), function(x){
chr <- names(prediction_by_chr)[x]; message(chr)
rtracklayer::export.bw(prediction_by_chr[[chr]], con = "file.bw")
})
Error in FUN(extractROWS(unlisted_X, IRanges(X_elt_start[i], X_elt_end[i])), :
BigWig ranges cannot overlap
EDIT2: This solution seems to fix the issue by removing those overlaps:
hits <- findOverlaps(prediction, prediction)
overlaps <- hits[hits@from != hits@to]
o <- data.frame(seqnames = unlist(extractList(seqnames(prediction), overlaps)),
start = unlist(extractList(start(prediction), overlaps)),
end = unlist(extractList(end(prediction), overlaps)))
o_gr <- GenomicRanges::makeGRangesFromDataFrame(o)
prediction_fixed <- prediction[!(prediction %over% o_gr)]