forked from jimhester/GenomicRanges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGIntervalTree-class.R
175 lines (156 loc) · 4.75 KB
/
GIntervalTree-class.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#' GIntervalTree class
#'
#' Defines persistent interval trees for GRanges objects.
#'
#'
#' @name GIntervalTree-class
#' @family GIntervalTree
#'
#' @exportClass GIntervalTree
#' @import GenomicRanges
#' @import BiocGenerics
setClass("GIntervalTree",
contains="GenomicRanges",
representation(
ranges="IntervalForest",
rngidx="IRanges",
strand="Rle",
elementMetadata="DataFrame",
seqinfo="Seqinfo"),
prototype(
strand=Rle(strand()))
)
.valid.GIntervalTree.rngidx <- function(x) {
x_len <- length(x)
if (sum(width(x@rngidx)) != x_len)
return("ivalid 'rngidx' slot")
NULL
}
.valid.GIntervalTree.ranges <- function(x) {
if (class(x@ranges) != "IntervalForest")
return("'x@ranges' must be an IntervalForest instance")
if (nobj(x@ranges@partitioning) != length(x))
return("'nobj(x@ranges@partitioning)' must equal 'length(x)'")
NULL
}
.valid.GIntervalTree <- function(x) {
c(.valid.GIntervalTree.ranges(x),
.valid.GIntervalTree.rngidx(x))
}
setValidity2("GIntervalTree", .valid.GIntervalTree)
.GT_getIndex <- function(from) {
fvals <- as.integer(runValue(seqnames(from)))
if (S4Vectors:::isNotSorted(fvals)) {
flens <- runLength(seqnames(from))
idx <- S4Vectors:::orderInteger(fvals)
rngidx <- successiveIRanges(flens)[idx]
idx <- S4Vectors:::orderInteger(start(rngidx))
return(IRanges(end=cumsum(width(rngidx))[idx], width=width(rngidx)[idx]))
}
IRanges()
}
.GT_reorderValue <- function(obj, val, rngidx=NULL)
{
if (!is(obj, "GIntervalTree") && is.null(rngidx))
stop("obj must be 'GIntervalTree' object")
if (is.null(rngidx))
rngidx <- obj@rngidx
if (length(rngidx))
val <- extractROWS(val, rngidx)
val
}
#' seqnames accessor
#'
#'
#' @rdname GIntervalTree-class
#' @family GIntervalTree
#' @export
#' @importMethodsFrom GenomicRanges seqnames
setMethod("seqnames", "GIntervalTree",
function(x) Rle(.GT_reorderValue(x, space(x@ranges))))
#' ranges accessor
#'
#' @rdname GIntervalTree-class
#' @family GIntervalTree
#' @export
#' @importMethodsFrom GenomicRanges ranges
setMethod("ranges", "GIntervalTree",
function(x) .GT_reorderValue(x, as(x@ranges, "IRanges")))
#' strand accessor
#'
#' @rdname GIntervalTree-class
#' @family GIntervalTree
#' @export
#' @importMethodsFrom GenomicRanges strand
setMethod("strand", "GIntervalTree", function(x) x@strand)
#' seqinfo accessor
#'
#' @rdname GIntervalTree-class
#' @family GIntervalTree
#' @export
#' @importMethodsFrom GenomicRanges seqinfo
setMethod("seqinfo", "GIntervalTree", function(x) x@seqinfo)
setMethod("start", "GIntervalTree",
function(x, ...) .GT_reorderValue(x,start(x@ranges, ...)@unlistData))
setMethod("end", "GIntervalTree",
function(x, ...) .GT_reorderValue(x, end(x@ranges, ...)@unlistData))
setMethod("width", "GIntervalTree",
function(x) .GT_reorderValue(x, width(x@ranges)@unlistData))
#' construct from GRanges object via coercion
#'
#' @name as
#' @family GIntervalTree
#' @importClassesFrom GenomicRanges GRanges
setAs("GRanges", "GIntervalTree",
function(from) {
msg <- c("GIntervalTree objects and the \"intervaltree\" algorithm ",
"used in findOverlaps() and family are defunct. Please ",
"use GNCList objects or the \"nclist\" algorithm instead. ",
"See ?GNCList and the 'algorithm' argument in ?findOverlaps ",
"for more information.")
.Defunct(msg=wmsg(msg))
if (any(isCircular(from), na.rm=TRUE))
stop("'GIntervalTree' objects not supported for circular sequences")
rl <- split(unname(ranges(from)), seqnames(from))
new2("GIntervalTree",
strand=strand(from),
elementMetadata=mcols(from),
seqinfo=seqinfo(from),
ranges=IntervalForest(rl),
rngidx=.GT_getIndex(from),
check=FALSE)
}
)
#' constructor function using GRanges object
#'
#' @family GIntervalTree
#' @export
GIntervalTree <- function(x) {
as(x, "GIntervalTree")
}
#' coercion from GIntervalTree to GRanges object
#'
#' @family GIntervalTree
#' @name as
#' @importClassesFrom GenomicRanges GRanges
setAs("GIntervalTree", "GRanges",
function(from) {
out=new("GRanges",
seqnames=seqnames(from),
strand=strand(from),
elementMetadata=mcols(from),
seqinfo=seqinfo(from),
ranges=ranges(from))
out
}
)
#' subsetting
#'
#' @family GIntervalTree
#' @rdname GIntervalTree-class
#' @export
setMethod("[", "GIntervalTree",
function(x, i, j, ...) {
gr <- callGeneric(as(x, "GRanges"),i=i, ...)
as(gr, "GIntervalTree")
})