-
Notifications
You must be signed in to change notification settings - Fork 1
/
track-attr.R
239 lines (210 loc) · 7.54 KB
/
track-attr.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
.gtrack.attr.set <- function(trackstr, attr, value, overwrite.readonly) {
table <- data.frame(value)
colnames(table) <- attr
rownames(table) <- trackstr
.gtrack.attr.import(table, FALSE, overwrite.readonly)
}
.gtrack.attr.import <- function(table, remove.others, overwrite.readonly) {
table[, 1:ncol(table)] <- sapply(table[, 1:ncol(table)], as.character)
readonly_attrs <- NULL
if (!overwrite.readonly) {
readonly_attrs <- gdb.get_readonly_attrs()
}
.gcall("gset_tracks_attrs", table, remove.others, readonly_attrs, .misha_env())
}
#' Returns track attributes values
#'
#' Returns track attributes values.
#'
#' This function returns a data frame that contains track attributes values.
#' Column names of the data frame consist of the attribute names, row names
#' contain the track names.
#'
#' The list of required tracks is specified by 'tracks' argument. If 'tracks'
#' is 'NULL' the attribute values of all existing tracks are returned.
#'
#' Likewise the list of required attributes is controlled by 'attrs' argument.
#' If 'attrs' is 'NULL' all attribute values of the specified tracks are
#' returned. The columns are also sorted then by "popularity" of an attribute,
#' i.e. the number of tracks containing this attribute. This sorting is not
#' applied if 'attrs' is not 'NULL'.
#'
#' Empty character string in a table cell marks a non-existing attribute.
#'
#' @param tracks a vector of track names or 'NULL'
#' @param attrs a vector of attribute names or 'NULL'
#' @return A data frame containing track attributes values.
#' @seealso \code{\link{gtrack.attr.import}}, \code{\link{gtrack.attr.get}},
#' \code{\link{gtrack.attr.set}}
#' @keywords ~attr ~attribute
#' @examples
#' \dontshow{
#' options(gmax.processes = 2)
#' }
#'
#' gdb.init_examples()
#' gtrack.attr.export()
#' gtrack.attr.export(tracks = c("sparse_track", "dense_track"))
#' gtrack.attr.export(attrs = "created.by")
#'
#' @export gtrack.attr.export
gtrack.attr.export <- function(tracks = NULL, attrs = NULL) {
.gcheckroot()
if (.ggetOption(".ginteractive", FALSE)) {
trackstr <- do.call(.gexpr2str, list(substitute(tracks)), envir = parent.frame())
if (!is.na(match(trackstr, get("GTRACKS", envir = .misha)))) {
tracks <- trackstr
}
}
if (!is.null(tracks)) {
tracks <- unique(tracks)
}
if (!is.null(attrs)) {
attrs <- unique(attrs)
}
if (is.null(tracks)) {
tracks <- get("GTRACKS", envir = .misha)
} else {
idx <- which(!(tracks %in% get("GTRACKS", envir = .misha)))[1]
if (!is.na(idx)) {
stop(sprintf("Track %s does not exist", tracks[idx]), call. = FALSE)
}
}
.gcall("gget_tracks_attrs", tracks, attrs, .misha_env())
}
#' Imports track attributes values
#'
#' Imports track attributes values.
#'
#' This function makes imports attribute values contained in a data frame
#' 'table'. The format of a table is similar to the one returned by
#' 'gtrack.attr.export'. The values of the table must be character strings.
#' Column names of the table should specify the attribute names, while row
#' names should contain the track names.
#'
#' The specified attributes of the specified tracks are modified. If an
#' attribute value is an empty string this attribute is removed from the track.
#'
#' If 'remove.others' is 'TRUE' all non-readonly attributes that do not appear
#' in the table are removed, otherwise they are preserved unchanged.
#'
#' Error is reported on an attempt to modify a value of a read-only attribute.
#'
#' @param table a data frame containing attribute values
#' @param remove.others specifies what to do with the attributes that are not
#' in the table
#' @return None.
#' @seealso \code{\link{gtrack.attr.import}}, \code{\link{gtrack.attr.set}},
#' \code{\link{gtrack.attr.get}}, \code{\link{gdb.get_readonly_attrs}}
#' @keywords ~attr ~attribute
#' @examples
#' \dontshow{
#' options(gmax.processes = 2)
#' }
#'
#' gdb.init_examples()
#' t <- gtrack.attr.export()
#' t$newattr <- as.character(1:dim(t)[1])
#' gtrack.attr.import(t)
#' gtrack.attr.export(attrs = "newattr")
#'
#' # roll-back the changes
#' t$newattr <- ""
#' gtrack.attr.import(t)
#'
#' @export gtrack.attr.import
gtrack.attr.import <- function(table = NULL, remove.others = FALSE) {
if (is.null(table)) {
stop("Usage: gtrack.attr.import(table, remove.others = FALSE)", call. = FALSE)
}
.gcheckroot()
tracks <- rownames(table)
attrs <- colnames(table)
if (!is.data.frame(table) || any(dim(table) < 1) || !length(tracks) || !length(attrs) || any(is.na(tracks)) || any(is.na(attrs)) || any(attrs == "")) {
stop("Invalid format of attributes table", call. = FALSE)
}
idx <- which(!(tracks %in% get("GTRACKS", envir = .misha)))[1]
if (!is.na(idx)) {
stop(sprintf("Track %s does not exist", tracks[idx]), call. = FALSE)
}
idx <- which(duplicated(tracks))[1]
if (!is.na(idx)) {
stop(sprintf("Track %s appears more than once", tracks[idx]), call. = FALSE)
}
idx <- which(duplicated(attrs))[1]
if (!is.na(idx)) {
stop(sprintf("Attribute %s appears more than once", attrs[idx]), call. = FALSE)
}
.gtrack.attr.import(table, remove.others, FALSE)
retv <- 0 # suppress return value
}
#' Returns value of a track attribute
#'
#' Returns value of a track attribute.
#'
#' This function returns the value of a track attribute. If the attribute does
#' not exist an empty sting is returned.
#'
#' @param track track name
#' @param attr attribute name
#' @return Track attribute value.
#' @seealso \code{\link{gtrack.attr.import}}, \code{\link{gtrack.attr.set}}
#' @keywords ~attr ~attribute
#' @examples
#' \dontshow{
#' options(gmax.processes = 2)
#' }
#'
#' gdb.init_examples()
#' gtrack.attr.set("sparse_track", "test_attr", "value")
#' gtrack.attr.get("sparse_track", "test_attr")
#' gtrack.attr.set("sparse_track", "test_attr", "")
#'
#' @export gtrack.attr.get
gtrack.attr.get <- function(track = NULL, attr = NULL) {
if (is.null(substitute(track)) || is.null(attr)) {
stop("Usage: gtrack.attr.get(track, attr)", call. = FALSE)
}
.gcheckroot()
trackstr <- do.call(.gexpr2str, list(substitute(track)), envir = parent.frame())
res <- gtrack.attr.export(trackstr, attr)
res[1, 1]
}
#' Assigns value to a track attribute
#'
#' Assigns value to a track attribute.
#'
#' This function creates a track attribute and assigns 'value' to it. If the
#' attribute already exists its value is overwritten.
#'
#' If 'value' is an empty string the attribute is removed.
#'
#' Error is reported on an attempt to modify a value of a read-only attribute.
#'
#' @param track track name
#' @param attr attribute name
#' @param value value
#' @return None.
#' @seealso \code{\link{gtrack.attr.get}}, \code{\link{gtrack.attr.import}},
#' \code{\link{gtrack.var.set}}, \code{\link{gdb.get_readonly_attrs}}
#' @keywords ~attr ~attribute
#' @examples
#' \dontshow{
#' options(gmax.processes = 2)
#' }
#'
#' gdb.init_examples()
#' gtrack.attr.set("sparse_track", "test_attr", "value")
#' gtrack.attr.get("sparse_track", "test_attr")
#' gtrack.attr.set("sparse_track", "test_attr", "")
#'
#' @export gtrack.attr.set
gtrack.attr.set <- function(track = NULL, attr = NULL, value = NULL) {
if (is.null(substitute(track)) || is.null(attr) || is.null(value)) {
stop("Usage: gtrack.attr.set(track, attr, value)", call. = FALSE)
}
.gcheckroot()
trackstr <- do.call(.gexpr2str, list(substitute(track)), envir = parent.frame())
.gtrack.attr.set(trackstr, attr, value, FALSE)
retv <- 0 # suppress return value
}