diff --git a/R/nearest-methods.R b/R/nearest-methods.R index baf0deb..024961a 100644 --- a/R/nearest-methods.R +++ b/R/nearest-methods.R @@ -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, @@ -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, @@ -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, @@ -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") { @@ -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) } ) @@ -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) } ) @@ -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) } } diff --git a/man/nearest-methods.Rd b/man/nearest-methods.Rd index f3564b9..bfd88d4 100644 --- a/man/nearest-methods.Rd +++ b/man/nearest-methods.Rd @@ -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, ...) }