A problem needs to be noted when using chunk fetching of BigWigFile. I am updating the NGSLib for this (1.1.12). An example wig file.
0 50 3
50 100 5
100 150 4
Since the bigwig file has bin size (for example 50bp), when you fetch all the items in a chunk of 50 but started from 30: 30 to 80, it returns all the overlapped bins: 0-50 and 50-100. then when you do fetch 80~130, you get 50-100 and 100-150. the bin 50-100 is double counted. You need to either remove the ones overlapped with the boundary in the later fetch, or modify the wig start and end positions accordingly.
The current solution is : (specify the end position instead of None.)
end = bw.sizes[bw.chroms.index[chrom]] # get the chromosome length.
def getwig(bw,chrom,start,end,chunksize=1000):
for cstart in xrange(start,end,chunksize):
cend = cstart + chunksize
if cend > stop: cend = end
for wig in wWigIO.getIntervals(self.infile,chrom,cstart,cend):
if wig[0] < cstart: wig[0] = cstart
if wig[1] > cend: wig[1] = cend
yield wig
Alternative solution: Instead of doing that, you may you BigWigFile.pileup function to get the all the depth from 30-80, then 80-130. this will yield a numpy object with chunksize each time, except the last one.
for cstart in xrange(start,end,chunksize):
cend = min (start+chunksize, end)
yield bw.pileup(chrom,cstart,cend)
Hope this helps.
I am not sure how the fetch method is implemented, but if it is a generator then you could yield from it. If it is not, then you can modify it to make it so using yield. Look here for some info http://stackoverflow.com/questions/9708902/in-practice-what-are-the-main-uses-for-the-new-yield-from-syntax-in-python-3#9709131