File: NaArray-Logic-methods.R

package info (click to toggle)
r-bioc-sparsearray 1.6.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,768 kB
  • sloc: ansic: 16,138; makefile: 2
file content (181 lines) | stat: -rw-r--r-- 5,901 bytes parent folder | download
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
176
177
178
179
180
181
### =========================================================================
### 'Logic' operations on NaArray objects
### -------------------------------------------------------------------------
###
### 'Logic' operations:   "&", "|"
###
### See '?S4groupGeneric' for more information.
###
### We also implement a logical negation ("!") method for NaArray objects.
###


### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Logical negation
###

.logical_neg_NaArray <- function(x)
{
    stopifnot(is(x, "NaArray"))
    check_svt_version(x)

    ## Check types.
    x_type <- type(x)
    if (!(x_type %in% LOGIC_INPUT_TYPES))
        stop(wmsg("logical negation (\"!\") of an NaArray object ",
                  "of type() \"", x_type, "\" is not supported"))

    new_NaSVT <- SparseArray.Call("C_logical_neg_NaSVT", x@dim, x@type, x@NaSVT)
    BiocGenerics:::replaceSlots(x, type="logical", NaSVT=new_NaSVT, check=FALSE)
}

setMethod("!", "NaArray", .logical_neg_NaArray)


### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### 'Logic' group
###

.Logic_NaSVT1_v2 <- function(op, x, y)
{
    stopifnot(isSingleString(op), is(x, "NaArray"))
    check_svt_version(x)

    ## Check types.
    x_type <- type(x)
    check_Logic_input_type(x_type, "NaArray object")
    if (!(type(y) %in% LOGIC_INPUT_TYPES))
        stop(wmsg("\"", op, "\" between an NaArray object ",
                  "and a ", class(y), " vector is not supported"))

    ## Check 'y'.
    if (length(y) != 1L)
        stop(wmsg("\"", op, "\" between an NaArray object ",
                  "and a vector of length != 1 is not supported"))

    if (is.na(y)) {
        new_NaSVT <- SparseArray.Call("C_Logic_NaSVT1_na",
                                      x@dim, x@type, x@NaSVT, op)
        BiocGenerics:::replaceSlots(x, type="logical", NaSVT=new_NaSVT,
                                    check=FALSE)
    } else {
        if (op == "&" && isFALSE(y))
            error_on_left_NAsparsity_not_preserved(op, "y is FALSE")
        if (op == "|" && isTRUE(y))
            error_on_left_NAsparsity_not_preserved(op, "y is TRUE")
        x
    }
}

setMethod("Logic", c("NaArray", "vector"),
    function(e1, e2) .Logic_NaSVT1_v2(.Generic, e1, e2)
)

setMethod("Logic", c("vector", "NaArray"),
    function(e1, e2) .Logic_NaSVT1_v2(.Generic, e2, e1)
)

### Returns an NaArray object.
.Logic_NaSVT1_NaSVT2 <- function(op, x, y)
{
    stopifnot(isSingleString(op), is(x, "NaArray"), is(y, "NaArray"))
    check_svt_version(x)
    check_svt_version(y)

    ## Check types.
    check_Logic_input_type(type(x), "NaArray object")
    check_Logic_input_type(type(y), "NaArray object")

    ## Check array conformability.
    x_dim <- dim(x)
    y_dim <- dim(y)
    if (!identical(x_dim, y_dim))
        stop(wmsg("non-conformable arrays"))

    ## Compute 'ans_dimnames'.
    ans_dimnames <- S4Arrays:::get_first_non_NULL_dimnames(list(x, y))

    ans_NaSVT <- SparseArray.Call("C_Logic_SVT1_SVT2",
                                  x_dim, x@type, x@NaSVT, TRUE,
                                  y_dim, y@type, y@NaSVT, TRUE, op)
    new_NaArray(x_dim, ans_dimnames, NaSVT=ans_NaSVT, check=FALSE)
}

setMethod("Logic", c("NaArray", "NaArray"),
    function(e1, e2) .Logic_NaSVT1_NaSVT2(.Generic, e1, e2)
)

setMethod("Logic", c("NaArray", "array"),
    function(e1, e2) .Logic_NaSVT1_NaSVT2(.Generic, e1, as(e2, "NaArray"))
)

setMethod("Logic", c("array", "NaArray"),
    function(e1, e2) .Logic_NaSVT1_NaSVT2(.Generic, as(e1, "NaArray"), e2)
)

### Returns an NaArray object if 'op' is "|" (because NA | FALSE is NA),
### and a SparseArray if 'op' is "&" (because NA & FALSE is FALSE).
.Logic_NaSVT1_SVT2 <- function(op, x, y)
{
    stopifnot(isSingleString(op), is(x, "NaArray"), is(y, "SVT_SparseArray"))
    check_svt_version(x)
    check_svt_version(y)

    ## Check types.
    check_Logic_input_type(type(x), "NaArray object")
    check_Logic_input_type(type(y), "SparseArray object")

    ## Check array conformability.
    x_dim <- dim(x)
    y_dim <- dim(y)
    if (!identical(x_dim, y_dim))
        stop(wmsg("non-conformable arrays"))

    ## Compute 'ans_dimnames'.
    ans_dimnames <- S4Arrays:::get_first_non_NULL_dimnames(list(x, y))

    ans_SVT <- SparseArray.Call("C_Logic_SVT1_SVT2",
                                x_dim, x@type, x@NaSVT, TRUE,
                                y_dim, y@type, y@SVT, FALSE, op)
    if (op == "|")
        return(new_NaArray(x_dim, ans_dimnames, NaSVT=ans_SVT, check=FALSE))
    new_SVT_SparseArray(x_dim, ans_dimnames, SVT=ans_SVT, check=FALSE)
}

### Returns an NaArray object if 'op' is "|" (because FALSE | NA is NA),
### and a SparseArray if 'op' is "&" (because FALSE & NA is FALSE).
.Logic_SVT1_NaSVT2 <- function(op, x, y)
{
    stopifnot(isSingleString(op), is(x, "SVT_SparseArray"), is(y, "NaArray"))
    check_svt_version(x)
    check_svt_version(y)

    ## Check types.
    check_Logic_input_type(type(x), "SparseArray object")
    check_Logic_input_type(type(y), "NaArray object")

    ## Check array conformability.
    x_dim <- dim(x)
    y_dim <- dim(y)
    if (!identical(x_dim, y_dim))
        stop(wmsg("non-conformable arrays"))

    ## Compute 'ans_dimnames'.
    ans_dimnames <- S4Arrays:::get_first_non_NULL_dimnames(list(x, y))

    ans_SVT <- SparseArray.Call("C_Logic_SVT1_SVT2",
                                x_dim, x@type, x@SVT, FALSE,
                                y_dim, y@type, y@NaSVT, TRUE, op)
    if (op == "|")
        return(new_NaArray(x_dim, ans_dimnames, NaSVT=ans_SVT, check=FALSE))
    new_SVT_SparseArray(x_dim, ans_dimnames, SVT=ans_SVT, check=FALSE)
}

setMethod("Logic", c("NaArray", "SVT_SparseArray"),
    function(e1, e2) .Logic_NaSVT1_SVT2(.Generic, e1, e2)
)

setMethod("Logic", c("SVT_SparseArray", "NaArray"),
    function(e1, e2) .Logic_SVT1_NaSVT2(.Generic, e1, e2)
)