Skip to content

Instantly share code, notes, and snippets.

@al2na
Last active August 29, 2015 14:17
Show Gist options
  • Save al2na/88b862bb5f5a09d626c5 to your computer and use it in GitHub Desktop.
Save al2na/88b862bb5f5a09d626c5 to your computer and use it in GitHub Desktop.
The tiling doesn't work in some cases where inferred chromosome length is smaller than win.size-step.size. This may fix that
setGeneric("tileMethylCounts3",
function(object,win.size=1000,step.size=1000,cov.bases=0)
standardGeneric("tileMethylCounts3") )
#' @aliases tileMethylCounts,methylRaw-method
#' @rdname tileMethylCounts-methods
setMethod("tileMethylCounts3", signature(object="methylRaw"),
function(object,win.size,step.size,cov.bases){
g.meth =as(object,"GRanges")
#chrs =IRanges::levels(seqnames(g.meth))
chrs =as.character(unique(seqnames(g.meth)))
#widths =seqlengths(g.meth) # this doesn't work with BioC 3.0
widths =sapply(chrs,function(x,y) max(end(y[seqnames(y)==x,])),g.meth )# lengths of max bp in each chr
all.wins=GRanges()
for(i in 1:length(chrs))
{
# get max length of feature covered chromosome
max.length=max(IRanges::end(g.meth[seqnames(g.meth)==chrs[i],]))
#get sliding windows with covered CpGs
numTiles=floor( (max.length-(win.size-step.size) )/step.size )+1
numTiles=ifelse(numTiles<1, 1,numTiles)
temp.wins=GRanges(seqnames=rep(chrs[i],numTiles),
ranges=IRanges(start=1+0:(numTiles-1)*step.size,
width=rep(win.size,numTiles)) )
all.wins=suppressWarnings(c(all.wins,temp.wins))
}
regionCounts(object,all.wins,cov.bases,strand.aware=FALSE)
}
)
#' @aliases tileMethylCounts,methylRawList-method
#' @rdname tileMethylCounts-methods
setMethod("tileMethylCounts3", signature(object="methylRawList"),
function(object,win.size,step.size,cov.bases){
new.list=lapply(object,tileMethylCounts3,win.size,step.size,cov.bases)
new("methylRawList", new.list,treatment=object@treatment)
})
#' @aliases tileMethylCounts,methylBase-method
#' @rdname tileMethylCounts-methods
setMethod("tileMethylCounts3", signature(object="methylBase"),
function(object,win.size,step.size,cov.bases){
g.meth =as(object,"GRanges")
#chrs =IRanges::levels(seqnames(g.meth))
chrs =as.character(unique(seqnames(g.meth)))
#widths =seqlengths(g.meth)
widths =sapply(chrs,function(x,y) max(end(y[seqnames(y)==x,])),g.meth )# lengths of max bp in each chr
all.wins=GRanges()
for(i in 1:length(chrs))
{
# get max length of feature covered chromosome
max.length=max(IRanges::end(g.meth[seqnames(g.meth)==chrs[i],]))
#get sliding windows with covered CpGs
numTiles=floor( (max.length-(win.size-step.size) )/step.size )+1
numTiles=ifelse(numTiles<1, 1,numTiles)
temp.wins=GRanges(seqnames=rep(chrs[i],numTiles),
ranges=IRanges(start=1+0:(numTiles-1)*step.size,
width=rep(win.size,numTiles)) )
all.wins=suppressWarnings(c(all.wins,temp.wins))
}
regionCounts(object,all.wins,cov.bases,strand.aware=FALSE)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment