[ea2224]: / R / XenaPrepare.R

Download this file

278 lines (256 with data), 8.4 kB

  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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
##' Prepare (Load) Downloaded Datasets to R
##'
##' @author Shixiang Wang <w_shixiang@163.com>
##' @param objects a object of character vector or data.frame. If `objects` is data.frame,
##' it should be returned object of [XenaDownload] function. More easier way is
##' that objects can be character vector specify local files/directory and download urls.
##' @param objectsName specify names for elements of return object, i.e. names of list
##' @param use_chunk default is `FALSE`. If you want to select subset of original data, please set it to
##' `TRUE` and specify corresponding arguments: `chunk_size`, `select_direction`, `select_names`,
##' `callback`.
##' @param chunk_size the number of rows to include in each chunk
##' @param subset_rows logical expression indicating elements or rows to keep:
##' missing values are taken as false. `x` can be a representation of data frame
##' you wanna do subset operation. Of note, the first colname of most of datasets
##' in Xena will be set to "sample", you can use it to select rows.
##' @param select_cols expression, indicating columns to select from a data frame.
##' 'x' can be a representation of data frame you wanna do subset operation,
##' e.g. `select_cols = colnames(x)[1:3]` will keep only first to third column.
##' @param callback a function to call on each chunk, default is `NULL`,
##' this option will overvide operations of subset_rows and select_cols.
##' @param comment a character specify comment rows in files
##' @param na a character vectory specify `NA` values in files
##' @param ... other arguments transfer to `read_tsv` function or
##' `read_tsv_chunked` function (when `use_chunk` is `TRUE`) of `readr` package.
##' @return a list contains file data, which in way of tibbles
##' @export
##' @importFrom readr read_tsv
##' @importFrom readr read_tsv_chunked
##' @importFrom readr cols
##' @examples
##' \dontrun{
##' xe = XenaGenerate(subset = XenaHostNames == "tcgaHub")
##' hosts(xe)
##' xe_query = XenaQuery(xe)
##'
##' xe_download = XenaDownload(xe_query)
##' dat = XenaPrepare(xe_download)
##' }
XenaPrepare <- function(objects,
objectsName = NULL,
use_chunk = FALSE,
chunk_size = 100,
subset_rows = TRUE,
select_cols = TRUE,
callback = NULL,
comment = "#",
na = c("", "NA", "[Discrepancy]"),
...) {
# objects can be url, local files/directory or xena object from xena download process
stopifnot(
is.character(objects) |
is.data.frame(objects),
is.logical(use_chunk)
)
subset_rows.bk <- subset_rows
select_cols.bk <- select_cols
subset_rows <- substitute(subset_rows)
if (is.name(subset_rows)) {
subset_rows <- substitute(eval(subset_rows.bk))
}
select_cols <- substitute(select_cols)
if (is.name(select_cols)) {
select_cols <- substitute(eval(select_cols.bk))
}
# subset_rows <- substitute(subset_rows)
# select_cols <- substitute(select_cols)
# subset_direction = match.arg(subset_direction)
objects2 <- objects
if (is.character(objects)) {
if (length(objects) == 0) {
stop("Please check you input!")
}
# Is the input directory?
if (all(dir.exists(objects))) {
if (length(objects) > 1) {
stop("We do not accept multiple directories as input.")
} else {
files <- paste0(objects, "/", dir(objects))
res <- lapply(files, function(x) {
if (use_chunk) {
if (is.null(callback)) {
f <- function(x, pos) {
subset(x,
eval(subset_rows),
select = eval(select_cols)
)
}
} else {
f <- callback
}
y <- readr::read_tsv_chunked(
x,
readr::DataFrameCallback$new(f),
chunk_size = chunk_size,
comment = comment,
na = na,
col_types = readr::cols()
)
} else {
y <- readr::read_tsv(
x,
comment = comment,
na = na,
col_types = readr::cols(),
...
)
}
y
})
if (is.null(objectsName)) {
objectsName <- make.names(dir(objects))
names(res) <- objectsName
}
}
} else if (all(file.exists(objects))) {
res <- lapply(objects, function(x) {
if (use_chunk) {
if (is.null(eval(callback))) {
f <- function(x, pos) {
subset(x,
eval(subset_rows),
select = eval(select_cols)
)
}
} else {
f <- callback
}
y <- readr::read_tsv_chunked(
x,
readr::DataFrameCallback$new(f),
chunk_size = chunk_size,
comment = comment,
na = na,
col_types = readr::cols()
)
} else {
y <- readr::read_tsv(
x,
comment = comment,
na = na,
col_types = readr::cols(),
...
)
}
y
})
if (is.null(objectsName)) {
objectsName <- make.names(basename(objects))
names(res) <- objectsName
}
if (length(res) == 1) {
res <- res[[1]]
}
}
else {
# check urls
all_right <- grepl(pattern = "http", x = objects)
if (any(all_right)) {
objects <- objects[all_right]
if (length(objects) == 1) {
if (use_chunk) {
if (is.null(callback)) {
f <- function(x, pos) {
subset(x,
eval(subset_rows),
select = eval(select_cols)
)
}
} else {
f <- callback
}
res <- readr::read_tsv_chunked(
objects,
readr::DataFrameCallback$new(f),
chunk_size = chunk_size,
comment = comment,
na = na,
col_types = readr::cols()
)
} else {
res <- readr::read_tsv(
objects,
comment = comment,
na = na,
col_types = readr::cols(),
...
)
}
# res = suppressMessages(read_tsv(objects, comment=comment, na=na, ...))
} else {
res <- lapply(objects, function(x) {
if (use_chunk) {
if (is.null(callback)) {
f <- function(x, pos) {
subset(x,
eval(subset_rows),
select = eval(select_cols)
)
}
} else {
f <- callback
}
y <- readr::read_tsv_chunked(
x,
readr::DataFrameCallback$new(f),
chunk_size = chunk_size,
comment = comment,
na = na,
col_types = readr::cols()
)
} else {
y <- readr::read_tsv(
x,
comment = comment,
na = na,
col_types = readr::cols(),
...
)
}
y
})
if (is.null(objectsName)) {
objectsName <- make.names(basename(objects))
names(res) <- objectsName
}
}
}
all_wrong <- !all_right
if (any(all_wrong)) {
bad_urls <- objects2[all_wrong]
message("Some inputs are wrong, maybe you should check:")
print(bad_urls)
}
}
} else {
if (!"destfiles" %in% colnames(objects)) {
stop(
"Input data.frame should contain 'destfiles' column which generated by XenaDownload functions. Please check your input."
)
}
files <- objects$destfiles
res <- XenaPrepare(
files,
objectsName = objectsName,
use_chunk = use_chunk,
chunk_size = chunk_size,
subset_rows = subset_rows,
select_cols = select_cols,
callback = callback,
comment = comment,
na = na,
...
)
}
return(res)
}