Skip to content

Commit

Permalink
ScoreMatrixBin
Browse files Browse the repository at this point in the history
  • Loading branch information
katwre committed May 18, 2015
1 parent 2fdfebc commit 8a53824
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
8 changes: 4 additions & 4 deletions R/scoreMatrix.R
Expand Up @@ -75,7 +75,7 @@ readBam = function(target, windows, rpm=FALSE,
}
}else{
if(class(param) == 'ScanBamParam'){
bamWhich(param) <- reduce(windows, ignore.strand=TRUE) #TODO?
bamWhich(param) <- reduce(windows, ignore.strand=TRUE)
}else{
stop('param needs to be an object of class ScanBamParam')
}
Expand All @@ -85,7 +85,7 @@ readBam = function(target, windows, rpm=FALSE,

alnpairs <- readGAlignmentPairs(target, param=param, use.names=TRUE)
# remove duplicate rows
#e.g. when every read from pair maps into two different windows, then the pair is duplicated in alnpairs
#e.g. when each read from pair maps into two different windows, then the pair is duplicated in alnpairs
uniq.names.alnpairs <- unique(names(alnpairs))
ap <- GAlignmentPairs(names=names(alnpairs[1]), first=first(alnpairs[1]), last=last(alnpairs[1]), isProperPair=TRUE)
for(i in 2:length(uniq.names.alnpairs)){
Expand Down Expand Up @@ -355,8 +355,8 @@ setMethod("ScoreMatrix",signature("character","GRanges"),

if(type == 'bam')
covs = readBam(target, windows, rpm=rpm, unique=unique,
extend=extend, param=param, paired.end=bam.paired.end,
stranded=stranded)
extend=extend, param=param,
paired.end=bam.paired.end, stranded=stranded)
if(type == 'bigWig')
covs = readBigWig(target=target, windows=windows)

Expand Down
19 changes: 14 additions & 5 deletions R/scoreMatrixBin.R
Expand Up @@ -143,7 +143,10 @@ summarizeViewsRle = function(my.vList, windows, bin.op, bin.num, strand.aware){
#' based on chr, start, end and strand
#' @param extend numeric which tells the function to extend the reads to width=extend
#' @param param ScanBamParam object
#'
#' @param bam.paired.end boolean indicating whether given BAM file contains paired-end reads (default:FALSE).
#' Paired-reads will be treated as fragments.
#' @param stranded boolean which tells whether given BAM file is from a strand-specific protocol (default:TRUE). If FALSE then
#' strands of reads will be set up to "*".
#'
#' @return returns a \code{scoreMatrix} object
#'
Expand Down Expand Up @@ -173,7 +176,9 @@ setGeneric("ScoreMatrixBin",
rpm=FALSE,
unique=FALSE,
extend=0,
param=NULL
param=NULL,
bam.paired.end=FALSE,
stranded=TRUE
)
standardGeneric("ScoreMatrixBin") )

Expand Down Expand Up @@ -246,11 +251,14 @@ setMethod("ScoreMatrixBin",signature("GRanges","GRanges"),
# ---------------------------------------------------------------------------- #
#' @aliases ScoreMatrixBin,character,GRanges-method
#' @rdname ScoreMatrixBin-methods
#' @usage \\S4method{ScoreMatrixBin}{character,GRanges}(target, windows, bin.num=10, bin.op='mean', strand.aware, type, rpm, unique, extend, param)
#' @usage \\S4method{ScoreMatrixBin}{character,GRanges}(target, windows, bin.num=10, bin.op='mean',
#' strand.aware, type, rpm, unique, extend, param,
#' bam.paired.end=FALSE, stranded=TRUE)
setMethod("ScoreMatrixBin",signature("character","GRanges"),
function(target, windows, bin.num=10,
bin.op='mean', strand.aware,
type, rpm, unique, extend, param){
type, rpm, unique, extend, param,
bam.paired.end=FALSE, stranded=TRUE){

if(!file.exists(target)){
stop("Indicated 'target' file does not exist\n")
Expand All @@ -267,7 +275,8 @@ setMethod("ScoreMatrixBin",signature("character","GRanges"),

if(type == 'bam')
covs = readBam(target, windows, rpm=rpm, unique=unique,
extend=extend, param=param)
extend=extend, param=param,
paired.end=bam.paired.end, stranded=stranded)
if(type == 'bigWig')
covs = readBigWig(target=target, windows=windows)

Expand Down
14 changes: 12 additions & 2 deletions inst/unitTests/test_ScoreMatrixBin.R
Expand Up @@ -100,7 +100,11 @@ test_ScoreMatrixBin_character_GRanges = function()
strand=c('-', '-', '-', '-', '+', '-', '+', '-', '-', '-', '-', '-', '-', '+'))
windows = GRanges(rep(c(1,2),each=2), IRanges(rep(c(1,2), times=2), width=5),
strand=c('-','+','-','+'))

target.paired.end = GRanges(rep(1,each=7),
IRanges(c(1,1,2,3,7,8,9), width=c(19, 19, 19, 19, 16, 16, 16)),
strand=rep("+", times=7))
windows.paired.end = GRanges(rep(c(1),each=4), IRanges(c(7,8,20, 21), width=10),
strand=c('+','+','+','+'))
# -----------------------------------------------#
# usage
# bam file
Expand All @@ -121,10 +125,16 @@ test_ScoreMatrixBin_character_GRanges = function()
checkEquals(s3,m3)

#bam file, rpm=FALSE, unique=TRUE, extend=1
s4 = ScoreMatrixBin(bam.file, windows, type='bam',bin.num=2, unique=T, extend=1)
s4 = ScoreMatrixBin(bam.file, windows, type='bam', bin.num=2, unique=T, extend=1)
m4 = ScoreMatrixBin(resize(unique(target), width=1), windows, bin.num=2)
checkEquals(s4,m4)

# bam file with paired-end reads, rpm=FALSE, unique=TRUE, extend=16
bam.pe.file = system.file('unitTests/test_pairedend.bam', package='genomation')
s5 = ScoreMatrixBin(bam.pe.file, windows.paired.end, type='bam', bam.paired.end=TRUE, unique=TRUE, extend=16)
m5 = ScoreMatrixBin(resize(unique(target.paired.end), width=16), windows.paired.end)
checkEquals(s5,m5)

#bigWig file - missing

# -----------------------------------------------#
Expand Down
13 changes: 11 additions & 2 deletions man/ScoreMatrixBin-methods.Rd
Expand Up @@ -10,13 +10,16 @@
\usage{
ScoreMatrixBin(target, windows, bin.num = 10, bin.op = "mean",
strand.aware = FALSE, weight.col = NULL, is.noCovNA = FALSE,
type = "", rpm = FALSE, unique = FALSE, extend = 0, param = NULL)
type = "", rpm = FALSE, unique = FALSE, extend = 0, param = NULL,
bam.paired.end = FALSE, stranded = TRUE)

\\S4method{ScoreMatrixBin}{RleList,GRanges}(target, windows, bin.num, bin.op, strand.aware)

\\S4method{ScoreMatrixBin}{GRanges,GRanges}(target,windows,bin.num,bin.op,strand.aware,weight.col,is.noCovNA)

\\S4method{ScoreMatrixBin}{character,GRanges}(target, windows, bin.num=10, bin.op='mean', strand.aware, type, rpm, unique, extend, param)
\\S4method{ScoreMatrixBin}{character,GRanges}(target, windows, bin.num=10, bin.op='mean',
strand.aware, type, rpm, unique, extend, param,
bam.paired.end=FALSE, stranded=TRUE)
}
\arguments{
\item{target}{\code{RleList}, \code{GRanges}, BAM file or a bigWig file
Expand Down Expand Up @@ -63,6 +66,12 @@ based on chr, start, end and strand}
\item{extend}{numeric which tells the function to extend the reads to width=extend}

\item{param}{ScanBamParam object}

\item{bam.paired.end}{boolean indicating whether given BAM file contains paired-end reads (default:FALSE).
Paired-reads will be treated as fragments.}

\item{stranded}{boolean which tells whether given BAM file is from a strand-specific protocol (default:TRUE). If FALSE then
strands of reads will be set up to "*".}
}
\value{
returns a \code{scoreMatrix} object
Expand Down

0 comments on commit 8a53824

Please sign in to comment.