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 #' https://git.arvados.org/arvados.git/tree/HEAD:/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.
32 #' collection <- Collection$new(arv, CollectionUUID)
34 initialize = function(api, uuid)
36 private$REST <- api$getRESTService()
41 #' Adds ArvadosFile or Subcollection specified by content to the collection. Used only with ArvadosFile or Subcollection.
42 #' @param content Content to be added.
43 #' @param relativePath Path to add content.
44 add = function(content, relativePath = "")
46 if(is.null(private$tree))
47 private$generateCollectionTreeStructure()
49 if(relativePath == "" ||
50 relativePath == "." ||
53 subcollection <- private$tree$getTree()
57 relativePath <- trimFromEnd(relativePath, "/")
58 subcollection <- self$get(relativePath)
61 if(is.null(subcollection))
62 stop(paste("Subcollection", relativePath, "doesn't exist."))
64 if("ArvadosFile" %in% class(content) ||
65 "Subcollection" %in% class(content))
67 if(!is.null(content$getCollection()))
68 stop("Content already belongs to a collection.")
70 if(content$getName() == "")
71 stop("Content has invalid name.")
73 subcollection$add(content)
78 stop(paste0("Expected AravodsFile or Subcollection object, got ",
79 paste0("(", paste0(class(content), collapse = ", "), ")"),
86 #' @param file Name of the file.
87 #' @param col Collection from which the file is read.
88 #' @param sep Separator used in reading tsv, csv file format.
89 #' @param istable Used in reading txt file to check if the file is table or not.
90 #' @param fileclass Used in reading fasta file to set file class.
91 #' @param Ncol Used in reading binary file to set numbers of columns in data.frame.
92 #' @param Nrow Used in reading binary file to set numbers of rows in data.frame size.
95 #' collection <- Collection$new(arv, collectionUUID)
96 #' readFile <- collection$readArvFile(arvadosFile, istable = 'yes') # table
97 #' readFile <- collection$readArvFile(arvadosFile, istable = 'no') # text
98 #' readFile <- collection$readArvFile(arvadosFile) # xlsx, csv, tsv, rds, rdata
99 #' readFile <- collection$readArvFile(arvadosFile, fileclass = 'fasta') # fasta
100 #' readFile <- collection$readArvFile(arvadosFile, Ncol= 4, Nrow = 32) # binary, only numbers
101 #' readFile <- collection$readArvFile(arvadosFile, Ncol = 5, Nrow = 150, istable = "factor") # binary with factor or text
103 readArvFile = function(file, con, sep = ',', istable = NULL, fileclass = "SeqFastadna", Ncol = NULL, Nrow = NULL, wantedFunction = NULL)
105 arvFile <- self$get(file)
106 FileName <- arvFile$getName()
107 FileName <- tolower(FileName)
108 FileFormat <- gsub(".*\\.", "", FileName)
111 ARVADOS_API_TOKEN <- Sys.getenv("ARVADOS_API_TOKEN")
112 ARVADOS_API_HOST <- Sys.getenv("ARVADOS_API_HOST")
113 my_collection <- self$uuid
114 key <- gsub("/", "_", ARVADOS_API_TOKEN)
117 "AWS_ACCESS_KEY_ID" = key,
118 "AWS_SECRET_ACCESS_KEY" = key,
119 "AWS_DEFAULT_REGION" = "collections",
120 "AWS_S3_ENDPOINT" = gsub("api[.]", "", ARVADOS_API_HOST))
122 if (FileFormat == "txt") {
123 if (is.null(istable)){
124 stop(paste('You need to paste whether it is a text or table file'))
125 } else if (istable == 'no') {
126 fileContent <- arvFile$read("text") # used to read
127 fileContent <- gsub("[\r\n]", " ", fileContent)
128 } else if (istable == 'yes') {
129 arvConnection <- arvFile$connection("r") # used to make possible use different function later
130 fileContent <- read.table(arvConnection)
133 else if (FileFormat == "xlsx") {
134 fileContent <- aws.s3::s3read_using(FUN = openxlsx::read.xlsx, object = file, bucket = my_collection)
136 else if (FileFormat == "csv" || FileFormat == "tsv") {
137 arvConnection <- arvFile$connection("r")
138 if (FileFormat == "tsv"){
139 mytable <- read.table(arvConnection, sep = '\t')
140 } else if (FileFormat == "csv" & sep == '\t') {
141 mytable <- read.table(arvConnection, sep = '\t')
142 } else if (FileFormat == "csv") {
143 mytable <- read.table(arvConnection, sep = ',')
145 stop(paste('File format not supported, use arvadosFile$connection() and customise it'))
148 else if (FileFormat == "fasta") {
149 fastafile <- aws.s3::s3read_using(FUN = seqinr::read.fasta, as.string = TRUE, object = file, bucket = my_collection)
151 else if (FileFormat == "dat" || FileFormat == "bin") {
152 fileContent <- gzcon(arvFile$connection("rb"))
154 # function to precess data to binary format
155 read_bin.file <- function(fileContent) {
157 column.names <- readBin(fileContent, character(), n = Ncol)
158 bindata <- readBin(fileContent, numeric(), Nrow*Ncol+Ncol)
160 res <- which(bindata < 0.0000001)
162 bindata <- bindata[-res]
167 data <- data.frame(matrix(data = NA, nrow = Nrow, ncol = Ncol))
169 data[,i] <- bindata[(1+Nrow*(i-1)):(Nrow*i)]
171 colnames(data) = column.names
173 len <- which(is.na(data[,Ncol])) # error if sth went wrong
174 if (length(len) == 0) {
177 stop(paste("there is a factor or text in the table, customize the function by typing more arguments"))
180 if (is.null(Nrow) | is.null(Ncol)){
181 stop(paste('You need to specify numbers of columns and rows'))
183 if (is.null(istable)) {
184 fileContent <- read_bin.file(fileContent) # call a function
185 } else if (istable == "factor") { # if there is a table with col name
186 fileContent <- read_bin.file(fileContent)
189 else if (FileFormat == "rds" || FileFormat == "rdata") {
190 arvConnection <- arvFile$connection("rb")
191 mytable <- readRDS(gzcon(arvConnection))
194 stop(parse(('File format not supported, use arvadosFile$connection() and customise it')))
199 #' Write file content
200 #' @param name Name of the file.
201 #' @param file File to be saved.
202 #' @param istable Used in writing txt file to check if the file is table or not.
205 #' collection <- Collection$new(arv, collectionUUID)
206 #' writeFile <- collection$writeFile(name = "myoutput.csv", file = file, fileFormat = "csv", istable = NULL, collectionUUID = collectionUUID) # csv
207 #' writeFile <- collection$writeFile(name = "myoutput.tsv", file = file, fileFormat = "tsv", istable = NULL, collectionUUID = collectionUUID) # tsv
208 #' writeFile <- collection$writeFile(name = "myoutput.fasta", file = file, fileFormat = "fasta", istable = NULL, collectionUUID = collectionUUID) # fasta
209 #' writeFile <- collection$writeFile(name = "myoutputtable.txt", file = file, fileFormat = "txt", istable = "yes", collectionUUID = collectionUUID) # txt table
210 #' writeFile <- collection$writeFile(name = "myoutputtext.txt", file = file, fileFormat = "txt", istable = "no", collectionUUID = collectionUUID) # txt text
211 #' writeFile <- collection$writeFile(name = "myoutputbinary.dat", file = file, fileFormat = "dat", collectionUUID = collectionUUID) # binary
212 #' writeFile <- collection$writeFile(name = "myoutputxlsx.xlsx", file = file, fileFormat = "xlsx", collectionUUID = collectionUUID) # xlsx
214 writeFile = function(name, file, collectionUUID, fileFormat, istable = NULL, seqName = NULL)
217 ARVADOS_API_TOKEN <- Sys.getenv("ARVADOS_API_TOKEN")
218 ARVADOS_API_HOST <- Sys.getenv("ARVADOS_API_HOST")
219 my_collection <- self$uuid
220 key <- gsub("/", "_", ARVADOS_API_TOKEN)
223 "AWS_ACCESS_KEY_ID" = key,
224 "AWS_SECRET_ACCESS_KEY" = key,
225 "AWS_DEFAULT_REGION" = "collections",
226 "AWS_S3_ENDPOINT" = gsub("api[.]", "", ARVADOS_API_HOST))
229 if (fileFormat == "txt") {
230 if (istable == "yes") {
231 aws.s3::s3write_using(file, FUN = write.table, object = name, bucket = collectionUUID)
232 } else if (istable == "no") {
233 aws.s3::s3write_using(file, FUN = writeChar, object = name, bucket = collectionUUID)
235 stop(paste("Specify parametr istable"))
237 } else if (fileFormat == "csv") {
238 aws.s3::s3write_using(file, FUN = write.csv, object = name, bucket = collectionUUID)
239 } else if (fileFormat == "tsv") {
240 aws.s3::s3write_using(file, FUN = write.table, row.names = FALSE, sep = "\t", object = name, bucket = collectionUUID)
241 } else if (fileFormat == "fasta") {
242 aws.s3::s3write_using(file, FUN = seqinr::write.fasta, name = seqName, object = name, bucket = collectionUUID)
243 } else if (fileFormat == "xlsx") {
244 aws.s3::s3write_using(file, FUN = openxlsx::write.xlsx, object = name, bucket = collectionUUID)
245 } else if (fileFormat == "dat" || fileFormat == "bin") {
246 aws.s3::s3write_using(file, FUN = writeBin, object = name, bucket = collectionUUID)
248 stop(parse(('File format not supported, use arvadosFile$connection() and customise it')))
253 #' Creates one or more ArvadosFiles and adds them to the collection at specified path.
254 #' @param files Content to be created.
257 #' collection <- arv$collections_create(name = collectionTitle, description = collectionDescription, owner_uuid = collectionOwner, properties = list("ROX37196928443768648" = "ROX37742976443830153"))
259 create = function(files)
261 if(is.null(private$tree))
262 private$generateCollectionTreeStructure()
264 if(is.character(files))
266 sapply(files, function(file)
268 childWithSameName <- self$get(file)
269 if(!is.null(childWithSameName))
270 stop("Destination already contains file with same name.")
272 newTreeBranch <- private$tree$createBranch(file)
273 private$tree$addBranch(private$tree$getTree(), newTreeBranch)
275 private$REST$create(file, self$uuid)
276 newTreeBranch$setCollection(self)
282 stop(paste0("Expected character vector, got ",
283 paste0("(", paste0(class(files), collapse = ", "), ")"),
289 #' Remove one or more files from the collection.
290 #' @param paths Content to be removed.
293 #' collection$remove(fileName.format)
295 remove = function(paths)
297 if(is.null(private$tree))
298 private$generateCollectionTreeStructure()
300 if(is.character(paths))
302 sapply(paths, function(filePath)
304 filePath <- trimFromEnd(filePath, "/")
305 file <- self$get(filePath)
308 stop(paste("File", filePath, "doesn't exist."))
310 parent <- file$getParent()
313 stop("You can't delete root folder.")
315 parent$remove(file$getName())
322 stop(paste0("Expected character vector, got ",
323 paste0("(", paste0(class(paths), collapse = ", "), ")"),
329 #' Moves ArvadosFile or Subcollection to another location in the collection.
330 #' @param content Content to be moved.
331 #' @param destination Path to move content.
334 #' collection$move("fileName.format", path)
336 move = function(content, destination)
338 if(is.null(private$tree))
339 private$generateCollectionTreeStructure()
341 content <- trimFromEnd(content, "/")
343 elementToMove <- self$get(content)
345 if(is.null(elementToMove))
346 stop("Content you want to move doesn't exist in the collection.")
348 elementToMove$move(destination)
352 #' Copies ArvadosFile or Subcollection to another location in the collection.
353 #' @param content Content to be moved.
354 #' @param destination Path to move content.
357 #' copied <- collection$copy("oldName.format", "newName.format")
359 copy = function(content, destination)
361 if(is.null(private$tree))
362 private$generateCollectionTreeStructure()
364 content <- trimFromEnd(content, "/")
366 elementToCopy <- self$get(content)
368 if(is.null(elementToCopy))
369 stop("Content you want to copy doesn't exist in the collection.")
371 elementToCopy$copy(destination)
375 #' Refreshes the environment.
378 #' collection$refresh()
382 if(!is.null(private$tree))
384 private$tree$getTree()$setCollection(NULL, setRecursively = TRUE)
390 #' Returns collections file content as character vector.
393 #' list <- collection$getFileListing()
395 getFileListing = function()
397 if(is.null(private$tree))
398 private$generateCollectionTreeStructure()
400 content <- private$REST$getCollectionContent(self$uuid)
401 content[order(tolower(content))]
405 #' If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.
406 #' @param relativePath Path from content is taken.
409 #' arvadosFile <- collection$get(fileName)
411 get = function(relativePath)
413 if(is.null(private$tree))
414 private$generateCollectionTreeStructure()
416 private$tree$getElement(relativePath)
419 getRESTService = function() private$REST,
420 setRESTService = function(newRESTService) private$REST <- newRESTService
425 #' @tree beautiful tree of sth
429 generateCollectionTreeStructure = function(relativePath = NULL)
431 if(is.null(self$uuid))
432 stop("Collection uuid is not defined.")
434 if(is.null(private$REST))
435 stop("REST service is not defined.")
437 private$fileContent <- private$REST$getCollectionContent(self$uuid, relativePath)
438 private$tree <- CollectionTree$new(private$fileContent, self)
447 #' Custom print function for Collection class
449 #' @param x Instance of Collection class
450 #' @param ... Optional arguments.
452 print.Collection = function(x, ...)
454 cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")
455 cat(paste0("uuid: ", "\"", x$uuid, "\""), sep = "\n")