sbg / sevenbridges-r

Seven Bridges API Client, CWL Schema, Meta Schema, and SDK Helper in R
https://sbg.github.io/sevenbridges-r/
Apache License 2.0
35 stars 14 forks source link

Get list of file within folders in cavatica project #73

Closed kgaonkar6 closed 5 years ago

kgaonkar6 commented 5 years ago

Hello,

Thank you for creating the sevenbridges package for R, it's been very helpful. We've recently been trying to make folders in the projects to be more organized however I'm not able to do the same calls to access files as I did before. For example: fl<-a$project(id = pid)$file(name = as.character(f_name) , exact=TRUE)) worked before to get the File ID however now it doesnt find the file because the file in within a directory structure. When I do a$project(id = pid)$file(complete=TRUE) only the ID and information 2 main folders on the first page are displayed.

Please let me know if I'm not doing this right, or if there is a workaround for this.

Thanks, Krutika

nanxstats commented 5 years ago

Hi @kgaonkar6 -thanks for using our package and the feedback.

I guess this is probably the expected behavior when folders exist in a project -- the file does not live under the project root folder so it cannot be listed by a$project(id = ...)$file(name = ..., exact = TRUE) anymore. The deeper reason behind is that it would be a recursive API call to locate a specific file by its name within all folders and subfolders, which is suboptimal.

I think the current steps to locate a file under a (non-root) folder is:

library("sevenbridges")

# authenticate
a <- Auth(token = "your_token", platform = "cavatica")

# helper function
get_name <- function(x) sapply(x, "[[", "name")

# get project
p <- a$project(id = "user/project")

# get project root folder
folder_root <- p$get_root_folder()

# get all folders under the project root folder
contents_root <- folder_root$list_folder_contents(type = "folder", complete = TRUE)

# get the folder named `folder1`
folder1 <- contents_root[[which(get_name(contents_root) == "folder1")]]

# list all files under `folder1` (excluding folders)
contents_folder1 <- folder1$list_folder_contents(type = "file", complete = TRUE)

# get the file named `file1.bam`
file1 <- contents_folder1[[which(get_name(contents_folder1) == "file1.bam")]]

Similarly, you can locate a file in an even deeper folder structure.

Here are most of the useful folder operations which were added to the package last year.

Hope this helps! -Nan

kgaonkar6 commented 5 years ago

Thank you for the explanation,Nan!

I did refer to the tutorial but I didn't quite understood how I would use the folder names to get into the folder to list the files. So this is useful, it did work for my project directory structure as well.

Thanks, Krutika