Skip to content

Commit

Permalink
add 'algorithm' arg to "nearest" and "distanceToNearest" methods
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Dec 5, 2014
1 parent c87572d commit 63e8013
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
47 changes: 27 additions & 20 deletions R/nearest-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
###

setMethod("precede", c("GenomicRanges", "GenomicRanges"),
function(x, subject, select = c("arbitrary", "all"), ignore.strand=FALSE)
function(x, subject, select=c("arbitrary", "all"), ignore.strand=FALSE)
{
select <- match.arg(select)
.GenomicRanges_findPrecedeFollow(x, subject, select, ignore.strand,
Expand All @@ -182,7 +182,7 @@ setMethod("precede", c("GenomicRanges", "GenomicRanges"),
)

setMethod("precede", c("GenomicRanges", "missing"),
function(x, subject, select = c("arbitrary", "all"), ignore.strand=FALSE)
function(x, subject, select=c("arbitrary", "all"), ignore.strand=FALSE)
{
select <- match.arg(select)
.GenomicRanges_findPrecedeFollow(x, subject, select, ignore.strand,
Expand All @@ -191,7 +191,7 @@ setMethod("precede", c("GenomicRanges", "missing"),
)

setMethod("follow", c("GenomicRanges", "GenomicRanges"),
function(x, subject, select = c("arbitrary", "all"), ignore.strand=FALSE)
function(x, subject, select=c("arbitrary", "all"), ignore.strand=FALSE)
{
select <- match.arg(select)
.GenomicRanges_findPrecedeFollow(x, subject, select, ignore.strand,
Expand Down Expand Up @@ -220,16 +220,16 @@ setMethod("follow", c("GenomicRanges", "missing"),
m
}

.nearest <- function(x, subject, select, ignore.strand, ignoreSelf=FALSE)
.nearest <- function(x, subject, select, algorithm,
ignore.strand, ignoreSelf=FALSE)
{
## overlapping ranges
if (ignoreSelf) {
ol <- findOverlaps(x,
select=select, ignore.strand=ignore.strand,
ignoreSelf=TRUE)
ol <- findOverlaps(x, select=select, algorithm=algorithm,
ignore.strand=ignore.strand, ignoreSelf=TRUE)
} else {
ol <- findOverlaps(x, subject,
select=select, ignore.strand=ignore.strand)
ol <- findOverlaps(x, subject, select=select, algorithm=algorithm,
ignore.strand=ignore.strand)
}

if (select == "all") {
Expand Down Expand Up @@ -295,19 +295,22 @@ setMethod("follow", c("GenomicRanges", "missing"),


setMethod("nearest", c("GenomicRanges", "GenomicRanges"),
function(x, subject, select=c("arbitrary", "all"), ignore.strand=FALSE)
function(x, subject, select=c("arbitrary", "all"),
algorithm=c("nclist", "intervaltree"), ignore.strand=FALSE)
{
select <- match.arg(select)
.nearest(x, subject, select=select, ignore.strand=ignore.strand)
.nearest(x, subject, select=select, algorithm=match.arg(algorithm),
ignore.strand=ignore.strand)
}
)

setMethod("nearest", c("GenomicRanges", "missing"),
function(x, subject, select=c("arbitrary", "all"), ignore.strand=FALSE)
function(x, subject, select=c("arbitrary", "all"),
algorithm=c("nclist", "intervaltree"), ignore.strand=FALSE)
{
select <- match.arg(select)
.nearest(x, x, select=select, ignore.strand=ignore.strand,
ignoreSelf=TRUE)
.nearest(x, x, select=select, algorithm=match.arg(algorithm),
ignore.strand=ignore.strand, ignoreSelf=TRUE)
}
)

Expand Down Expand Up @@ -339,17 +342,21 @@ setMethod("distance", c("GenomicRanges", "GenomicRanges"),
###

setMethod("distanceToNearest", c("GenomicRanges", "GenomicRanges"),
function(x, subject, ignore.strand=FALSE, ...)
function(x, subject, algorithm=c("nclist", "intervaltree"),
ignore.strand=FALSE, ...)
{
x_nearest <- nearest(x, subject, ignore.strand=ignore.strand, ...)
x_nearest <- nearest(x, subject, algorithm=match.arg(algorithm),
ignore.strand=ignore.strand, ...)
.distanceToNearest(x_nearest, x, subject, ignore.strand=ignore.strand)
}
)

setMethod("distanceToNearest", c("GenomicRanges", "missing"),
function(x, subject, ignore.strand=FALSE, ...)
function(x, subject, algorithm=c("nclist", "intervaltree"),
ignore.strand=FALSE, ...)
{
x_nearest <- nearest(x, ignore.strand=ignore.strand, ...)
x_nearest <- nearest(x, algorithm=match.arg(algorithm),
ignore.strand=ignore.strand, ...)
.distanceToNearest(x_nearest, x, x, ignore.strand=ignore.strand)
}
)
Expand All @@ -371,8 +378,8 @@ setMethod("distanceToNearest", c("GenomicRanges", "missing"),
subjectLength=length(subject),
distance=integer(0))
} else {
distance = distance(x[queryHits], subject[subjectHits],
ignore.strand=ignore.strand)
distance <- distance(x[queryHits], subject[subjectHits],
ignore.strand=ignore.strand)
Hits(queryHits, subjectHits, length(x), length(subject), distance)
}
}
Expand Down
14 changes: 9 additions & 5 deletions man/nearest-methods.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@
\S4method{follow}{GenomicRanges,GenomicRanges}(x, subject, select=c("arbitrary", "all"), ignore.strand=FALSE)
\S4method{follow}{GenomicRanges,missing}(x, subject, select=c("arbitrary", "all"), ignore.strand=FALSE)

\S4method{nearest}{GenomicRanges,GenomicRanges}(x, subject, select=c("arbitrary", "all"), ignore.strand=FALSE)
\S4method{nearest}{GenomicRanges,missing}(x, subject, select=c("arbitrary", "all"), ignore.strand=FALSE)

\S4method{distanceToNearest}{GenomicRanges,GenomicRanges}(x, subject, ignore.strand=FALSE, ...)
\S4method{distanceToNearest}{GenomicRanges,missing}(x, subject, ignore.strand=FALSE, ...)
\S4method{nearest}{GenomicRanges,GenomicRanges}(x, subject, select=c("arbitrary", "all"),
algorithm=c("nclist", "intervaltree"), ignore.strand=FALSE)
\S4method{nearest}{GenomicRanges,missing}(x, subject, select=c("arbitrary", "all"),
algorithm=c("nclist", "intervaltree"), ignore.strand=FALSE)

\S4method{distanceToNearest}{GenomicRanges,GenomicRanges}(x, subject, algorithm=c("nclist", "intervaltree"),
ignore.strand=FALSE, ...)
\S4method{distanceToNearest}{GenomicRanges,missing}(x, subject, algorithm=c("nclist", "intervaltree"),
ignore.strand=FALSE, ...)

\S4method{distance}{GenomicRanges,GenomicRanges}(x, y, ignore.strand=FALSE, ...)
}
Expand Down

0 comments on commit 63e8013

Please sign in to comment.