1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 #' R6 Class Representing Arvados Collection
8 #' Collection class provides interface for working with Arvados collections,
9 #' for exaplme actions like creating, updating, moving or removing are possible.
12 #' \code{\link{https://github.com/arvados/arvados/tree/main/sdk/R}}
16 Collection <- R6::R6Class(
22 #' @field uuid Autentic for Collection UUID.
26 #' Initialize new enviroment.
27 #' @param api Arvados enviroment.
28 #' @param uuid The UUID Autentic for Collection UUID.
29 #' @return A new `Collection` object.
31 #' collection <- Collection$new(arv, CollectionUUID)
32 initialize = function(api, uuid)
34 private$REST <- api$getRESTService()
39 #' Adds ArvadosFile or Subcollection specified by content to the collection. Used only with ArvadosFile or Subcollection.
40 #' @param content Content to be added.
41 #' @param relativePath Path to add content.
42 add = function(content, relativePath = "")
44 if(is.null(private$tree))
45 private$generateCollectionTreeStructure()
47 if(relativePath == "" ||
48 relativePath == "." ||
51 subcollection <- private$tree$getTree()
55 relativePath <- trimFromEnd(relativePath, "/")
56 subcollection <- self$get(relativePath)
59 if(is.null(subcollection))
60 stop(paste("Subcollection", relativePath, "doesn't exist."))
62 if("ArvadosFile" %in% class(content) ||
63 "Subcollection" %in% class(content))
65 if(!is.null(content$getCollection()))
66 stop("Content already belongs to a collection.")
68 if(content$getName() == "")
69 stop("Content has invalid name.")
71 subcollection$add(content)
76 stop(paste0("Expected AravodsFile or Subcollection object, got ",
77 paste0("(", paste0(class(content), collapse = ", "), ")"),
84 #' @param file Name of the file.
85 #' @param col Collection from which the file is read.
86 #' @param sep Separator used in reading tsv, csv file format.
87 #' @param istable Used in reading txt file to check if the file is table or not.
88 #' @param fileclass Used in reading fasta file to set file class.
89 #' @param Ncol Used in reading binary file to set numbers of columns in data.frame.
90 #' @param Nrow Used in reading binary file to set numbers of rows in data.frame size.
92 #' collection <- Collection$new(arv, collectionUUID)
93 #' readFile <- collection$readArvFile(arvadosFile, istable = 'yes') # table
94 #' readFile <- collection$readArvFile(arvadosFile, istable = 'no') # text
95 #' readFile <- collection$readArvFile(arvadosFile) # xlsx, csv, tsv, rds, rdata
96 #' readFile <- collection$readArvFile(arvadosFile, fileclass = 'fasta') # fasta
97 #' readFile <- collection$readArvFile(arvadosFile, Ncol= 4, Nrow = 32) # binary, only numbers
98 #' readFile <- collection$readArvFile(arvadosFile, Ncol = 5, Nrow = 150, istable = "factor") # binary with factor or text
99 readArvFile = function(file, con, sep = ',', istable = NULL, fileclass = "SeqFastadna", Ncol = NULL, Nrow = NULL, wantedFunction = NULL)
101 arvFile <- self$get(file)
102 FileName <- arvFile$getName()
103 FileName <- tolower(FileName)
104 FileFormat <- gsub(".*\\.", "", FileName)
107 ARVADOS_API_TOKEN <- Sys.getenv("ARVADOS_API_TOKEN")
108 ARVADOS_API_HOST <- Sys.getenv("ARVADOS_API_HOST")
109 my_collection <- self$uuid
110 key <- gsub("/", "_", ARVADOS_API_TOKEN)
113 "AWS_ACCESS_KEY_ID" = key,
114 "AWS_SECRET_ACCESS_KEY" = key,
115 "AWS_DEFAULT_REGION" = "collections",
116 "AWS_S3_ENDPOINT" = gsub("api[.]", "", ARVADOS_API_HOST))
118 if (FileFormat == "txt") {
119 if (is.null(istable)){
120 stop(paste('You need to paste whether it is a text or table file'))
121 } else if (istable == 'no') {
122 fileContent <- arvFile$read("text") # used to read
123 fileContent <- gsub("[\r\n]", " ", fileContent)
124 } else if (istable == 'yes') {
125 arvConnection <- arvFile$connection("r") # used to make possible use different function later
126 fileContent <- read.table(arvConnection)
129 else if (FileFormat == "xlsx") {
130 fileContent <- aws.s3::s3read_using(FUN = openxlsx::read.xlsx, object = file, bucket = my_collection)
132 else if (FileFormat == "csv" || FileFormat == "tsv") {
133 arvConnection <- arvFile$connection("r")
134 if (FileFormat == "tsv"){
135 mytable <- read.table(arvConnection, sep = '\t')
136 } else if (FileFormat == "csv" & sep == '\t') {
137 mytable <- read.table(arvConnection, sep = '\t')
138 } else if (FileFormat == "csv") {
139 mytable <- read.table(arvConnection, sep = ',')
141 stop(paste('File format not supported, use arvadosFile$connection() and customise it'))
144 else if (FileFormat == "fasta") {
145 fastafile <- aws.s3::s3read_using(FUN = seqinr::read.fasta, as.string = TRUE, object = file, bucket = my_collection)
147 else if (FileFormat == "dat" || FileFormat == "bin") {
148 fileContent <- gzcon(arvFile$connection("rb"))
150 # function to precess data to binary format
151 read_bin.file <- function(fileContent) {
153 column.names <- readBin(fileContent, character(), n = Ncol)
154 bindata <- readBin(fileContent, numeric(), Nrow*Ncol+Ncol)
156 res <- which(bindata < 0.0000001)
158 bindata <- bindata[-res]
163 data <- data.frame(matrix(data = NA, nrow = Nrow, ncol = Ncol))
165 data[,i] <- bindata[(1+Nrow*(i-1)):(Nrow*i)]
167 colnames(data) = column.names
169 len <- which(is.na(data[,Ncol])) # error if sth went wrong
170 if (length(len) == 0) {
173 stop(paste("there is a factor or text in the table, customize the function by typing more arguments"))
176 if (is.null(Nrow) | is.null(Ncol)){
177 stop(paste('You need to specify numbers of columns and rows'))
179 if (is.null(istable)) {
180 fileContent <- read_bin.file(fileContent) # call a function
181 } else if (istable == "factor") { # if there is a table with col name
182 fileContent <- read_bin.file(fileContent)
185 else if (FileFormat == "rds" || FileFormat == "rdata") {
186 arvConnection <- arvFile$connection("rb")
187 mytable <- readRDS(gzcon(arvConnection))
190 stop(parse(('File format not supported, use arvadosFile$connection() and customise it')))
195 #' Write file content
196 #' @param name Name of the file.
197 #' @param file File to be saved.
198 #' @param istable Used in writing txt file to check if the file is table or not.
200 #' collection <- Collection$new(arv, collectionUUID)
201 #' writeFile <- collection$writeFile("myoutput.csv", file, istable = NULL) # csv
202 #' writeFile <- collection$writeFile("myoutput.fasta", file, istable = NULL) # fasta
203 #' writeFile <- collection$writeFile("myoutputtable.txt", file, istable = "yes") # txt table
204 #' writeFile <- collection$writeFile("myoutputtext.txt", file, istable = "no") # txt text
205 #' writeFile <- collection$writeFile("myoutputbinary.dat", file) # binary
206 #' writeFile <- collection$writeFile("myoutputxlsx.xlsx", file) # xlsx
207 writeFile = function(name, file, istable = NULL, seqName = NULL)
209 # prepare file and connection
210 arvFile <- collection$create(name)[[1]]
211 arvFile <- collection$get(name)
212 arvConnection <- arvFile$connection("w")
214 FileName <- arvFile$getName()
215 FileName <- tolower(FileName)
216 FileFormat <- gsub(".*\\.", "", FileName)
219 ARVADOS_API_TOKEN <- Sys.getenv("ARVADOS_API_TOKEN")
220 ARVADOS_API_HOST <- Sys.getenv("ARVADOS_API_HOST")
221 my_collection <- self$uuid
222 key <- gsub("/", "_", ARVADOS_API_TOKEN)
225 "AWS_ACCESS_KEY_ID" = key,
226 "AWS_SECRET_ACCESS_KEY" = key,
227 "AWS_DEFAULT_REGION" = "collections",
228 "AWS_S3_ENDPOINT" = gsub("api[.]", "", ARVADOS_API_HOST))
230 if (FileFormat == "txt") {
231 if (istable == "yes") {
232 aws.s3::s3write_using(file, FUN = write.table, object = name, bucket = my_collection)
233 } else if (istable == "no") {
234 aws.s3::s3write_using(file, FUN = writeChar, object = name, bucket = my_collection)
236 stop(paste("Specify parametr istable"))
238 } else if (FileFormat == "csv") {
239 aws.s3::s3write_using(file, FUN = write.csv, object = name, bucket = my_collection)
240 } else if (FileFormat == "fasta") {
241 aws.s3::s3write_using(file, FUN = seqinr::write.fasta, name = seqName, object = name, bucket = my_collection)
242 } else if (FileFormat == "xlsx") {
243 aws.s3::s3write_using(file, FUN = openxlsx::write.xlsx, object = name, bucket = my_collection)
244 } else if (FileFormat == "dat" || FileFormat == "bin") {
245 aws.s3::s3write_using(file, FUN = writeBin, object = name, bucket = my_collection)
247 stop(parse(('File format not supported, use arvadosFile$connection() and customise it')))
252 #' Creates one or more ArvadosFiles and adds them to the collection at specified path.
253 #' @param files Content to be created.
255 #' collection <- arv$collections_create(name = collectionTitle, description = collectionDescription, owner_uuid = collectionOwner, properties = list("ROX37196928443768648" = "ROX37742976443830153"))
256 create = function(files)
258 if(is.null(private$tree))
259 private$generateCollectionTreeStructure()
261 if(is.character(files))
263 sapply(files, function(file)
265 childWithSameName <- self$get(file)
266 if(!is.null(childWithSameName))
267 stop("Destination already contains file with same name.")
269 newTreeBranch <- private$tree$createBranch(file)
270 private$tree$addBranch(private$tree$getTree(), newTreeBranch)
272 private$REST$create(file, self$uuid)
273 newTreeBranch$setCollection(self)
279 stop(paste0("Expected character vector, got ",
280 paste0("(", paste0(class(files), collapse = ", "), ")"),
286 #' Remove one or more files from the collection.
287 #' @param paths Content to be removed.
289 #' collection$remove(fileName.format)
290 remove = function(paths)
292 if(is.null(private$tree))
293 private$generateCollectionTreeStructure()
295 if(is.character(paths))
297 sapply(paths, function(filePath)
299 filePath <- trimFromEnd(filePath, "/")
300 file <- self$get(filePath)
303 stop(paste("File", filePath, "doesn't exist."))
305 parent <- file$getParent()
308 stop("You can't delete root folder.")
310 parent$remove(file$getName())
317 stop(paste0("Expected character vector, got ",
318 paste0("(", paste0(class(paths), collapse = ", "), ")"),
324 #' Moves ArvadosFile or Subcollection to another location in the collection.
325 #' @param content Content to be moved.
326 #' @param destination Path to move content.
328 #' collection$move("fileName.format", path)
329 move = function(content, destination)
331 if(is.null(private$tree))
332 private$generateCollectionTreeStructure()
334 content <- trimFromEnd(content, "/")
336 elementToMove <- self$get(content)
338 if(is.null(elementToMove))
339 stop("Content you want to move doesn't exist in the collection.")
341 elementToMove$move(destination)
345 #' Copies ArvadosFile or Subcollection to another location in the collection.
346 #' @param content Content to be moved.
347 #' @param destination Path to move content.
349 #' copied <- collection$copy("oldName.format", "newName.format")
350 copy = function(content, destination)
352 if(is.null(private$tree))
353 private$generateCollectionTreeStructure()
355 content <- trimFromEnd(content, "/")
357 elementToCopy <- self$get(content)
359 if(is.null(elementToCopy))
360 stop("Content you want to copy doesn't exist in the collection.")
362 elementToCopy$copy(destination)
366 #' Refreshes the environment.
368 #' collection$refresh()
371 if(!is.null(private$tree))
373 private$tree$getTree()$setCollection(NULL, setRecursively = TRUE)
379 #' Returns collections file content as character vector.
381 #' list <- collection$getFileListing()
382 getFileListing = function()
384 if(is.null(private$tree))
385 private$generateCollectionTreeStructure()
387 content <- private$REST$getCollectionContent(self$uuid)
388 content[order(tolower(content))]
392 #' If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.
393 #' @param relativePath Path from content is taken.
395 #' arvadosFile <- collection$get(fileName)
396 get = function(relativePath)
398 if(is.null(private$tree))
399 private$generateCollectionTreeStructure(relativePath)
401 private$tree$getElement(relativePath)
404 getRESTService = function() private$REST,
405 setRESTService = function(newRESTService) private$REST <- newRESTService
410 #' @tree beautiful tree of sth
414 generateCollectionTreeStructure = function(relativePath = NULL)
416 if(is.null(self$uuid))
417 stop("Collection uuid is not defined.")
419 if(is.null(private$REST))
420 stop("REST service is not defined.")
422 private$fileContent <- private$REST$getCollectionContent(self$uuid, relativePath)
423 private$tree <- CollectionTree$new(private$fileContent, self)
432 #' Custom print function for Collection class
434 #' @param x Instance of Collection class
435 #' @param ... Optional arguments.
437 print.Collection = function(x, ...)
439 cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")
440 cat(paste0("uuid: ", "\"", x$uuid, "\""), sep = "\n")