-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbigFmatrix.R
131 lines (110 loc) · 3.95 KB
/
bigFmatrix.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
#' @export
setClass("bigFmatrix",
contains = "big.matrix",
representation(backingpath = "character",
temp = "logical")
)
#' @export
setClass("bigFmatrix.descriptor",
contains = "big.matrix.descriptor",
representation(backingpath = "character",
temp = "logical")
)
#' @export
setGeneric("dir.name", function(x) standardGeneric("dir.name"))
setMethod("dir.name", signature(x = "bigFmatrix"), function(x) {
x@backingpath
})
#' @export
setGeneric("path.name", function(x) standardGeneric("path.name"))
setMethod("path.name", signature(x = "bigFmatrix"), function(x) {
file.path(dir.name(x), file.name(x))
})
#' @export
setGeneric("is.temp", function(x) standardGeneric("is.temp"))
setMethod("is.temp", signature(x = "bigFmatrix"), function(x) {
x@temp
})
#' @export
setMethod("describe", signature(x = "bigFmatrix"), function(x) {
new("bigFmatrix.descriptor",
description = callNextMethod()@description,
backingpath = x@backingpath,
temp = x@temp)
})
#' @export
bigFmatrix <- function(nrow, ncol, type = options()$bigmemory.default.type,
init = NULL, backingfile,
backingpath = "backingfiles",
temp = FALSE) {
if (!dir.exists(backingpath)) dir.create(backingpath)
tmp <- filebacked.big.matrix(nrow, ncol, type = type, init = init,
backingfile = backingfile,
backingpath = backingpath,
descriptorfile = paste0(backingfile, ".desc"))
new("bigFmatrix",
address = tmp@address,
backingpath = backingpath,
temp = temp)
}
#' @export
bigTFmatrix <- function(nrow, ncol, type = options()$bigmemory.default.type,
init = NULL, backingfile = basename(tempfile()),
backingpath = file.path("backingfiles", "tmp"),
temp = TRUE) {
bigFmatrix(nrow, ncol, type = type, init = init,
backingfile = backingfile,
backingpath = backingpath,
temp = temp)
}
#' @export
attach.bigFmatrix = function(obj, ...)
{
descriptorfile <- basename(obj)
backingpath <- dirname(obj)
tmp <- attach.big.matrix(descriptorfile, backingpath = backingpath, ...)
new("bigFmatrix",
address = tmp@address,
backingpath = backingpath,
temp = FALSE)
}
#' @export
setGeneric('sub.bigFmatrix', function(x, firstRow = 1, lastRow = NULL,
firstCol = 1, lastCol = NULL)
standardGeneric('sub.bigFmatrix')
)
setMethod('sub.bigFmatrix', signature(x = 'bigFmatrix'),
function(x, firstRow, lastRow, firstCol, lastCol) {
sub.bigFmatrix(describe(x), firstRow, lastRow, firstCol, lastCol)
})
#' @param x A descriptor object
#' @param firstRow the first row of the submatrix
#' @param lastRow the last row of the submatrix if not NULL
#' @param firstCol the first column of the submatrix
#' @param lastCol of the submatrix if not NULL
setMethod('sub.bigFmatrix', signature(x = 'bigFmatrix.descriptor'),
function(x, firstRow, lastRow, firstCol, lastCol) {
backingpath <- x@backingpath
tmp <- sub.big.matrix(x, firstRow, lastRow,
firstCol, lastCol,
backingpath)
new("bigFmatrix",
address = tmp@address,
backingpath = backingpath,
temp = x@temp)
})
setGeneric("rmT", function(x) standardGeneric("rmT"))
setMethod("rmT", signature(x = "bigFmatrix"), function(x) {
if (is.sub.big.matrix(x)) {
warning("You can't delete a sub.bigFmatrix.")
} else {
if (x@temp) {
a <- deparse(substitute(x))
path <- path.name(x)
rm(list = a, pos = 1)
unlink(paste0(path, c("", ".desc")))
} else {
warning("You can't delete a non-temporary bigFmatrix. Use only `rm` instead.")
}
}
})