forked from jimhester/GenomicRanges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrand-utils.R
158 lines (136 loc) · 4.42 KB
/
strand-utils.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
### =========================================================================
### Strand utilities
### -------------------------------------------------------------------------
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Some "strand" and "strand<-" methods
###
setMethod("strand", "missing", function(x) factor(levels=c("+","-","*")))
setMethod("strand", "NULL", function(x) strand())
setMethod("strand", "character",
function(x)
{
lvls <- levels(strand())
if (!all(x %in% lvls))
stop("strand values must be in '", paste(lvls, collapse="' '"), "'")
factor(x, levels=lvls)
}
)
setMethod("strand", "factor",
function(x)
{
if (any(is.na(x)))
stop("NA not a valid strand value, use \"*\" instead")
lvls <- levels(strand())
x_levels <- levels(x)
if (identical(x_levels, lvls))
return(x)
invalid_levels <- setdiff(x_levels, lvls)
if (length(invalid_levels) != 0L)
stop("invalid strand levels in 'x': ",
paste(invalid_levels, collapse=", "))
factor(x, levels=lvls)
}
)
setMethod("strand", "integer",
function(x)
{
lvls <- c(1L, -1L, NA)
if (!all(x %in% lvls))
stop("strand values must be in '", paste(lvls, collapse="' '"), "'")
ans <- rep.int(strand("*"), length(x))
ans[x == 1L] <- "+"
ans[x == -1L] <- "-"
ans
}
)
setMethod("strand", "logical",
function(x)
{
ans <- rep.int(strand("*"), length(x))
ans[!x] <- "+"
ans[ x] <- "-"
ans
}
)
setMethod("strand", "Rle",
function(x)
{
x_runValue <- runValue(x)
if (!(is.character(x_runValue) ||
is.factor(x_runValue) ||
is.integer(x_runValue) ||
is.logical(x_runValue)))
stop("\"strand\" method for Rle objects only works on a ",
"character-, factor-, integer-, or logical-Rle object")
runValue(x) <- strand(x_runValue)
x
}
)
setMethod("strand", "DataTable",
function(x)
{
ans <- x[["strand"]]
if (is.null(ans)) {
ans <- rep.int(strand("*"), nrow(x))
} else {
ans <- strand(ans)
}
ans
}
)
setReplaceMethod("strand", "DataTable", function(x, value) {
x$strand <- normargGenomicRangesStrand(value, nrow(x))
x
})
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### compatibleStrand() generic and methods
###
setGeneric("compatibleStrand", signature=c("x","y"), # not exported
function(x, y) standardGeneric("compatibleStrand")
)
setMethod("compatibleStrand", c("factor", "factor"), # not exported
function(x, y)
{
lvls <- levels(strand())
if (length(x) != length(y))
stop("'x' and 'y' must be of equal length")
if (!identical(levels(x), lvls) || !identical(levels(y), lvls))
stop("strand values must be in '", paste(lvls, collapse="' '"), "'")
levels(x) <- c("1", "-1", "0")
x <- as.integer(as.character(x))
levels(y) <- c("1", "-1", "0")
y <- as.integer(as.character(y))
ans <- x * y != -1L
if (S4Vectors:::anyMissing(ans)) {
fix <- which(is.na(ans))
ans[fix] <- (x[fix] == 0L) | (y[fix] == 0L)
if (S4Vectors:::anyMissing(ans))
ans[is.na(ans)] <- FALSE
}
ans
}
)
setMethod("compatibleStrand", c("Rle", "Rle"), # not exported
function(x, y)
{
lvls <- levels(strand())
if (length(x) != length(y))
stop("'x' and 'y' must be of equal length")
if (!identical(levels(runValue(x)), lvls) ||
!identical(levels(runValue(y)), lvls))
stop("strand values must be in '", paste(lvls, collapse="' '"), "'")
levels(x) <- c("1", "-1", "0")
runValue(x) <- as.integer(as.character(runValue(x)))
levels(y) <- c("1", "-1", "0")
runValue(y) <- as.integer(as.character(runValue(y)))
ans <- x * y != -1L
if (S4Vectors:::anyMissing(runValue(ans))) {
fix <- which(is.na(runValue(ans)))
runValue(ans)[fix] <-
(runValue(x) == 0L)[fix] | (runValue(y)[fix] == 0L)
if (S4Vectors:::anyMissing(runValue(ans)))
runValue(ans)[is.na(runValue(ans))] <- FALSE
}
ans
}
)