1
### =========================================================================
3
### -------------------------------------------------------------------------
6
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7
### Some "strand" and "strand<-" methods
3
10
setMethod("strand", "missing", function(x) factor(levels=c("+","-","*")))
11
setMethod("strand", "NULL", function(x) strand())
5
13
setMethod("strand", "character",
7
16
lvls <- levels(strand())
8
if (!all(is.na(x) | (x %in% lvls)))
17
if (!all(x %in% lvls))
9
18
stop("strand values must be in '", paste(lvls, collapse="' '"), "'")
10
19
factor(x, levels=lvls)
13
23
setMethod("strand", "factor",
27
stop("NA not a valid strand value, use \"*\" instead")
15
28
lvls <- levels(strand())
16
if (length(setdiff(levels(x), lvls)))
17
stop("strand values must be in '", paste(lvls, collapse="' '"), "'")
30
if (identical(x_levels, lvls))
32
invalid_levels <- setdiff(x_levels, lvls)
33
if (length(invalid_levels) != 0L)
34
stop("invalid strand levels in 'x': ",
35
paste(invalid_levels, collapse=", "))
18
36
factor(x, levels=lvls)
21
40
setMethod("strand", "integer",
23
43
lvls <- c(1L, -1L, NA)
24
if (length(setdiff(x, lvls)))
44
if (!all(x %in% lvls))
25
45
stop("strand values must be in '", paste(lvls, collapse="' '"), "'")
27
length(ans) <- length(x)
46
ans <- rep.int(strand("*"), length(x))
28
47
ans[x == 1L] <- "+"
29
48
ans[x == -1L] <- "-"
33
53
setMethod("strand", "logical",
36
length(ans) <- length(x)
56
ans <- rep.int(strand("*"), length(x))
42
63
setMethod("strand", "Rle",
44
66
x_runValue <- runValue(x)
45
if (!is.logical(x_runValue))
46
stop("only 'logical'-Rle's are supported by \"strand\" method",
47
"for Rle objects at the moment, sorry")
67
if (!(is.character(x_runValue) ||
68
is.factor(x_runValue) ||
69
is.integer(x_runValue) ||
70
is.logical(x_runValue)))
71
stop("\"strand\" method for Rle objects only works on a ",
72
"character-, factor-, integer-, or logical-Rle object")
48
73
runValue(x) <- strand(x_runValue)
52
78
setMethod("strand", "DataTable",
54
81
ans <- x[["strand"]]
56
ans <- strand(rep(NA_character_, nrow(x)))
57
else if (is.character(ans))
83
ans <- rep.int(strand("*"), nrow(x))
59
else if (is(ans, "Rle"))
64
91
setReplaceMethod("strand", "DataTable", function(x, value) {
65
92
x$strand <- normargGenomicRangesStrand(value, nrow(x))
97
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98
### compatibleStrand() generic and methods
69
101
setGeneric("compatibleStrand", signature=c("x","y"), # not exported
70
102
function(x, y) standardGeneric("compatibleStrand")