1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
3 source("./R/RESTService.R")
8 #' Collection class provides interface for working with Arvados collections.
11 #' \preformatted{collection = Collection$new(arv, uuid)}
13 #' @section Arguments:
15 #' \item{arv}{Arvados object.}
16 #' \item{uuid}{UUID of a collection.}
21 #' \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the collection.}
22 #' \item{create(fileNames, relativePath = "")}{Creates one or more ArvadosFiles and adds them to the collection at specified path.}
23 #' \item{remove(fileNames)}{Remove one or more files from the collection.}
24 #' \item{move(content, newLocation)}{Moves ArvadosFile or Subcollection to another location in the collection.}
25 #' \item{getFileListing()}{Returns collections file content as character vector.}
26 #' \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.}
32 #' arv <- Arvados$new("your Arvados token", "example.arvadosapi.com")
33 #' collection <- Collection$new(arv, "uuid")
35 #' newFile <- ArvadosFile$new("myFile")
36 #' collection$add(newFile, "myFolder")
38 #' createdFiles <- collection$create(c("main.cpp", lib.dll), "cpp/src/")
40 #' collection$remove("location/to/my/file.cpp")
42 #' collection$move("folder/file.cpp", "file.cpp")
44 #' arvadosFile <- collection$get("location/to/my/file.cpp")
45 #' arvadosSubcollection <- collection$get("location/to/my/directory/")
50 Collection <- R6::R6Class(
59 initialize = function(api, uuid)
62 private$REST <- api$getRESTService()
66 private$fileContent <- private$REST$getCollectionContent(uuid)
67 private$tree <- CollectionTree$new(private$fileContent, self)
70 add = function(content, relativePath = "")
72 if(is.null(private$tree))
73 private$genereateCollectionTreeStructure()
75 if(relativePath == "" ||
76 relativePath == "." ||
79 subcollection <- private$tree$getTree()
83 relativePath <- trimFromEnd(relativePath, "/")
84 subcollection <- self$get(relativePath)
87 if(is.null(subcollection))
88 stop(paste("Subcollection", relativePath, "doesn't exist."))
90 if("ArvadosFile" %in% class(content) ||
91 "Subcollection" %in% class(content))
93 if(content$getName() == "")
94 stop("Content has invalid name.")
96 subcollection$add(content)
101 stop(paste0("Expected AravodsFile or Subcollection object, got ",
102 paste0("(", paste0(class(content), collapse = ", "), ")"),
107 create = function(fileNames, relativePath = "")
109 if(is.null(private$tree))
110 private$genereateCollectionTreeStructure()
112 if(relativePath == "" ||
113 relativePath == "." ||
114 relativePath == "./")
116 subcollection <- private$tree$getTree()
120 relativePath <- trimFromEnd(relativePath, "/")
121 subcollection <- self$get(relativePath)
124 if(is.null(subcollection))
125 stop(paste("Subcollection", relativePath, "doesn't exist."))
127 if(is.character(fileNames))
130 sapply(fileNames, function(fileName)
132 childWithSameName <- subcollection$get(fileName)
133 if(!is.null(childWithSameName))
134 stop("Destination already contains file with same name.")
136 newFile <- ArvadosFile$new(fileName)
137 subcollection$add(newFile)
139 arvadosFiles <<- c(arvadosFiles, newFile)
142 if(length(arvadosFiles) == 1)
143 return(arvadosFiles[[1]])
149 stop(paste0("Expected character vector, got ",
150 paste0("(", paste0(class(fileNames), collapse = ", "), ")"),
155 remove = function(paths)
157 if(is.null(private$tree))
158 private$genereateCollectionTreeStructure()
160 if(is.character(paths))
162 sapply(paths, function(filePath)
164 filePath <- trimFromEnd(filePath, "/")
165 file <- self$get(filePath)
168 stop(paste("File", filePath, "doesn't exist."))
170 parent <- file$getParent()
173 stop("You can't delete root folder.")
175 parent$remove(file$getName())
182 stop(paste0("Expected character vector, got ",
183 paste0("(", paste0(class(paths), collapse = ", "), ")"),
188 move = function(content, newLocation)
190 if(is.null(private$tree))
191 private$genereateCollectionTreeStructure()
193 content <- trimFromEnd(content, "/")
195 elementToMove <- self$get(content)
197 if(is.null(elementToMove))
198 stop("Content you want to move doesn't exist in the collection.")
200 elementToMove$move(newLocation)
203 getFileListing = function()
205 if(is.null(private$tree))
206 private$genereateCollectionTreeStructure()
208 content <- private$REST$getCollectionContent(self$uuid)
209 content[order(tolower(content))]
212 get = function(relativePath)
214 if(is.null(private$tree))
215 private$genereateCollectionTreeStructure()
217 private$tree$getElement(relativePath)
222 fields <- sapply(private$classFields, function(field)
227 jsonlite::toJSON(list("collection" =
228 Filter(Negate(is.null), fields)), auto_unbox = TRUE)
231 isEmpty = function() {
232 fields <- sapply(private$classFields,
233 function(field) self[[field]])
235 if(any(sapply(fields, function(field) !is.null(field) && field != "")))
241 getRESTService = function() private$REST,
242 setRESTService = function(newRESTService) private$REST <- newRESTService
252 genereateCollectionTreeStructure = function()
254 if(is.null(self$uuid))
255 stop("Collection uuid is not defined.")
257 if(is.null(private$REST))
258 stop("REST service is not defined.")
260 private$fileContent <- private$REST$getCollectionContent(self$uuid)
261 private$tree <- CollectionTree$new(private$fileContent, self)
270 #' Custom print function for Collection class
272 #' @param x Instance of Collection class
273 #' @param ... Optional arguments.
275 print.Collection = function(x, ...)
277 cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")
278 cat(paste0("uuid: ", "\"", x$uuid, "\""), sep = "\n")