How to set a negative coverage to ZERO
1
0
Entering edit mode
vanhzh • 0
@vanhzh-9993
Last seen 5.1 years ago

Dear all, 

I try to subtract one coverage from another and set the negative value in the result coverage object to 0 so that the coverage at each position would be a non-negative value.  I want all the negative values in <cov.gr2> object to be zero in this dummy example.

Thanks very much for your help!

Zhenhua Hu

library(GenomicRanges)
gr1 <- GRanges(seqnames = Rle(c("chr1", "chr2"), c(2,2)), ranges = IRanges(start = c(3,6,1,9), width = c(100, 200, 10, 40)))

cov.gr1 <- coverage(gr1)
cov.gr2 <-cov.gr1 * 0.5 - cov.gr1
cov.gr2
> cov.gr2
RleList of length 2
$chr1
numeric-Rle of length 205 with 4 runs
  Lengths:    2    3   97  103
  Values :    0 -0.5   -1 -0.5

$chr2
numeric-Rle of length 48 with 3 runs
  Lengths:    8    2   38
  Values : -0.5   -1 -0.5
genomicranges coverage • 1.5k views
ADD COMMENT
0
Entering edit mode
@martin-morgan-1513
Last seen 3 months ago
United States

Subset and replace as you might a regular vector

cov.gr2[cov.gr2 < 0] = 0
ADD COMMENT
0
Entering edit mode

Hi Martin Morgan,

Thank you very much for your help. This is exactly what I want!

ADD REPLY

Login before adding your answer.

Traffic: 584 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6